msl 1.3.0
Loading...
Searching...
No Matches
Dictionary.h
Go to the documentation of this file.
1// Copyright 2024-2025 Julien Lamy, ICube, Université de Strasbourg-CNRS.
2// Part of msl, distributed under the terms of the MIT license.
3
4#ifndef _5724fb56_798a_4131_aeb5_cbcdcbe26c97
5#define _5724fb56_798a_4131_aeb5_cbcdcbe26c97
6
7#include <initializer_list>
8#include <memory>
9#include <string>
10#include <unordered_map>
11
12#include <boost/any.hpp>
13
14namespace msl
15{
16
18#define DECLARE_POINTERS(name) \
19 \
20 using Pointer = std::shared_ptr<name>; \
21 \
22 using ConstPointer = std::shared_ptr<name const>;
23
26{
27public:
29 using container = std::unordered_map<std::string, boost::any>;
30
32 using value_type = container::value_type;
33
35 using iterator = container::iterator;
37 using const_iterator = container::const_iterator;
38
40
42 static std::shared_ptr<Dictionary> New(
43 std::initializer_list<value_type> && data)
44 {
45 return std::make_shared<Dictionary>(data);
46 }
47
49 Dictionary(std::initializer_list<container::value_type> values);
50
51 Dictionary() = default;
52 Dictionary(Dictionary const &) = default;
53 Dictionary(Dictionary &&) = default;
54 Dictionary & operator=(Dictionary const &) = default;
56 ~Dictionary() = default;
57
63 container::mapped_type get(std::string const & key) const;
64
71 template<typename T>
72 T const & get(std::string const & key) const
73 {
74 try
75 {
76 return boost::any_cast<T const &>(this->_dictionary.at(key));
77 }
78 catch(std::out_of_range const &)
79 {
80 throw std::out_of_range("No such key: '"+key+"'");
81 }
82 catch(boost::bad_any_cast const &)
83 {
84 throw std::runtime_error("Wrong type for key: '"+key+"'");
85 }
86 }
87
94 template<typename T>
95 T & get(std::string const & key)
96 {
97 try
98 {
99 return boost::any_cast<T &>(this->_dictionary.at(key));
100 }
101 catch(std::out_of_range const &)
102 {
103 throw std::out_of_range("No such key: '"+key+"'");
104 }
105 catch(boost::bad_any_cast const &)
106 {
107 throw std::runtime_error("Wrong type for key: '"+key+"'");
108 }
109 }
110
112 bool has(std::string const & key) const;
113
119 boost::typeindex::type_info const & type(std::string const & key) const;
120
122 template<typename T>
123 Dictionary & set(std::string const & key, T && value)
124 {
125 this->_dictionary[key] = value;
126 return *this;
127 }
128
130 bool empty() const;
131
133 size_t size() const;
134
141
148
151
153 Dictionary & insert(std::initializer_list<value_type> && data);
154
155private:
156 container _dictionary;
157};
158
159}
160
161#endif // _5724fb56_798a_4131_aeb5_cbcdcbe26c97
#define DECLARE_POINTERS(name)
Declare pointer type aliases.
Definition Dictionary.h:18
Generic key-value associative container.
Definition Dictionary.h:26
Dictionary(std::initializer_list< container::value_type > values)
Create a dictionary from pairs of (key, value)
Dictionary & operator=(Dictionary const &)=default
size_t size() const
Return the number of items in the dictionary.
container::iterator iterator
Iterator on the elements.
Definition Dictionary.h:35
const_iterator cend() const
Return a read-only iterator past the last element.
boost::typeindex::type_info const & type(std::string const &key) const
Return the type of a key.
const_iterator cbegin() const
Return a read-only iterator to the first element.
Dictionary(Dictionary &&)=default
std::unordered_map< std::string, boost::any > container
Underlying container.
Definition Dictionary.h:29
Dictionary & clear()
Erases all elements.
T & get(std::string const &key)
Return a typed value.
Definition Dictionary.h:95
const_iterator begin() const
Return a read-only iterator to the first element.
Dictionary & insert(std::initializer_list< value_type > &&data)
Inserts elements.
Dictionary()=default
~Dictionary()=default
bool has(std::string const &key) const
Check whether the key is in the dictionary.
const_iterator end() const
Return a read-only iterator past the last element.
T const & get(std::string const &key) const
Return a typed value (read-only)
Definition Dictionary.h:72
iterator end()
Return a read-write iterator past the last element.
container::mapped_type get(std::string const &key) const
Return a generic value.
Dictionary(Dictionary const &)=default
iterator begin()
Return a read-write iterator to the first element.
container::value_type value_type
Pair of (key, value)
Definition Dictionary.h:32
Dictionary & set(std::string const &key, T &&value)
Set the value at given key, create it if required.
Definition Dictionary.h:123
bool empty() const
Check whether the dictionary is empty.
container::const_iterator const_iterator
Read-only iterator on the elements.
Definition Dictionary.h:37
static std::shared_ptr< Dictionary > New(std::initializer_list< value_type > &&data)
Create a dictionary from pairs of (key, value)
Definition Dictionary.h:42
Dictionary & operator=(Dictionary &&)=default
Top-level namespace of the msl library.
Definition acceleration.h:17