The Sparta Modeling Framework
Loading...
Searching...
No Matches
sparta::ParameterTree::Node Class Reference

Node containing a Parameter and value to apply. Can be used to describes a value extracted from the tree when using get or tryGet. This is a copy of the value and is not in any way synchronized with the tree. More...

#include <ParameterTree.hpp>

Classes

class  MatchIterator
 

Public Types

typedef std::vector< std::unique_ptr< Node > > ChildVector
 Vector of children owned by this node.
 

Public Member Functions

 Node ()=delete
 Not default-constructable.
 
 Node (Node *parent, const std::string &name, const std::string &value, const std::string &origin)
 
 Node (Node *parent, const std::string &name)
 Value-less constructor.
 
 Node (Node *parent, ParameterTree *tree)
 Root node constructor. Constructs node pointing to a new tree having no name. Normal nodes to not have tree pointers (getOwner)
 
 Node (Node *parent, const Node &n)
 Deep-Copy constructor.
 
Nodeoperator= (const Node &n)
 Parent-preserving deep-copy assignment operator.
 
void dump (std::ostream &o) const
 Dumps the content of this node to an ostream on a single line. Does not recurs into children.
 
const std::string & getName () const
 Gets the name of this node.
 
NodegetParent ()
 Gets the parent of this node.
 
Node const * getParent () const
 Gets the parent of this node.
 
NodegetRoot ()
 Gets the parent of this node.
 
Node const * getRoot () const
 Gets the parent of this node.
 
ParameterTreegetOwner ()
 Gets the ParameterTree object that owns this node.
 
const ParameterTreegetOwner () const
 Gets the ParameterTree object that owns this node.
 
std::string getPath () const
 Gets the path to this node including the root node.
 
bool isRoot () const
 Is this a root node.
 
void incrementReadCount () const
 Increment the read count of this node.
 
uint32_t getReadCount () const
 Gets the number of times this node has been accessed to be read (i.e. with get/tryGet)
 
const std::string & getValue () const
 Gets the value of this object as a string.
 
const std::string & peekValue () const
 Gets the value of this object as a string.
 
const std::string & getOrigin () const
 Gets the origin associated with the value at this node.
 
template<typename T , typename = typename std::enable_if<!std::is_convertible<T, std::string>::value>::type>
getAs () const
 Gets the value in this object.
 
template<typename T , typename = typename std::enable_if<std::is_convertible<T, std::string>::value>::type>
const std::string & getAs () const
 getAs template instance for string types (e.g. char[], const char*, std::string)
 
 operator std::string () const
 Get value as a string.
 
template<typename T >
 operator T () const
 Get value as a specific type through getAs.
 
template<typename T >
bool operator== (const T &rhp) const
 Equality test. Attempts to lexically cast underlying string to requested data-type.
 
NodegetChild (const std::string &name) const
 Gets the most recently created child of this node by a concrete child name.
 
Nodecreate (const std::string &path, bool required)
 Get a child for setting a parameter, creating it if needed.
 
NodegetPriorityChildMatch (const std::string &name) const
 Attempts to get an immediate child with an exact match for a given name or pattern string. There is no pattern matching in this method. Patterns are treated as raw strings.
 
NodeaddChild (const std::string &name, bool required)
 
Nodeoperator[] (const std::string &name) const
 Gets a child of this node by its name.
 
Nodeoperator[] (const char *name) const
 
bool hasValue () const
 Does this node have a value written to it which can be accessed through:
 
void setValue (const std::string &val, bool required=true, const std::string &origin="")
 Set a value on this node directly.
 
void incRequired ()
 Increment the required count.
 
void unrequire ()
 Clear the required count. This is necessary if a parameter is flagged as deprecated or removed in a configuration file. This affects this node only.
 
std::unique_ptr< Noderelease ()
 Release this node and its children from the tree.
 
bool set (const std::string &path, const std::string &val, bool required, const std::string &origin="")
 Set the string value of a child of this node. Note that this may not affect this node directly because of the way pattern nodes work.
 
const std::string & operator= (const std::string &val)
 String value assignment operator.
 
bool isRequired () const
 Return true if this parameter node is required to exist by the client by 1 or more "set"-ers using this object.
 
uint32_t getRequiredCount () const
 Returns the number of times this node has been flagged as required.
 
std::vector< Node * > getChildren ()
 Gets vector of pointers to children of this node.
 
std::vector< const Node * > getChildren () const
 Gets vector of pointers to children of this node.
 
void recursePrint (std::ostream &o, uint32_t indent=0) const
 Recursively print.
 
void appendTree (const Node *ot)
 Appends a tree as a child of this node.
 
MatchIterator getMatcherBegin () const
 Get most recent child added.
 
