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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
/*
* (C) 2014,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#ifndef BOTAN_ALGO_REGISTRY_H__
#define BOTAN_ALGO_REGISTRY_H__
#include <botan/types.h>
#include <functional>
#include <stdexcept>
#include <mutex>
#include <map>
namespace Botan {
size_t static_provider_weight(const std::string& prov_name);
template<typename T>
class Algo_Registry
{
public:
typedef typename T::Spec Spec;
typedef std::function<T* (const Spec&)> maker_fn;
static Algo_Registry<T>& global_registry()
{
static Algo_Registry<T> g_registry;
return g_registry;
}
void add(const std::string& name, const std::string& provider, maker_fn fn)
{
std::unique_lock<std::mutex> lock(m_mutex);
if(!m_maker_fns[name][provider])
m_maker_fns[name][provider] = fn;
}
T* make(const std::string& spec_str)
{
const Spec spec(spec_str);
return make(spec_str, "");
}
T* make(const Spec& spec, const std::string& provider = "")
{
maker_fn maker = find_maker(spec, provider);
try
{
return maker(spec);
}
catch(std::exception& e)
{
//return nullptr; // ??
throw std::runtime_error("Creating '" + spec.as_string() + "' failed: " + e.what());
}
}
class Add
{
public:
Add(const std::string& basename, maker_fn fn, const std::string& provider = "builtin")
{
Algo_Registry<T>::global_registry().add(basename, provider, fn);
}
Add(bool cond, const std::string& basename, maker_fn fn, const std::string& provider)
{
if(cond)
Algo_Registry<T>::global_registry().add(basename, provider, fn);
}
};
private:
Algo_Registry() {}
maker_fn find_maker(const Spec& spec, const std::string& provider)
{
const std::string basename = spec.algo_name();
std::unique_lock<std::mutex> lock(m_mutex);
auto providers = m_maker_fns.find(basename);
if(providers != m_maker_fns.end() && !providers->second.empty())
{
const std::map<std::string, maker_fn>& prov = providers->second;
if(provider != "")
{
// find one explicit provider requested by user, or fail
auto i = prov.find(provider);
if(i != prov.end())
return i->second;
}
else
{
if(prov.size() == 1)
{
return prov.begin()->second;
}
else if(prov.size() > 1)
{
// TODO choose best of available options (how?)
//throw std::runtime_error("multiple choice not implemented");
return prov.begin()->second;
}
}
}
// Default result is a function producing a null pointer
return [](const Spec&) { return nullptr; };
}
std::mutex m_mutex;
std::map<std::string, std::map<std::string, maker_fn>> m_maker_fns;
};
template<typename T> T*
make_a(const typename T::Spec& spec, const std::string provider = "")
{
return Algo_Registry<T>::global_registry().make(spec, provider);
}
template<typename T> T*
make_a(const std::string& spec_str, const std::string provider = "")
{
typename T::Spec spec(spec_str);
return make_a<T>(spec, provider);
}
template<typename T> T*
make_new_T(const typename Algo_Registry<T>::Spec&) { return new T; }
template<typename T, size_t DEF_VAL> T*
make_new_T_1len(const typename Algo_Registry<T>::Spec& spec)
{
return new T(spec.arg_as_integer(0, DEF_VAL));
}
template<typename T, size_t DEF1, size_t DEF2> T*
make_new_T_2len(const typename Algo_Registry<T>::Spec& spec)
{
return new T(spec.arg_as_integer(0, DEF1), spec.arg_as_integer(1, DEF2));
}
template<typename T> T*
make_new_T_1str(const typename Algo_Registry<T>::Spec& spec, const std::string& def)
{
return new T(spec.arg(0, def));
}
template<typename T> T*
make_new_T_1str_req(const typename Algo_Registry<T>::Spec& spec)
{
return new T(spec.arg(0));
}
template<typename T, typename X> T*
make_new_T_1X(const typename Algo_Registry<T>::Spec& spec)
{
std::unique_ptr<X> x(Algo_Registry<X>::global_registry().make(spec.arg(0)));
if(!x)
throw std::runtime_error(spec.arg(0));
return new T(x.release());
}
#define BOTAN_REGISTER_NAMED_T(T, namestr, type, maker) \
namespace { Algo_Registry<T>::Add g_ ## type ## _reg(namestr, maker); }
#define BOTAN_REGISTER_T(T, name, maker) \
namespace { Algo_Registry<T>::Add g_ ## name ## _reg(#name, maker); }
#define BOTAN_REGISTER_T_NOARGS(T, name) \
namespace { Algo_Registry<T>::Add g_ ## name ## _reg(#name, make_new_T<name>); }
#define BOTAN_REGISTER_T_1LEN(T, name, def) \
namespace { Algo_Registry<T>::Add g_ ## name ## _reg(#name, make_new_T_1len<name, def>); }
#define BOTAN_REGISTER_NAMED_T_NOARGS(T, type, name, provider) \
namespace { Algo_Registry<T>::Add g_ ## type ## _reg(name, make_new_T<type>, provider); }
#define BOTAN_COND_REGISTER_NAMED_T_NOARGS(cond, T, type, name, provider) \
namespace { Algo_Registry<T>::Add g_ ## type ## _reg(cond, name, make_new_T<type>, provider); }
#define BOTAN_REGISTER_NAMED_T_2LEN(T, type, name, provider, len1, len2) \
namespace { Algo_Registry<T>::Add g_ ## type ## _reg(name, make_new_T_2len<type, len1, len2>, provider); }
// TODO move elsewhere:
#define BOTAN_REGISTER_TRANSFORM(name, maker) BOTAN_REGISTER_T(Transform, name, maker)
#define BOTAN_REGISTER_TRANSFORM_NOARGS(name) BOTAN_REGISTER_T_NOARGS(Transform, name)
}
#endif
|