33 py::class_<SimpleBase, SimpleBaseTrampoline>(
m,
"SimpleBase")
37 [](
const py::object &
self) {
40 d =
self.attr(
"__dict__");
44 [](
const py::tuple &
t) {
46 throw std::runtime_error(
"Invalid state!");
49 cpp_state->num =
t[0].cast<
int>();
50 auto py_state =
t[1].cast<py::dict>();
51 return std::make_pair(std::move(cpp_state), py_state);
54 m.def(
"make_SimpleCppDerivedAsBase",
56 m.def(
"check_dynamic_cast_SimpleCppDerived", [](
const SimpleBase *base_ptr) {
64 m.def(
"simple_callable", []() {
return 20220426; });
69 explicit Pickleable(
const std::string &
value) : m_value(
value) {}
70 const std::string &
value()
const {
return m_value; }
72 void setExtra1(
int extra1) { m_extra1 = extra1; }
73 void setExtra2(
int extra2) { m_extra2 = extra2; }
74 int extra1()
const {
return m_extra1; }
75 int extra2()
const {
return m_extra2; }
83 class PickleableNew :
public Pickleable {
85 using Pickleable::Pickleable;
88 py::class_<Pickleable> pyPickleable(
m,
"Pickleable");
89 pyPickleable.def(py::init<std::string>())
91 .def(
"extra1", &Pickleable::extra1)
92 .def(
"extra2", &Pickleable::extra2)
93 .def(
"setExtra1", &Pickleable::setExtra1)
94 .def(
"setExtra2", &Pickleable::setExtra2)
97 .def(
"__getstate__", [](
const Pickleable &
p) {
102 pyPickleable.def(
"__setstate__", [](Pickleable &
p,
const py::tuple &
t) {
104 throw std::runtime_error(
"Invalid state!");
107 new (&
p) Pickleable(
t[0].cast<std::string>());
110 p.setExtra1(
t[1].cast<int>());
111 p.setExtra2(
t[2].cast<int>());
115 py::class_<PickleableNew, Pickleable>(
m,
"PickleableNew")
116 .def(py::init<std::string>())
118 [](
const PickleableNew &
p) {
121 [](
const py::tuple &
t) {
123 throw std::runtime_error(
"Invalid state!");
125 auto p = PickleableNew(
t[0].cast<std::string>());
127 p.setExtra1(
t[1].cast<int>());
128 p.setExtra2(
t[2].cast<int>());
132 #if !defined(PYPY_VERSION)
134 class PickleableWithDict {
136 explicit PickleableWithDict(
const std::string &
value) :
value(
value) {}
142 class PickleableWithDictNew :
public PickleableWithDict {
144 using PickleableWithDict::PickleableWithDict;
147 py::class_<PickleableWithDict> pyPickleableWithDict(
148 m,
"PickleableWithDict", py::dynamic_attr());
149 pyPickleableWithDict.def(py::init<std::string>())
151 .def_readwrite(
"extra", &PickleableWithDict::extra)
152 .def(
"__getstate__", [](
const py::object &
self) {
154 return py::make_tuple(
self.attr(
"value"),
self.attr(
"extra"),
self.attr(
"__dict__"));
157 pyPickleableWithDict.def(
"__setstate__", [](
const py::object &
self,
const py::tuple &
t) {
159 throw std::runtime_error(
"Invalid state!");
162 auto &
p =
self.cast<PickleableWithDict &>();
163 new (&
p) PickleableWithDict(
t[0].cast<std::string>());
166 p.extra =
t[1].cast<
int>();
169 self.attr(
"__dict__") =
t[2];
173 py::class_<PickleableWithDictNew, PickleableWithDict>(
m,
"PickleableWithDictNew")
174 .def(py::init<std::string>())
176 [](
const py::object &
self) {
178 self.attr(
"value"),
self.attr(
"extra"),
self.attr(
"__dict__"));
180 [](
const py::tuple &
t) {
182 throw std::runtime_error(
"Invalid state!");
185 auto cpp_state = PickleableWithDictNew(
t[0].cast<std::string>());
186 cpp_state.extra =
t[1].cast<
int>();
188 auto py_state =
t[2].cast<py::dict>();
189 return std::make_pair(cpp_state, py_state);