test_copy_move.cpp 9.0 KB

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