Cantera 2.6.0
Classes | Public Member Functions | Static Public Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
AnyMap Class Reference

A map of string keys to values whose type can vary at runtime. More...

#include <AnyMap.h>

Inheritance diagram for AnyMap:
[legend]
Collaboration diagram for AnyMap:
[legend]

Classes

class  Iterator
 Defined to allow use with range-based for loops. More...
 
class  OrderedIterator
 Defined to allow the OrderedProxy class to be used with range-based for loops. More...
 
class  OrderedProxy
 Proxy for iterating over an AnyMap in the defined output ordering. More...
 

Public Member Functions

std::string toYamlString () const
 
AnyValueoperator[] (const std::string &key)
 Get the value of the item stored in key. More...
 
const AnyValueoperator[] (const std::string &key) const
 
AnyValuecreateForYaml (const std::string &key, int line, int column)
 Used to create a new item which will be populated from a YAML input string, where the item with key occurs at the specified line and column within the string. More...
 
const AnyValueat (const std::string &key) const
 Get the value of the item stored in key. More...
 
bool empty () const
 Return boolean indicating whether AnyMap is empty. More...
 
bool hasKey (const std::string &key) const
 Returns true if the map contains an item named key. More...
 
void erase (const std::string &key)
 Erase the value held by key. More...
 
void clear ()
 Erase all items in the mapping. More...
 
void update (const AnyMap &other, bool keepExisting=true)
 Add items from other to this AnyMap. More...
 
std::string keys_str () const
 Return a string listing the keys in this AnyMap, for use in error messages, for example. More...
 
void setMetadata (const std::string &key, const AnyValue &value)
 Set a metadata value that applies to this AnyMap and its children. More...
 
void copyMetadata (const AnyMap &other)
 Copy metadata including input line/column from an existing AnyMap. More...
 
void propagateMetadata (shared_ptr< AnyMap > &file)
 Propagate metadata to any child elements. More...
 
bool getBool (const std::string &key, bool default_) const
 If key exists, return it as a bool, otherwise return default_. More...
 
long int getInt (const std::string &key, long int default_) const
 If key exists, return it as a long int, otherwise return default_. More...
 
double getDouble (const std::string &key, double default_) const
 If key exists, return it as a double, otherwise return default_. More...
 
const std::string & getString (const std::string &key, const std::string &default_) const
 If key exists, return it as a string, otherwise return default_. More...
 
double convert (const std::string &key, const std::string &units) const
 Convert the item stored by the given key to the units specified in units. More...
 
double convert (const std::string &key, const Units &units) const
 
double convert (const std::string &key, const std::string &units, double default_) const
 Convert the item stored by the given key to the units specified in units. More...
 
vector_fp convertVector (const std::string &key, const std::string &units, size_t nMin=npos, size_t nMax=npos) const
 Convert a vector of dimensional values. More...
 
Iterator begin () const
 Defined to allow use with range-based for loops. More...
 
Iterator end () const
 Defined to allow use with range-based for loops. More...
 
OrderedProxy ordered () const
 
size_t size () const
 Returns the number of elements in this map. More...
 
bool operator== (const AnyMap &other) const
 
bool operator!= (const AnyMap &other) const
 
const UnitSystemunits () const
 Return the default units that should be used to convert stored values. More...
 
void applyUnits ()
 Use the supplied UnitSystem to set the default units, and recursively process overrides from nodes named units. More...
 
void applyUnits (shared_ptr< UnitSystem > &units)
 
void setUnits (const UnitSystem &units)
 Set the unit system for this AnyMap. More...
 
void setFlowStyle (bool flow=true)
 Use "flow" style when outputting this AnyMap to YAML. More...
 
- Public Member Functions inherited from AnyBase
void setLoc (int line, int column)
 For values which are derived from an input file, set the line and column of this value in that file. More...
 
const AnyValuegetMetadata (const std::string &key) const
 Get a value from the metadata applicable to the AnyMap tree containing this node. More...
 

Static Public Member Functions

