Cantera
3.1.0a1
|
A map of string keys to values whose type can vary at runtime. More...
#include <AnyMap.h>
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.
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 | |
string | toYamlString () const |
AnyValue & | operator[] (const string &key) |
Get the value of the item stored in key . More... | |
const AnyValue & | operator[] (const string &key) const |
AnyValue & | createForYaml (const 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 AnyValue & | at (const 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 string &key) const |
Returns true if the map contains an item named key . More... | |
void | erase (const 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... | |
string | keys_str () const |
Return a string listing the keys in this AnyMap, for use in error messages, for example. More... | |
set< string > | keys () const |
Return an unordered set of keys. More... | |
void | setMetadata (const 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 string &key, bool default_) const |
If key exists, return it as a bool , otherwise return default_ . More... | |
long int | getInt (const string &key, long int default_) const |
If key exists, return it as a long int , otherwise return default_ . More... | |
double | getDouble (const string &key, double default_) const |
If key exists, return it as a double , otherwise return default_ . More... | |
const string & | getString (const string &key, const string &default_) const |
If key exists, return it as a string , otherwise return default_ . More... | |
double | convert (const string &key, const string &units) const |
Convert the item stored by the given key to the units specified in units . More... | |
double | convert (const string &key, const Units &units) const |
double | convert (const string &key, const string &units, double default_) const |
Convert the item stored by the given key to the units specified in units . More... | |
vector< double > | convertVector (const string &key, const 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 UnitSystem & | units () const |
Return the default units that should be used to convert stored values. More... | |
shared_ptr< UnitSystem > | unitsShared () 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) |
See applyUnits() More... | |
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 AnyValue & | getMetadata (const 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 string &name, const string &parent_name="") |
Create an AnyMap from a YAML file. More... | |
static AnyMap | fromYamlString (const string &yaml) |
Create an AnyMap from a string containing a YAML document. More... | |
static bool | addOrderingRules (const string &objectType, const vector< vector< string >> &specs) |
Add global rules for setting the order of elements when outputting AnyMap objects to YAML. More... | |
static void | clearCachedFile (const string &filename) |
Remove the specified file from the input cache if it is present. More... | |
Private Attributes | |
std::unordered_map< string, AnyValue > | m_data |
The stored data. More... | |
shared_ptr< UnitSystem > | m_units |
The default units that are used to convert stored values. More... | |
Static Private Attributes | |
static std::unordered_map< string, pair< AnyMap, std::filesystem::file_time_type > > | s_cache |
Cache for previously-parsed input (YAML) files. More... | |
static std::unordered_map< string, vector< string > > | s_headFields |
Information about fields that should appear first when outputting to YAML. More... | |
static std::unordered_map< string, vector< 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 = -1 |
The line where this value occurs in the input file. More... | |
int | m_column = 0 |
If m_line >= 0, the column where this value occurs in the input file. More... | |
shared_ptr< AnyMap > | m_metadata |
Metadata relevant to an entire AnyMap tree, such as information about. More... | |
|
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 1771 of file AnyMap.cpp.
|
static |
Create an AnyMap from a string containing a YAML document.
Definition at line 1755 of file AnyMap.cpp.
AnyValue & operator[] | ( | const string & | key | ) |
Get the value of the item stored in key
.
Definition at line 1362 of file AnyMap.cpp.
AnyValue & createForYaml | ( | const 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 1396 of file AnyMap.cpp.
const AnyValue & at | ( | const string & | key | ) | const |
Get the value of the item stored in key
.
Raises an exception if the value does not exist.
Definition at line 1408 of file AnyMap.cpp.
bool empty | ( | ) | const |
Return boolean indicating whether AnyMap is empty.
Definition at line 1418 of file AnyMap.cpp.
bool hasKey | ( | const string & | key | ) | const |
Returns true
if the map contains an item named key
.
Definition at line 1423 of file AnyMap.cpp.
void erase | ( | const string & | key | ) |
Erase the value held by key
.
Definition at line 1428 of file AnyMap.cpp.
void clear | ( | ) |
Erase all items in the mapping.
Definition at line 1433 of file AnyMap.cpp.
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 1438 of file AnyMap.cpp.
string keys_str | ( | ) | const |
Return a string listing the keys in this AnyMap, for use in error messages, for example.
Definition at line 1447 of file AnyMap.cpp.
set< string > keys | ( | ) | const |
Return an unordered set of keys.
Definition at line 1462 of file AnyMap.cpp.
void setMetadata | ( | const 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 1481 of file AnyMap.cpp.
void copyMetadata | ( | const AnyMap & | other | ) |
Copy metadata including input line/column from an existing AnyMap.
Definition at line 1493 of file AnyMap.cpp.
void propagateMetadata | ( | shared_ptr< AnyMap > & | file | ) |
Propagate metadata to any child elements.
Definition at line 1473 of file AnyMap.cpp.
bool getBool | ( | const string & | key, |
bool | default_ | ||
) | const |
If key
exists, return it as a bool
, otherwise return default_
.
Definition at line 1515 of file AnyMap.cpp.
long int getInt | ( | const string & | key, |
long int | default_ | ||
) | const |
If key
exists, return it as a long int
, otherwise return default_
.
Definition at line 1525 of file AnyMap.cpp.
double getDouble | ( | const string & | key, |
double | default_ | ||
) | const |
If key
exists, return it as a double
, otherwise return default_
.
Definition at line 1520 of file AnyMap.cpp.
const string & getString | ( | const string & | key, |
const string & | default_ | ||
) | const |
If key
exists, return it as a string
, otherwise return default_
.
Definition at line 1530 of file AnyMap.cpp.
double convert | ( | const string & | key, |
const 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 1535 of file AnyMap.cpp.
double convert | ( | const string & | key, |
const 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 1545 of file AnyMap.cpp.
vector< double > convertVector | ( | const string & | key, |
const 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.
key | Location of the vector in this AnyMap |
units | Units to convert to |
nMin | Minimum 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. |
nMax | Maximum allowed length of the vector. An exception is thrown if this condition is not met. |
Definition at line 1555 of file AnyMap.cpp.
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
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.
Definition at line 1693 of file AnyMap.cpp.
void applyUnits | ( | shared_ptr< UnitSystem > & | units | ) |
See applyUnits()
Definition at line 1698 of file AnyMap.cpp.
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 1714 of file AnyMap.cpp.
void setFlowStyle | ( | bool | flow = true | ) |
Use "flow" style when outputting this AnyMap to YAML.
Definition at line 1726 of file AnyMap.cpp.
|
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:
objectType | Apply rules to maps where the hidden __type__ key has the corresponding value. |
specs | A 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 |
true
, to facilitate static initialization Definition at line 1730 of file AnyMap.cpp.
|
static |
Remove the specified file from the input cache if it is present.
Definition at line 1747 of file AnyMap.cpp.
|
private |
|
private |
|
staticprivate |
|
staticprivate |
|
staticprivate |