Cantera  2.4.0
Public Member Functions | 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>

Public Member Functions

AnyValueoperator[] (const std::string &key)
AnyValueat (const std::string &key)
bool hasKey (const std::string &key) const

Private Attributes

std::unordered_map< std::string, AnyValuem_data


class AnyValue

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;
// Equivalently:
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>();

Checking for elements

try {
} except (std::exception& err) {
// Exception will be thrown.
// 'breakfast' will have an empty key named "waffle"
try {"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 173 of file AnyMap.h.

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