4#include <boost/optional.hpp>
5#include <boost/serialization/access.hpp>
6#include <boost/serialization/nvp.hpp>
12template<
class Key,
class T,
class Compare=std::less<Key>,
class Alloc=std::allocator<std::pair<const Key, T> > >
13class Map:
public std::map<Key, T, Compare, Alloc> {
15 typedef std::map<Key, T, Compare, Alloc> map_type;
17#ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
19 friend class boost::serialization::access;
22 void serialize(S &s,
const unsigned ) {
23 s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(map_type);
31 explicit Map(
const Compare& comp,
const Alloc& alloc = Alloc())
32 : map_type(comp, alloc) {}
34 template <
class InputIterator>
35 Map(InputIterator first, InputIterator last,
const Compare& comp = Compare(),
const Alloc& alloc = Alloc())
36 : map_type(first, last, comp, alloc) {};
52 boost::optional<T>
get(
const Key &key)
const {
53 typename map_type::const_iterator found = this->find(key);
54 return found==this->end() ? boost::optional<T>() : boost::optional<T>(found->second);
62 typename map_type::const_iterator found = this->find(key);
63 if (found==this->end())
64 throw std::domain_error(
"key not present in map");
68 typename map_type::iterator found = this->find(key);
69 if (found==this->end())
70 throw std::domain_error(
"key not present in map");
79 typename map_type::const_iterator found = this->find(key);
80 return found==this->end() ? dflt : found->second;
83 typename map_type::iterator found = this->find(key);
84 return found==this->end() ? dflt : found->second;
92 bool exists(
const Key &key)
const {
return this->find(key)!=this->end(); }
Extends std::map with methods that return optional values.
const T & get_value_or(const Key &key, const T &dflt) const
Convenience for getting a value from an Option.
bool exists(const Key &key) const
Convenience for determining if a key exists in this map.
boost::optional< T > get(const Key &key) const
Lookup and return a value or nothing.
T & get_value_or(const Key &key, T &dflt)
Convenience for getting a value from an Option.
T & get_one(const Key &key)
Look up one value or throw an exception.
const T & get_one(const Key &key) const
Look up one value or throw an exception.