μ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
6namespace py = pybind11;
7using namespace pybind11::literals;
8
10 using Initializer = void (*)(py::module_ &);
11
12public:
14 test_initializer(const char *submodule_name, Initializer init);
15};
16
17#define TEST_SUBMODULE(name, variable) \
18 void test_submodule_##name(py::module_ &); \
19 test_initializer name(#name, test_submodule_##name); \
20 void test_submodule_##name(py::module_ &(variable))
21
23struct UnregisteredType {};
24
26class UserType {
27public:
28 UserType() = default;
29 explicit UserType(int i) : i(i) {}
30
31 int value() const { return i; }
32 void set(int set) { i = set; }
33
34private:
35 int i = -1;
36};
37
39class IncType : public UserType {
40public:
42 IncType() = default;
43 IncType(const IncType &other) : IncType(other.value() + 1) {}
44 IncType(IncType &&) = delete;
45 IncType &operator=(const IncType &) = delete;
46 IncType &operator=(IncType &&) = delete;
47};
48
50union IntFloat {
51 int i;
52 float f;
53};
54
57struct RValueCaster {};
60template <>
62public:
65 return py::str("rvalue").release();
66 }
68 return py::str("lvalue").release();
69 }
70};
73
74template <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 )",
84 py::dict(py::arg("body") = py::cpp_function(body)));
85}
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
test_initializer(Initializer init)
test_initializer(const char *submodule_name, Initializer init)
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.