NeoFOAM
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
dictionary.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2023 NeoFOAM authors
3#pragma once
4
5#include <unordered_map>
6#include <any>
7#include <string>
8#include <vector>
9
11
12namespace NeoFOAM
13{
14
16 const std::out_of_range& e,
17 const std::string& key,
18 const std::unordered_map<std::string, std::any>& data
19);
20
33{
34public:
35
36 Dictionary() = default;
37
38 Dictionary(const std::unordered_map<std::string, std::any>& keyValuePairs);
39
40 Dictionary(const std::initializer_list<std::pair<std::string, std::any>>& initList);
41
47 void insert(const std::string& key, const std::any& value);
48
54 [[nodiscard]] bool contains(const std::string& key) const;
55
64 void remove(const std::string& key);
65
71 [[nodiscard]] std::any& operator[](const std::string& key);
72
78 [[nodiscard]] const std::any& operator[](const std::string& key) const;
79
88 template<typename T>
89 [[nodiscard]] T& get(const std::string& key)
90 {
91 try
92 {
93 return std::any_cast<T&>(operator[](key));
94 }
95 catch (const std::bad_any_cast& e)
96 {
97 logBadAnyCast<T>(e, key, data_);
98 throw e;
99 }
100 }
101
110 template<typename T>
111 [[nodiscard]] const T& get(const std::string& key) const
112 {
113 try
114 {
115 return std::any_cast<const T&>(operator[](key));
116 }
117 catch (const std::bad_any_cast& e)
118 {
119 logBadAnyCast<T>(e, key, data_);
120 throw e;
121 }
122 }
123
129 [[nodiscard]] bool isDict(const std::string& key) const;
130
136 Dictionary& subDict(const std::string& key);
137
143 const Dictionary& subDict(const std::string& key) const;
144
149 std::vector<std::string> keys() const;
150
155 std::unordered_map<std::string, std::any>& getMap();
156
161 const std::unordered_map<std::string, std::any>& getMap() const;
162
167 bool empty() const { return data_.empty(); }
168
169private:
170
171 std::unordered_map<std::string, std::any> data_;
172};
173
174} // namespace NeoFOAM
A class representing a dictionary that stores key-value pairs.
std::any & operator[](const std::string &key)
Accesses the value associated with the given key.
void insert(const std::string &key, const std::any &value)
Inserts a key-value pair into the dictionary.
void remove(const std::string &key)
Removes an entry from the dictionary based on the specified key.
Dictionary(const std::unordered_map< std::string, std::any > &keyValuePairs)
std::vector< std::string > keys() const
Retrieves the keys of the dictionary.
Dictionary(const std::initializer_list< std::pair< std::string, std::any > > &initList)
const std::any & operator[](const std::string &key) const
Accesses the value associated with the given key.
bool empty() const
Checks whether the dictionary is empty.
bool contains(const std::string &key) const
Checks if the given key is present in the dictionary.
bool isDict(const std::string &key) const
Checks if the value associated with the given key is a dictionary.
const T & get(const std::string &key) const
Retrieves the value associated with the given key, casting it to the specified type.
const Dictionary & subDict(const std::string &key) const
Retrieves a sub-dictionary associated with the given key.
std::unordered_map< std::string, std::any > & getMap()
Retrieves the underlying unordered map of the dictionary.
T & get(const std::string &key)
Retrieves the value associated with the given key, casting it to the specified type.
const std::unordered_map< std::string, std::any > & getMap() const
Retrieves the underlying unordered map of the dictionary.
Dictionary & subDict(const std::string &key)
Retrieves a sub-dictionary associated with the given key.
void logOutRange(const std::out_of_range &e, const std::string &key, const std::unordered_map< std::string, std::any > &data)