msl 1.3.0
Loading...
Searching...
No Matches
Dictionary.h
Go to the documentation of this file.
1// Copyright 2024-2026 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
14#include "msl/helpers.h"
15
16namespace msl
17{
18
21{
22public:
24 using container = std::unordered_map<std::string, boost::any>;
25
27 using value_type = container::value_type;
28
30 using iterator = container::iterator;
32 using const_iterator = container::const_iterator;
33
35
37 static std::shared_ptr<Dictionary> New(
38 std::initializer_list<value_type> && data)
39 {
40 return std::make_shared<Dictionary>(data);
41 }
42
44 Dictionary(std::initializer_list<container::value_type> values);
45
46 Dictionary() = default;
47 Dictionary(Dictionary const &) = default;
48 Dictionary(Dictionary &&) = default;
49 Dictionary & operator=(Dictionary const &) = default;
50 Dictionary & operator=(Dictionary &&) = default;
51 ~Dictionary() = default;
52
58 container::mapped_type get(std::string const & key) const;
59
66 template<typename T>
67 T const & get(std::string const & key) const
68 {
69 try
70 {
71 return boost::any_cast<T const &>(this->_dictionary.at(key));
72 }
73 catch(std::out_of_range const &)
74 {
75 throw std::out_of_range("No such key: '"+key+"'");
76 }
77 catch(boost::bad_any_cast const &)
78 {
79 throw std::runtime_error("Wrong type for key: '"+key+"'");
80 }
81 }
82
89 template<typename T>
90 T & get(std::string const & key)
91 {
92 try
93 {
94 return boost::any_cast<T &>(this->_dictionary.at(key));
95 }
96 catch(std::out_of_range const &)
97 {
98 throw std::out_of_range("No such key: '"+key+"'");
99 }
100 catch(boost::bad_any_cast const &)
101 {
102 throw std::runtime_error("Wrong type for key: '"+key+"'");
103 }
104 }
105
107 bool has(std::string const & key) const;
108
114 boost::typeindex::type_info const & type(std::string const & key) const;
115
117 template<typename T>
118 Dictionary & set(std::string const & key, T && value)
119 {
120 this->_dictionary[key] = value;
121 return *this;
122 }
123
125 bool empty() const;
126
128 size_t size() const;
129
136
143
146
148 Dictionary & insert(std::initializer_list<value_type> && data);
149
150private:
151 container _dictionary;
152};
153
154}
155
156#endif // _5724fb56_798a_4131_aeb5_cbcdcbe26c97
Dictionary(std::initializer_list< container::value_type > values)
Create a dictionary from pairs of (key, value).
size_t size() const
Return the number of items in the dictionary.
container::iterator iterator
Iterator on the elements.
Definition Dictionary.h:30
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.
std::unordered_map< std::string, boost::any > container
Underlying container.
Definition Dictionary.h:24
Dictionary & clear()
Erases all elements.
T & get(std::string const &key)
Return a typed value.
Definition Dictionary.h:90
const_iterator begin() const
Return a read-only iterator to the first element.
Dictionary & insert(std::initializer_list< value_type > &&data)
Inserts elements.
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:67
iterator end()
Return a read-write iterator past the last element.
container::mapped_type get(std::string const &key) const
Return a generic value.
iterator begin()
Return a read-write iterator to the first element.
container::value_type value_type
Pair of (key, value).
Definition Dictionary.h:27
Dictionary & set(std::string const &key, T &&value)
Set the value at given key, create it if required.
Definition Dictionary.h:118
bool empty() const
Check whether the dictionary is empty.
container::const_iterator const_iterator
Read-only iterator on the elements.
Definition Dictionary.h:32
static std::shared_ptr< Dictionary > New(std::initializer_list< value_type > &&data)
Create a dictionary from pairs of (key, value).
Definition Dictionary.h:37
#define DECLARE_POINTERS(name)
Declare pointer type aliases.
Definition helpers.h:83
Top-level namespace of the msl library.
Definition acceleration.h:17