MatchIterator getMatcherEnd () const
 Get end of child match iterator (past oldest child added)
 

Static Public Member Functions

static bool matches (const std::string &pattern, const std::string &other)
 Does a string, name, interpreted as a sparta TreeNode pattern, match another string interpreted as a concrete (no-wildcards) node name.
 

Friends

class MatchIterator
 

Detailed Description

Node containing a Parameter and value to apply. Can be used to describes a value extracted from the tree when using get or tryGet. This is a copy of the value and is not in any way synchronized with the tree.

It is illegal to try and read the value if there is no value set (see hasValue) - An exception will be thrown.

Can be written as well

Contains a string that can be lexically cast using the getAs<T> method. Supports all types supported by sparta::lexicalCast

Definition at line 56 of file ParameterTree.hpp.

Member Typedef Documentation

◆ ChildVector

typedef std::vector<std::unique_ptr<Node> > sparta::ParameterTree::Node::ChildVector

Vector of children owned by this node.

Definition at line 63 of file ParameterTree.hpp.

Constructor & Destructor Documentation

◆ Node() [1/4]

sparta::ParameterTree::Node::Node ( Node parent,
const std::string &  name,
const std::string &  value,
const std::string &  origin 
)
inline

Definition at line 90 of file ParameterTree.hpp.

◆ Node() [2/4]

sparta::ParameterTree::Node::Node ( Node parent,
const std::string &  name 
)
inline

Value-less constructor.

Definition at line 103 of file ParameterTree.hpp.

◆ Node() [3/4]

sparta::ParameterTree::Node::Node ( Node parent,
ParameterTree tree 
)
inline

Root node constructor. Constructs node pointing to a new tree having no name. Normal nodes to not have tree pointers (getOwner)

Definition at line 116 of file ParameterTree.hpp.

◆ Node() [4/4]

sparta::ParameterTree::Node::Node ( Node parent,
const Node n 
)
inline

Deep-Copy constructor.

Note
Assigns new parent

Definition at line 129 of file ParameterTree.hpp.

Here is the call graph for this function:

Member Function Documentation

◆ addChild()

Node * sparta::ParameterTree::Node::addChild ( const std::string &  name,
bool  required 
)
inline

\Create a child of this node at the end of the list (meaning it is the most recent)

Definition at line 502 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ appendTree()

void sparta::ParameterTree::Node::appendTree ( const Node ot)
inline

Appends a tree as a child of this node.

Parameters
otOther tree to append. If ot is a root node (isRoot), it is merged over the current node, adding or replacing all its children. Otherwise, ot is added as a child of this node. To add all children of a particular node without adding the parent, those children must currently be iterated and separately appended to node through appendTree calls. However, since only leaf nodes are expected to have actual parameter values, it is typically safe to simply call appendTree with a node that is not a leaf when its children (subtrees) are the only nodes of interest

Definition at line 703 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ create()

Node * sparta::ParameterTree::Node::create ( const std::string &  path,
bool  required 
)
inline

Get a child for setting a parameter, creating it if needed.

Parameters
pathPath of immediate child to get/create. If there is a child by this path which was not created before any wildcard nodes matching name, then the existing node will be returned. Otherwise, a new child will be created.
requiredIs this node required to exist in the tree. This is a deferred value
Note
When not creating new children, it is probably better to invoke "create" from the ParameterTree with a complete path rather than this node. Otherwise the intended precedence of parameters might not match that represented by the tree.

Definition at line 400 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ dump()

void sparta::ParameterTree::Node::dump ( std::ostream &  o) const
inline

Dumps the content of this node to an ostream on a single line. Does not recurs into children.

Parameters
oOstream to write content to

Definition at line 168 of file ParameterTree.hpp.

◆ getAs() [1/2]

template<typename T , typename = typename std::enable_if<!std::is_convertible<T, std::string>::value>::type>
T sparta::ParameterTree::Node::getAs ( ) const
inline

Gets the value in this object.

Template Parameters
Templateparameter
Note
This method handles non-string-convertible types. The other getAs handles string-convertible types

Example

* val.getAs<std::string>()
* val.getAs<uint32_t>()
* 

Definition at line 314 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ getAs() [2/2]

template<typename T , typename = typename std::enable_if<std::is_convertible<T, std::string>::value>::type>
const std::string & sparta::ParameterTree::Node::getAs ( ) const
inline

getAs template instance for string types (e.g. char[], const char*, std::string)

Definition at line 322 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ getChild()

Node * sparta::ParameterTree::Node::getChild ( const std::string &  name) const
inline

Gets the most recently created child of this node by a concrete child name.

