blob: e80cdb8e201c28b1e267abf22372cf3c5d0dcfcf [file] [log] [blame]
Jason Rhinelander813d7e82017-05-14 15:57:26 -04001/*
2 tests/test_copy_move_policies.cpp -- 'copy' and 'move' return value policies
3 and related tests
4
5 Copyright (c) 2016 Ben North <ben@redfrontdoor.org>
6
7 All rights reserved. Use of this source code is governed by a
8 BSD-style license that can be found in the LICENSE file.
9*/
10
11#include "pybind11_tests.h"
12#include "constructor_stats.h"
13#include <pybind11/stl.h>
14
15template <typename derived>
16struct empty {
17 static const derived& get_one() { return instance_; }
18 static derived instance_;
19};
20
21struct lacking_copy_ctor : public empty<lacking_copy_ctor> {
22 lacking_copy_ctor() {}
23 lacking_copy_ctor(const lacking_copy_ctor& other) = delete;
24};
25
26template <> lacking_copy_ctor empty<lacking_copy_ctor>::instance_ = {};
27
28struct lacking_move_ctor : public empty<lacking_move_ctor> {
29 lacking_move_ctor() {}
30 lacking_move_ctor(const lacking_move_ctor& other) = delete;
31 lacking_move_ctor(lacking_move_ctor&& other) = delete;
32};
33
34template <> lacking_move_ctor empty<lacking_move_ctor>::instance_ = {};
35
36/* Custom type caster move/copy test classes */
37class MoveOnlyInt {
38public:
39 MoveOnlyInt() { print_default_created(this); }
40 MoveOnlyInt(int v) : value{std::move(v)} { print_created(this, value); }
41 MoveOnlyInt(MoveOnlyInt &&m) { print_move_created(this, m.value); std::swap(value, m.value); }
42 MoveOnlyInt &operator=(MoveOnlyInt &&m) { print_move_assigned(this, m.value); std::swap(value, m.value); return *this; }
43 MoveOnlyInt(const MoveOnlyInt &) = delete;
44 MoveOnlyInt &operator=(const MoveOnlyInt &) = delete;
45 ~MoveOnlyInt() { print_destroyed(this); }
46
47 int value;
48};
49class MoveOrCopyInt {
50public:
51 MoveOrCopyInt() { print_default_created(this); }
52 MoveOrCopyInt(int v) : value{std::move(v)} { print_created(this, value); }
53 MoveOrCopyInt(MoveOrCopyInt &&m) { print_move_created(this, m.value); std::swap(value, m.value); }
54 MoveOrCopyInt &operator=(MoveOrCopyInt &&m) { print_move_assigned(this, m.value); std::swap(value, m.value); return *this; }
55 MoveOrCopyInt(const MoveOrCopyInt &c) { print_copy_created(this, c.value); value = c.value; }
56 MoveOrCopyInt &operator=(const MoveOrCopyInt &c) { print_copy_assigned(this, c.value); value = c.value; return *this; }
57 ~MoveOrCopyInt() { print_destroyed(this); }
58
59 int value;
60};
61class CopyOnlyInt {
62public:
63 CopyOnlyInt() { print_default_created(this); }
64 CopyOnlyInt(int v) : value{std::move(v)} { print_created(this, value); }
65 CopyOnlyInt(const CopyOnlyInt &c) { print_copy_created(this, c.value); value = c.value; }
66 CopyOnlyInt &operator=(const CopyOnlyInt &c) { print_copy_assigned(this, c.value); value = c.value; return *this; }
67 ~CopyOnlyInt() { print_destroyed(this); }
68
69 int value;
70};
71namespace pybind11 { namespace detail {
72template <> struct type_caster<MoveOnlyInt> {
73 PYBIND11_TYPE_CASTER(MoveOnlyInt, _("MoveOnlyInt"));
74 bool load(handle src, bool) { value = MoveOnlyInt(src.cast<int>()); return true; }
75 static handle cast(const MoveOnlyInt &m, return_value_policy r, handle p) { return pybind11::cast(m.value, r, p); }
76};
77
78template <> struct type_caster<MoveOrCopyInt> {
79 PYBIND11_TYPE_CASTER(MoveOrCopyInt, _("MoveOrCopyInt"));
80 bool load(handle src, bool) { value = MoveOrCopyInt(src.cast<int>()); return true; }
81 static handle cast(const MoveOrCopyInt &m, return_value_policy r, handle p) { return pybind11::cast(m.value, r, p); }
82};
83
84template <> struct type_caster<CopyOnlyInt> {
85protected:
86 CopyOnlyInt value;
87public:
88 static PYBIND11_DESCR name() { return _("CopyOnlyInt"); }
89 bool load(handle src, bool) { value = CopyOnlyInt(src.cast<int>()); return true; }
90 static handle cast(const CopyOnlyInt &m, return_value_policy r, handle p) { return pybind11::cast(m.value, r, p); }
91 static handle cast(const CopyOnlyInt *src, return_value_policy policy, handle parent) {
92 if (!src) return none().release();
93 return cast(*src, policy, parent);
94 }
95 operator CopyOnlyInt*() { return &value; }
96 operator CopyOnlyInt&() { return value; }
97 template <typename T> using cast_op_type = pybind11::detail::cast_op_type<T>;
98};
99}}
100
Dean Moldovane27ea472017-06-08 18:21:12 +0200101struct PrivateOpNew {
102 int value = 1;
103
104private:
105 void *operator new(size_t bytes);
106};
Jason Rhinelander813d7e82017-05-14 15:57:26 -0400107
108test_initializer copy_move_policies([](py::module &m) {
109 py::class_<lacking_copy_ctor>(m, "lacking_copy_ctor")
110 .def_static("get_one", &lacking_copy_ctor::get_one,
111 py::return_value_policy::copy);
112 py::class_<lacking_move_ctor>(m, "lacking_move_ctor")
113 .def_static("get_one", &lacking_move_ctor::get_one,
114 py::return_value_policy::move);
115
116 m.def("move_only", [](MoveOnlyInt m) {
117 return m.value;
118 });
119 m.def("move_or_copy", [](MoveOrCopyInt m) {
120 return m.value;
121 });
122 m.def("copy_only", [](CopyOnlyInt m) {
123 return m.value;
124 });
125 m.def("move_and_copy_casts", [](py::object o) {
126 int r = 0;
127 r += py::cast<MoveOrCopyInt>(o).value; /* moves */
128 r += py::cast<MoveOnlyInt>(o).value; /* moves */
129 r += py::cast<CopyOnlyInt>(o).value; /* copies */
130 MoveOrCopyInt m1(py::cast<MoveOrCopyInt>(o)); /* moves */
131 MoveOnlyInt m2(py::cast<MoveOnlyInt>(o)); /* moves */
132 CopyOnlyInt m3(py::cast<CopyOnlyInt>(o)); /* copies */
133 r += m1.value + m2.value + m3.value;
134
135 return r;
136 });
137 m.def("move_pair", [](std::pair<MoveOnlyInt, MoveOrCopyInt> p) {
138 return p.first.value + p.second.value;
139 });
140 m.def("move_tuple", [](std::tuple<MoveOnlyInt, MoveOrCopyInt, MoveOnlyInt> t) {
141 return std::get<0>(t).value + std::get<1>(t).value + std::get<2>(t).value;
142 });
143 m.def("copy_tuple", [](std::tuple<CopyOnlyInt, CopyOnlyInt> t) {
144 return std::get<0>(t).value + std::get<1>(t).value;
145 });
146 m.def("move_copy_nested", [](std::pair<MoveOnlyInt, std::pair<std::tuple<MoveOrCopyInt, CopyOnlyInt, std::tuple<MoveOnlyInt>>, MoveOrCopyInt>> x) {
147 return x.first.value + std::get<0>(x.second.first).value + std::get<1>(x.second.first).value +
148 std::get<0>(std::get<2>(x.second.first)).value + x.second.second.value;
149 });
150 m.def("move_and_copy_cstats", []() {
151 ConstructorStats::gc();
152 // Reset counts to 0 so that previous tests don't affect later ones:
153 auto &mc = ConstructorStats::get<MoveOrCopyInt>();
154 mc.move_assignments = mc.move_constructions = mc.copy_assignments = mc.copy_constructions = 0;
155 auto &mo = ConstructorStats::get<MoveOnlyInt>();
156 mo.move_assignments = mo.move_constructions = mo.copy_assignments = mo.copy_constructions = 0;
157 auto &co = ConstructorStats::get<CopyOnlyInt>();
158 co.move_assignments = co.move_constructions = co.copy_assignments = co.copy_constructions = 0;
159 py::dict d;
160 d["MoveOrCopyInt"] = py::cast(mc, py::return_value_policy::reference);
161 d["MoveOnlyInt"] = py::cast(mo, py::return_value_policy::reference);
162 d["CopyOnlyInt"] = py::cast(co, py::return_value_policy::reference);
163 return d;
164 });
165#ifdef PYBIND11_HAS_OPTIONAL
166 m.attr("has_optional") = true;
167 m.def("move_optional", [](std::optional<MoveOnlyInt> o) {
168 return o->value;
169 });
170 m.def("move_or_copy_optional", [](std::optional<MoveOrCopyInt> o) {
171 return o->value;
172 });
173 m.def("copy_optional", [](std::optional<CopyOnlyInt> o) {
174 return o->value;
175 });
176 m.def("move_optional_tuple", [](std::optional<std::tuple<MoveOrCopyInt, MoveOnlyInt, CopyOnlyInt>> x) {
177 return std::get<0>(*x).value + std::get<1>(*x).value + std::get<2>(*x).value;
178 });
179#else
180 m.attr("has_optional") = false;
181#endif
182
Dean Moldovane27ea472017-06-08 18:21:12 +0200183 // #70 compilation issue if operator new is not public
184 py::class_<PrivateOpNew>(m, "PrivateOpNew").def_readonly("value", &PrivateOpNew::value);
185 m.def("private_op_new_value", []() { return PrivateOpNew(); });
186 m.def("private_op_new_reference", []() -> const PrivateOpNew & {
187 static PrivateOpNew x{};
188 return x;
189 }, py::return_value_policy::reference);
Dean Moldovanbdfb50f2017-06-07 16:52:50 +0200190
191 // #389: rvp::move should fall-through to copy on non-movable objects
192 struct MoveIssue1 {
193 int v;
194 MoveIssue1(int v) : v{v} {}
195 MoveIssue1(const MoveIssue1 &c) = default;
196 MoveIssue1(MoveIssue1 &&) = delete;
197 };
198
199 struct MoveIssue2 {
200 int v;
201 MoveIssue2(int v) : v{v} {}
202 MoveIssue2(MoveIssue2 &&) = default;
203 };
204
205 py::class_<MoveIssue1>(m, "MoveIssue1").def(py::init<int>()).def_readwrite("value", &MoveIssue1::v);
206 py::class_<MoveIssue2>(m, "MoveIssue2").def(py::init<int>()).def_readwrite("value", &MoveIssue2::v);
207 m.def("get_moveissue1", [](int i) { return new MoveIssue1(i); }, py::return_value_policy::move);
208 m.def("get_moveissue2", [](int i) { return MoveIssue2(i); }, py::return_value_policy::move);
Jason Rhinelander813d7e82017-05-14 15:57:26 -0400209});