ROSE 0.11.145.250
util/Map.h
1#ifndef ROSE_Map_H
2#define ROSE_Map_H
3
4#include <boost/optional.hpp>
5
6#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
7#include <boost/serialization/access.hpp>
8#include <boost/serialization/nvp.hpp>
9#endif
10
11#include <stdexcept>
12#include <map>
13
15template<class Key, class T, class Compare=std::less<Key>, class Alloc=std::allocator<std::pair<const Key, T> > >
16class Map: public std::map<Key, T, Compare, Alloc> {
17public:
18 typedef std::map<Key, T, Compare, Alloc> map_type;
19
20#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
21private:
22 friend class boost::serialization::access;
23
24 template<class S>
25 void serialize(S &s, const unsigned /*version*/) {
26 s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(map_type);
27 }
28#endif
29
30public:
31 // Constructors are the same as for std::map
32 Map() {}
33
34 explicit Map(const Compare& comp, const Alloc& alloc = Alloc())
35 : map_type(comp, alloc) {}
36
37 template <class InputIterator>
38 Map(InputIterator first, InputIterator last, const Compare& comp = Compare(), const Alloc& alloc = Alloc())
39 : map_type(first, last, comp, alloc) {};
40
41 Map(const Map& other)
42 : map_type(other) {}
43
44 Map& operator=(const Map&) = default;
45
57 boost::optional<T> get(const Key &key) const {
58 typename map_type::const_iterator found = this->find(key);
59 return found==this->end() ? boost::optional<T>() : boost::optional<T>(found->second);
60 }
61
66 const T& get_one(const Key &key) const {
67 typename map_type::const_iterator found = this->find(key);
68 if (found==this->end())
69 throw std::domain_error("key not present in map");
70 return found->second;
71 }
72 T& get_one(const Key &key) {
73 typename map_type::iterator found = this->find(key);
74 if (found==this->end())
75 throw std::domain_error("key not present in map");
76 return found->second;
77 }
83 const T& get_value_or(const Key& key, const T& dflt) const {
84 typename map_type::const_iterator found = this->find(key);
85 return found==this->end() ? dflt : found->second;
86 }
87 T& get_value_or(const Key& key, T& dflt) {
88 typename map_type::iterator found = this->find(key);
89 return found==this->end() ? dflt : found->second;
90 }
97 bool exists(const Key &key) const { return this->find(key)!=this->end(); }
98};
99
100#endif
Extends std::map with methods that return optional values.
Definition util/Map.h:16
const T & get_value_or(const Key &key, const T &dflt) const
Convenience for getting a value from an Option.
Definition util/Map.h:83
bool exists(const Key &key) const
Convenience for determining if a key exists in this map.
Definition util/Map.h:97
boost::optional< T > get(const Key &key) const
Lookup and return a value or nothing.
Definition util/Map.h:57
T & get_value_or(const Key &key, T &dflt)
Convenience for getting a value from an Option.
Definition util/Map.h:87
T & get_one(const Key &key)
Look up one value or throw an exception.
Definition util/Map.h:72
const T & get_one(const Key &key) const
Look up one value or throw an exception.
Definition util/Map.h:66