μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
local_bindings.h
Go to the documentation of this file.
1#pragma once
2#include "pybind11_tests.h"
3
4#include <utility>
5
7template <int>
8class LocalBase {
9public:
10 explicit LocalBase(int i) : i(i) {}
11 int i = -1;
12};
13
26
30
31using LocalVec = std::vector<LocalType>;
32using LocalVec2 = std::vector<NonLocal2>;
33using LocalMap = std::unordered_map<std::string, LocalType>;
34using NonLocalVec = std::vector<NonLocalType>;
35using NonLocalVec2 = std::vector<NonLocal2>;
36using NonLocalMap = std::unordered_map<std::string, NonLocalType>;
37using NonLocalMap2 = std::unordered_map<std::string, uint8_t>;
38
39// Exception that will be caught via the module local translator.
40class LocalException : public std::exception {
41public:
42 explicit LocalException(const char *m) : message{m} {}
43 const char *what() const noexcept override { return message.c_str(); }
44
45private:
46 std::string message = "";
47};
48
49// Exception that will be registered with register_local_exception_translator
50class LocalSimpleException : public std::exception {
51public:
52 explicit LocalSimpleException(const char *m) : message{m} {}
53 const char *what() const noexcept override { return message.c_str(); }
54
55private:
56 std::string message = "";
57};
58
63// PYBIND11_MAKE_OPAQUE(NonLocalVec2); // same type as LocalVec2
66
67// Simple bindings (used with the above):
68template <typename T, int Adjust = 0, typename... Args>
69py::class_<T> bind_local(Args &&...args) {
70 return py::class_<T>(std::forward<Args>(args)...).def(py::init<int>()).def("get", [](T &i) {
71 return i.i + Adjust;
72 });
73};
74
75// Simulate a foreign library base class (to match the example in the docs):
76namespace pets {
77class Pet {
78public:
79 explicit Pet(std::string name) : name_(std::move(name)) {}
80 std::string name_;
81 const std::string &name() const { return name_; }
82};
83} // namespace pets
84
85struct MixGL {
86 int i;
87 explicit MixGL(int i) : i{i} {}
88};
89struct MixGL2 {
90 int i;
91 explicit MixGL2(int i) : i{i} {}
92};
Simple class used to test py::local:
Definition: local_bindings.h:8
LocalBase(int i)
const char * what() const noexcept override
std::string message
LocalException(const char *m)
const char * what() const noexcept override
LocalSimpleException(const char *m)
Definition: pytypes.h:1776
const std::string & name() const
Pet(std::string name)
std::string name_
#define PYBIND11_MAKE_OPAQUE(...)
Definition: cast.h:1650
@ move
Use std::move to move the return value contents into a new instance that will be owned by Python.
std::unordered_map< std::string, uint8_t > NonLocalMap2
std::vector< NonLocalType > NonLocalVec
std::vector< LocalType > LocalVec
std::vector< NonLocal2 > LocalVec2
std::unordered_map< std::string, LocalType > LocalMap
std::vector< NonLocal2 > NonLocalVec2
std::unordered_map< std::string, NonLocalType > NonLocalMap
py::class_< T > bind_local(Args &&...args)
MixGL2(int i)
MixGL(int i)
Annotation for function names.
Definition: attr.h:47