/
function_registration.hpp
138 lines (120 loc) · 4.17 KB
/
function_registration.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
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
// Copyright (c) 2011 Thomas Heller
// Copyright (c) 2013 Hartmut Kaiser
// Copyright (c) 2014 Agustin Berge
//
// 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)
#ifndef HPX_UTIL_DETAIL_FUNCTION_REGISTRATION_HPP
#define HPX_UTIL_DETAIL_FUNCTION_REGISTRATION_HPP
#include <hpx/config.hpp>
#include <hpx/util/detail/get_table.hpp>
#include <hpx/runtime/serialization/detail/polymorphic_intrusive_factory.hpp>
#include <hpx/traits/needs_automatic_registration.hpp>
#include <hpx/util/demangle_helper.hpp>
#include <hpx/util/tuple.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
namespace hpx { namespace util { namespace detail
{
///////////////////////////////////////////////////////////////////////////
template <typename Function>
struct get_function_name_impl
{
static char const* call()
#ifndef HPX_HAVE_AUTOMATIC_SERIALIZATION_REGISTRATION
;
#else
{
// If you encounter this assert while compiling code, that means
// that you have a HPX_UTIL_REGISTER_[UNIQUE_]FUNCTION macro
// somewhere in a source file, but the header in which the function
// is defined misses a HPX_UTIL_REGISTER_[UNIQUE_]FUNCTION_DECLARATION
BOOST_MPL_ASSERT_MSG(
traits::needs_automatic_registration<Function>::value
, HPX_UTIL_REGISTER_FUNCTION_DECLARATION_MISSING
, (Function)
);
return util::type_id<Function>::typeid_.type_id();
}
#endif
};
///////////////////////////////////////////////////////////////////////////
template <typename Function>
char const* get_function_name()
{
return get_function_name_impl<Function>::call();
}
///////////////////////////////////////////////////////////////////////////
struct HPX_EXPORT function_registration_info_base
{
virtual void const* get_table_ptr() const = 0;
virtual ~function_registration_info_base() {}
};
template <typename VTable, typename T>
struct function_registration_info : function_registration_info_base
{
virtual void const* get_table_ptr() const
{
return detail::get_table<VTable, T>();
}
};
template <typename VTable, typename T>
struct function_registration
{
typedef function_registration_info_base base_type;
static void * create()
{
static function_registration_info<VTable, T> ri;
return &ri;
}
function_registration()
{
hpx::serialization::detail::polymorphic_intrusive_factory::instance().
register_class(
detail::get_function_name<util::tuple<VTable, T> >()
, &function_registration::create
);
}
};
template <typename VTable>
VTable const* get_table_ptr(std::string const& name)
{
detail::function_registration_info_base *
p(
hpx::serialization::detail::polymorphic_intrusive_factory::instance().
create<function_registration_info_base>(name)
);
return static_cast<VTable const*>(p->get_table_ptr());
}
template <
typename VTablePair
, typename Enable =
typename traits::needs_automatic_registration<VTablePair>::type
>
struct automatic_function_registration
{
automatic_function_registration()
{
function_registration<
typename util::tuple_element<0, VTablePair>::type
, typename util::tuple_element<1, VTablePair>::type
> auto_register;
}
automatic_function_registration& register_function()
{
return *this;
}
};
template <typename VTablePair>
struct automatic_function_registration<VTablePair, boost::mpl::false_>
{
automatic_function_registration()
{}
automatic_function_registration& register_function()
{
return *this;
}
};
}}}
#endif