Cantera  2.5.1
Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
AnyValue Class Reference

A wrapper for a variable whose type is determined at runtime. More...

#include <AnyMap.h>

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

Public Member Functions

 AnyValue (AnyValue const &other)
 
 AnyValue (AnyValue &&other)
 
AnyValueoperator= (AnyValue const &other)
 
AnyValueoperator= (AnyValue &&other)
 
bool operator== (const AnyValue &other) const
 
bool operator!= (const AnyValue &other) const
 
AnyValueoperator[] (const std::string &key)
 If this AnyValue is an AnyMap, return the value stored in key. More...
 
const AnyValueoperator[] (const std::string &key) const
 
bool hasKey (const std::string &key) const
 Returns true if this AnyValue is an AnyMap and that map contains a key with the given name. More...
 
void setKey (const std::string &key)
 Set the name of the key storing this value in an AnyMap. More...
 
void propagateMetadata (shared_ptr< AnyMap > &file)
 Propagate metadata to any child elements. More...
 
template<class T >
const T & as () const
 Get the value of this key as the specified type. More...
 
template<class T >
T & as ()
 
const std::type_info & type () const
 Returns the type of the held value. More...
 
std::string type_str () const
 Returns a string specifying the type of the held value. More...
 
template<class T >
bool is () const
 Returns true if the held value is of the specified type. More...
 
