@@ -1313,6 +1313,30 @@ class class_ : public detail::generic_type {
1313
1313
}
1314
1314
};
1315
1315
1316
+ // / Binds an existing constructor taking arguments Args...
1317
+ template <typename ... Args> detail::initimpl::constructor<Args...> init () { return {}; }
1318
+ // / Like `init<Args...>()`, but the instance is always constructed through the alias class (even
1319
+ // / when not inheriting on the Python side).
1320
+ template <typename ... Args> detail::initimpl::alias_constructor<Args...> init_alias () { return {}; }
1321
+
1322
+ // / Binds a factory function as a constructor
1323
+ template <typename Func, typename Ret = detail::initimpl::factory<Func>>
1324
+ Ret init (Func &&f) { return {std::forward<Func>(f)}; }
1325
+
1326
+ // / Dual-argument factory function: the first function is called when no alias is needed, the second
1327
+ // / when an alias is needed (i.e. due to python-side inheritance). Arguments must be identical.
1328
+ template <typename CFunc, typename AFunc, typename Ret = detail::initimpl::factory<CFunc, AFunc>>
1329
+ Ret init (CFunc &&c, AFunc &&a) {
1330
+ return {std::forward<CFunc>(c), std::forward<AFunc>(a)};
1331
+ }
1332
+
1333
+ // / Binds pickling functions `__getstate__` and `__setstate__` and ensures that the type
1334
+ // / returned by `__getstate__` is the same as the argument accepted by `__setstate__`.
1335
+ template <typename GetState, typename SetState>
1336
+ detail::initimpl::pickle_factory<GetState, SetState> pickle (GetState &&g, SetState &&s) {
1337
+ return {std::forward<GetState>(g), std::forward<SetState>(s)};
1338
+ };
1339
+
1316
1340
// / Binds C++ enumerations and enumeration classes to Python
1317
1341
template <typename Type> class enum_ : public class_ <Type> {
1318
1342
public:
@@ -1340,7 +1364,7 @@ template <typename Type> class enum_ : public class_<Type> {
1340
1364
m[kv.first ] = kv.second ;
1341
1365
return m;
1342
1366
}, return_value_policy::copy);
1343
- def (" __init__ " , [](Type& value, Scalar i) { value = ( Type)i ; });
1367
+ def (init ( [](Scalar i) { return static_cast < Type>(i) ; }) );
1344
1368
def (" __int__" , [](Type value) { return (Scalar) value; });
1345
1369
#if PY_MAJOR_VERSION < 3
1346
1370
def (" __long__" , [](Type value) { return (Scalar) value; });
@@ -1378,8 +1402,8 @@ template <typename Type> class enum_ : public class_<Type> {
1378
1402
}
1379
1403
def (" __hash__" , [](const Type &value) { return (Scalar) value; });
1380
1404
// Pickling and unpickling -- needed for use with the 'multiprocessing' module
1381
- def (" __getstate__ " , [](const Type &value) { return pybind11::make_tuple ((Scalar) value); });
1382
- def ( " __setstate__ " , [](Type &p, tuple t) { new (&p) Type ((Type) t[0 ].cast <Scalar>()); });
1405
+ def (pickle ( [](const Type &value) { return pybind11::make_tuple ((Scalar) value); },
1406
+ [](tuple t) { return static_cast < Type>( t[0 ].cast <Scalar>()); }) );
1383
1407
}
1384
1408
1385
1409
// / Export enumeration entries into the parent scope
@@ -1402,30 +1426,6 @@ template <typename Type> class enum_ : public class_<Type> {
1402
1426
handle m_parent;
1403
1427
};
1404
1428
1405
- // / Binds an existing constructor taking arguments Args...
1406
- template <typename ... Args> detail::initimpl::constructor<Args...> init () { return {}; }
1407
- // / Like `init<Args...>()`, but the instance is always constructed through the alias class (even
1408
- // / when not inheriting on the Python side).
1409
- template <typename ... Args> detail::initimpl::alias_constructor<Args...> init_alias () { return {}; }
1410
-
1411
- // / Binds a factory function as a constructor
1412
- template <typename Func, typename Ret = detail::initimpl::factory<Func>>
1413
- Ret init (Func &&f) { return {std::forward<Func>(f)}; }
1414
-
1415
- // / Dual-argument factory function: the first function is called when no alias is needed, the second
1416
- // / when an alias is needed (i.e. due to python-side inheritance). Arguments must be identical.
1417
- template <typename CFunc, typename AFunc, typename Ret = detail::initimpl::factory<CFunc, AFunc>>
1418
- Ret init (CFunc &&c, AFunc &&a) {
1419
- return {std::forward<CFunc>(c), std::forward<AFunc>(a)};
1420
- }
1421
-
1422
- // / Binds pickling functions `__getstate__` and `__setstate__` and ensures that the type
1423
- // / returned by `__getstate__` is the same as the argument accepted by `__setstate__`.
1424
- template <typename GetState, typename SetState>
1425
- detail::initimpl::pickle_factory<GetState, SetState> pickle (GetState &&g, SetState &&s) {
1426
- return {std::forward<GetState>(g), std::forward<SetState>(s)};
1427
- };
1428
-
1429
1429
NAMESPACE_BEGIN (detail)
1430
1430
1431
1431
0 commit comments