cppyabm  1.0.17
An agent-based library to integrate C++ and Python
pybind11_tests.h
Go to the documentation of this file.
1 #pragma once
2 #include <pybind11/pybind11.h>
3 #include <pybind11/eval.h>
4 
5 #if defined(_MSC_VER) && _MSC_VER < 1910
6 // We get some really long type names here which causes MSVC 2015 to emit warnings
7 # pragma warning(disable: 4503) // warning C4503: decorated name length exceeded, name was truncated
8 #endif
9 
10 namespace py = pybind11;
11 using namespace pybind11::literals;
12 
14  using Initializer = void (*)(py::module_ &);
15 
16 public:
17  test_initializer(Initializer init);
18  test_initializer(const char *submodule_name, Initializer init);
19 };
20 
21 #define TEST_SUBMODULE(name, variable) \
22  void test_submodule_##name(py::module_ &); \
23  test_initializer name(#name, test_submodule_##name); \
24  void test_submodule_##name(py::module_ &variable)
25 
26 
27 /// Dummy type which is not exported anywhere -- something to trigger a conversion error
28 struct UnregisteredType { };
29 
30 /// A user-defined type which is exported and can be used by any test
31 class UserType {
32 public:
33  UserType() = default;
34  UserType(int i) : i(i) { }
35 
36  int value() const { return i; }
37  void set(int set) { i = set; }
38 
39 private:
40  int i = -1;
41 };
42 
43 /// Like UserType, but increments `value` on copy for quick reference vs. copy tests
44 class IncType : public UserType {
45 public:
46  using UserType::UserType;
47  IncType() = default;
48  IncType(const IncType &other) : IncType(other.value() + 1) { }
49  IncType(IncType &&) = delete;
50  IncType &operator=(const IncType &) = delete;
51  IncType &operator=(IncType &&) = delete;
52 };
53 
54 /// A simple union for basic testing
55 union IntFloat {
56  int i;
57  float f;
58 };
59 
60 /// Custom cast-only type that casts to a string "rvalue" or "lvalue" depending on the cast context.
61 /// Used to test recursive casters (e.g. std::tuple, stl containers).
62 struct RValueCaster {};
65 template<> class type_caster<RValueCaster> {
66 public:
67  PYBIND11_TYPE_CASTER(RValueCaster, _("RValueCaster"));
68  static handle cast(RValueCaster &&, return_value_policy, handle) { return py::str("rvalue").release(); }
69  static handle cast(const RValueCaster &, return_value_policy, handle) { return py::str("lvalue").release(); }
70 };
73 
74 template <typename F>
76  py::exec(R"(
77  message = "pybind11-bound class '.+' is using an old-style placement-new '(?:__init__|__setstate__)' which has been deprecated"
78 
79  import warnings
80  with warnings.catch_warnings():
81  warnings.filterwarnings("ignore", message=message, category=FutureWarning)
82  body()
83  )", py::dict(py::arg("body") = py::cpp_function(body)));
84 }
test_multiple_inheritance.i
i
Definition: test_multiple_inheritance.py:22
PYBIND11_NAMESPACE_BEGIN
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: common.h:16
UserType::UserType
UserType()=default
PYBIND11_NAMESPACE_END
#define PYBIND11_NAMESPACE_END(name)
Definition: common.h:17
set
Definition: pytypes.h:1369
_
constexpr descr< N - 1 > _(char const(&text)[N])
Definition: descr.h:54
exec
void exec(str expr, object global=globals(), object local=object())
Definition: eval.h:78
IntFloat
A simple union for basic testing.
Definition: pybind11_tests.h:55
handle
Definition: pytypes.h:176
IntFloat::f
float f
Definition: pybind11_tests.h:57
type_caster
Definition: cast.h:954
UnregisteredType
Dummy type which is not exported anywhere – something to trigger a conversion error.
Definition: pybind11_tests.h:28
type_caster< RValueCaster >::PYBIND11_TYPE_CASTER
PYBIND11_TYPE_CASTER(RValueCaster, _("RValueCaster"))
UserType::value
int value() const
Definition: pybind11_tests.h:36
ignoreOldStyleInitWarnings
void ignoreOldStyleInitWarnings(F &&body)
Definition: pybind11_tests.h:75
type_caster< RValueCaster >::cast
static handle cast(RValueCaster &&, return_value_policy, handle)
Definition: pybind11_tests.h:68
eval.h
IncType::operator=
IncType & operator=(IncType &&)=delete
IncType::IncType
IncType(IncType &&)=delete
type_caster< RValueCaster >::cast
static handle cast(const RValueCaster &, return_value_policy, handle)
Definition: pybind11_tests.h:69
init
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Definition: pybind11.h:1587
IncType::UserType
UserType()=default
pybind11.h
test_initializer
Definition: pybind11_tests.h:13
return_value_policy
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
Definition: common.h:357
RValueCaster
Definition: pybind11_tests.h:62
pybind11
Definition: __init__.py:1
IntFloat::i
int i
Definition: pybind11_tests.h:56
UserType::set
void set(int set)
Definition: pybind11_tests.h:37
IncType::IncType
IncType()=default
IncType::IncType
IncType(const IncType &other)
Definition: pybind11_tests.h:48
test_callbacks.value
value
Definition: test_callbacks.py:126
IncType::operator=
IncType & operator=(const IncType &)=delete