summaryrefslogtreecommitdiff
path: root/project2/common/safeMapFind.h
blob: b461074e028178d8704cf8998c60da4f8037e4b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef SAFEMAPFIND_H
#define SAFEMAPFIND_H

template <class Ex, class Map>
typename Map::const_iterator
safeMapFind(const Map & map, const typename Map::key_type & key)
{
	typename Map::const_iterator i = map.find(key);
	if (i == map.end()) {
		throw Ex(key);
	}
	return i;
}

template <class Map>
typename Map::mapped_type
defaultMapFind(const Map & map, const typename Map::key_type & key, const typename Map::mapped_type & def = typename Map::mapped_type())
{
	typename Map::const_iterator i = map.find(key);
	if (i == map.end()) {
		return def;
	}
	return i->second;
}

template <class Ex, class Map>
const typename Map::mapped_type &
safeMapLookup(const Map & map, const typename Map::key_type & key)
{
	typename Map::const_iterator i = map.find(key);
	if (i == map.end()) {
		throw Ex(key);
	}
	return i->second;
}

#endif