28 static constexpr
auto name = const_name<value_and_holder>();
38 throw type_error(
"pybind11::init(): factory function returned nullptr");
43 template <
typename Class>
45 template <
typename Class>
46 using Alias =
typename Class::type_alias;
47 template <
typename Class>
48 using Holder =
typename Class::holder_type;
50 template <
typename Class>
54 template <
typename Class, enable_if_t<Class::has_alias,
int> = 0>
69 template <
typename Class,
73 return new Class(std::forward<Args>(
args)...);
75 template <
typename Class,
79 return new Class{std::forward<Args>(
args)...};
87 template <
typename Class>
93 template <
typename Class>
97 throw type_error(
"pybind11::init(): unable to convert returned instance to required " 98 "alias class: no `Alias<Class>(Class &&)` constructor available");
103 template <
typename Class>
106 "pybind11::init(): init function must return a compatible pointer, " 114 template <
typename Class>
129 v_h.
type->init_instance(v_h.
inst,
nullptr);
131 v_h.
type->dealloc(v_h);
143 template <
typename Class, enable_if_t<Class::has_alias,
int> = 0>
153 template <
typename Class>
160 throw type_error(
"pybind11::init(): construction failed: returned holder-wrapped instance " 161 "is not an alias instance");
165 v_h.
type->init_instance(v_h.
inst, &holder);
172 template <
typename Class>
176 "pybind11::init() return-by-value factory function requires a movable class");
187 template <
typename Class>
191 "pybind11::init() return-by-alias-value factory function requires a movable alias class");
196 template <
typename... Args>
203 v_h.value_ptr() = construct_or_initialize<Cpp<Class>>(std::forward<Args>(args)...);
209 template <
typename Class,
217 if (Py_TYPE(v_h.inst) == v_h.type->type) {
219 = construct_or_initialize<Cpp<Class>>(std::forward<Args>(args)...);
222 = construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...);
229 template <
typename Class,
238 = construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...);
246 template <
typename... Args>
248 template <
typename Class,
257 = construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...);
265 template <
typename CFunc,
272 template <
typename Func,
typename Return,
typename... Args>
284 template <
typename Class,
typename... Extra>
286 #if defined(PYBIND11_CPP14) 289 [
func = std::move(class_factory)]
291 auto &
func = class_factory;
298 v_h, func(std::forward<Args>(args)...), Py_TYPE(v_h.inst) != v_h.type->type);
306 template <
typename CFunc,
312 struct factory<CFunc, AFunc, CReturn(CArgs...), AReturn(AArgs...)> {
313 static_assert(
sizeof...(CArgs) ==
sizeof...(AArgs),
314 "pybind11::init(class_factory, alias_factory): class and alias factories " 315 "must have identical argument signatures");
316 static_assert(
all_of<std::is_same<CArgs, AArgs>...>::
value,
317 "pybind11::init(class_factory, alias_factory): class and alias factories " 318 "must have identical argument signatures");
328 template <
typename Class,
typename... Extra>
330 static_assert(Class::has_alias,
331 "The two-argument version of `py::init()` can " 332 "only be used if the class has an alias");
333 #if defined(PYBIND11_CPP14) 336 [class_func = std::move(class_factory), alias_func = std::move(alias_factory)]
338 auto &class_func = class_factory;
339 auto &alias_func = alias_factory;
342 [class_func, alias_func]
345 if (Py_TYPE(v_h.inst) == v_h.type->type) {
348 construct<Class>(v_h, class_func(std::forward<CArgs>(args)...), false);
350 construct<Class>(v_h, alias_func(std::forward<CArgs>(args)...), true);
359 template <
typename Class,
typename T>
361 construct<Class>(v_h, std::forward<T>(
result), need_alias);
365 template <
typename Class,
370 construct<Class>(v_h, std::move(
result.first), need_alias);
372 if (PyDict_Check(d.ptr()) && PyDict_Size(d.ptr()) == 0) {
377 setattr((PyObject *) v_h.inst,
"__dict__", d);
381 template <
typename Get,
387 template <
typename Get,
391 typename NewInstance,
395 "The type returned by `__getstate__` must be the same " 396 "as the argument accepted by `__setstate__`");
403 template <
typename Class,
typename... Extra>
405 cl.def(
"__getstate__", std::move(
get));
407 #if defined(PYBIND11_CPP14) 410 [
func = std::move(
set)]
419 v_h,
func(std::forward<ArgState>(state)), Py_TYPE(v_h.inst) != v_h.type->type);
void execute(Class &cl, const Extra &...extra) &&
void construct_alias_from_cpp(std::true_type, value_and_holder &v_h, Cpp< Class > &&base)
#define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...)
const detail::type_info * type
factory(CFunc &&c, AFunc &&a)
Class * construct_or_initialize(Args &&...args)
std::is_constructible< Alias< Class >, Cpp< Class > && > is_alias_constructible
void no_nullptr(void *ptr)
static void execute(Class &cl, const Extra &...extra)
Tag for a new-style __init__ defined in detail/init.h
#define PYBIND11_SILENCE_MSVC_C4127(...)
bool load(handle h, bool)
void set_instance_registered(bool v=true)
static void execute(Class &cl, const Extra &...extra)
#define PYBIND11_NAMESPACE
conditional_t< std::is_function< F >::value, F, typename conditional_t< std::is_pointer< F >::value||std::is_member_pointer< F >::value, std::remove_pointer< F >, strip_function_object< F > >::type > function_signature_t
void execute(Class &cl, const Extra &...extra) &&
void setstate(value_and_holder &v_h, T &&result, bool need_alias)
Set just the C++ state. Same as __init__.
void execute(Class &cl, const Extra &...extra) &&
Eigen::Triplet< double > T
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
typename Class::holder_type Holder
typename std::remove_reference< T >::type remove_reference_t
remove_reference_t< CFunc > class_factory
Helper type to replace 'void' in some expressions.
std::is_same< bools< Ts::value..., true >, bools< true, Ts::value... > > all_of
remove_reference_t< Func > class_factory
bool is_alias(Cpp< Class > *ptr)
typename intrinsic_type< T >::type intrinsic_t
pickle_factory(Get get, Set set)
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
remove_reference_t< AFunc > alias_factory
Annotation for function names.
Annotation indicating that a class derives from another given type.
PyObject * ptr() const
Return the underlying PyObject * pointer.
Container::iterator get(Container &c, Position position)
typename Class::type_alias Alias
void setattr(handle obj, handle name, handle value)
Implementation for py::pickle(GetState, SetState)
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)