NeoN
A framework for CFD software
Loading...
Searching...
No Matches
dictionary.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2026 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <unordered_map>
8#include <any>
9#include <string>
10#include <vector>
11
14
15namespace NeoN
16{
17
19 const std::out_of_range& e,
20 const std::string& key,
21 const std::unordered_map<std::string, std::any>& data
22);
23
24namespace detail
25{
26
30template<typename HoldType>
32{
33 const HoldType& c;
34};
35
36}
37
50{
51public:
52
53 Dictionary() = default;
54
55 Dictionary(const std::unordered_map<std::string, std::any>& keyValuePairs);
56
57 Dictionary(const std::initializer_list<std::pair<std::string, std::any>>& initList);
58
64 void insert(const std::string& key, const std::any& value);
65
71 void insert(const std::string& key, std::any&& value);
72
78 [[nodiscard]] bool contains(const std::string& key) const;
79
88 void remove(const std::string& key);
89
95 [[nodiscard]] std::any& operator[](const std::string& key);
96
102 [[nodiscard]] const std::any& operator[](const std::string& key) const;
103
112 template<typename T>
113 [[nodiscard]] T& get(const std::string& key)
114 {
115 try
116 {
117 return std::any_cast<T&>(operator[](key));
118 }
119 catch (const std::bad_any_cast& e)
120 {
121 logBadAnyCast<T>(e, key, data_);
122 throw;
123 }
124 }
125
134 template<typename T>
135 [[nodiscard]] const T& get(const std::string& key) const
136 {
137 try
138 {
139 return std::any_cast<const T&>(operator[](key));
140 }
141 catch (const std::bad_any_cast& e)
142 {
143 logBadAnyCast<T>(e, key, data_);
144 throw;
145 }
146 }
147
156 template<typename T>
157 [[nodiscard]] const T get(const std::string& key, T defaultValue) const
158 {
159 if (contains(key))
160 {
161 return T(get<T>(key));
162 }
163 return defaultValue;
164 }
165
171 template<typename T>
172 [[nodiscard]] bool isType(const std::string& key) const
173 {
174 return contains(key) && std::any_cast<T>(&data_.at(key));
175 }
176
182 [[nodiscard]] bool isDict(const std::string& key) const;
183
184
190 Dictionary& subDict(const std::string& key);
191
197 const Dictionary& subDict(const std::string& key) const;
198
203 std::vector<std::string> keys() const;
204
209 std::unordered_map<std::string, std::any>& getMap();
210
215 const std::unordered_map<std::string, std::any>& getMap() const;
216
221 bool empty() const { return data_.empty(); }
222
223private:
224
225 std::unordered_map<std::string, std::any> data_;
226};
227
228std::ostream& operator<<(std::ostream& os, const Dictionary& in);
229
230} // namespace NeoN
A class representing a dictionary that stores key-value pairs.
void remove(const std::string &key)
Removes an entry from the dictionary based on the specified key.
bool isDict(const std::string &key) const
Checks if the value associated with the given key is a dictionary.
T & get(const std::string &key)
Retrieves the value associated with the given key, casting it to the specified type.
const T get(const std::string &key, T defaultValue) const
Retrieves the value associated with the given key, casting it to the specified type.
void insert(const std::string &key, std::any &&value)
Inserts a key-value pair into the dictionary, moving the value.
const T & get(const std::string &key) const
Retrieves the value associated with the given key, casting it to the specified type.
std::any & operator[](const std::string &key)
Accesses the value associated with the given key.
Dictionary & subDict(const std::string &key)
Retrieves a sub-dictionary associated with the given key.
const std::any & operator[](const std::string &key) const
Accesses the value associated with the given key.
const std::unordered_map< std::string, std::any > & getMap() const
Retrieves the underlying unordered map of the dictionary.
bool isType(const std::string &key) const
Checks if the value associated with the given key is of given Type T.
const Dictionary & subDict(const std::string &key) const
Retrieves a sub-dictionary associated with the given key.
Dictionary(const std::initializer_list< std::pair< std::string, std::any > > &initList)
std::vector< std::string > keys() const
Retrieves the keys of the dictionary.
void insert(const std::string &key, const std::any &value)
Inserts a key-value pair into the dictionary.
bool empty() const
Checks whether the dictionary is empty.
std::unordered_map< std::string, std::any > & getMap()
Retrieves the underlying unordered map of the dictionary.
bool contains(const std::string &key) const
Checks if the given key is present in the dictionary.
Dictionary()=default
Dictionary(const std::unordered_map< std::string, std::any > &keyValuePairs)
Integer types used throughout NeoN.
Definition array.hpp:18
std::ostream & operator<<(std::ostream &os, const Dictionary &in)
void logOutRange(const std::out_of_range &e, const std::string &key, const std::unordered_map< std::string, std::any > &data)
void logBadAnyCast(const std::bad_any_cast &e, const Key &key, const Container &data)
Definition demangle.hpp:20
A wrapper class to put references in dictionaries to overcome the issue that std::any cannot hold a r...
const HoldType & c
#define NEON_TYPE_VISIBLE