MCPcopy Create free account
hub / github.com/davisking/dlib / initialize

Method initialize

dlib/external/pybind11/include/pybind11/pybind11.h:212–357  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

210 /// Special internal constructor for functors, lambda functions, etc.
211 template <typename Func, typename Return, typename... Args, typename... Extra>
212 void initialize(Func &&f, Return (*)(Args...), const Extra &...extra) {
213 using namespace detail;
214 struct capture {
215 remove_reference_t<Func> f;
216 };
217
218 /* Store the function including any extra state it might have (e.g. a lambda capture
219 * object) */
220 // The unique_ptr makes sure nothing is leaked in case of an exception.
221 auto unique_rec = make_function_record();
222 auto *rec = unique_rec.get();
223
224 /* Store the capture object directly in the function record if there is enough space */
225 if (sizeof(capture) <= sizeof(rec->data)) {
226 /* Without these pragmas, GCC warns that there might not be
227 enough space to use the placement new operator. However, the
228 'if' statement above ensures that this is the case. */
229 PYBIND11_WARNING_PUSH
230
231#if defined(__GNUG__) && __GNUC__ >= 6
232 PYBIND11_WARNING_DISABLE_GCC("-Wplacement-new")
233#endif
234
235 new ((capture *) &rec->data) capture{std::forward<Func>(f)};
236
237#if !PYBIND11_HAS_STD_LAUNDER
238 PYBIND11_WARNING_DISABLE_GCC("-Wstrict-aliasing")
239#endif
240
241 // UB without std::launder, but without breaking ABI and/or
242 // a significant refactoring it's "impossible" to solve.
243 if (!std::is_trivially_destructible<capture>::value) {
244 rec->free_data = [](function_record *r) {
245 auto data = PYBIND11_STD_LAUNDER((capture *) &r->data);
246 (void) data;
247 data->~capture();
248 };
249 }
250 PYBIND11_WARNING_POP
251 } else {
252 rec->data[0] = new capture{std::forward<Func>(f)};
253 rec->free_data = [](function_record *r) { delete ((capture *) r->data[0]); };
254 }
255
256 /* Type casters for the function arguments and return value */
257 using cast_in = argument_loader<Args...>;
258 using cast_out
259 = make_caster<conditional_t<std::is_void<Return>::value, void_type, Return>>;
260
261 static_assert(
262 expected_num_args<Extra...>(
263 sizeof...(Args), cast_in::args_pos >= 0, cast_in::has_kwargs),
264 "The number of argument annotations does not match the number of function arguments");
265
266 /* Dispatch code which converts function arguments and performs the actual function call */
267 rec->impl = [](function_call &call) -> handle {
268 cast_in args_converter;
269

Callers

nothing calls this directly

Calls 11

moveFunction · 0.85
noneClass · 0.85
initFunction · 0.85
constexpr_sumFunction · 0.85
const_nameFunction · 0.85
load_argsMethod · 0.80
castFunction · 0.70
policyClass · 0.50
getMethod · 0.45
releaseMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected