-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvptr_vector.hpp
More file actions
209 lines (180 loc) · 7.83 KB
/
vptr_vector.hpp
File metadata and controls
209 lines (180 loc) · 7.83 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) 2018-2025 Jean-Louis Leroy
// 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 BOOST_OPENMETHOD_POLICY_VPTR_VECTOR_HPP
#define BOOST_OPENMETHOD_POLICY_VPTR_VECTOR_HPP
#include <boost/openmethod/preamble.hpp>
#include <variant>
#include <vector>
namespace boost::openmethod {
namespace detail {
template<class Registry>
inline std::vector<vptr_type> vptr_vector_vptrs;
template<class Registry>
inline std::vector<const vptr_type*> vptr_vector_indirect_vptrs;
} // namespace detail
namespace policies {
//! Stores v-table pointers in a vector.
//!
//! `vptr_vector` stores v-table pointers in a global vector. If `Registry`
//! contains a @ref type_hash policy, it is used to convert `type_id`s to
//! indices. Otherwise, `type_id`s are used directly as indices.
//!
//! If the registry contains the @ref indirect_vptr policy, stores pointers to
//! pointers to v-tables in the vector.
struct vptr_vector : vptr {
public:
//! A VptrFn metafunction.
//!
//! Keeps track of v-table pointers using a `std::vector`.
//!
//! If `Registry` contains a @ref type_hash policy, it is used to convert
//! `type_id`s to indices; otherwise, `type_id`s are used as indices.
//!
//! If `Registry` contains the @ref indirect_vptr policy, stores pointers to
//! pointers to v-tables in the map.
//!
//! @tparam Registry The registry containing this policy.
template<class Registry>
struct fn {
using type_hash =
typename Registry::template policy<policies::type_hash>;
static constexpr auto has_type_hash = !std::is_same_v<type_hash, void>;
//! Stores the v-table pointers.
//!
//! If `Registry` contains a @ref type_hash policy, its `initialize`
//! function is called. Its result determines the size of the vector.
//! The v-table pointers are copied into the vector.
//!
//! @tparam Context An @ref InitializeContext.
//! @tparam Options... Zero or more option types.
//! @param ctx A Context object.
//! @param options A tuple of option objects.
template<class Context, class... Options>
static auto initialize(
const Context& ctx, const std::tuple<Options...>& options) -> void {
std::size_t size;
(void)options;
if constexpr (has_type_hash) {
auto [_, max_value] = type_hash::initialize(ctx, options);
size = max_value + 1;
} else {
size = 0;
for (auto iter = ctx.classes_begin(); iter != ctx.classes_end();
++iter) {
for (auto type_iter = iter->type_id_begin();
type_iter != iter->type_id_end(); ++type_iter) {
size = (std::max)(size, std::size_t(*type_iter));
}
}
++size;
}
if constexpr (Registry::has_indirect_vptr) {
detail::vptr_vector_indirect_vptrs<Registry>.resize(size);
} else {
detail::vptr_vector_vptrs<Registry>.resize(size);
}
for (auto iter = ctx.classes_begin(); iter != ctx.classes_end();
++iter) {
for (auto type_iter = iter->type_id_begin();
type_iter != iter->type_id_end(); ++type_iter) {
std::size_t index;
if constexpr (has_type_hash) {
index = type_hash::hash(*type_iter);
} else {
index = std::size_t(*type_iter);
}
if constexpr (Registry::has_indirect_vptr) {
detail::vptr_vector_indirect_vptrs<Registry>[index] =
&iter->vptr();
} else {
detail::vptr_vector_vptrs<Registry>[index] =
iter->vptr();
}
}
}
}
//! Returns a *reference* to a v-table pointer for an object.
//!
//! Acquires the dynamic @ref type_id of `arg`, using the registry's
//! @ref rtti policy.
//!
//! If the registry has a @ref type_hash policy, uses it to convert the
//! type id to an index; otherwise, uses the type_id as the index.
//!
//! If the registry contains the @ref runtime_checks policy, verifies
//! that the index falls within the limits of the vector. If it does
//! not, and if the registry contains a @ref error_handler policy, calls
//! its @ref error function with a @ref missing_class value, then
//! terminates the program with @ref abort.
//!
//! @tparam Class A registered class.
//! @param arg A reference to a const object of type `Class`.
//! @return A reference to a the v-table pointer for `Class`.
template<class Class>
static auto dynamic_vptr(const Class& arg) -> const vptr_type& {
return type_vptr(Registry::rtti::dynamic_type(arg));
};
//! Returns a *reference* to a v-table pointer for a type.
//!
//! If the registry has a @ref type_hash policy, uses it to convert the
//! type id to an index; otherwise, uses the type_id as the index.
//!
//! If the registry contains the @ref runtime_checks policy, verifies
//! that the index falls within the limits of the vector. If it does
//! not, and if the registry contains a @ref error_handler policy, calls
//! its @ref error function with a @ref missing_class value, then
//! terminates the program with @ref abort.
//!
//! @param type A `type_id`.
//! @return A reference to a the v-table pointer for `type`.
static auto type_vptr(type_id type) -> const vptr_type& {
std::size_t index;
if constexpr (has_type_hash) {
index = type_hash::hash(type);
} else {
index = std::size_t(type);
if constexpr (Registry::has_runtime_checks) {
std::size_t max_index = 0;
if constexpr (Registry::has_indirect_vptr) {
max_index =
detail::vptr_vector_indirect_vptrs<Registry>.size();
} else {
max_index = detail::vptr_vector_vptrs<Registry>.size();
}
if (index >= max_index) {
if constexpr (Registry::has_error_handler) {
missing_class error;
error.type = type;
Registry::error_handler::error(error);
}
abort();
}
}
}
if constexpr (Registry::has_indirect_vptr) {
return *detail::vptr_vector_indirect_vptrs<Registry>[index];
} else {
return detail::vptr_vector_vptrs<Registry>[index];
}
}
//! Releases the memory allocated by `initialize`.
//!
//! @tparam Options... Zero or more option types, deduced from the function
//! arguments.
//! @param options Zero or more option objects.
template<class... Options>
static auto finalize(const std::tuple<Options...>&) -> void {
using namespace policies;
if constexpr (Registry::has_indirect_vptr) {
detail::vptr_vector_indirect_vptrs<Registry>.clear();
} else {
detail::vptr_vector_vptrs<Registry>.clear();
}
}
};
};
} // namespace policies
} // namespace boost::openmethod
#endif