A wrapper for a variable whose type is determined at runtime. More...
#include <AnyMap.h>
A wrapper for a variable whose type is determined at runtime.
Instances of AnyValue are used as values in an AnyMap. Values are converted to a concrete type using the templated as() method or convenience methods such as asString() and asDouble(). See AnyMap for usage examples.
Elements are set using assignment, and the assignment operator has been overloaded for specific types so that only those types are allowed to be used in an AnyValue. The allowed types are:
AnyMap
double
long int
bool
string
vector
of any of the above Public Member Functions | |
bool | operator== (const AnyValue &other) const |
bool | operator!= (const AnyValue &other) const |
AnyValue & | operator[] (const string &key) |
If this AnyValue is an AnyMap, return the value stored in key . | |
const AnyValue & | operator[] (const string &key) const |
bool | hasKey (const string &key) const |
Returns true if this AnyValue is an AnyMap and that map contains a key with the given name. | |
void | setKey (const string &key) |
Set the name of the key storing this value in an AnyMap. | |
void | propagateMetadata (shared_ptr< AnyMap > &file) |
Propagate metadata to any child elements. | |
template<class T > | |
const T & | as () const |
Get the value of this key as the specified type. | |
template<class T > | |
T & | as () |
const std::type_info & | type () const |
Returns the type of the held value. | |
string | type_str () const |
Returns a string specifying the type of the held value. | |
bool | empty () const |
Return boolean indicating whether AnyValue is empty. | |
template<class T > | |
bool | is () const |
Returns true if the held value is of the specified type. | |
template<class T > | |
bool | isVector () const |
Returns true if the held value is a vector of the specified type, such as vector<double> . | |
template<class T > | |
bool | isMatrix (size_t cols=npos) const |
Returns true if the held value is a matrix of the specified type and a consistent number of columns, such as vector<vector<double>> . | |
bool | isScalar () const |
Returns true if the held value is a scalar type (such as double , long int , string , or bool ). | |
size_t | vectorSize () const |
Returns size of the held vector. | |
pair< size_t, size_t > | matrixShape () const |
Returns rows and columns of a matrix. | |
AnyValue (const string &value) | |
AnyValue (const char *value) | |
AnyValue & | operator= (const string &value) |
AnyValue & | operator= (const char *value) |
const string & | asString () const |
Return the held value, if it is a string. | |
bool | operator== (const string &other) const |
bool | operator!= (const string &other) const |
AnyValue (double value) | |
AnyValue & | operator= (double value) |
double & | asDouble () |
Return the held value as a double , if it is a double or a long int . | |
const double & | asDouble () const |
bool | operator== (const double &other) const |
bool | operator!= (const double &other) const |
AnyValue (bool value) | |
AnyValue & | operator= (bool value) |
bool & | asBool () |
Return the held value, if it is a bool . | |
const bool & | asBool () const |
AnyValue (long int value) | |
AnyValue (int value) | |
AnyValue & | operator= (long int value) |
AnyValue & | operator= (int value) |
long int & | asInt () |
Return the held value, if it is a long int . | |
const long int & | asInt () const |
bool | operator== (const long int &other) const |
bool | operator!= (const long int &other) const |
bool | operator== (const int &other) const |
bool | operator!= (const int &other) const |
template<class T > | |
AnyValue & | operator= (const vector< T > &value) |
template<class T > | |
const vector< T > & | asVector (size_t nMin=npos, size_t nMax=npos) const |
Return the held value, if it is a vector of type T . | |
template<class T > | |
vector< T > & | asVector (size_t nMin=npos, size_t nMax=npos) |
AnyValue (const AnyMap &value) | |
AnyValue & | operator= (const AnyMap &value) |
AnyValue & | operator= (AnyMap &&value) |
template<class T > | |
AnyValue & | operator= (const std::unordered_map< string, T > items) |
template<class T > | |
AnyValue & | operator= (const map< string, T > items) |
template<class T > | |
map< string, T > | asMap () const |
Return the held AnyMap as a map where all of the values have the specified type. | |
std::unordered_map< string, const AnyMap * > | asMap (const string &name) const |
Access a vector<AnyMap> as a mapping using the value of name from each item as the key in the new mapping. | |
std::unordered_map< string, AnyMap * > | asMap (const string &name) |
AnyMap & | getMapWhere (const string &key, const string &value, bool create=false) |
Treating the value as vector<AnyMap> , return the item where the given key has the specified value. | |
const AnyMap & | getMapWhere (const string &key, const string &value) const |
bool | hasMapWhere (const string &key, const string &value) const |
Returns true when getMapWhere() would succeed. | |
pair< int, int > | order () const |
Return values used to determine the sort order when outputting to YAML. | |
void | applyUnits (shared_ptr< UnitSystem > &units) |
See AnyMap::applyUnits() | |
void | setFlowStyle (bool flow=true) |
See AnyMap::setFlowStyle() | |
template<> | |
bool | is () const |
template<> | |
const vector< AnyValue > & | asVector (size_t nMin, size_t nMax) const |
template<> | |
vector< AnyValue > & | asVector (size_t nMin, size_t nMax) |
template<> | |
const vector< double > & | asVector (size_t nMin, size_t nMax) const |
template<> | |
vector< double > & | asVector (size_t nMin, size_t nMax) |
template<> | |
const vector< vector< double > > & | asVector (size_t nMin, size_t nMax) const |
template<> | |
vector< vector< double > > & | asVector (size_t nMin, size_t nMax) |
template<> | |
const vector< AnyMap > & | asVector (size_t nMin, size_t nMax) const |
template<> | |
vector< AnyMap > & | asVector (size_t nMin, size_t nMax) |
template<> | |
const vector< AnyValue > & | asVector (size_t nMin, size_t nMax) const |
Implicit conversion to vector<AnyValue> | |
template<> | |
vector< AnyValue > & | asVector (size_t nMin, size_t nMax) |
template<> | |
const vector< double > & | asVector (size_t nMin, size_t nMax) const |
Implicit conversion of long int to double if accessed as a vector<double> | |
template<> | |
vector< double > & | asVector (size_t nMin, size_t nMax) |
template<> | |
const vector< vector< double > > & | asVector (size_t nMin, size_t nMax) const |
Implicit conversion of long int to double if accessed as a vector<vector<double>> | |
template<> | |
vector< vector< double > > & | asVector (size_t nMin, size_t nMax) |
template<> | |
const vector< AnyMap > & | asVector (size_t nMin, size_t nMax) const |
Implicit conversion of AnyMap to a vector<AnyMap> of length 1, or an empty vector<AnyValue> an empty vector<AnyMap> | |
template<> | |
vector< AnyMap > & | asVector (size_t nMin, size_t nMax) |
template<> | |
bool | is () const |
template<> | |
AnyMap & | as () |
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. | |
const AnyValue & | getMetadata (const string &key) const |
Get a value from the metadata applicable to the AnyMap tree containing this node. | |
Private Types | |
typedef bool(* | Comparer) (const std::any &, const std::any &) |
Private Member Functions | |
template<class T > | |
void | checkSize (const vector< T > &v, size_t nMin, size_t nMax) const |
Static Private Member Functions | |
template<typename T > | |
static bool | eq_comparer (const std::any &lhs, const std::any &rhs) |
Equality comparison function used when lhs is of type T | |
template<class T , class U > | |
static bool | vector_eq (const std::any &lhs, const std::any &rhs) |
Helper function for comparing vectors of different (but comparable) types, for example vector<double> and vector<long int> | |
template<class T , class U > | |
static bool | vector2_eq (const std::any &lhs, const std::any &rhs) |
Helper function for comparing nested vectors of different (but comparable) types, for example vector<vector<double>> and vector<vector<long int>> | |
Private Attributes | |
string | m_key |
Key of this value in a parent AnyMap | |
std::any | m_value |
The held value. | |
Comparer | m_equals |
Friends | |
bool | operator== (const string &lhs, const AnyValue &rhs) |
bool | operator!= (const string &lhs, const AnyValue &rhs) |
bool | operator== (const double &lhs, const AnyValue &rhs) |
bool | operator!= (const double &lhs, const AnyValue &rhs) |
bool | operator== (const long int &lhs, const AnyValue &rhs) |
bool | operator!= (const long int &lhs, const AnyValue &rhs) |
bool | operator== (const int &lhs, const AnyValue &rhs) |
bool | operator!= (const int &lhs, const AnyValue &rhs) |
YAML::Emitter & | YAML::operator<< (YAML::Emitter &out, const AnyValue &rhs) |
Quantity conversions | |
Assign a quantity consisting of one or more values and their corresponding units, which will be converted to a target unit system when the applyUnits() function is later called on the root of the AnyMap. | |
typedef function< void(AnyValue &, const UnitSystem &)> | unitConverter |
void | setQuantity (double value, const string &units, bool is_act_energy=false) |
Assign a scalar quantity with units as a string, for example {3.0, "m^2"} . | |
void | setQuantity (double value, const Units &units) |
Assign a scalar quantity with units as a Units object, for cases where the units vary and are determined dynamically, such as reaction pre-exponential factors. | |
void | setQuantity (const vector< double > &values, const string &units) |
Assign a vector where all the values have the same units. | |
void | setQuantity (const AnyValue &value, const unitConverter &converter) |
Assign a value of any type where the unit conversion requires a different behavior besides scaling all values by the same factor. | |
Additional Inherited Members | |
Protected Attributes inherited from AnyBase | |
int | m_line = -1 |
The line where this value occurs in the input file. | |
int | m_column = 0 |
If m_line >= 0, the column where this value occurs in the input file. | |
shared_ptr< AnyMap > | m_metadata |
Metadata relevant to an entire AnyMap tree, such as information about. | |
typedef function<void(AnyValue&, const UnitSystem&)> unitConverter |
|
private |
AnyValue | ( | ) |
Definition at line 591 of file AnyMap.cpp.
|
explicit |
Definition at line 717 of file AnyMap.cpp.
|
explicit |
Definition at line 722 of file AnyMap.cpp.
|
explicit |
Definition at line 813 of file AnyMap.cpp.
|
explicit |
Definition at line 860 of file AnyMap.cpp.
|
explicit |
Definition at line 881 of file AnyMap.cpp.
|
explicit |
Definition at line 886 of file AnyMap.cpp.
Definition at line 959 of file AnyMap.cpp.
bool operator== | ( | const AnyValue & | other | ) | const |
Definition at line 597 of file AnyMap.cpp.
bool operator!= | ( | const AnyValue & | other | ) | const |
Definition at line 602 of file AnyMap.cpp.
AnyValue & operator[] | ( | const string & | key | ) |
If this AnyValue is an AnyMap, return the value stored in key
.
Definition at line 607 of file AnyMap.cpp.
const AnyValue & operator[] | ( | const string & | key | ) | const |
Definition at line 612 of file AnyMap.cpp.
bool hasKey | ( | const string & | key | ) | const |
Returns true
if this AnyValue is an AnyMap and that map contains a key with the given name.
Definition at line 617 of file AnyMap.cpp.
void setKey | ( | const string & | key | ) |
Set the name of the key storing this value in an AnyMap.
Used for providing informative error messages in class InputFileError.
Definition at line 621 of file AnyMap.cpp.
void propagateMetadata | ( | shared_ptr< AnyMap > & | file | ) |
Propagate metadata to any child elements.
Definition at line 627 of file AnyMap.cpp.
const T & as |
Get the value of this key as the specified type.
Definition at line 16 of file AnyMap.inl.h.
T & as |
Definition at line 61 of file AnyMap.inl.h.
const std::type_info & type | ( | ) | const |
Returns the type of the held value.
Definition at line 623 of file AnyMap.cpp.
string type_str | ( | ) | const |
Returns a string specifying the type of the held value.
Definition at line 643 of file AnyMap.cpp.
bool empty | ( | ) | const |
Return boolean indicating whether AnyValue is empty.
Definition at line 647 of file AnyMap.cpp.
bool is |
Returns true
if the held value is of the specified type.
Definition at line 68 of file AnyMap.inl.h.
bool isVector |
Returns true
if the held value is a vector of the specified type, such as vector<double>
.
Definition at line 75 of file AnyMap.inl.h.
bool isMatrix | ( | size_t | cols = npos | ) | const |
Returns true
if the held value is a matrix of the specified type and a consistent number of columns, such as vector<vector<double>>
.
If the number of columns is provided, a match is required.
Definition at line 80 of file AnyMap.inl.h.
bool isScalar | ( | ) | const |
Returns true
if the held value is a scalar type (such as double
, long int
, string
, or bool
).
Definition at line 651 of file AnyMap.cpp.
size_t vectorSize | ( | ) | const |
Returns size of the held vector.
If not a vector or the type is not supported npos is returned. Types considered include vector<double>
, vector<long int>
, vector<string>
, and vector<bool
.
Definition at line 655 of file AnyMap.cpp.
pair< size_t, size_t > matrixShape | ( | ) | const |
Returns rows and columns of a matrix.
If the number of columns is not consistent, the number of columns is set to npos; if the type is not supported, a npos pair is returned. Types considered include vector<vector<double>>
, vector<vector<long int>>
, vector<vector<string>>
and vector<vector<bool>>
.
Definition at line 671 of file AnyMap.cpp.
AnyValue & operator= | ( | const string & | value | ) |
Definition at line 727 of file AnyMap.cpp.
AnyValue & operator= | ( | const char * | value | ) |
Definition at line 733 of file AnyMap.cpp.
const string & asString | ( | ) | const |
Return the held value, if it is a string.
Definition at line 739 of file AnyMap.cpp.
bool operator== | ( | const string & | other | ) | const |
Definition at line 743 of file AnyMap.cpp.
bool operator!= | ( | const string & | other | ) | const |
Definition at line 752 of file AnyMap.cpp.
void setQuantity | ( | double | value, |
const string & | units, | ||
bool | is_act_energy = false |
||
) |
Assign a scalar quantity with units as a string, for example {3.0, "m^2"}
.
If the is_act_energy
flag is set to true
, the units will be converted using the special rules for activation energies.
Definition at line 769 of file AnyMap.cpp.
void setQuantity | ( | double | value, |
const Units & | units | ||
) |
Assign a scalar quantity with units as a Units object, for cases where the units vary and are determined dynamically, such as reaction pre-exponential factors.
Definition at line 774 of file AnyMap.cpp.
void setQuantity | ( | const vector< double > & | values, |
const string & | units | ||
) |
Assign a vector where all the values have the same units.
Definition at line 779 of file AnyMap.cpp.
void setQuantity | ( | const AnyValue & | value, |
const unitConverter & | converter | ||
) |
Assign a value of any type where the unit conversion requires a different behavior besides scaling all values by the same factor.
Definition at line 786 of file AnyMap.cpp.
AnyValue & operator= | ( | double | value | ) |
Definition at line 818 of file AnyMap.cpp.
double & asDouble | ( | ) |
Return the held value as a double
, if it is a double
or a long int
.
Definition at line 824 of file AnyMap.cpp.
const double & asDouble | ( | ) | const |
Definition at line 828 of file AnyMap.cpp.
bool operator== | ( | const double & | other | ) | const |
Definition at line 832 of file AnyMap.cpp.
bool operator!= | ( | const double & | other | ) | const |
Definition at line 843 of file AnyMap.cpp.
AnyValue & operator= | ( | bool | value | ) |
Definition at line 865 of file AnyMap.cpp.
bool & asBool | ( | ) |
Return the held value, if it is a bool
.
Definition at line 871 of file AnyMap.cpp.
const bool & asBool | ( | ) | const |
Definition at line 875 of file AnyMap.cpp.
AnyValue & operator= | ( | long int | value | ) |
Definition at line 891 of file AnyMap.cpp.
AnyValue & operator= | ( | int | value | ) |
Definition at line 897 of file AnyMap.cpp.
long int & asInt | ( | ) |
Return the held value, if it is a long int
.
Definition at line 903 of file AnyMap.cpp.
const long int & asInt | ( | ) | const |
Definition at line 907 of file AnyMap.cpp.
bool operator== | ( | const long int & | other | ) | const |
Definition at line 911 of file AnyMap.cpp.
bool operator!= | ( | const long int & | other | ) | const |
Definition at line 922 of file AnyMap.cpp.
bool operator== | ( | const int & | other | ) | const |
Definition at line 927 of file AnyMap.cpp.
bool operator!= | ( | const int & | other | ) | const |
Definition at line 932 of file AnyMap.cpp.
AnyValue & operator= | ( | const vector< T > & | value | ) |
Definition at line 102 of file AnyMap.inl.h.
Return the held value, if it is a vector of type T
.
If called with one argument, requires the vector to be of the specified size. If called with two arguments, requires the vector to be within the range specified by the two values, inclusive.
Definition at line 109 of file AnyMap.inl.h.
Definition at line 116 of file AnyMap.inl.h.
Definition at line 964 of file AnyMap.cpp.
Definition at line 970 of file AnyMap.cpp.
AnyValue & operator= | ( | const std::unordered_map< string, T > | items | ) |
Definition at line 123 of file AnyMap.inl.h.
AnyValue & operator= | ( | const map< string, T > | items | ) |
Definition at line 134 of file AnyMap.inl.h.
map< string, T > asMap |
Return the held AnyMap
as a map
where all of the values have the specified type.
Definition at line 162 of file AnyMap.inl.h.
std::unordered_map< string, const AnyMap * > asMap | ( | const string & | name | ) | const |
Access a vector<AnyMap>
as a mapping using the value of name
from each item as the key in the new mapping.
For example, for the list:
calling asMap("name")
will create a map with keys O2
and CH4
.
Definition at line 976 of file AnyMap.cpp.
std::unordered_map< string, AnyMap * > asMap | ( | const string & | name | ) |
Definition at line 990 of file AnyMap.cpp.
AnyMap & getMapWhere | ( | const string & | key, |
const string & | value, | ||
bool | create = false |
||
) |
Treating the value as vector<AnyMap>
, return the item where the given key has the specified value.
If value is the empty string, returns the first item in the list.
If the contained type is just AnyMap
rather than vector<AnyMap>
, it will be treated as a vector of length 1.
If the value does not exist but the create
flag is set to true, a new map with that key and value will be created and returned.
Definition at line 1034 of file AnyMap.cpp.
const AnyMap & getMapWhere | ( | const string & | key, |
const string & | value | ||
) | const |
Definition at line 1004 of file AnyMap.cpp.
bool hasMapWhere | ( | const string & | key, |
const string & | value | ||
) | const |
Returns true
when getMapWhere() would succeed.
Definition at line 1084 of file AnyMap.cpp.
pair< int, int > order | ( | ) | const |
Return values used to determine the sort order when outputting to YAML.
Definition at line 1107 of file AnyMap.cpp.
void applyUnits | ( | shared_ptr< UnitSystem > & | units | ) |
Definition at line 1112 of file AnyMap.cpp.
void setFlowStyle | ( | bool | flow = true | ) |
Definition at line 1209 of file AnyMap.cpp.
|
private |
Definition at line 172 of file AnyMap.inl.h.
|
staticprivate |
Equality comparison function used when lhs is of type T
Definition at line 216 of file AnyMap.inl.h.
|
staticprivate |
Helper function for comparing vectors of different (but comparable) types, for example vector<double>
and vector<long int>
Definition at line 187 of file AnyMap.inl.h.
|
staticprivate |
Helper function for comparing nested vectors of different (but comparable) types, for example vector<vector<double>>
and vector<vector<long int>>
Definition at line 199 of file AnyMap.inl.h.
bool is | ( | ) | const |
Definition at line 793 of file AnyMap.cpp.
const vector< AnyValue > & asVector | ( | size_t | nMin, |
size_t | nMax | ||
) | const |
Definition at line 1217 of file AnyMap.cpp.
vector< AnyValue > & asVector | ( | size_t | nMin, |
size_t | nMax | ||
) |
Definition at line 1217 of file AnyMap.cpp.
const vector< double > & asVector | ( | size_t | nMin, |
size_t | nMax | ||
) | const |
Definition at line 1256 of file AnyMap.cpp.
vector< double > & asVector | ( | size_t | nMin, |
size_t | nMax | ||
) |
Definition at line 1256 of file AnyMap.cpp.
const vector< vector< double > > & asVector | ( | size_t | nMin, |
size_t | nMax | ||
) | const |
Definition at line 1288 of file AnyMap.cpp.
vector< vector< double > > & asVector | ( | size_t | nMin, |
size_t | nMax | ||
) |
Definition at line 1288 of file AnyMap.cpp.
const vector< AnyMap > & asVector | ( | size_t | nMin, |
size_t | nMax | ||
) | const |
Definition at line 1326 of file AnyMap.cpp.
vector< AnyMap > & asVector | ( | size_t | nMin, |
size_t | nMax | ||
) |
Definition at line 1326 of file AnyMap.cpp.
const vector< AnyValue > & asVector | ( | size_t | nMin, |
size_t | nMax | ||
) | const |
Implicit conversion to vector<AnyValue>
const vector< double > & asVector | ( | size_t | nMin, |
size_t | nMax | ||
) | const |
Implicit conversion of long int to double if accessed as a vector<double>
const vector< vector< double > > & asVector | ( | size_t | nMin, |
size_t | nMax | ||
) | const |
Implicit conversion of long int to double if accessed as a vector<vector<double>>
const vector< AnyMap > & asVector | ( | size_t | nMin, |
size_t | nMax | ||
) | const |
Implicit conversion of AnyMap to a vector<AnyMap> of length 1, or an empty vector<AnyValue> an empty vector<AnyMap>
|
inline |
Definition at line 145 of file AnyMap.inl.h.
|
friend |
Definition at line 757 of file AnyMap.cpp.
|
friend |
Definition at line 762 of file AnyMap.cpp.
|
friend |
Definition at line 848 of file AnyMap.cpp.
|
friend |
Definition at line 853 of file AnyMap.cpp.
|
friend |
Definition at line 937 of file AnyMap.cpp.
|
friend |
Definition at line 942 of file AnyMap.cpp.
|
friend |
Definition at line 947 of file AnyMap.cpp.
|
friend |
Definition at line 952 of file AnyMap.cpp.