NeoN
A framework for CFD software
Loading...
Searching...
No Matches
dictionary.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2025 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
13
14namespace NeoN
15{
16
18 const std::out_of_range& e,
19 const std::string& key,
20 const std::unordered_map<std::string, std::any>& data
21);
22
35{
36public:
37
38 Dictionary() = default;
39
40 Dictionary(const std::unordered_map<std::string, std::any>& keyValuePairs);
41
42 Dictionary(const std::initializer_list<std::pair<std::string, std::any>>& initList);
43
49 void insert(const std::string& key, const std::any& value);
50
56 [[nodiscard]] bool contains(const std::string& key) const;
57
66 void remove(const std::string& key);
67
73 [[nodiscard]] std::any& operator[](const std::string& key);
74
80 [[nodiscard]] const std::any& operator[](const std::string& key) const;
81
90 template<typename T>
91 [[nodiscard]] T& get(const std::string& key)
92 {
93 try
94 {
95 return std::any_cast<T&>(operator[](key));
96 }
97 catch (const std::bad_any_cast& e)
98 {
99 logBadAnyCast<T>(e, key, data_);
100 throw;
101 }
102 }
103
112 template<typename T>
113 [[nodiscard]] const T& get(const std::string& key) const
114 {
115 try
116 {
117 return std::any_cast<const T&>(operator[](key));
118 }
119 catch (const std::bad_any_cast& e)
120 {
121 logBadAnyCast<T>(e, key, data_);
122 throw;
123 }
124 }
125
131 [[nodiscard]] bool isDict(const std::string& key) const;
132
138 Dictionary& subDict(const std::string& key);
139
145 const Dictionary& subDict(const std::string& key) const;
146
151 std::vector<std::string> keys() const;
152
157 std::unordered_map<std::string, std::any>& getMap();
158
163 const std::unordered_map<std::string, std::any>& getMap() const;
164
169 bool empty() const { return data_.empty(); }
170
171private:
172
173 std::unordered_map<std::string, std::any> data_;
174};
175
176std::ostream& operator<<(std::ostream& os, const Dictionary& in);
177
178} // 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) 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.
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)
Definition array.hpp:20
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)