-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapdefault.hpp
67 lines (58 loc) · 1.91 KB
/
mapdefault.hpp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//@ {"targets":[{"name":"mapdefault.hpp","type":"include"}]}
#ifndef TEMPLE_MAPDEFAULT_HPP
#define TEMPLE_MAPDEFAULT_HPP
#include "error.hpp"
#include "type.hpp"
#include <map>
namespace Temple
{
template<class StorageModel,class ItemType>
class MapDefault:private std::map<typename StorageModel::KeyType,ItemType>
{
public:
using key_type=typename StorageModel::KeyType;
using std::map<key_type,ItemType>::insert;
using std::map<key_type,ItemType>::find;
using std::map<key_type,ItemType>::size;
using std::map<key_type,ItemType>::begin;
using std::map<key_type,ItemType>::end;
using std::map<key_type,ItemType>::value_type;
using std::map<key_type,ItemType>::iterator;
using std::map<key_type,ItemType>::const_iterator;
using std::map<key_type,ItemType>::emplace;
template<class Type,class ExceptionHandler>
const Type& find_typed(const key_type& key,ExceptionHandler&& eh) const
{
auto& x=find(key,eh);
if(!x.template has<Type>())
{
raise(Error("Key «",key.c_str(),"» does not map to a value of type "
,type(IdGet<Type,StorageModel>::id),'.'),eh);
}
return x.template value<Type>();
}
template<class Type,class ExceptionHandler>
Type& find_typed(const key_type& key,ExceptionHandler&& eh)
{
return const_cast<Type&>(static_cast<const MapDefault&>(*this).find_typed<Type>(key,eh));
}
template<class ExceptionHandler>
const auto& find(const key_type& key,ExceptionHandler&& eh) const
{
auto i=find(key);
if(i==end())
{raise(Error("Key «",key.c_str(),"» not found."),eh);}
return *(i->second.get());
}
template<class ExceptionHandler>
auto& find(const key_type& key,ExceptionHandler&& eh)
{
return unconst_cast(static_cast<const MapDefault&>(*this).find(key,eh));
}
private:
template<class T>
static T& unconst_cast(const T &v) noexcept
{return const_cast<T &>(v);}
};
}
#endif