local_bindings.h
Go to the documentation of this file.
1 #pragma once
2 #include "pybind11_tests.h"
3 
4 #include <utility>
5 
7 template <int>
8 class LocalBase {
9 public:
10  explicit LocalBase(int i) : i(i) {}
11  int i = -1;
12 };
13 
26 
30 
31 using LocalVec = std::vector<LocalType>;
32 using LocalVec2 = std::vector<NonLocal2>;
33 using LocalMap = std::unordered_map<std::string, LocalType>;
34 using NonLocalVec = std::vector<NonLocalType>;
35 using NonLocalVec2 = std::vector<NonLocal2>;
36 using NonLocalMap = std::unordered_map<std::string, NonLocalType>;
37 using NonLocalMap2 = std::unordered_map<std::string, uint8_t>;
38 
39 // Exception that will be caught via the module local translator.
40 class LocalException : public std::exception {
41 public:
42  explicit LocalException(const char *m) : message{m} {}
43  const char *what() const noexcept override { return message.c_str(); }
44 
45 private:
46  std::string message = "";
47 };
48 
49 // Exception that will be registered with register_local_exception_translator
50 class LocalSimpleException : public std::exception {
51 public:
52  explicit LocalSimpleException(const char *m) : message{m} {}
53  const char *what() const noexcept override { return message.c_str(); }
54 
55 private:
56  std::string message = "";
57 };
58 
63 // PYBIND11_MAKE_OPAQUE(NonLocalVec2); // same type as LocalVec2
66 
67 // Simple bindings (used with the above):
68 template <typename T, int Adjust = 0, typename... Args>
69 py::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):
76 namespace pets {
77 class Pet {
78 public:
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 
85 struct MixGL {
86  int i;
87  explicit MixGL(int i) : i{i} {}
88 };
89 struct MixGL2 {
90  int i;
91  explicit MixGL2(int i) : i{i} {}
92 };
PYBIND11_MAKE_OPAQUE
PYBIND11_MAKE_OPAQUE(LocalVec)
LocalSimpleException::LocalSimpleException
LocalSimpleException(const char *m)
Definition: local_bindings.h:52
pets::Pet::name
const std::string & name() const
Definition: local_bindings.h:81
name
Annotation for function names.
Definition: attr.h:51
NonLocalVec2
std::vector< NonLocal2 > NonLocalVec2
Definition: local_bindings.h:35
LocalSimpleException::message
std::string message
Definition: local_bindings.h:56
T
Eigen::Triplet< double > T
Definition: Tutorial_sparse_example.cpp:6
LocalException::LocalException
LocalException(const char *m)
Definition: local_bindings.h:42
pets::Pet::Pet
Pet(std::string name)
Definition: local_bindings.h:79
pets
Definition: local_bindings.h:76
NonLocalVec
std::vector< NonLocalType > NonLocalVec
Definition: local_bindings.h:34
MixGL::MixGL
MixGL(int i)
Definition: local_bindings.h:87
MixGL2
Definition: local_bindings.h:89
NonLocalMap
std::unordered_map< std::string, NonLocalType > NonLocalMap
Definition: local_bindings.h:36
NonLocalMap2
std::unordered_map< std::string, uint8_t > NonLocalMap2
Definition: local_bindings.h:37
pets::Pet::name_
std::string name_
Definition: local_bindings.h:80
LocalVec2
std::vector< NonLocal2 > LocalVec2
Definition: local_bindings.h:32
LocalSimpleException::what
const char * what() const noexcept override
Definition: local_bindings.h:53
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
LocalMap
std::unordered_map< std::string, LocalType > LocalMap
Definition: local_bindings.h:33
Eigen::Triplet< double >
pets::Pet
Definition: local_bindings.h:77
LocalBase
Simple class used to test py::local:
Definition: local_bindings.h:8
MixGL::i
int i
Definition: local_bindings.h:86
LocalException::message
std::string message
Definition: local_bindings.h:46
LocalVec
std::vector< LocalType > LocalVec
Definition: local_bindings.h:31
bind_local
py::class_< T > bind_local(Args &&...args)
Definition: local_bindings.h:69
LocalException
Definition: local_bindings.h:40
move
detail::enable_if_t<!detail::move_never< T >::value, T > move(object &&obj)
Definition: cast.h:1243
MixGL2::i
int i
Definition: local_bindings.h:90
LocalBase::LocalBase
LocalBase(int i)
Definition: local_bindings.h:10
pybind11_tests.h
LocalBase::i
int i
Definition: local_bindings.h:11
std
Definition: BFloat16.h:88
args
Definition: pytypes.h:2210
MixGL
Definition: local_bindings.h:85
LocalException::what
const char * what() const noexcept override
Definition: local_bindings.h:43
MixGL2::MixGL2
MixGL2(int i)
Definition: local_bindings.h:91
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
LocalSimpleException
Definition: local_bindings.h:50


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:43