Blame external/pybind11/tests/test_copy_move.cpp

Packit 534379
/*
Packit 534379
    tests/test_copy_move_policies.cpp -- 'copy' and 'move' return value policies
Packit 534379
                                         and related tests
Packit 534379
Packit 534379
    Copyright (c) 2016 Ben North <ben@redfrontdoor.org>
Packit 534379
Packit 534379
    All rights reserved. Use of this source code is governed by a
Packit 534379
    BSD-style license that can be found in the LICENSE file.
Packit 534379
*/
Packit 534379
Packit 534379
#include "pybind11_tests.h"
Packit 534379
#include "constructor_stats.h"
Packit 534379
#include <pybind11/stl.h>
Packit 534379
Packit 534379
template <typename derived>
Packit 534379
struct empty {
Packit 534379
    static const derived& get_one() { return instance_; }
Packit 534379
    static derived instance_;
Packit 534379
};
Packit 534379
Packit 534379
struct lacking_copy_ctor : public empty<lacking_copy_ctor> {
Packit 534379
    lacking_copy_ctor() {}
Packit 534379
    lacking_copy_ctor(const lacking_copy_ctor& other) = delete;
Packit 534379
};
Packit 534379
Packit 534379
template <> lacking_copy_ctor empty<lacking_copy_ctor>::instance_ = {};
Packit 534379
Packit 534379
struct lacking_move_ctor : public empty<lacking_move_ctor> {
Packit 534379
    lacking_move_ctor() {}
Packit 534379
    lacking_move_ctor(const lacking_move_ctor& other) = delete;
Packit 534379
    lacking_move_ctor(lacking_move_ctor&& other) = delete;
Packit 534379
};
Packit 534379
Packit 534379
template <> lacking_move_ctor empty<lacking_move_ctor>::instance_ = {};
Packit 534379
Packit 534379
/* Custom type caster move/copy test classes */
Packit 534379
class MoveOnlyInt {
Packit 534379
public:
Packit 534379
    MoveOnlyInt() { print_default_created(this); }
Packit 534379
    MoveOnlyInt(int v) : value{std::move(v)} { print_created(this, value); }
Packit 534379
    MoveOnlyInt(MoveOnlyInt &&m) { print_move_created(this, m.value); std::swap(value, m.value); }
Packit 534379
    MoveOnlyInt &operator=(MoveOnlyInt &&m) { print_move_assigned(this, m.value); std::swap(value, m.value); return *this; }
Packit 534379
    MoveOnlyInt(const MoveOnlyInt &) = delete;
Packit 534379
    MoveOnlyInt &operator=(const MoveOnlyInt &) = delete;
Packit 534379
    ~MoveOnlyInt() { print_destroyed(this); }
Packit 534379
Packit 534379
    int value;
Packit 534379
};
Packit 534379
class MoveOrCopyInt {
Packit 534379
public:
Packit 534379
    MoveOrCopyInt() { print_default_created(this); }
Packit 534379
    MoveOrCopyInt(int v) : value{std::move(v)} { print_created(this, value); }
Packit 534379
    MoveOrCopyInt(MoveOrCopyInt &&m) { print_move_created(this, m.value); std::swap(value, m.value); }
Packit 534379
    MoveOrCopyInt &operator=(MoveOrCopyInt &&m) { print_move_assigned(this, m.value); std::swap(value, m.value); return *this; }
Packit 534379
    MoveOrCopyInt(const MoveOrCopyInt &c) { print_copy_created(this, c.value); value = c.value; }
Packit 534379
    MoveOrCopyInt &operator=(const MoveOrCopyInt &c) { print_copy_assigned(this, c.value); value = c.value; return *this; }
Packit 534379
    ~MoveOrCopyInt() { print_destroyed(this); }
Packit 534379
Packit 534379
    int value;
Packit 534379
};
Packit 534379
class CopyOnlyInt {
Packit 534379
public:
Packit 534379
    CopyOnlyInt() { print_default_created(this); }
Packit 534379
    CopyOnlyInt(int v) : value{std::move(v)} { print_created(this, value); }
Packit 534379
    CopyOnlyInt(const CopyOnlyInt &c) { print_copy_created(this, c.value); value = c.value; }
Packit 534379
    CopyOnlyInt &operator=(const CopyOnlyInt &c) { print_copy_assigned(this, c.value); value = c.value; return *this; }
Packit 534379
    ~CopyOnlyInt() { print_destroyed(this); }
Packit 534379
Packit 534379
    int value;
Packit 534379
};
Packit 534379
NAMESPACE_BEGIN(pybind11)
Packit 534379
NAMESPACE_BEGIN(detail)
Packit 534379
template <> struct type_caster<MoveOnlyInt> {
Packit 534379
    PYBIND11_TYPE_CASTER(MoveOnlyInt, _("MoveOnlyInt"));
Packit 534379
    bool load(handle src, bool) { value = MoveOnlyInt(src.cast<int>()); return true; }
Packit 534379
    static handle cast(const MoveOnlyInt &m, return_value_policy r, handle p) { return pybind11::cast(m.value, r, p); }
Packit 534379
};
Packit 534379
Packit 534379
template <> struct type_caster<MoveOrCopyInt> {
Packit 534379
    PYBIND11_TYPE_CASTER(MoveOrCopyInt, _("MoveOrCopyInt"));
Packit 534379
    bool load(handle src, bool) { value = MoveOrCopyInt(src.cast<int>()); return true; }
Packit 534379
    static handle cast(const MoveOrCopyInt &m, return_value_policy r, handle p) { return pybind11::cast(m.value, r, p); }
Packit 534379
};
Packit 534379
Packit 534379
template <> struct type_caster<CopyOnlyInt> {
Packit 534379
protected:
Packit 534379
    CopyOnlyInt value;
Packit 534379
public:
Packit 534379
    static constexpr auto name = _("CopyOnlyInt");
Packit 534379
    bool load(handle src, bool) { value = CopyOnlyInt(src.cast<int>()); return true; }
Packit 534379
    static handle cast(const CopyOnlyInt &m, return_value_policy r, handle p) { return pybind11::cast(m.value, r, p); }
Packit 534379
    static handle cast(const CopyOnlyInt *src, return_value_policy policy, handle parent) {
Packit 534379
        if (!src) return none().release();
Packit 534379
        return cast(*src, policy, parent);
Packit 534379
    }
Packit 534379
    operator CopyOnlyInt*() { return &value; }
Packit 534379
    operator CopyOnlyInt&() { return value; }
Packit 534379
    template <typename T> using cast_op_type = pybind11::detail::cast_op_type<T>;
Packit 534379
};
Packit 534379
NAMESPACE_END(detail)
Packit 534379
NAMESPACE_END(pybind11)
Packit 534379
Packit 534379
TEST_SUBMODULE(copy_move_policies, m) {
Packit 534379
    // test_lacking_copy_ctor
Packit 534379
    py::class_<lacking_copy_ctor>(m, "lacking_copy_ctor")
Packit 534379
        .def_static("get_one", &lacking_copy_ctor::get_one,
Packit 534379
                    py::return_value_policy::copy);
Packit 534379
    // test_lacking_move_ctor
Packit 534379
    py::class_<lacking_move_ctor>(m, "lacking_move_ctor")
Packit 534379
        .def_static("get_one", &lacking_move_ctor::get_one,
Packit 534379
                    py::return_value_policy::move);
Packit 534379
Packit 534379
    // test_move_and_copy_casts
Packit 534379
    m.def("move_and_copy_casts", [](py::object o) {
Packit 534379
        int r = 0;
Packit 534379
        r += py::cast<MoveOrCopyInt>(o).value; /* moves */
Packit 534379
        r += py::cast<MoveOnlyInt>(o).value; /* moves */
Packit 534379
        r += py::cast<CopyOnlyInt>(o).value; /* copies */
Packit 534379
        MoveOrCopyInt m1(py::cast<MoveOrCopyInt>(o)); /* moves */
Packit 534379
        MoveOnlyInt m2(py::cast<MoveOnlyInt>(o)); /* moves */
Packit 534379
        CopyOnlyInt m3(py::cast<CopyOnlyInt>(o)); /* copies */
Packit 534379
        r += m1.value + m2.value + m3.value;
Packit 534379
Packit 534379
        return r;
Packit 534379
    });
Packit 534379
Packit 534379
    // test_move_and_copy_loads
Packit 534379
    m.def("move_only", [](MoveOnlyInt m) { return m.value; });
Packit 534379
    m.def("move_or_copy", [](MoveOrCopyInt m) { return m.value; });
Packit 534379
    m.def("copy_only", [](CopyOnlyInt m) { return m.value; });
Packit 534379
    m.def("move_pair", [](std::pair<MoveOnlyInt, MoveOrCopyInt> p) {
Packit 534379
        return p.first.value + p.second.value;
Packit 534379
    });
Packit 534379
    m.def("move_tuple", [](std::tuple<MoveOnlyInt, MoveOrCopyInt, MoveOnlyInt> t) {
Packit 534379
        return std::get<0>(t).value + std::get<1>(t).value + std::get<2>(t).value;
Packit 534379
    });
Packit 534379
    m.def("copy_tuple", [](std::tuple<CopyOnlyInt, CopyOnlyInt> t) {
Packit 534379
        return std::get<0>(t).value + std::get<1>(t).value;
Packit 534379
    });
Packit 534379
    m.def("move_copy_nested", [](std::pair<MoveOnlyInt, std::pair<std::tuple<MoveOrCopyInt, CopyOnlyInt, std::tuple<MoveOnlyInt>>, MoveOrCopyInt>> x) {
Packit 534379
        return x.first.value + std::get<0>(x.second.first).value + std::get<1>(x.second.first).value +
Packit 534379
            std::get<0>(std::get<2>(x.second.first)).value + x.second.second.value;
Packit 534379
    });
Packit 534379
    m.def("move_and_copy_cstats", []() {
Packit 534379
        ConstructorStats::gc();
Packit 534379
        // Reset counts to 0 so that previous tests don't affect later ones:
Packit 534379
        auto &mc = ConstructorStats::get<MoveOrCopyInt>();
Packit 534379
        mc.move_assignments = mc.move_constructions = mc.copy_assignments = mc.copy_constructions = 0;
Packit 534379
        auto &mo = ConstructorStats::get<MoveOnlyInt>();
Packit 534379
        mo.move_assignments = mo.move_constructions = mo.copy_assignments = mo.copy_constructions = 0;
Packit 534379
        auto &co = ConstructorStats::get<CopyOnlyInt>();
Packit 534379
        co.move_assignments = co.move_constructions = co.copy_assignments = co.copy_constructions = 0;
Packit 534379
        py::dict d;
Packit 534379
        d["MoveOrCopyInt"] = py::cast(mc, py::return_value_policy::reference);
Packit 534379
        d["MoveOnlyInt"] = py::cast(mo, py::return_value_policy::reference);
Packit 534379
        d["CopyOnlyInt"] = py::cast(co, py::return_value_policy::reference);
Packit 534379
        return d;
Packit 534379
    });
Packit 534379
#ifdef PYBIND11_HAS_OPTIONAL
Packit 534379
    // test_move_and_copy_load_optional
Packit 534379
    m.attr("has_optional") = true;
Packit 534379
    m.def("move_optional", [](std::optional<MoveOnlyInt> o) {
Packit 534379
        return o->value;
Packit 534379
    });
Packit 534379
    m.def("move_or_copy_optional", [](std::optional<MoveOrCopyInt> o) {
Packit 534379
        return o->value;
Packit 534379
    });
Packit 534379
    m.def("copy_optional", [](std::optional<CopyOnlyInt> o) {
Packit 534379
        return o->value;
Packit 534379
    });
Packit 534379
    m.def("move_optional_tuple", [](std::optional<std::tuple<MoveOrCopyInt, MoveOnlyInt, CopyOnlyInt>> x) {
Packit 534379
        return std::get<0>(*x).value + std::get<1>(*x).value + std::get<2>(*x).value;
Packit 534379
    });
Packit 534379
#else
Packit 534379
    m.attr("has_optional") = false;
Packit 534379
#endif
Packit 534379
Packit 534379
    // #70 compilation issue if operator new is not public
Packit 534379
    struct PrivateOpNew {
Packit 534379
        int value = 1;
Packit 534379
    private:
Packit 534379
#if defined(_MSC_VER)
Packit 534379
#  pragma warning(disable: 4822) // warning C4822: local class member function does not have a body
Packit 534379
#endif
Packit 534379
        void *operator new(size_t bytes);
Packit 534379
    };
Packit 534379
    py::class_<PrivateOpNew>(m, "PrivateOpNew").def_readonly("value", &PrivateOpNew::value);
Packit 534379
    m.def("private_op_new_value", []() { return PrivateOpNew(); });
Packit 534379
    m.def("private_op_new_reference", []() -> const PrivateOpNew & {
Packit 534379
        static PrivateOpNew x{};
Packit 534379
        return x;
Packit 534379
    }, py::return_value_policy::reference);
Packit 534379
Packit 534379
    // test_move_fallback
Packit 534379
    // #389: rvp::move should fall-through to copy on non-movable objects
Packit 534379
    struct MoveIssue1 {
Packit 534379
        int v;
Packit 534379
        MoveIssue1(int v) : v{v} {}
Packit 534379
        MoveIssue1(const MoveIssue1 &c) = default;
Packit 534379
        MoveIssue1(MoveIssue1 &&) = delete;
Packit 534379
    };
Packit 534379
    py::class_<MoveIssue1>(m, "MoveIssue1").def(py::init<int>()).def_readwrite("value", &MoveIssue1::v);
Packit 534379
Packit 534379
    struct MoveIssue2 {
Packit 534379
        int v;
Packit 534379
        MoveIssue2(int v) : v{v} {}
Packit 534379
        MoveIssue2(MoveIssue2 &&) = default;
Packit 534379
    };
Packit 534379
    py::class_<MoveIssue2>(m, "MoveIssue2").def(py::init<int>()).def_readwrite("value", &MoveIssue2::v);
Packit 534379
Packit 534379
    m.def("get_moveissue1", [](int i) { return new MoveIssue1(i); }, py::return_value_policy::move);
Packit 534379
    m.def("get_moveissue2", [](int i) { return MoveIssue2(i); }, py::return_value_policy::move);
Packit 534379
}