|
Nektar++
|
A single node of a format-neutral session configuration tree. More...
#include <SessionNode.h>
Public Types | |
| typedef std::vector< std::pair< std::string, std::string > > | AttributeMap |
| Ordered list of (name, value) attribute pairs. | |
| typedef std::vector< SessionNode > | ChildList |
| Ordered list of child nodes. | |
Public Member Functions | |
| SessionNode ()=default | |
| Construct an empty, unnamed node. | |
| SessionNode (std::string name, std::string text="") | |
Construct a node called name, optionally carrying text. | |
| const std::string & | GetName () const |
| Returns the name of this node, e.g. "FORCE". | |
| const std::string & | GetPath () const |
| Returns the path of this node from the root of the tree it belongs to, e.g. "FORCING/FORCE/COEFF". | |
| const std::string & | GetText () const |
| Returns the text carried by this node, or an empty string if it carries none. | |
| bool | DefinesText () const |
| Returns true if this node carries a non-empty block of text. | |
| std::string | Describe () const |
| Returns a short human-readable rendering of this node, naming its path and its attributes, for use in diagnostics. | |
| bool | DefinesAttribute (const std::string &name) const |
Returns true if this node has an attribute called name. | |
| const std::string & | GetAttribute (const std::string &name) const |
Returns the value of attribute name; a fatal error is raised if the attribute is not defined. | |
| std::string | GetAttribute (const std::string &name, const std::string &def) const |
Returns the value of attribute name, or def if it is not defined. | |
| const AttributeMap & | GetAttributes () const |
| Returns all of this node's attributes, in the order they were supplied. | |
| template<typename T > | |
| T | GetAttributeAs (const std::string &name) const |
Returns attribute name converted to a T; a fatal error is raised if the attribute is not defined or does not convert. | |
| template<typename T > | |
| T | GetAttributeAs (const std::string &name, const T &def) const |
Returns attribute name converted to a T, or def if the attribute is not defined; a fatal error is raised if it is defined but does not convert. | |
| bool | DefinesChild (const std::string &name) const |
Returns true if this node has at least one child called name. | |
| const SessionNode * | GetChildPtr (const std::string &name) const |
Returns the first child called name, or nullptr if there is none. | |
| const SessionNode & | GetChild (const std::string &name) const |
Returns the first child called name; a fatal error is raised if there is none. | |
| const ChildList & | GetChildren () const |
| Returns every child of this node, in document order. | |
| std::vector< const SessionNode * > | GetChildren (const std::string &name) const |
Returns every child called name, in document order. | |
| const std::string & | GetValue (const std::string &name) const |
Returns the text of the first child called name. | |
| std::string | GetValue (const std::string &name, const std::string &def) const |
Returns the text of the first child called name, or def if there is no such child. | |
| template<typename T > | |
| T | GetValueAs (const std::string &name) const |
Returns the text of the first child called name converted to a T; a fatal error is raised if there is no such child or it does not convert. | |
| template<typename T > | |
| T | GetValueAs (const std::string &name, const T &def) const |
Returns the text of the first child called name converted to a T, or def if there is no such child; a fatal error is raised if the child exists but does not convert. | |
| const SessionNode * | GetTimeLevel (const size_t timeLevel, const bool enableCheck=true) const |
Resolves the TIMELEVEL indirection used by parallel-in-time runs. | |
| void | AddChild (SessionNode child) |
Appends child to this node's list of children. | |
| void | SetAttribute (std::string name, std::string value) |
Sets attribute name to value, replacing any existing value. | |
| void | SetText (std::string text) |
| Sets the text carried by this node. | |
Private Member Functions | |
| void | UpdatePath (const std::string &parentPath) |
Recomputes m_path from parentPath, recursing into m_children. | |
| void | ThrowConversionError (const std::string &kind, const std::string &name, const std::string &str) const |
| Raises a fatal error describing a failed string conversion. | |
Private Attributes | |
| std::string | m_name |
| Name of this node. | |
| std::string | m_path |
| Path of this node from the root of its tree; see GetPath(). | |
| std::string | m_text |
| Text carried by this node, if any. | |
| AttributeMap | m_attributes |
| Attributes of this node, in the order supplied. | |
| ChildList | m_children |
| Children of this node, in the order supplied. | |
A single node of a format-neutral session configuration tree.
A SessionNode is a named node carrying an optional block of text, an ordered list of named string attributes, and an ordered list of child nodes. Child names need not be unique: repeated names are how repeated entries (such as the individual FORCE entries of a FORCING section) are represented, and document order is preserved.
The point of the class is that it carries no knowledge of the format the configuration was written in. Solver components that need to read their own bespoke configuration section – forcings, mappings, couplings and so on – take a SessionNode rather than reaching into the session's XML document, so that the same components work unchanged whether the configuration arrived from an XML file, some other file format, or was assembled programmatically by an embedding application.
Trees are cheap to build and are expected to be small (configuration, not data), so nodes own their children by value and are freely copyable.
Reading a section typically looks like:
Accessors come in three flavours: a Defines... predicate; a form taking a default, which is used when the entry is absent; and a form taking no default, which raises a fatal error naming the node's path if the entry is absent. The ...As<T> forms additionally convert the string, raising a fatal error if the text is not a valid T.
Definition at line 120 of file SessionNode.h.
| typedef std::vector<std::pair<std::string, std::string> > Nektar::LibUtilities::SessionNode::AttributeMap |
Ordered list of (name, value) attribute pairs.
Definition at line 124 of file SessionNode.h.
| typedef std::vector<SessionNode> Nektar::LibUtilities::SessionNode::ChildList |
Ordered list of child nodes.
Definition at line 126 of file SessionNode.h.
|
default |
Construct an empty, unnamed node.
|
explicit |
Construct a node called name, optionally carrying text.
Definition at line 212 of file SessionNode.cpp.
| void Nektar::LibUtilities::SessionNode::AddChild | ( | SessionNode | child | ) |
Appends child to this node's list of children.
Intended for the readers that build trees and for applications assembling configuration programmatically. Note that this rewrites the paths of child and its descendants, so any pointer or reference previously obtained from child is invalidated.
Definition at line 452 of file SessionNode.cpp.
References m_children, and m_path.
Referenced by Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), and Nektar::SessionNodeUnitTests::BuildForcingSection().
| bool Nektar::LibUtilities::SessionNode::DefinesAttribute | ( | const std::string & | name | ) | const |
Returns true if this node has an attribute called name.
Definition at line 274 of file SessionNode.cpp.
References m_attributes.
Referenced by Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), GetAttributeAs(), Nektar::AcousticSystem::v_InitObject(), and Nektar::Dummy::v_InitObject().
| bool Nektar::LibUtilities::SessionNode::DefinesChild | ( | const std::string & | name | ) | const |
Returns true if this node has at least one child called name.
Definition at line 333 of file SessionNode.cpp.
References GetChildPtr().
Referenced by Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SolverUtils::ForcingAbsorption::CalcAbsorption(), Nektar::ForcingMovingBody::CheckIsFromFile(), Nektar::RigidSolver::GetFilterInfo(), GetValueAs(), Nektar::RigidSolver::InitBodySolver(), Nektar::RigidSolver::LoadParameters(), Nektar::GlobalMapping::Mapping::v_InitObject(), Nektar::SolverUtils::ForcingAbsorption::v_InitObject(), Nektar::SolverUtils::ForcingBody::v_InitObject(), Nektar::SolverUtils::ForcingNoise::v_InitObject(), Nektar::ForcingQuasi1D::v_InitObject(), and Nektar::SolverUtils::ForcingLorentz::v_InitObject().
| bool Nektar::LibUtilities::SessionNode::DefinesText | ( | ) | const |
Returns true if this node carries a non-empty block of text.
Definition at line 245 of file SessionNode.cpp.
References m_text.
Referenced by Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), and Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE().
| std::string Nektar::LibUtilities::SessionNode::Describe | ( | ) | const |
Returns a short human-readable rendering of this node, naming its path and its attributes, for use in diagnostics.
For example: COUPLING/I [PROPERTY = "RECEIVESTEPS", VALUE = "1"]. This is deliberately not XML: it describes the node as the rest of the code sees it, whatever format it was read from.
Definition at line 253 of file SessionNode.cpp.
References m_attributes, and m_path.
Referenced by Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE().
| const std::string & Nektar::LibUtilities::SessionNode::GetAttribute | ( | const std::string & | name | ) | const |
Returns the value of attribute name; a fatal error is raised if the attribute is not defined.
Definition at line 289 of file SessionNode.cpp.
References Nektar::ErrorUtil::efatal, m_attributes, m_path, and NEKERROR.
Referenced by Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), GetAttributeAs(), Nektar::ForcingMovingBody::InitialiseFilter(), Nektar::GlobalMapping::Mapping::Load(), Nektar::Stimulus::Stimulus(), Nektar::SolverUtils::Coupling::v_Init(), Nektar::AcousticSystem::v_InitObject(), and Nektar::Dummy::v_InitObject().
| std::string Nektar::LibUtilities::SessionNode::GetAttribute | ( | const std::string & | name, |
| const std::string & | def | ||
| ) | const |
Returns the value of attribute name, or def if it is not defined.
Definition at line 309 of file SessionNode.cpp.
References m_attributes.
|
inline |
Returns attribute name converted to a T; a fatal error is raised if the attribute is not defined or does not convert.
Definition at line 187 of file SessionNode.h.
References Nektar::LibUtilities::SessionNodeConvert::FromString(), GetAttribute(), and ThrowConversionError().
Referenced by Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE().
|
inline |
Returns attribute name converted to a T, or def if the attribute is not defined; a fatal error is raised if it is defined but does not convert.
Definition at line 202 of file SessionNode.h.
References DefinesAttribute().
| const SessionNode::AttributeMap & Nektar::LibUtilities::SessionNode::GetAttributes | ( | ) | const |
Returns all of this node's attributes, in the order they were supplied.
Definition at line 325 of file SessionNode.cpp.
References m_attributes.
Referenced by Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE().
| const SessionNode & Nektar::LibUtilities::SessionNode::GetChild | ( | const std::string & | name | ) | const |
Returns the first child called name; a fatal error is raised if there is none.
Definition at line 356 of file SessionNode.cpp.
References Nektar::ErrorUtil::efatal, GetChildPtr(), m_path, and NEKERROR.
Referenced by Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), GetValue(), and Nektar::Stimulus::Stimulus().
| const SessionNode * Nektar::LibUtilities::SessionNode::GetChildPtr | ( | const std::string & | name | ) | const |
Returns the first child called name, or nullptr if there is none.
Definition at line 341 of file SessionNode.cpp.
References m_children.
Referenced by Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), DefinesChild(), GetChild(), and GetValue().
| const SessionNode::ChildList & Nektar::LibUtilities::SessionNode::GetChildren | ( | ) | const |
Returns every child of this node, in document order.
Definition at line 375 of file SessionNode.cpp.
References m_children.
Referenced by Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::IncNavierStokes::DefinedForcing(), GetTimeLevel(), Nektar::ForcingMovingBody::InitialiseFilter(), Nektar::SolverUtils::Forcing::Load(), Nektar::Stimulus::LoadStimuli(), and Nektar::SolverUtils::Coupling::v_Init().
| std::vector< const SessionNode * > Nektar::LibUtilities::SessionNode::GetChildren | ( | const std::string & | name | ) | const |
Returns every child called name, in document order.
Definition at line 383 of file SessionNode.cpp.
References m_children.
| const std::string & Nektar::LibUtilities::SessionNode::GetName | ( | ) | const |
Returns the name of this node, e.g. "FORCE".
Definition at line 221 of file SessionNode.cpp.
References m_name.
Referenced by Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), and Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE().
| const std::string & Nektar::LibUtilities::SessionNode::GetPath | ( | ) | const |
Returns the path of this node from the root of the tree it belongs to, e.g. "FORCING/FORCE/COEFF".
The path is maintained by AddChild() and is used to make error messages intelligible; it carries no other meaning.
Definition at line 229 of file SessionNode.cpp.
References m_path.
Referenced by Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), and Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE().
| const std::string & Nektar::LibUtilities::SessionNode::GetText | ( | ) | const |
Returns the text carried by this node, or an empty string if it carries none.
Definition at line 237 of file SessionNode.cpp.
References m_text.
Referenced by Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE().
| const SessionNode * Nektar::LibUtilities::SessionNode::GetTimeLevel | ( | const size_t | timeLevel, |
| const bool | enableCheck = true |
||
| ) | const |
Resolves the TIMELEVEL indirection used by parallel-in-time runs.
A section may replace its contents with a series of TIMELEVEL children, each carrying a VALUE attribute, so that different time levels see different settings.
If this node has no TIMELEVEL children then it is itself returned, so that the common case of a section which does not vary with time level needs no special handling by the caller. Otherwise the child matching timeLevel is returned, or nullptr if none matches – meaning that this section simply does not apply at timeLevel, and the caller should skip it.
| timeLevel | The time level to select. |
| enableCheck | If true, a fatal error is raised, rather than nullptr returned, when this node has TIMELEVEL children but none matches timeLevel. |
Definition at line 420 of file SessionNode.cpp.
References Nektar::ErrorUtil::efatal, GetChildren(), m_path, and NEKERROR.
Referenced by Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::GlobalMapping::Mapping::Load(), Nektar::SolverUtils::Forcing::Load(), and Nektar::GlobalMapping::Mapping::ReplaceField().
| const std::string & Nektar::LibUtilities::SessionNode::GetValue | ( | const std::string & | name | ) | const |
Returns the text of the first child called name.
This is the dominant way of reading a configuration section, in which each setting is a child node carrying a single value, as in <COEFF> SpongeCoefficient </COEFF>. A fatal error is raised if there is no such child.
Definition at line 402 of file SessionNode.cpp.
References GetChild(), and m_text.
Referenced by Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SolverUtils::ForcingAbsorption::CalcAbsorption(), Nektar::ForcingMovingBody::CheckIsFromFile(), Nektar::RigidSolver::GetFilterInfo(), GetValueAs(), Nektar::RigidSolver::InitBodySolver(), Nektar::RigidSolver::LoadParameters(), Nektar::GlobalMapping::Mapping::v_InitObject(), Nektar::SolverUtils::ForcingAbsorption::v_InitObject(), Nektar::SolverUtils::ForcingBody::v_InitObject(), Nektar::ForcingQuasi1D::v_InitObject(), and Nektar::SolverUtils::ForcingLorentz::v_InitObject().
| std::string Nektar::LibUtilities::SessionNode::GetValue | ( | const std::string & | name, |
| const std::string & | def | ||
| ) | const |
Returns the text of the first child called name, or def if there is no such child.
Definition at line 410 of file SessionNode.cpp.
References GetChildPtr(), and m_text.
|
inline |
Returns the text of the first child called name converted to a T; a fatal error is raised if there is no such child or it does not convert.
Definition at line 253 of file SessionNode.h.
References Nektar::LibUtilities::SessionNodeConvert::FromString(), GetValue(), and ThrowConversionError().
Referenced by Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::ProtocolS1::ProtocolS1(), Nektar::ProtocolS1S2::ProtocolS1S2(), Nektar::ProtocolSingle::ProtocolSingle(), Nektar::StimulusCirc::StimulusCirc(), Nektar::StimulusPoint::StimulusPoint(), Nektar::StimulusRect::StimulusRect(), Nektar::GlobalMapping::Mapping::v_InitObject(), and Nektar::SolverUtils::ForcingNoise::v_InitObject().
|
inline |
Returns the text of the first child called name converted to a T, or def if there is no such child; a fatal error is raised if the child exists but does not convert.
Definition at line 268 of file SessionNode.h.
References DefinesChild().
| void Nektar::LibUtilities::SessionNode::SetAttribute | ( | std::string | name, |
| std::string | value | ||
| ) |
Sets attribute name to value, replacing any existing value.
Definition at line 461 of file SessionNode.cpp.
References m_attributes.
Referenced by Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BOOST_AUTO_TEST_CASE(), Nektar::SessionNodeUnitTests::BuildForcingSection(), and Nektar::LibUtilities::SessionNodeFromXml().
| void Nektar::LibUtilities::SessionNode::SetText | ( | std::string | text | ) |
Sets the text carried by this node.
Definition at line 478 of file SessionNode.cpp.
References m_text.
|
private |
Raises a fatal error describing a failed string conversion.
Definition at line 499 of file SessionNode.cpp.
References Nektar::ErrorUtil::efatal, m_path, and NEKERROR.
Referenced by GetAttributeAs(), and GetValueAs().
|
private |
Recomputes m_path from parentPath, recursing into m_children.
Definition at line 486 of file SessionNode.cpp.
References m_children, m_name, and m_path.
|
private |
Attributes of this node, in the order supplied.
Definition at line 328 of file SessionNode.h.
Referenced by DefinesAttribute(), Describe(), GetAttribute(), GetAttribute(), GetAttributes(), and SetAttribute().
|
private |
Children of this node, in the order supplied.
Definition at line 330 of file SessionNode.h.
Referenced by AddChild(), GetChildPtr(), GetChildren(), GetChildren(), and UpdatePath().
|
private |
Name of this node.
Definition at line 322 of file SessionNode.h.
Referenced by GetName(), SessionNode(), and UpdatePath().
|
private |
Path of this node from the root of its tree; see GetPath().
Definition at line 324 of file SessionNode.h.
Referenced by AddChild(), Describe(), GetAttribute(), GetChild(), GetPath(), GetTimeLevel(), SessionNode(), ThrowConversionError(), and UpdatePath().
|
private |
Text carried by this node, if any.
Definition at line 326 of file SessionNode.h.
Referenced by DefinesText(), GetText(), GetValue(), GetValue(), and SetText().