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 [[nodiscard]] bool contains(const std::string& key) const;
72
81 void remove(const std::string& key);
82
88 [[nodiscard]] std::any& operator[](const std::string& key);
89
95 [[nodiscard]] const std::any& operator[](const std::string& key) const;
96
105 template<typename T>
106 [[nodiscard]] T& get(const std::string& key)
107 {
108 try
109 {
110 return std::any_cast<T&>(operator[](key));
111 }
112 catch (const std::bad_any_cast& e)
113 {
114 logBadAnyCast<T>(e, key, data_);
115 throw;
116 }
117 }
118
127 template<typename T>
128 [[nodiscard]] const T& get(const std::string& key) const
129 {
130 try
131 {
132 return std::any_cast<const T&>(operator[](key));
133 }
134 catch (const std::bad_any_cast& e)
135 {
136 logBadAnyCast<T>(e, key, data_);
137 throw;
138 }
139 }
140
149 template<typename T>
150 [[nodiscard]] const T get(const std::string& key, T defaultValue) const
151 {
152 if (contains(key))
153 {
154 return T(get<T>(key));
155 }
156 return defaultValue;
157 }
158
164 template<typename T>
165 [[nodiscard]] bool isType(const std::string& key) const
166 {
167 return contains(key) && std::any_cast<T>(&data_.at(key));
168 }
169
175 [[nodiscard]] bool isDict(const std::string& key) const;
176
177
183 Dictionary& subDict(const std::string& key);
184
190 const Dictionary& subDict(const std::string& key) const;
191
196 std::vector<std::string> keys() const;
197
202 std::unordered_map<std::string, std::any>& getMap();
203
208 const std::unordered_map<std::string, std::any>& getMap() const;
209
214 bool empty() const { return data_.empty(); }
215
216private:
217
218 std::unordered_map<std::string, std::any> data_;
219};
220
221std::ostream& operator<<(std::ostream& os, const Dictionary& in);
222
223} // 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.
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)
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