-
Notifications
You must be signed in to change notification settings - Fork 298
/
factory.hpp
63 lines (51 loc) · 1.66 KB
/
factory.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
/*
* Copyright Jeremy Pack 2008
* Distributed under the Boost Software License, Version 1.0. (See
* accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* See http://www.boost.org/ for latest version.
*/
// No header guard - this file is intended to be included multiple times.
# define N BOOST_PP_ITERATION()
template <class T BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_PARAMS(N, class Param) >
class factory<T BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_PARAMS(N, Param) >
{
public:
// This assertion will fire if a factory type is instantiated
// with something that is not a class.
BOOST_STATIC_ASSERT((is_class<T>::value));
// This assertion will fire if T is const.
BOOST_STATIC_ASSERT((!is_const<T>::value));
template <class D>
void set() {
// This assertion will fire if a factory type is
// set to a class from which it doesn't inherit.
BOOST_STATIC_ASSERT((is_base_of<T, D>::value));
// This assertion will fire if D is const.
BOOST_STATIC_ASSERT((!is_const<D>::value));
this->func = &impl::create_function<
T, D BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_PARAMS(N,Param)
>::create;
}
factory() : func(0) {
}
factory(factory<T> const& first) : func(first.func) {}
factory& operator=(factory<T> const& first) {
this->func = first->func;
return *this;
}
bool is_valid() const { return this->func != 0; }
T* create(BOOST_PP_ENUM_BINARY_PARAMS(N, Param, p)) const {
if (this->func) {
return this->func(BOOST_PP_ENUM_PARAMS(N, p));
}
else {
return 0;
}
}
private:
typedef T* (*func_ptr_type)(BOOST_PP_ENUM_PARAMS(N, Param));
func_ptr_type func;
};
#undef N