bool isScalar () const
 Returns true if the held value is a scalar type (e.g. More...
 
 AnyValue (const std::string &value)
 
 AnyValue (const char *value)
 
AnyValueoperator= (const std::string &value)
 
AnyValueoperator= (const char *value)
 
const std::string & asString () const
 Return the held value, if it is a string. More...
 
bool operator== (const std::string &other) const
 
bool operator!= (const std::string &other) const
 
 AnyValue (double value)
 
AnyValueoperator= (double value)
 
double & asDouble ()
 Return the held value as a double, if it is a double or a long int. More...
 
const double & asDouble () const
 
bool operator== (const double &other) const
 
bool operator!= (const double &other) const
 
 AnyValue (bool value)
 
AnyValueoperator= (bool value)
 
bool & asBool ()
 Return the held value, if it is a bool. More...
 
const bool & asBool () const
 
 AnyValue (long int value)
 
 AnyValue (int value)
 
AnyValueoperator= (long int value)
 
AnyValueoperator= (int value)
 
long int & asInt ()
 Return the held value, if it is a long int. More...
 
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 >
AnyValueoperator= (const std::vector< T > &value)
 
template<class T >
const std::vector< T > & asVector (size_t nMin=npos, size_t nMax=npos) const
 Return the held value, if it is a vector of type T. More...
 
template<class T >
std::vector< T > & asVector (size_t nMin=npos, size_t nMax=npos)
 
 AnyValue (const AnyMap &value)
 
AnyValueoperator= (const AnyMap &value)
 
AnyValueoperator= (AnyMap &&value)
 
template<class T >
AnyValueoperator= (const std::unordered_map< std::string, T > items)
 
template<class T >
AnyValueoperator= (const std::map< std::string, T > items)
 
template<class T >
std::map< std::string, T > asMap () const
 Return the held AnyMap as a std::map where all of the values have the specified type. More...
 
std::unordered_map< std::string, const AnyMap * > asMap (const std::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. More...
 
std::unordered_map< std::string, AnyMap * > asMap (const std::string &name)
 
AnyMapgetMapWhere (const std::string &key, const std::string &value, bool create=false)
 Treating the value as vector<AnyMap>, return the item where the given key has the specified value. More...
 
const AnyMapgetMapWhere (const std::string &key, const std::string &value) const
 
bool hasMapWhere (const std::string &key, const std::string &value) const
 Returns true when getMapWhere() would succeed. More...
 
void applyUnits (const UnitSystem &units)
 
template<>
const std::vector< AnyValue > & asVector (size_t nMin, size_t nMax) const
 
template<>
std::vector< AnyValue > & asVector (size_t nMin, size_t nMax)
 
template<>
const std::vector< double > & asVector (size_t nMin, size_t nMax) const
 
template<>
std::vector< double > & asVector (size_t nMin, size_t nMax)
 
template<>
const std::vector< vector_fp > & asVector (size_t nMin, size_t nMax) const
 
template<>
std::vector< vector_fp > & asVector (size_t nMin, size_t nMax)
 
template<>
const std::vector< AnyMap > & asVector (size_t nMin, size_t nMax) const
 
template<>
std::vector< AnyMap > & asVector (size_t nMin, size_t nMax)
 
template<>
const std::vector< AnyValue > & asVector (size_t nMin, size_t nMax) const
 Implicit conversion to vector<AnyValue> More...
 
template<>
std::vector< AnyValue > & asVector (size_t nMin, size_t nMax)
 
template<>
const std::vector< double > & asVector (size_t nMin, size_t nMax) const
 Implicit conversion of long int to double if accessed as a vector<double> More...
 
template<>
std::vector< double > & asVector (size_t nMin, size_t nMax)
 
template<>
const std::vector< vector_fp > & asVector (size_t nMin, size_t nMax) const
 Implicit conversion of long int to double if accessed as a vector<vector<double>> More...
 
template<>
std::vector< vector_fp > & asVector (size_t nMin, size_t nMax)
 
template<>
const std::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> More...
 
template<>
std::vector< AnyMap > & asVector (size_t nMin, size_t nMax)
 
template<>
AnyMapas ()
 
- 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...
 

Private Types

typedef bool(* Comparer) (const boost::any &, const boost::any &)
 

Private Member Functions

std::string demangle (const std::type_info &type) const
 
template<class T >
void checkSize (const std::vector< T > &v, size_t nMin, size_t nMax) const
 

Static Private Member Functions

template<typename T >
static bool eq_comparer (const boost::any &lhs, const boost::any &rhs)
 Equality comparison function used when lhs is of type T More...
 
template<class T , class U >
static bool vector_eq (const boost::any &lhs, const boost::any &rhs)
 Helper function for comparing vectors of different (but comparable) types, e.g. More...
 
template<class T , class U >
static bool vector2_eq (const boost::any &lhs, const boost::any &rhs)
 Helper function for comparing nested vectors of different (but comparable) types, e.g. More...
 

Private Attributes

std::string m_key
 Key of this value in a parent AnyMap More...
 
std::unique_ptr< boost::any > m_value
 The held value. More...
 
Comparer m_equals
 

Static Private Attributes

static std::map< std::string, std::string > s_typenames
 Human-readable names for some common types, for use when boost::demangle is not available. More...
 

Friends

bool operator== (const std::string &lhs, const AnyValue &rhs)
 
bool operator!= (const std::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)
 

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 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:

Definition at line 76 of file AnyMap.h.

Member Function Documentation

◆ operator[]()

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

If this AnyValue is an AnyMap, return the value stored in key.

Definition at line 356 of file AnyMap.cpp.

◆ hasKey()

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

Returns true if this AnyValue is an AnyMap and that map contains a key with the given name.

Definition at line 366 of file AnyMap.cpp.

Referenced by AnyValue::getMapWhere(), and AnyValue::hasMapWhere().

◆ setKey()

void setKey ( const std::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 370 of file AnyMap.cpp.

References AnyValue::m_key.

Referenced by AnyMap::operator[]().

◆ propagateMetadata()

void propagateMetadata ( shared_ptr< AnyMap > &  file)

Propagate metadata to any child elements.

Definition at line 376 of file AnyMap.cpp.

References AnyValue::is(), and AnyBase::m_metadata.

Referenced by AnyMap::operator[]().

◆ as()

const T & as

Get the value of this key as the specified type.

Definition at line 17 of file AnyMap.inl.h.

◆ type()

const std::type_info & type ( ) const

Returns the type of the held value.

Definition at line 372 of file AnyMap.cpp.

References AnyValue::m_value.

Referenced by AnyValue::type_str().

◆ type_str()

std::string type_str ( ) const

Returns a string specifying the type of the held value.

Definition at line 392 of file AnyMap.cpp.

References AnyValue::type().

◆ is()

bool is

Returns true if the held value is of the specified type.

Definition at line 61 of file AnyMap.inl.h.

References AnyValue::m_value.

Referenced by AnyValue::applyUnits(), AnyValue::getMapWhere(), AnyValue::hasMapWhere(), AnyValue::propagateMetadata(), and MolalityVPSSTP::setState().

◆ isScalar()

bool isScalar ( ) const

Returns true if the held value is a scalar type (e.g.

double, long int, string, or bool).

Definition at line 396 of file AnyMap.cpp.

◆ asString()

const std::string & asString ( ) const

Return the held value, if it is a string.

Definition at line 424 of file AnyMap.cpp.

Referenced by MolalityVPSSTP::setState().

◆ asDouble()

double & asDouble ( )

Return the held value as a double, if it is a double or a long int.

Definition at line 465 of file AnyMap.cpp.

◆ asBool()

bool & asBool ( )

Return the held value, if it is a bool.

Definition at line 512 of file AnyMap.cpp.

◆ asInt()

long int & asInt ( )

Return the held value, if it is a long int.

Definition at line 544 of file AnyMap.cpp.

◆ asVector() [1/5]

const std::vector< T > & asVector ( size_t  nMin = npos,
size_t  nMax = npos 
) const

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

Referenced by Cantera::getReactions(), and Cantera::getSpecies().

◆ asMap() [1/2]

std::map< std::string, T > asMap

Return the held AnyMap as a std::map where all of the values have the specified type.

Definition at line 126 of file AnyMap.inl.h.

Referenced by MolalityVPSSTP::setState().

◆ asMap() [2/2]

std::unordered_map< std::string, const AnyMap * > asMap ( const std::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:

[{name: O2, weight: 32}, {name: CH4, weight: 16}]

calling asMap("name") will create a map with keys O2 and CH4.

Definition at line 617 of file AnyMap.cpp.

◆ getMapWhere()

AnyMap & getMapWhere ( const std::string &  key,
const std::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 675 of file AnyMap.cpp.

References AnyMap::at(), AnyValue::hasKey(), AnyValue::is(), and AnyValue::m_key.

◆ hasMapWhere()

bool hasMapWhere ( const std::string &  key,
const std::string &  value 
) const

Returns true when getMapWhere() would succeed.

Definition at line 725 of file AnyMap.cpp.

References AnyValue::hasKey(), and AnyValue::is().

◆ applyUnits()

void applyUnits ( const UnitSystem units)
See also
AnyMap::applyUnits

Definition at line 748 of file AnyMap.cpp.

References AnyValue::is(), and UnitSystem::setDefaults().

◆ eq_comparer()

bool eq_comparer ( const boost::any &  lhs,
const boost::any &  rhs 
)
staticprivate

Equality comparison function used when lhs is of type T

Definition at line 180 of file AnyMap.inl.h.

References AssertThrowMsg.

◆ vector_eq()

bool vector_eq ( const boost::any &  lhs,
const boost::any &  rhs 
)
staticprivate

Helper function for comparing vectors of different (but comparable) types, e.g.

vector<double> and vector<long int>

Definition at line 151 of file AnyMap.inl.h.

◆ vector2_eq()

bool vector2_eq ( const boost::any &  lhs,
const boost::any &  rhs 
)
staticprivate

Helper function for comparing nested vectors of different (but comparable) types, e.g.

vector<vector<double>> and vector<vector<long int>>

Definition at line 163 of file AnyMap.inl.h.

◆ asVector() [2/5]

const std::vector<AnyValue>& asVector ( size_t  nMin,
size_t  nMax 
) const

Implicit conversion to vector<AnyValue>

◆ asVector() [3/5]

const std::vector<double>& asVector ( size_t  nMin,
size_t  nMax 
) const

Implicit conversion of long int to double if accessed as a vector<double>

◆ asVector() [4/5]

const std::vector<vector_fp>& asVector ( size_t  nMin,
size_t  nMax 
) const

Implicit conversion of long int to double if accessed as a vector<vector<double>>

◆ asVector() [5/5]

const std::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>

Member Data Documentation

◆ m_key

std::string m_key
private

Key of this value in a parent AnyMap

Definition at line 234 of file AnyMap.h.

Referenced by AnyValue::getMapWhere(), and AnyValue::setKey().

◆ m_value

std::unique_ptr<boost::any> m_value
private

The held value.

Definition at line 237 of file AnyMap.h.

Referenced by AnyValue::is(), and AnyValue::type().

◆ s_typenames

std::map< std::string, std::string > s_typenames
staticprivate
Initial value:
= {
{typeid(double).name(), "double"},
{typeid(long int).name(), "long int"},
{typeid(std::string).name(), "string"},
{typeid(std::vector<double>).name(), "vector<double>"},
{typeid(AnyMap).name(), "AnyMap"},
}

Human-readable names for some common types, for use when boost::demangle is not available.

Definition at line 241 of file AnyMap.h.


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