static AnyMap fromYamlFile (const std::string &name, const std::string &parent_name="")
 Create an AnyMap from a YAML file. More...
 
static AnyMap fromYamlString (const std::string &yaml)
 Create an AnyMap from a string containing a YAML document. More...
 
static bool addOrderingRules (const std::string &objectType, const std::vector< std::vector< std::string > > &specs)
 Add global rules for setting the order of elements when outputting AnyMap objects to YAML. More...
 
static void clearCachedFile (const std::string &filename)
 Remove the specified file from the input cache if it is present. More...
 

Private Attributes

std::unordered_map< std::string, AnyValuem_data
 The stored data. More...
 
std::shared_ptr< UnitSystemm_units
 The default units that are used to convert stored values. More...
 

Static Private Attributes

static std::unordered_map< std::string, std::pair< AnyMap, int > > s_cache
 Cache for previously-parsed input (YAML) files. More...
 
static std::unordered_map< std::string, std::vector< std::string > > s_headFields
 Information about fields that should appear first when outputting to YAML. More...
 
static std::unordered_map< std::string, std::vector< std::string > > s_tailFields
 Information about fields that should appear last when outputting to YAML. More...
 

Friends

class AnyValue
 
YAML::Emitter & YAML::operator<< (YAML::Emitter &out, const AnyMap &rhs)
 

Additional Inherited Members

- Protected Attributes inherited from AnyBase
int m_line
 The line where this value occurs in the input file. More...
 
int m_column
 If m_line >= 0, the column where this value occurs in the input file. More...
 
shared_ptr< AnyMapm_metadata
 Metadata relevant to an entire AnyMap tree, such as information about. More...
 

Detailed Description

A map of string keys to values whose type can vary at runtime.

Values in an AnyMap are held by instances of AnyValue. Instances of AnyMap can be nested to form a tree.

Setting elements

AnyMap breakfast;
breakfast["spam"] = 123.4; // Creates a value of type 'double'
breakfast["eggs"] = "scrambled"; // Creates a value of type 'std::string'
// Create a nested AnyMap named "beans" which has a key named "baked"
// whose value is a vector<double>
std::vector<double> v{3.14, 1.59, 2.65};
breakfast["beans"]["baked"] = v;
// Create a nested AnyMap with values of the same type
std::map<std::string, double> breads{{"wheat", 4.0}, {"white", 2.5}};
breakfast["toast"] = breads;
// Equivalent to:
breakfast["toast"]["wheat"] = 4.0
breakfast["toast"]["white"] = 2.5

Accessing elements

double val1 = breakfast["spam"].asDouble();
std::string val2 = breakfast["eggs"].asString();
vector_fp val3 = breakfast["beans"]["baked"].asVector<double>();
std::map<std::string, double> = breakfast["toast"].asMap<double>();
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
Definition: ct_defs.h:184

Checking for elements

try {
breakfast["waffle"].asDouble();
} except (std::exception& err) {
// Exception will be thrown.
// 'breakfast' will have an empty key named 'waffle' unless 'breakfast'
// is a 'const AnyMap'.
}
try {
breakfast.at("grits").asDouble();
} except (std::exception& err) {
// Exception will be thrown and no new key will be added
}
if (breakfast.hasKey("grits")) {
// do something with this entry
}

Checking element types

if (breakfast["sausage"].is<vector<double>>()) {
// access using asVector<double>
} else if (breakfast["sausage"].type() == typeid(vector<std::string>)) {
// access using asVector<std::string>
}

Definition at line 398 of file AnyMap.h.

Constructor & Destructor Documentation

◆ AnyMap()

AnyMap ( )

Definition at line 1336 of file AnyMap.cpp.

Member Function Documentation

◆ fromYamlFile()

AnyMap fromYamlFile ( const std::string &  name,
const std::string &  parent_name = "" 
)
static

Create an AnyMap from a YAML file.

Searches the directory containing the optionally-specified parent file first, followed by the current working directory and the Cantera include path.

Definition at line 1743 of file AnyMap.cpp.

References Cantera::findInputFile(), Cantera::npos, AnyMap::s_cache, AnyBase::setLoc(), AnyMap::setMetadata(), and AnyBase::warn_deprecated.

Referenced by Phase::addElement(), Cantera::addReactions(), RedlichKwongMFTP::getCoeff(), IonsFromNeutralVPSSTP::initThermo(), ThermoPhase::initThermoFile(), Cantera::newInterface(), Cantera::newKinetics(), Cantera::newPhase(), Cantera::newSolution(), Sim1D::restore(), and Cantera::setupPhase().

◆ fromYamlString()

AnyMap fromYamlString ( const std::string &  yaml)
static

Create an AnyMap from a string containing a YAML document.

Definition at line 1727 of file AnyMap.cpp.

References AnyMap::applyUnits(), AnyBase::setLoc(), and AnyMap::setMetadata().

◆ toYamlString()

std::string toYamlString ( ) const

Definition at line 1802 of file AnyMap.cpp.

◆ operator[]() [1/2]

AnyValue & operator[] ( const std::string &  key)

Get the value of the item stored in key.

Definition at line 1341 of file AnyMap.cpp.

References AnyBase::m_column, AnyMap::m_data, AnyBase::m_metadata, AnyValue::propagateMetadata(), AnyValue::setKey(), and AnyBase::setLoc().

◆ operator[]() [2/2]

const AnyValue & operator[] ( const std::string &  key) const

Definition at line 1367 of file AnyMap.cpp.

◆ createForYaml()

AnyValue & createForYaml ( const std::string &  key,
int  line,
int  column 
)

Used to create a new item which will be populated from a YAML input string, where the item with key occurs at the specified line and column within the string.

Definition at line 1377 of file AnyMap.cpp.

References AnyMap::m_data, AnyBase::m_metadata, AnyValue::propagateMetadata(), AnyValue::setKey(), and AnyBase::setLoc().

◆ at()

const AnyValue & at ( const std::string &  key) const

Get the value of the item stored in key.

Raises an exception if the value does not exist.

Definition at line 1391 of file AnyMap.cpp.

References AnyMap::keys_str(), and AnyMap::m_data.

Referenced by Cantera::addReactions(), AnyMap::convert(), AnyMap::convertVector(), AnyValue::getMapWhere(), Cantera::newReactionRate(), and Cantera::setupPhase().

◆ empty()

bool empty ( ) const

◆ hasKey()

bool hasKey ( const std::string &  key) const

Returns true if the map contains an item named key.

Definition at line 1406 of file AnyMap.cpp.

References AnyMap::m_data.

Referenced by Cantera::addReactions(), AnyValue::applyUnits(), AnyMap::applyUnits(), InterfaceReaction2::calculateRateCoeffUnits(), Reaction::checkSpecies(), AnyMap::convert(), AnyMap::getBool(), AnyMap::getDouble(), AnyMap::getInt(), Reaction::getParameters(), AnyMap::getString(), BinarySolutionTabulatedThermo::initThermo(), DebyeHuckel::initThermo(), HMWSoln::initThermo(), IdealMolalSoln::initThermo(), IdealSolidSolnPhase::initThermo(), IdealSolnGasVPSS::initThermo(), IonsFromNeutralVPSSTP::initThermo(), LatticePhase::initThermo(), LatticeSolidPhase::initThermo(), MargulesVPSSTP::initThermo(), MetalPhase::initThermo(), PDSS_ConstVol::initThermo(), PDSS_HKFT::initThermo(), PDSS_IonsFromNeutral::initThermo(), PDSS_SSVol::initThermo(), PureFluidPhase::initThermo(), RedlichKisterVPSSTP::initThermo(), StoichSubstance::initThermo(), SurfPhase::initThermo(), Cantera::newPhase(), Cantera::newSolution(), Cantera::newSpecies(), TransportFactory::newTransport(), Reaction::Reaction(), Domain1D::restore(), StFlow::restore(), Sim1D::restore(), GasKinetics::setDerivativeSettings(), ThirdBody::setEfficiencies(), InterfaceRateBase::setParameters(), Reaction::setParameters(), FalloffRate::setParameters(), ArrheniusBase::setParameters(), ChebyshevRate::setParameters(), PlogRate::setParameters(), PlasmaPhase::setParameters(), LatticePhase::setSiteDensity(), MolalityVPSSTP::setState(), SurfPhase::setState(), ThermoPhase::setState(), StickingCoverage::setStickingParameters(), AnyMap::setUnits(), Cantera::setupPhase(), YamlWriter::toYamlString(), and AnyMap::update().

◆ erase()

void erase ( const std::string &  key)

Erase the value held by key.

Definition at line 1411 of file AnyMap.cpp.

References AnyMap::m_data.

Referenced by LatticeSolidPhase::getParameters(), Reaction::getParameters(), InterfaceReaction2::getParameters(), and ThermoPhase::setState().

◆ clear()

void clear ( )

Erase all items in the mapping.

Definition at line 1416 of file AnyMap.cpp.

References AnyMap::m_data.

Referenced by FalloffRate::getParameters().

◆ update()

void update ( const AnyMap other,
bool  keepExisting = true 
)

Add items from other to this AnyMap.

If keys in other also exist in this AnyMap, the keepExisting option determines which item is used.

Definition at line 1421 of file AnyMap.cpp.

References AnyMap::hasKey().

Referenced by Reaction::getParameters(), TransportData::parameters(), Reaction::parameters(), SpeciesThermoInterpType::parameters(), and ThermoPhase::parameters().

◆ keys_str()

std::string keys_str ( ) const

Return a string listing the keys in this AnyMap, for use in error messages, for example.

Definition at line 1430 of file AnyMap.cpp.

References AnyMap::begin(), and AnyMap::end().

Referenced by AnyMap::at().

◆ setMetadata()

void setMetadata ( const std::string &  key,
const AnyValue value 
)

Set a metadata value that applies to this AnyMap and its children.

Mainly for internal use in reading or writing from files.

Definition at line 1453 of file AnyMap.cpp.

References AnyBase::m_metadata, and AnyMap::propagateMetadata().

Referenced by AnyMap::fromYamlFile(), AnyMap::fromYamlString(), Solution::setSource(), and YamlWriter::toYamlString().

◆ copyMetadata()

void copyMetadata ( const AnyMap other)

Copy metadata including input line/column from an existing AnyMap.

Definition at line 1465 of file AnyMap.cpp.

References AnyBase::m_column, AnyBase::m_line, AnyBase::m_metadata, and AnyMap::propagateMetadata().

Referenced by Reaction::setParameters().

◆ propagateMetadata()

void propagateMetadata ( shared_ptr< AnyMap > &  file)

Propagate metadata to any child elements.

Definition at line 1445 of file AnyMap.cpp.

References AnyMap::m_data, and AnyBase::m_metadata.

Referenced by AnyMap::copyMetadata(), and AnyMap::setMetadata().

◆ getBool()

bool getBool ( const std::string &  key,
bool  default_ 
) const

◆ getInt()

long int getInt ( const std::string &  key,
long int  default_ 
) const

If key exists, return it as a long int, otherwise return default_.

Definition at line 1497 of file AnyMap.cpp.

References AnyMap::hasKey(), and AnyMap::m_data.

◆ getDouble()

double getDouble ( const std::string &  key,
double  default_ 
) const

If key exists, return it as a double, otherwise return default_.

Definition at line 1492 of file AnyMap.cpp.

References AnyMap::hasKey(), and AnyMap::m_data.

Referenced by Cantera::newSpecies(), ReactingSurf1D::restore(), StFlow::restore(), GasKinetics::setDerivativeSettings(), and ThirdBody::setEfficiencies().

◆ getString()

const std::string & getString ( const std::string &  key,
const std::string &  default_ 
) const

◆ convert() [1/3]

double convert ( const std::string &  key,
const std::string &  units 
) const

Convert the item stored by the given key to the units specified in units.

If the stored value is a double, convert it using the default units. If the input is a string, treat this as a dimensioned value, such as '988 kg/m^3' and convert from the specified units.

Definition at line 1508 of file AnyMap.cpp.

References AnyMap::at(), UnitSystem::convert(), and AnyMap::units().

Referenced by Phase::addElement(), DebyeHuckel::addSpecies(), LatticePhase::initThermo(), MetalPhase::initThermo(), PDSS_ConstVol::initThermo(), PDSS_HKFT::initThermo(), StoichSubstance::initThermo(), SurfPhase::initThermo(), and PlasmaPhase::setParameters().

◆ convert() [2/3]

double convert ( const std::string &  key,
const Units units 
) const

Definition at line 1513 of file AnyMap.cpp.

◆ convert() [3/3]

double convert ( const std::string &  key,
const std::string &  units,
double  default_ 
) const

Convert the item stored by the given key to the units specified in units.

If the stored value is a double, convert it using the default units. If the input is a string, treat this as a dimensioned value, such as '988 kg/m^3' and convert from the specified units. If the key is missing, the default_ value is returned.

Definition at line 1518 of file AnyMap.cpp.

References AnyMap::at(), UnitSystem::convert(), AnyMap::hasKey(), and AnyMap::units().

◆ convertVector()

vector_fp convertVector ( const std::string &  key,
const std::string &  units,
size_t  nMin = npos,
size_t  nMax = npos 
) const

Convert a vector of dimensional values.

For each item in the vector, if the stored value is a double, convert it using the default units. If the value is a string, treat it as a dimensioned value, such as '988 kg/m^3', and convert from the specified units.

Parameters
keyLocation of the vector in this AnyMap
unitsUnits to convert to
nMinMinimum allowed length of the vector. If nMax is not specified, this is also taken to be the maximum length. An exception is thrown if this condition is not met.
nMaxMaximum allowed length of the vector. An exception is thrown if this condition is not met.

Definition at line 1528 of file AnyMap.cpp.

References AnyMap::at(), UnitSystem::convert(), and AnyMap::units().

Referenced by BinarySolutionTabulatedThermo::initThermo().

◆ begin()

Iterator begin ( ) const
inline

Defined to allow use with range-based for loops.

Definition at line 531 of file AnyMap.h.

References AnyMap::m_data.

Referenced by AnyMap::keys_str().

◆ end()

Iterator end ( ) const
inline

Defined to allow use with range-based for loops.

Definition at line 536 of file AnyMap.h.

References AnyMap::m_data.

Referenced by AnyMap::keys_str().

◆ ordered()

OrderedProxy ordered ( ) const
inline

Definition at line 587 of file AnyMap.h.

◆ size()

size_t size ( ) const
inline

◆ operator==()

bool operator== ( const AnyMap other) const

Definition at line 1642 of file AnyMap.cpp.

◆ operator!=()

bool operator!= ( const AnyMap other) const

Definition at line 1660 of file AnyMap.cpp.

◆ units()

const UnitSystem & units ( ) const
inline

◆ applyUnits() [1/2]

void applyUnits ( )

Use the supplied UnitSystem to set the default units, and recursively process overrides from nodes named units.

If a units node is present in a map that contains other keys, the specified units are taken to be the defaults for that map. If the map contains only a units node, and is the first item in a list of maps, then the specified units are taken to be the defaults for all the maps in the list.

After being processed, the units nodes are removed. This function is called automatically by the fromYamlFile() and fromYamlString() constructors.

Warning
This function is an experimental part of the Cantera API and may be changed or removed without notice.

Definition at line 1665 of file AnyMap.cpp.

References AnyMap::applyUnits(), and AnyMap::m_units.

Referenced by AnyMap::applyUnits(), AnyValue::applyUnits(), and AnyMap::fromYamlString().

◆ applyUnits() [2/2]

void applyUnits ( shared_ptr< UnitSystem > &  units)
See also
applyUnits(const UnitSystem&)

