4#ifndef _5724fb56_798a_4131_aeb5_cbcdcbe26c97
5#define _5724fb56_798a_4131_aeb5_cbcdcbe26c97
7#include <initializer_list>
10#include <unordered_map>
12#include <boost/any.hpp>
17#define DECLARE_POINTERS(name) \
18 using Pointer = std::shared_ptr<name>; \
19 using ConstPointer = std::shared_ptr<name const>;
26 using container = std::unordered_map<std::string, boost::any>;
37 static std::shared_ptr<Dictionary>
New(
38 std::initializer_list<value_type> && data)
40 return std::make_shared<Dictionary>(data);
44 Dictionary(std::initializer_list<container::value_type> values);
56 container::mapped_type
get(std::string
const & key)
const;
65 T
const &
get(std::string
const & key)
const
69 return boost::any_cast<T const &>(this->_dictionary.at(key));
71 catch(std::out_of_range
const &)
73 throw std::out_of_range(
"No such key: '"+key+
"'");
75 catch(boost::bad_any_cast
const &)
77 throw std::runtime_error(
"Wrong type for key: '"+key+
"'");
88 T &
get(std::string
const & key)
92 return boost::any_cast<T &>(this->_dictionary.at(key));
94 catch(std::out_of_range
const &)
96 throw std::out_of_range(
"No such key: '"+key+
"'");
98 catch(boost::bad_any_cast
const &)
100 throw std::runtime_error(
"Wrong type for key: '"+key+
"'");
105 bool has(std::string
const & key)
const;
112 boost::typeindex::type_info
const &
type(std::string
const & key)
const;
118 this->_dictionary[key] = value;
Dictionary(std::initializer_list< container::value_type > values)
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:31
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:26
Dictionary & clear()
Erases all elements.
T & get(std::string const &key)
Return a typed value.
Definition Dictionary.h:88
const_iterator begin() const
Return a read-only iterator to the first element.
Dictionary & insert(std::initializer_list< value_type > &&data)
Inserts elements.
DECLARE_POINTERS(Dictionary)
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:65
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
Definition Dictionary.h:28
Dictionary & set(std::string const &key, T &&value)
Set the value at given key, create it if required.
Definition Dictionary.h:116
bool empty() const
Check whether the dictionary is empty.
container::const_iterator const_iterator
Read-only iterator on the elements.
Definition Dictionary.h:33
static std::shared_ptr< Dictionary > New(std::initializer_list< value_type > &&data)
Definition Dictionary.h:37
Dictionary & operator=(Dictionary &&)=default
Definition acceleration.h:17