Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
DBNode Class Reference

#include <DBNode.h>

Public Types

using Value = std::variant< BlockName, int, double, bool, std::shared_ptr< DBNode >, std::vector< std::shared_ptr< DBNode > > >
 Variant type used to store values in the tree.
 

Public Member Functions

 DBNode ()
 Default constructor.
 
template<typename... Keys>
Value get (Keys &&... keys) const
 Retrieves a value from the node using a sequence of keys.
 
std::vector< BlockNameget_keys ()
 Returns all the keys stored at the current node level.
 
template<typename T , typename Key , typename... Rest>
void set (T value, Key &&key, Rest &&... rest)
 Sets a value in the node using a sequence of keys.
 
std::map< BlockName, ValuegetGroup (const std::vector< BlockName > &keys) const
 Retrieves a whole sub-tree as a map from a path of keys.
 
void setGroup (const std::vector< BlockName > &keys, const std::map< BlockName, Value > &groupData)
 Sets the content of a whole sub-tree from a map of values.
 
void printJSON (int level=0) const
 Prints the node in JSON format to std::cout.
 
void printJSONToStream (std::ostream &os, int level=0) const
 Prints the node in JSON format to a given output stream.
 
void printYAMLToStream (std::ostream &os, int level=0) const
 Prints the node in YAML format to a given output stream.
 
void printYAML (int level=0) const
 Prints the node in YAML format to std::cout.
 
bool contains (const BlockName &key) const
 Checks whether the node contains a direct child with the given key.
 
int countChildren () const
 Returns the number of direct children of this node.
 
bool isList () const
 Returns true if this node represents a "list" node.
 

Detailed Description

Definition at line 33 of file DBNode.h.

Member Typedef Documentation

◆ Value

using DBNode::Value = std::variant<BlockName, int, double, bool, std::shared_ptr<DBNode>, std::vector<std::shared_ptr<DBNode> >>

Variant type used to store values in the tree.

A value can be:

  • BlockName : named scalar value,
  • int, double, bool : numeric / boolean scalar,
  • std::shared_ptr<DBNode> : nested object,
  • std::vector<std::shared_ptr<DBNode>> : list of objects (used for arrays).

Definition at line 45 of file DBNode.h.

Constructor & Destructor Documentation

◆ DBNode()

DBNode::DBNode ( )
default

Default constructor.

Constructs an empty node with no children.

Member Function Documentation

◆ contains()

bool DBNode::contains ( const BlockName key) const

Checks whether the node contains a direct child with the given key.

This does not inspect nested children; it only checks the current level of the map.

Parameters
keyKey to look up.
Returns
true if key exists in this node, false otherwise.

Definition at line 263 of file DBNode.cpp.

◆ countChildren()

int DBNode::countChildren ( ) const

Returns the number of direct children of this node.

Equivalent to data_.size().

Definition at line 270 of file DBNode.cpp.

◆ get()

template<typename... Keys>
Value DBNode::get ( Keys &&...  keys) const

Retrieves a value from the node using a sequence of keys.

This is a generic hierarchical accessor:

DBNode root;
// ...
auto v = root.get("BLOCK", "SUBBLOCK", "ENTRY");
Value get(Keys &&... keys) const
Retrieves a value from the node using a sequence of keys.

The provided keys are interpreted as a path: at each step, the corresponding child must be a nested DBNode (stored in a std::shared_ptr<DBNode>) until the final element is reached.

Template Parameters
KeysTypes of the keys (typically BlockName, std::string, const char*).
Parameters
keysPath of keys leading to the desired value.
Returns
The stored Value at the end of the path.
Exceptions
std::runtime_errorIf the path does not exist or the structure along the way is not compatible (e.g. non-DBNode where a DBNode is expected).

◆ get_keys()

std::vector< BlockName > DBNode::get_keys ( )

Returns all the keys stored at the current node level.

This does not recurse into children; it only returns the immediate keys present in the underlying map.

Returns
Vector of BlockName keys present in this node.

Definition at line 5 of file DBNode.cpp.

◆ getGroup()

std::map< BlockName, DBNode::Value > DBNode::getGroup ( const std::vector< BlockName > &  keys) const

Retrieves a whole sub-tree as a map from a path of keys.

The keys vector is interpreted as a path of nested nodes. At the end of the path, the underlying map<BlockName,Value> is returned.

Parameters
keysSequence of keys describing the path to the group node.
Returns
Map of key–value pairs stored at the target node.
Exceptions
std::runtime_errorIf the path does not exist or does not terminate on a DBNode.

Definition at line 13 of file DBNode.cpp.

◆ isList()

bool DBNode::isList ( ) const

Returns true if this node represents a "list" node.

A node is considered a list node if its first stored value is a std::vector<std::shared_ptr<DBNode>>. This is a heuristic used internally to differentiate arrays from objects during printing.

◆ printJSON()

void DBNode::printJSON ( int  level = 0) const

Prints the node in JSON format to std::cout.

This produces a pretty-printed JSON-like representation of the current node and all its descendants.

Parameters
levelIndentation level (used internally for recursion).

Definition at line 55 of file DBNode.cpp.

◆ printJSONToStream()

void DBNode::printJSONToStream ( std::ostream &  os,
int  level = 0 
) const

Prints the node in JSON format to a given output stream.

Parameters
osOutput stream.
levelIndentation level (used internally for recursion).

Definition at line 105 of file DBNode.cpp.

◆ printYAML()

void DBNode::printYAML ( int  level = 0) const

Prints the node in YAML format to std::cout.

Parameters
levelIndentation level (used internally for recursion).

Definition at line 148 of file DBNode.cpp.

◆ printYAMLToStream()

void DBNode::printYAMLToStream ( std::ostream &  os,
int  level = 0 
) const

Prints the node in YAML format to a given output stream.

Lists and nested objects are rendered using standard YAML indentation and list syntax.

Parameters
osOutput stream.
levelIndentation level (used internally for recursion).

Definition at line 286 of file DBNode.cpp.

◆ set()

template<typename T , typename Key , typename... Rest>
void DBNode::set ( T  value,
Key &&  key,
Rest &&...  rest 
)

Sets a value in the node using a sequence of keys.

Intermediate nodes on the path are created as needed and stored as std::shared_ptr<DBNode>. The last key in the sequence is associated with the provided value.

Template Parameters
TType of the value to store (compatible with Value).
KeyType of the first key.
RestTypes of additional keys.
Parameters
valueValue to store.
keyFirst key in the path.
restRemaining keys specifying the path.

◆ setGroup()

void DBNode::setGroup ( const std::vector< BlockName > &  keys,
const std::map< BlockName, Value > &  groupData 
)

Sets the content of a whole sub-tree from a map of values.

The keys vector is interpreted as a path. Intermediate nodes are created as needed. At the end of the path, the node's internal map is replaced with groupData.

Parameters
keysPath to the group node to fill.
groupDataMap of key–value pairs to store at that node.

Definition at line 30 of file DBNode.cpp.


The documentation for this class was generated from the following files: