Cantera  2.5.1
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...
 

Public Member Functions

AnyValueoperator[] (const std::string &key)
 Get the value of the item stored in key. More...
 
const AnyValueoperator[] (const std::string &key) const
 
const AnyValueat (const std::string &key) const
 Get the value of the item stored in key. 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...
 
std::string keys_str () const
 Return a string listing the keys in this AnyMap, e.g. More...
 
void setMetadata (const std::string &key, const AnyValue &value)
 Set a metadata value that applies to this AnyMap and its children. 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...
 
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 (const UnitSystem &units)
 Use the supplied UnitSystem to set the default units, and recursively process overrides from nodes named units. 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...
 

Private Attributes

std::unordered_map< std::string, AnyValuem_data
 The stored data. More...
 
UnitSystem m_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...
 

Friends

class AnyValue
 

Additional Inherited Members

- Protected Attributes inherited from AnyBase
int m_line
 Line where this node occurs in the input file. More...
 
int m_column
 Column where this node 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:180

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 359 of file AnyMap.h.

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 1156 of file AnyMap.cpp.

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

Referenced by IonsFromNeutralVPSSTP::initThermo(), and ThermoPhase::initThermoFile().

◆ fromYamlString()

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

Create an AnyMap from a string containing a YAML document.

Definition at line 1140 of file AnyMap.cpp.

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

◆ operator[]()

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

Get the value of the item stored in key.

Definition at line 942 of file AnyMap.cpp.

References AnyBase::m_column, AnyMap::m_data, AnyBase::m_line, 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 974 of file AnyMap.cpp.

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

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

◆ hasKey()

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

◆ erase()

void erase ( const std::string &  key)

Erase the value held by key.

Definition at line 989 of file AnyMap.cpp.

References AnyMap::m_data.

Referenced by ThermoPhase::setState().

◆ clear()

void clear ( )

Erase all items in the mapping.

Definition at line 994 of file AnyMap.cpp.

References AnyMap::m_data.

◆ keys_str()

std::string keys_str ( ) const

Return a string listing the keys in this AnyMap, e.g.

for use in error messages

Definition at line 999 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 1022 of file AnyMap.cpp.

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

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

◆ propagateMetadata()

void propagateMetadata ( shared_ptr< AnyMap > &  file)

Propagate metadata to any child elements.

Definition at line 1014 of file AnyMap.cpp.

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

Referenced by AnyMap::setMetadata().

◆ getBool()

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

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

Definition at line 1034 of file AnyMap.cpp.

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

Referenced by Cantera::addReactions(), PDSS_IonsFromNeutral::initThermo(), and Cantera::setupPhase().

◆ 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 1044 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 1039 of file AnyMap.cpp.

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

Referenced by Cantera::newSpecies().

◆ getString()

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

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

Definition at line 1049 of file AnyMap.cpp.

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

Referenced by IonsFromNeutralVPSSTP::initThermo(), Cantera::newKinetics(), Cantera::newTransportData(), and Cantera::setupPhase().

◆ convert() [1/2]

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, e.g. '988 kg/m^3' and convert from the specified units.

Definition at line 1055 of file AnyMap.cpp.

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

Referenced by DebyeHuckel::addSpecies(), ConstDensityThermo::initThermo(), FixedChemPotSSTP::initThermo(), LatticePhase::initThermo(), MetalPhase::initThermo(), PDSS_HKFT::initThermo(), StoichSubstance::initThermo(), and SurfPhase::initThermo().

◆ convert() [2/2]

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, e.g. '988 kg/m^3' and convert from the specified units. If the key is missing, the default_ value is returned.

Definition at line 1065 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, e.g. '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 1075 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 474 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 479 of file AnyMap.h.

References AnyMap::m_data.

Referenced by AnyMap::keys_str().

◆ size()

size_t size ( ) const
inline

Returns the number of elements in this map.

Definition at line 484 of file AnyMap.h.

References AnyMap::m_data.

Referenced by BinarySolutionTabulatedThermo::initThermo(), and ThermoPhase::setState().

◆ units()

const UnitSystem& units ( ) const
inline

Return the default units that should be used to convert stored values.

Definition at line 492 of file AnyMap.h.

References AnyMap::m_units.

Referenced by AnyMap::applyUnits(), AnyMap::convert(), AnyMap::convertVector(), PDSS_HKFT::initThermo(), PDSS_SSVol::initThermo(), RedlichKwongMFTP::initThermo(), and Cantera::newSpecies().

◆ applyUnits()

void applyUnits ( const UnitSystem units)

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, so this function should be called only once, on the root AnyMap. 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 1128 of file AnyMap.cpp.

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

Referenced by AnyMap::fromYamlString().

Member Data Documentation

◆ m_data

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

◆ m_units

UnitSystem m_units
private

The default units that are used to convert stored values.

Definition at line 517 of file AnyMap.h.

Referenced by AnyMap::applyUnits(), 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 522 of file AnyMap.h.

Referenced by AnyMap::fromYamlFile().


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