blob: 898fc39d114b920cf47871455e913323723226c2 [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
101
102test_initializer copy_move_policies([](py::module &m) {
103 py::class_<lacking_copy_ctor>(m, "lacking_copy_ctor")
104 .def_static("get_one", &lacking_copy_ctor::get_one,
105 py::return_value_policy::copy);
106 py::class_<lacking_move_ctor>(m, "lacking_move_ctor")
107 .def_static("get_one", &lacking_move_ctor::get_one,
108 py::return_value_policy::move);
109
110 m.def("move_only", [](MoveOnlyInt m) {
111 return m.value;
112 });
113 m.def("move_or_copy", [](MoveOrCopyInt m) {
114 return m.value;
115 });
116 m.def("copy_only", [](CopyOnlyInt m) {
117 return m.value;
118 });
119 m.def("move_and_copy_casts", [](py::object o) {
120 int r = 0;
121 r += py::cast<MoveOrCopyInt>(o).value; /* moves */
122 r += py::cast<MoveOnlyInt>(o).value; /* moves */
123 r += py::cast<CopyOnlyInt>(o).value; /* copies */
124 MoveOrCopyInt m1(py::cast<MoveOrCopyInt>(o)); /* moves */
125 MoveOnlyInt m2(py::cast<MoveOnlyInt>(o)); /* moves */
126 CopyOnlyInt m3(py::cast<CopyOnlyInt>(o)); /* copies */
127 r += m1.value + m2.value + m3.value;
128
129 return r;
130 });
131 m.def("move_pair", [](std::pair<MoveOnlyInt, MoveOrCopyInt> p) {
132 return p.first.value + p.second.value;
133 });
134 m.def("move_tuple", [](std::tuple<MoveOnlyInt, MoveOrCopyInt, MoveOnlyInt> t) {
135 return std::get<0>(t).value + std::get<1>(t).value + std::get<2>(t).value;
136 });
137 m.def("copy_tuple", [](std::tuple<CopyOnlyInt, CopyOnlyInt> t) {
138 return std::get<0>(t).value + std::get<1>(t).value;
139 });
140 m.def("move_copy_nested", [](std::pair<MoveOnlyInt, std::pair<std::tuple<MoveOrCopyInt, CopyOnlyInt, std::tuple<MoveOnlyInt>>, MoveOrCopyInt>> x) {
141 return x.first.value + std::get<0>(x.second.first).value + std::get<1>(x.second.first).value +
142 std::get<0>(std::get<2>(x.second.first)).value + x.second.second.value;
143 });
144 m.def("move_and_copy_cstats", []() {
145 ConstructorStats::gc();
146 // Reset counts to 0 so that previous tests don't affect later ones:
147 auto &mc = ConstructorStats::get<MoveOrCopyInt>();
148 mc.move_assignments = mc.move_constructions = mc.copy_assignments = mc.copy_constructions = 0;
149 auto &mo = ConstructorStats::get<MoveOnlyInt>();
150 mo.move_assignments = mo.move_constructions = mo.copy_assignments = mo.copy_constructions = 0;
151 auto &co = ConstructorStats::get<CopyOnlyInt>();
152 co.move_assignments = co.move_constructions = co.copy_assignments = co.copy_constructions = 0;
153 py::dict d;
154 d["MoveOrCopyInt"] = py::cast(mc, py::return_value_policy::reference);
155 d["MoveOnlyInt"] = py::cast(mo, py::return_value_policy::reference);
156 d["CopyOnlyInt"] = py::cast(co, py::return_value_policy::reference);
157 return d;
158 });
159#ifdef PYBIND11_HAS_OPTIONAL
160 m.attr("has_optional") = true;
161 m.def("move_optional", [](std::optional<MoveOnlyInt> o) {
162 return o->value;
163 });
164 m.def("move_or_copy_optional", [](std::optional<MoveOrCopyInt> o) {
165 return o->value;
166 });
167 m.def("copy_optional", [](std::optional<CopyOnlyInt> o) {
168 return o->value;
169 });
170 m.def("move_optional_tuple", [](std::optional<std::tuple<MoveOrCopyInt, MoveOnlyInt, CopyOnlyInt>> x) {
171 return std::get<0>(*x).value + std::get<1>(*x).value + std::get<2>(*x).value;
172 });
173#else
174 m.attr("has_optional") = false;
175#endif
176
177});