Parameters
nameName to get a child with. Must not be a pattern. Pattern-based nodes must be added and cannot be searched for. Searching based on a pattern would be too complicated
Returns
First child node (newest-to-oldest order) whose patter matches on name

Definition at line 373 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ getChildren() [1/2]

std::vector< Node * > sparta::ParameterTree::Node::getChildren ( )
inline

Gets vector of pointers to children of this node.

Definition at line 654 of file ParameterTree.hpp.

◆ getChildren() [2/2]

std::vector< const Node * > sparta::ParameterTree::Node::getChildren ( ) const
inline

Gets vector of pointers to children of this node.

Definition at line 665 of file ParameterTree.hpp.

◆ getMatcherBegin()

MatchIterator sparta::ParameterTree::Node::getMatcherBegin ( ) const
inline

Get most recent child added.

Definition at line 765 of file ParameterTree.hpp.

◆ getMatcherEnd()

MatchIterator sparta::ParameterTree::Node::getMatcherEnd ( ) const
inline

Get end of child match iterator (past oldest child added)

Definition at line 770 of file ParameterTree.hpp.

◆ getName()

const std::string & sparta::ParameterTree::Node::getName ( ) const
inline

Gets the name of this node.

Definition at line 176 of file ParameterTree.hpp.

◆ getOrigin()

const std::string & sparta::ParameterTree::Node::getOrigin ( ) const
inline

Gets the origin associated with the value at this node.

Precondition
Value must be set for this node. See hasValue

Definition at line 296 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ getOwner() [1/2]

ParameterTree * sparta::ParameterTree::Node::getOwner ( )
inline

Gets the ParameterTree object that owns this node.

Definition at line 215 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ getOwner() [2/2]

const ParameterTree * sparta::ParameterTree::Node::getOwner ( ) const
inline

Gets the ParameterTree object that owns this node.

Definition at line 222 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ getParent() [1/2]

Node * sparta::ParameterTree::Node::getParent ( )
inline

Gets the parent of this node.

Definition at line 181 of file ParameterTree.hpp.

◆ getParent() [2/2]

Node const * sparta::ParameterTree::Node::getParent ( ) const
inline

Gets the parent of this node.

Definition at line 186 of file ParameterTree.hpp.

◆ getPath()

std::string sparta::ParameterTree::Node::getPath ( ) const
inline

Gets the path to this node including the root node.

Definition at line 229 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ getPriorityChildMatch()

Node * sparta::ParameterTree::Node::getPriorityChildMatch ( const std::string &  name) const
inline

Attempts to get an immediate child with an exact match for a given name or pattern string. There is no pattern matching in this method. Patterns are treated as raw strings.

This searches children in reverse order (priority order) for a match. This will find the most recently created child which matches the pattern

Definition at line 441 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ getReadCount()

uint32_t sparta::ParameterTree::Node::getReadCount ( ) const
inline

Gets the number of times this node has been accessed to be read (i.e. with get/tryGet)

Definition at line 267 of file ParameterTree.hpp.

◆ getRequiredCount()

uint32_t sparta::ParameterTree::Node::getRequiredCount ( ) const
inline

Returns the number of times this node has been flagged as required.

Definition at line 647 of file ParameterTree.hpp.

◆ getRoot() [1/2]

Node * sparta::ParameterTree::Node::getRoot ( )
inline

Gets the parent of this node.

Definition at line 191 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ getRoot() [2/2]

Node const * sparta::ParameterTree::Node::getRoot ( ) const
inline

Gets the parent of this node.

Definition at line 203 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ getValue()

const std::string & sparta::ParameterTree::Node::getValue ( ) const
inline

Gets the value of this object as a string.

Precondition
Value must be set for this node. See hasValue
Postcondition
Increments read count

Definition at line 276 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ hasValue()

bool sparta::ParameterTree::Node::hasValue ( ) const
inline

Does this node have a value written to it which can be accessed through:

  • operator std::string
  • getAs
  • operator T
  • getValue

Definition at line 538 of file ParameterTree.hpp.

◆ incrementReadCount()

void sparta::ParameterTree::Node::incrementReadCount ( ) const
inline

Increment the read count of this node.

Definition at line 259 of file ParameterTree.hpp.

◆ incRequired()

void sparta::ParameterTree::Node::incRequired ( )
inline

Increment the required count.

Definition at line 566 of file ParameterTree.hpp.

◆ isRequired()

bool sparta::ParameterTree::Node::isRequired ( ) const
inline

Return true if this parameter node is required to exist by the client by 1 or more "set"-ers using this object.

Note
Invokes ParameterTree::isRequired with this node's path

Definition at line 633 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ isRoot()

bool sparta::ParameterTree::Node::isRoot ( ) const
inline

