ROSE  0.11.96.11
Public Types | Public Member Functions | List of all members
Map< Key, T, Compare, Alloc > Class Template Reference

Description

template<class Key, class T, class Compare = std::less<Key>, class Alloc = std::allocator<std::pair<const Key, T> >>
class Map< Key, T, Compare, Alloc >

Extends std::map with methods that return optional values.

Definition at line 10 of file Map.h.

#include <Map.h>

Inheritance diagram for Map< Key, T, Compare, Alloc >:
Inheritance graph
[legend]
Collaboration diagram for Map< Key, T, Compare, Alloc >:
Collaboration graph
[legend]

Public Types

typedef std::map< Key, T, Compare, Alloc > map_type
 

Public Member Functions

 Map (const Compare &comp, const Alloc &alloc=Alloc())
 
template<class InputIterator >
 Map (InputIterator first, InputIterator last, const Compare &comp=Compare(), const Alloc &alloc=Alloc())
 
 Map (const Map &other)
 
boost::optional< T > get (const Key &key) const
 Lookup and return a value or nothing. More...
 
bool exists (const Key &key) const
 Convenience for determining if a key exists in this map. More...
 
const T & get_one (const Key &key) const
 Look up one value or throw an exception. More...
 
T & get_one (const Key &key)
 Look up one value or throw an exception. More...
 
const T & get_value_or (const Key &key, const T &dflt) const
 Convenience for getting a value from an Option. More...
 
T & get_value_or (const Key &key, T &dflt)
 Convenience for getting a value from an Option. More...
 

Member Function Documentation

◆ get()

template<class Key , class T , class Compare = std::less<Key>, class Alloc = std::allocator<std::pair<const Key, T> >>
boost::optional<T> Map< Key, T, Compare, Alloc >::get ( const Key &  key) const
inline

Lookup and return a value or nothing.

This is similar to the operator[] defined for std::map, but does not add a new pair to the map when the key does not exist. It returns a copy of the value that was found (if found). Here's an example of the usual way to use this:

...
std::string filename = "foo.txt";
FileInfo info = files.get(filename).get_value_or(FileInfo());

Definition at line 49 of file Map.h.

◆ get_one() [1/2]

template<class Key , class T , class Compare = std::less<Key>, class Alloc = std::allocator<std::pair<const Key, T> >>
const T& Map< Key, T, Compare, Alloc >::get_one ( const Key &  key) const
inline

Look up one value or throw an exception.

This is like get() except it can't handle the case where the map does not contain the requested key. It is unlike operator[] in that it doesn't add a new default-constructed value. The return value is a reference to the value stored in the map.

Definition at line 58 of file Map.h.

◆ get_one() [2/2]

template<class Key , class T , class Compare = std::less<Key>, class Alloc = std::allocator<std::pair<const Key, T> >>
T& Map< Key, T, Compare, Alloc >::get_one ( const Key &  key)
inline

Look up one value or throw an exception.

This is like get() except it can't handle the case where the map does not contain the requested key. It is unlike operator[] in that it doesn't add a new default-constructed value. The return value is a reference to the value stored in the map.

Definition at line 64 of file Map.h.

◆ get_value_or() [1/2]

template<class Key , class T , class Compare = std::less<Key>, class Alloc = std::allocator<std::pair<const Key, T> >>
const T& Map< Key, T, Compare, Alloc >::get_value_or ( const Key &  key,
const T &  dflt 
) const
inline

Convenience for getting a value from an Option.

Since the "map.get(key).get_value_or(dflt)" idiom is so frequenct, a get_value_or() method is defined directly on the map, combining both arguments into a single method call.

Definition at line 75 of file Map.h.

◆ get_value_or() [2/2]

template<class Key , class T , class Compare = std::less<Key>, class Alloc = std::allocator<std::pair<const Key, T> >>
T& Map< Key, T, Compare, Alloc >::get_value_or ( const Key &  key,
T &  dflt 
)
inline

Convenience for getting a value from an Option.

Since the "map.get(key).get_value_or(dflt)" idiom is so frequenct, a get_value_or() method is defined directly on the map, combining both arguments into a single method call.

Definition at line 79 of file Map.h.

◆ exists()

template<class Key , class T , class Compare = std::less<Key>, class Alloc = std::allocator<std::pair<const Key, T> >>
bool Map< Key, T, Compare, Alloc >::exists ( const Key &  key) const
inline

Convenience for determining if a key exists in this map.

The STL map requires one to do a find() and compare the result to end(). Although that can be more efficient when the value for that key will be used (because one just dereferences the already-obtained iterator rather than searching again), it requires more code when all one wants to do is check for existence.

Definition at line 89 of file Map.h.


The documentation for this class was generated from the following file:
Map::get
boost::optional< T > get(const Key &key) const
Lookup and return a value or nothing.
Definition: Map.h:49
Map
Extends std::map with methods that return optional values.
Definition: Map.h:10