μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
pybind11_tests.h
Go to the documentation of this file.
1#pragma once
2
3#include <pybind11/eval.h>
4#include <pybind11/pybind11.h>
5
6#if defined(_MSC_VER) && _MSC_VER < 1910
7// We get some really long type names here which causes MSVC 2015 to emit warnings
8# pragma warning( \
9 disable : 4503) // NOLINT: warning C4503: decorated name length exceeded, name was truncated
10#endif
11
12namespace py = pybind11;
13using namespace pybind11::literals;
14
16 using Initializer = void (*)(py::module_ &);
17
18public:
20 test_initializer(const char *submodule_name, Initializer init);
21};
22
23#define TEST_SUBMODULE(name, variable) \
24 void test_submodule_##name(py::module_ &); \
25 test_initializer name(#name, test_submodule_##name); \
26 void test_submodule_##name(py::module_ &(variable))
27
30
32class UserType {
33public:
34 UserType() = default;
35 explicit UserType(int i) : i(i) {}
36
37 int value() const { return i; }
38 void set(int set) { i = set; }
39
40private:
41 int i = -1;
42};
43
45class IncType : public UserType {
46public:
48 IncType() = default;
49 IncType(const IncType &other) : IncType(other.value() + 1) {}
50 IncType(IncType &&) = delete;
51 IncType &operator=(const IncType &) = delete;
52 IncType &operator=(IncType &&) = delete;
53};
54
56union IntFloat {
57 int i;
58 float f;
59};
60
63struct RValueCaster {};
66template <>
68public:
71 return py::str("rvalue").release();
72 }
74 return py::str("lvalue").release();
75 }
76};
79
80template <typename F>
82 py::exec(R"(
83 message = "pybind11-bound class '.+' is using an old-style placement-new '(?:__init__|__setstate__)' which has been deprecated"
84
85 import warnings
86 with warnings.catch_warnings():
87 warnings.filterwarnings("ignore", message=message, category=FutureWarning)
88 body()
89 )",
90 py::dict(py::arg("body") = py::cpp_function(body)));
91}
Like UserType, but increments value on copy for quick reference vs. copy tests.
IncType(const IncType &other)
IncType()=default
IncType & operator=(IncType &&)=delete
IncType & operator=(const IncType &)=delete
IncType(IncType &&)=delete
A user-defined type which is exported and can be used by any test.
UserType()=default
void set(int set)
int value() const
UserType(int i)
\rst Holds a reference to a Python object (no reference counting)
Definition: pytypes.h:194
Definition: pytypes.h:1783
void(*)(py::module_ &) Initializer
static handle cast(const RValueCaster &, return_value_policy, handle)
static handle cast(RValueCaster &&, return_value_policy, handle)
PYBIND11_TYPE_CASTER(RValueCaster, const_name("RValueCaster"))
#define PYBIND11_NAMESPACE_END(name)
Definition: common.h:21
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: common.h:20
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
Definition: common.h:470
constexpr descr< N - 1 > const_name(char const (&text)[N])
Definition: descr.h:60
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Definition: pybind11.h:1900
void ignoreOldStyleInitWarnings(F &&body)
Custom cast-only type that casts to a string "rvalue" or "lvalue" depending on the cast context.
Dummy type which is not exported anywhere – something to trigger a conversion error.
A simple union for basic testing.