21 value =
reinterpret_cast<value_and_holder *
>(h.
ptr());
26 operator value_and_holder &() {
return *
value; }
27 static constexpr
auto name = _<value_and_holder>();
30 value_and_holder *
value =
nullptr;
36 if (!ptr)
throw type_error(
"pybind11::init(): factory function returned nullptr");
41 template <
typename Class>
using Alias =
typename Class::type_alias;
42 template <
typename Class>
using Holder =
typename Class::holder_type;
47 template <
typename Class, enable_if_t<Class::has_alias,
int> = 0>
53 constexpr
bool is_alias(
void *) {
return false; }
70 template <
typename Class>
75 template <
typename Class>
78 throw type_error(
"pybind11::init(): unable to convert returned instance to required " 79 "alias class: no `Alias<Class>(Class &&)` constructor available");
84 template <
typename Class>
87 "pybind11::init(): init function must return a compatible pointer, " 95 template <
typename Class>
98 if (Class::has_alias && need_alias && !is_alias<Class>(ptr)) {
109 v_h.
type->init_instance(v_h.
inst,
nullptr);
111 v_h.
type->dealloc(v_h);
123 template <
typename Class, enable_if_t<Class::has_alias,
int> = 0>
132 template <
typename Class>
137 if (Class::has_alias && need_alias && !is_alias<Class>(
ptr))
138 throw type_error(
"pybind11::init(): construction failed: returned holder-wrapped instance " 139 "is not an alias instance");
142 v_h.
type->init_instance(v_h.
inst, &holder);
149 template <
typename Class>
152 "pybind11::init() return-by-value factory function requires a movable class");
153 if (Class::has_alias && need_alias)
162 template <
typename Class>
165 "pybind11::init() return-by-alias-value factory function requires a movable alias class");
170 template <
typename... Args>
175 v_h.value_ptr() = construct_or_initialize<Cpp<Class>>(std::forward<Args>(args)...);
179 template <
typename Class,
typename... Extra,
181 std::is_constructible<Cpp<Class>, Args...>
::value,
int> = 0>
184 if (Py_TYPE(v_h.inst) == v_h.type->type)
185 v_h.value_ptr() = construct_or_initialize<Cpp<Class>>(std::forward<Args>(args)...);
187 v_h.value_ptr() = construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...);
191 template <
typename Class,
typename... Extra,
193 !std::is_constructible<Cpp<Class>, Args...>
::value,
int> = 0>
196 v_h.value_ptr() = construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...);
203 template <
typename Class,
typename... Extra,
205 static void execute(Class &cl,
const Extra&... extra) {
207 v_h.value_ptr() = construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...);
213 template <
typename CFunc,
typename AFunc =
void_type (*)(),
218 template <
typename Func,
typename Return,
typename... Args>
229 template <
typename Class,
typename... Extra>
231 #if defined(PYBIND11_CPP14) 232 cl.def(
"__init__", [
func = std::move(class_factory)]
234 auto &
func = class_factory;
235 cl.def(
"__init__", [
func]
238 construct<Class>(v_h, func(std::forward<Args>(args)...),
239 Py_TYPE(v_h.inst) != v_h.type->type);
245 template <
typename CFunc,
typename AFunc,
246 typename CReturn,
typename... CArgs,
typename AReturn,
typename... AArgs>
247 struct factory<CFunc, AFunc, CReturn(CArgs...), AReturn(AArgs...)> {
248 static_assert(
sizeof...(CArgs) ==
sizeof...(AArgs),
249 "pybind11::init(class_factory, alias_factory): class and alias factories " 250 "must have identical argument signatures");
251 static_assert(
all_of<std::is_same<CArgs, AArgs>...>::
value,
252 "pybind11::init(class_factory, alias_factory): class and alias factories " 253 "must have identical argument signatures");
263 template <
typename Class,
typename... Extra>
265 static_assert(Class::has_alias,
"The two-argument version of `py::init()` can " 266 "only be used if the class has an alias");
267 #if defined(PYBIND11_CPP14) 268 cl.def(
"__init__", [class_func = std::move(class_factory), alias_func = std::move(alias_factory)]
270 auto &class_func = class_factory;
271 auto &alias_func = alias_factory;
272 cl.def(
"__init__", [class_func, alias_func]
275 if (Py_TYPE(v_h.inst) == v_h.type->type)
278 construct<Class>(v_h, class_func(std::forward<CArgs>(args)...), false);
280 construct<Class>(v_h, alias_func(std::forward<CArgs>(args)...), true);
286 template <
typename Class,
typename T>
288 construct<Class>(v_h, std::forward<T>(
result), need_alias);
292 template <
typename Class,
typename T,
typename O,
295 construct<Class>(v_h, std::move(
result.first), need_alias);
300 template <
typename Get,
typename Set,
304 template <
typename Get,
typename Set,
305 typename RetState,
typename Self,
typename NewInstance,
typename ArgState>
308 "The type returned by `__getstate__` must be the same " 309 "as the argument accepted by `__setstate__`");
317 template <
typename Class,
typename... Extra>
319 cl.def(
"__getstate__", std::move(
get));
321 #if defined(PYBIND11_CPP14) 322 cl.def(
"__setstate__", [
func = std::move(
set)]
325 cl.def(
"__setstate__", [
func]
328 setstate<Class>(v_h,
func(std::forward<ArgState>(state)),
329 Py_TYPE(v_h.inst) != v_h.type->type);
void construct_alias_from_cpp(std::true_type, value_and_holder &v_h, Cpp< Class > &&base)
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)
PyObject * ptr() const
Return the underlying PyObject * pointer.
static void execute(Class &cl, const Extra &...extra)
Tag for a new-style __init__ defined in detail/init.h
void execute(Class &cl, const Extra &...extra)&&
bool load(handle h, bool)
void set_instance_registered(bool v=true)
void execute(Class &cl, const Extra &...extra)&&
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 setstate(value_and_holder &v_h, T &&result, bool need_alias)
Set just the C++ state. Same as __init__.
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.
remove_reference_t< Func > class_factory
bool is_alias(Cpp< Class > *ptr)
typename intrinsic_type< T >::type intrinsic_t
std::is_same< bools< Ts::value..., true >, bools< true, Ts::value... >> all_of
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.
void execute(Class &cl, const Extra &...extra)&&
Annotation indicating that a class derives from another given type.
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)