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 };
Matrix3f m
LocalBase(int i)
py::class_< T > bind_local(Args &&...args)
LocalException(const char *m)
PYBIND11_MAKE_OPAQUE(LocalVec)
std::unordered_map< std::string, uint8_t > NonLocalMap2
std::vector< LocalType > LocalVec
Definition: pytypes.h:2012
std::vector< NonLocal2 > LocalVec2
const std::string & name() const
Definition: BFloat16.h:88
std::unordered_map< std::string, LocalType > LocalMap
Pet(std::string name)
std::vector< NonLocal2 > NonLocalVec2
const char * what() const noexcept override
const char * what() const noexcept override
Eigen::Triplet< double > T
MixGL(int i)
std::string name_
std::vector< NonLocalType > NonLocalVec
Annotation for function names.
Definition: attr.h:48
Simple class used to test py::local:
Definition: local_bindings.h:8
std::unordered_map< std::string, NonLocalType > NonLocalMap
MixGL2(int i)
LocalSimpleException(const char *m)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:33