Dictionary

The Dictionary class is a tree-like data structure that can store any type of data similar to json, yaml or toml format. It is used to store the input data for the simulation. The Dictionary class is a wrapper around std::unordered_map<std::string, std::any> and provides a simple interface to insert, retrieve and modify values.

This generic interface allows to deliver complex input data to the simulation code without the need to define a specific data structure. It can be used as followed (more details see test_Dictionary.cpp):

NeoFOAM::Dictionary dict;

dict.insert("key1", 42);
dict.insert("key2", std::string("Hello"));

dict.get<int>("key1") == 42;
dict.get<std::string>("key2") == "Hello";

If the dictionary is not passed as a const reference, the values can be modified as well:

NeoFOAM::Dictionary dict;
dict.insert("key", 42);
dict["key"] = 43;

dict.get<int>("key") == 43;

Accessing a non-existent key will throw an exception std::out_of_range:

NeoFOAM::Dictionary dict;
dict["non_existent_key"]; // will throw with std::out_of_range;

To check if a key exists, the found method can be used:

NeoFOAM::Dictionary dict;
dict.insert("key", 42);
dict.found("key") == true;

Dictionary provides a method to remove a key:

NeoFOAM::Dictionary dict;
dict.insert("key", 42);
dict["key"] = 43;
dict.remove("key");

dict.found("key") == false;

The Dictionary class also provides a method to access a sub-dictionary. This is useful to group related data together:

NeoFOAM::Dictionary dict;
NeoFOAM::Dictionary subDict;
subDict.insert("key1", 42);
subDict.insert("key2", std::string("Hello"));

dict.insert("subDict", subDict);

NeoFOAM::Dictionary& sDict = dict.subDict("subDict");
sDict.get<int>("key1") == 42;
sDict.get<std::string>("key2") == "Hello";

sDict.get<int>("key1") = 100;

// check if the value is modified
NeoFOAM::Dictionary& sDict2 = dict.subDict("subDict");
sDict2.get<int>("key1") == 100;
class Dictionary

A class representing a dictionary that stores key-value pairs.

The Dictionary class provides a way to store and retrieve values using string. It supports inserting key-value pairs, accessing values using the subscript operator, and retrieving values of specific types using the get function. It also supports storing sub-dictionaries, which can be accessed using the subDict function. The values are stored using std::any, which allows storing values of any type.

Public Functions

Dictionary() = default
void insert(const std::string &key, const std::any &value)

Inserts a key-value pair into the dictionary.

Parameters:
  • key – The key to insert.

  • value – The value to insert.

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

Checks if the given key is present in the dictionary.

Parameters:

key – The key to check.

Returns:

True if the key is present, false otherwise.

void remove(const std::string &key)

Removes an entry from the dictionary based on the specified key.

This function removes the entry with the specified key from the dictionary.

Parameters:

key – The key of the entry to be removed.

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

Accesses the value associated with the given key.

Parameters:

key – The key to access.

Returns:

A reference to the value associated with the key.

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

Accesses the value associated with the given key.

Parameters:

key – The key to access.

Returns:

A const reference to the value associated with the key.

template<typename T>
inline T &get(const std::string &key)

Retrieves the value associated with the given key, casting it to the specified type.

Template Parameters:

T – The type to cast the value to.

Parameters:

key – The key to retrieve the value for.

Returns:

A reference to the value associated with the key, casted to type T.

template<typename T>
inline const T &get(const std::string &key) const

Retrieves the value associated with the given key, casting it to the specified type.

Template Parameters:

T – The type to cast the value to.

Parameters:

key – The key to retrieve the value for.

Returns:

A const reference to the value associated with the key, casted to type T.

Dictionary &subDict(const std::string &key)

Retrieves a sub-dictionary associated with the given key.

Parameters:

key – The key to retrieve the sub-dictionary for.

Returns:

A reference to the sub-dictionary associated with the key.

std::unordered_map<std::string, std::any> &getMap()

Retrieves the underlying unordered map of the dictionary.

Returns:

A reference to the underlying unordered map.

const std::unordered_map<std::string, std::any> &getMap() const

Retrieves the underlying unordered map of the dictionary.

Returns:

A const reference to the underlying unordered map.