Definition at line 1670 of file AnyMap.cpp.

References AnyMap::hasKey(), AnyMap::m_data, AnyMap::m_units, and AnyMap::units().

◆ setUnits()

void setUnits ( const UnitSystem units)

Set the unit system for this AnyMap.

The applyUnits() method should be called on the root AnyMap object after all desired calls to setUnits() in the tree have been made.

Definition at line 1686 of file AnyMap.cpp.

References UnitSystem::getDelta(), AnyMap::hasKey(), AnyMap::m_data, AnyMap::m_units, and AnyMap::units().

Referenced by YamlWriter::toYamlString().

◆ setFlowStyle()

void setFlowStyle ( bool  flow = true)

◆ addOrderingRules()

bool addOrderingRules ( const std::string &  objectType,
const std::vector< std::vector< std::string > > &  specs 
)
static

Add global rules for setting the order of elements when outputting AnyMap objects to YAML.

Enables specifying keys that should appear at either the beginning or end of the generated YAML mapping. Only programmatically-added keys are rearranged. Keys which come from YAML input retain their existing ordering, and are output after programmatically-added keys.

This function should be called exactly once for any given spec that is to be added. To facilitate this, the method returns a bool so that it can be called as part of initializing a static variable. To avoid spurious compiler warnings about unused variables, the following structure can be used:

static bool reg = AnyMap::addOrderingRules("Reaction",
{{"head", "equation"}, {"tail", "duplicate"}});
if (reg) {
reactionMap["__type__"] = "Reaction";
}
static bool addOrderingRules(const std::string &objectType, const std::vector< std::vector< std::string > > &specs)
Add global rules for setting the order of elements when outputting AnyMap objects to YAML.
Definition: AnyMap.cpp:1702
Parameters
objectTypeApply rules to maps where the hidden __type__ key has the corresponding value.
specsA list of rule specifications. Each rule consists of two strings. The first string is either "head" or "tail", and the second string is the name of a key
Returns
true, to facilitate static initialization

Definition at line 1702 of file AnyMap.cpp.

References AnyMap::s_headFields, and AnyMap::s_tailFields.

Referenced by ThermoPhase::getParameters(), and Reaction::parameters().

◆ clearCachedFile()

void clearCachedFile ( const std::string &  filename)
static

Remove the specified file from the input cache if it is present.

Definition at line 1719 of file AnyMap.cpp.

References Cantera::findInputFile(), and AnyMap::s_cache.

Friends And Related Function Documentation

◆ AnyValue

friend class AnyValue
friend

Definition at line 686 of file AnyMap.h.

Member Data Documentation

◆ m_data

std::unordered_map<std::string, AnyValue> m_data
private

◆ m_units

std::shared_ptr<UnitSystem> m_units
private

The default units that are used to convert stored values.

Definition at line 669 of file AnyMap.h.

Referenced by AnyMap::applyUnits(), AnyMap::setUnits(), and AnyMap::units().

◆ s_cache

std::unordered_map< std::string, std::pair< AnyMap, int > > s_cache
staticprivate

Cache for previously-parsed input (YAML) files.

The key is the full path to the file, and the second element of the value is the last-modified time for the file, which is used to enable change detection.

Definition at line 674 of file AnyMap.h.

Referenced by AnyMap::clearCachedFile(), and AnyMap::fromYamlFile().

◆ s_headFields

std::unordered_map< std::string, std::vector< std::string > > s_headFields
staticprivate

Information about fields that should appear first when outputting to YAML.

Keys in this map are matched to __type__ keys in AnyMap objects, and values are a list of field names.

Definition at line 679 of file AnyMap.h.

Referenced by AnyMap::addOrderingRules().

◆ s_tailFields

std::unordered_map< std::string, std::vector< std::string > > s_tailFields
staticprivate

Information about fields that should appear last when outputting to YAML.

Keys in this map are matched to __type__ keys in AnyMap objects, and values are a list of field names.

Definition at line 684 of file AnyMap.h.

Referenced by AnyMap::addOrderingRules().


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