Is this a root node.

Definition at line 252 of file ParameterTree.hpp.

◆ matches()

static bool sparta::ParameterTree::Node::matches ( const std::string &  pattern,
const std::string &  other 
)
inlinestatic

Does a string, name, interpreted as a sparta TreeNode pattern, match another string interpreted as a concrete (no-wildcards) node name.

Parameters
patternString pattern to attempt to match with. This is a sparta::TreeNode single location pattern (no '.' characters)
otherString to attempt to match. Interpreted as a plain string, regardless of content. \erturn true if pattern matches other, false if not/

Definition at line 360 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ operator std::string()

sparta::ParameterTree::Node::operator std::string ( ) const
inline

Get value as a string.

Definition at line 329 of file ParameterTree.hpp.

◆ operator T()

template<typename T >
sparta::ParameterTree::Node::operator T ( ) const
inline

Get value as a specific type through getAs.

Template Parameters
TType to retrieve content as

Definition at line 338 of file ParameterTree.hpp.

◆ operator=() [1/2]

Node & sparta::ParameterTree::Node::operator= ( const Node n)
inline

Parent-preserving deep-copy assignment operator.

Postcondition
New node will have its parent preserved
write and read counts will be initialized to 0. If this node has a value after the assignment, write_count_ will be incremented to 1

Definition at line 149 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ operator=() [2/2]

const std::string & sparta::ParameterTree::Node::operator= ( const std::string &  val)
inline

String value assignment operator.

Note
Alias for set
Returns
Value argument so that multiple = calls can be strung together.
Note
Implies node will be required and with unkonwn ("") origin

Definition at line 623 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ operator==()

template<typename T >
bool sparta::ParameterTree::Node::operator== ( const T &  rhp) const
inline

Equality test. Attempts to lexically cast underlying string to requested data-type.

Definition at line 347 of file ParameterTree.hpp.

◆ operator[]() [1/2]

Node & sparta::ParameterTree::Node::operator[] ( const char *  name) const
inline

Definition at line 527 of file ParameterTree.hpp.

◆ operator[]() [2/2]

Node & sparta::ParameterTree::Node::operator[] ( const std::string &  name) const
inline

Gets a child of this node by its name.

Note
The subscript operator can be strung together for a multi-level lookup as in:
uint32_t val = ptree["foo"]["bar"]["buz"].getAs<uint32_t>()

Definition at line 520 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ peekValue()

const std::string & sparta::ParameterTree::Node::peekValue ( ) const
inline

Gets the value of this object as a string.

Precondition
Value must be set for this node. See hasValue
Postcondition
Does not increments read count

Definition at line 287 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ recursePrint()

void sparta::ParameterTree::Node::recursePrint ( std::ostream &  o,
uint32_t  indent = 0 
) const
inline

Recursively print.

Definition at line 676 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ release()

std::unique_ptr< Node > sparta::ParameterTree::Node::release ( )
inline

Release this node and its children from the tree.

Returns
The ChildVector including this node

Definition at line 586 of file ParameterTree.hpp.

◆ set()

bool sparta::ParameterTree::Node::set ( const std::string &  path,
const std::string &  val,
bool  required,
const std::string &  origin = "" 
)
inline

Set the string value of a child of this node. Note that this may not affect this node directly because of the way pattern nodes work.

Parameters
pathPath to child. To set this node, use path=""
valValue to assign
requiredIs this parameter location required?
originOrigin of the value (e.g. "foo.yaml line:50 col:23")
Postcondition
Increments write_count_
Returns
TEMPORARY: Returns true of parameter is set, false if not. May return false if parameter path contains parent reference (any '.' character whicih is proceded by another '.' or the beginnign of the string). This will be fixed eventually.

Definition at line 603 of file ParameterTree.hpp.

Here is the call graph for this function:

◆ setValue()

void sparta::ParameterTree::Node::setValue ( const std::string &  val,
bool  required = true,
const std::string &  origin = "" 
)
inline

Set a value on this node directly.

Parameters
valValue to assign.
requiredMust the parameter identified by this node be consumed (may be overwritten later)?
originOrigin of the value (e.g. "foo.yaml line:2 col:35")
Warning
This is direcly applied to this node, which may not have the intended effect

Definition at line 549 of file ParameterTree.hpp.

◆ unrequire()

void sparta::ParameterTree::Node::unrequire ( )
inline

Clear the required count. This is necessary if a parameter is flagged as deprecated or removed in a configuration file. This affects this node only.

Definition at line 574 of file ParameterTree.hpp.

Friends And Related Symbol Documentation

◆ MatchIterator

friend class MatchIterator
friend

Definition at line 760 of file ParameterTree.hpp.


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