mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-11 16:13:53 +00:00
Adding type_caster_bare_interface_demo.
This commit is contained in:
parent
2cb68ab051
commit
de10541b12
115
tests/test_type_caster_bare_interface_demo.cpp
Normal file
115
tests/test_type_caster_bare_interface_demo.cpp
Normal file
@ -0,0 +1,115 @@
|
||||
#include "pybind11_tests.h"
|
||||
|
||||
namespace pybind11_tests {
|
||||
namespace type_caster_bare_interface_demo {
|
||||
|
||||
struct mpty {};
|
||||
|
||||
mpty rtrn_mpty_valu() { mpty obj; return obj; }
|
||||
mpty&& rtrn_mpty_rref() { mpty obj; return std::move(obj); }
|
||||
mpty const& rtrn_mpty_cref() { static mpty obj; return obj; }
|
||||
mpty& rtrn_mpty_mref() { static mpty obj; return obj; }
|
||||
mpty const* rtrn_mpty_cptr() { static mpty obj; return &obj; }
|
||||
mpty* rtrn_mpty_mptr() { static mpty obj; return &obj; }
|
||||
|
||||
const char* pass_mpty_valu(mpty) { return "load_valu"; }
|
||||
const char* pass_mpty_rref(mpty&&) { return "load_rref"; }
|
||||
const char* pass_mpty_cref(mpty const&) { return "load_cref"; }
|
||||
const char* pass_mpty_mref(mpty&) { return "load_mref"; }
|
||||
const char* pass_mpty_cptr(mpty const*) { return "load_cptr"; }
|
||||
const char* pass_mpty_mptr(mpty*) { return "load_mptr"; }
|
||||
|
||||
} // namespace type_caster_bare_interface_demo
|
||||
} // namespace pybind11_tests
|
||||
|
||||
namespace pybind11 {
|
||||
namespace detail {
|
||||
|
||||
using namespace pybind11_tests::type_caster_bare_interface_demo;
|
||||
|
||||
template <>
|
||||
struct type_caster<mpty> {
|
||||
static constexpr auto name = _<mpty>();
|
||||
|
||||
// static handle cast(mpty, ...)
|
||||
// is redundant (leads to ambiguous overloads).
|
||||
|
||||
static handle cast(mpty&& /*src*/,
|
||||
return_value_policy /*policy*/,
|
||||
handle /*parent*/) {
|
||||
return str("cast_rref").release();
|
||||
}
|
||||
|
||||
static handle cast(mpty const& /*src*/,
|
||||
return_value_policy /*policy*/,
|
||||
handle /*parent*/) {
|
||||
return str("cast_cref").release();
|
||||
}
|
||||
|
||||
static handle cast(mpty& /*src*/,
|
||||
return_value_policy /*policy*/,
|
||||
handle /*parent*/) {
|
||||
return str("cast_mref").release();
|
||||
}
|
||||
|
||||
static handle cast(mpty const* /*src*/,
|
||||
return_value_policy /*policy*/,
|
||||
handle /*parent*/) {
|
||||
return str("cast_cptr").release();
|
||||
}
|
||||
|
||||
static handle cast(mpty* /*src*/,
|
||||
return_value_policy /*policy*/,
|
||||
handle /*parent*/) {
|
||||
return str("cast_mptr").release();
|
||||
}
|
||||
|
||||
template <typename T_>
|
||||
using cast_op_type = conditional_t<
|
||||
std::is_same<remove_reference_t<T_>, const mpty*>::value, const mpty*,
|
||||
conditional_t<
|
||||
std::is_same<remove_reference_t<T_>, mpty*>::value, mpty*,
|
||||
conditional_t<
|
||||
std::is_same<T_, const mpty&>::value, const mpty&,
|
||||
conditional_t<
|
||||
std::is_same<T_, mpty&>::value, mpty&,
|
||||
conditional_t<
|
||||
std::is_same<T_, mpty&&>::value, mpty&&,
|
||||
mpty>>>>>;
|
||||
|
||||
operator mpty() { return rtrn_mpty_valu(); }
|
||||
operator mpty&&() && { return rtrn_mpty_rref(); }
|
||||
operator mpty const&() { return rtrn_mpty_cref(); }
|
||||
operator mpty&() { return rtrn_mpty_mref(); }
|
||||
operator mpty const*() { return rtrn_mpty_cptr(); }
|
||||
operator mpty*() { return rtrn_mpty_mptr(); }
|
||||
|
||||
bool load(handle /*src*/, bool /*convert*/) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace pybind11
|
||||
|
||||
namespace pybind11_tests {
|
||||
namespace type_caster_bare_interface_demo {
|
||||
|
||||
TEST_SUBMODULE(type_caster_bare_interface_demo, m) {
|
||||
m.def("rtrn_mpty_valu", rtrn_mpty_valu);
|
||||
m.def("rtrn_mpty_rref", rtrn_mpty_rref);
|
||||
m.def("rtrn_mpty_cref", rtrn_mpty_cref);
|
||||
m.def("rtrn_mpty_mref", rtrn_mpty_mref);
|
||||
m.def("rtrn_mpty_cptr", rtrn_mpty_cptr);
|
||||
m.def("rtrn_mpty_mptr", rtrn_mpty_mptr);
|
||||
|
||||
m.def("pass_mpty_valu", pass_mpty_valu);
|
||||
m.def("pass_mpty_rref", pass_mpty_rref);
|
||||
m.def("pass_mpty_cref", pass_mpty_cref);
|
||||
m.def("pass_mpty_mref", pass_mpty_mref);
|
||||
m.def("pass_mpty_cptr", pass_mpty_cptr);
|
||||
m.def("pass_mpty_mptr", pass_mpty_mptr);
|
||||
}
|
||||
|
||||
} // namespace type_caster_bare_interface_demo
|
||||
} // namespace pybind11_tests
|
22
tests/test_type_caster_bare_interface_demo.py
Normal file
22
tests/test_type_caster_bare_interface_demo.py
Normal file
@ -0,0 +1,22 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import pytest
|
||||
|
||||
from pybind11_tests import type_caster_bare_interface_demo as m
|
||||
|
||||
|
||||
def test_cast():
|
||||
assert m.rtrn_mpty_valu() == "cast_rref"
|
||||
assert m.rtrn_mpty_rref() == "cast_rref"
|
||||
assert m.rtrn_mpty_cref() == "cast_cref"
|
||||
assert m.rtrn_mpty_mref() == "cast_mref"
|
||||
assert m.rtrn_mpty_cptr() == "cast_cptr"
|
||||
assert m.rtrn_mpty_mptr() == "cast_mptr"
|
||||
|
||||
|
||||
def test_load():
|
||||
assert m.pass_mpty_valu(None) == "load_valu"
|
||||
assert m.pass_mpty_rref(None) == "load_rref"
|
||||
assert m.pass_mpty_cref(None) == "load_cref"
|
||||
assert m.pass_mpty_mref(None) == "load_mref"
|
||||
assert m.pass_mpty_cptr(None) == "load_cptr"
|
||||
assert m.pass_mpty_mptr(None) == "load_mptr"
|
Loading…
Reference in New Issue
Block a user