blob: b6afb808664de1fdbde011a9bf7c38d3a8794127 [file] [log] [blame]
Jason Rhinelander7437c692017-07-28 22:03:44 -04001#pragma once
2#include "pybind11_tests.h"
3
4/// Simple class used to test py::local:
5template <int> class LocalBase {
6public:
7 LocalBase(int i) : i(i) { }
8 int i = -1;
9};
10
Dean Moldovan7b1de1e2017-09-03 01:31:47 +020011/// Registered with py::module_local in both main and secondary modules:
Jason Rhinelander7437c692017-07-28 22:03:44 -040012using LocalType = LocalBase<0>;
Dean Moldovan7b1de1e2017-09-03 01:31:47 +020013/// Registered without py::module_local in both modules:
Jason Rhinelander7437c692017-07-28 22:03:44 -040014using NonLocalType = LocalBase<1>;
15/// A second non-local type (for stl_bind tests):
16using NonLocal2 = LocalBase<2>;
17/// Tests within-module, different-compilation-unit local definition conflict:
18using LocalExternal = LocalBase<3>;
Jason Rhinelander4b159232017-08-04 13:05:12 -040019/// Mixed: registered local first, then global
20using MixedLocalGlobal = LocalBase<4>;
Jason Rhinelander5e14aa62017-08-17 11:38:05 -040021/// Mixed: global first, then local
Jason Rhinelander4b159232017-08-04 13:05:12 -040022using MixedGlobalLocal = LocalBase<5>;
Jason Rhinelander7437c692017-07-28 22:03:44 -040023
Dean Moldovan7b1de1e2017-09-03 01:31:47 +020024/// Registered with py::module_local only in the secondary module:
25using ExternalType1 = LocalBase<6>;
26using ExternalType2 = LocalBase<7>;
27
Dean Moldovan8d3cedb2017-08-13 03:03:06 +020028using LocalVec = std::vector<LocalType>;
29using LocalVec2 = std::vector<NonLocal2>;
30using LocalMap = std::unordered_map<std::string, LocalType>;
31using NonLocalVec = std::vector<NonLocalType>;
32using NonLocalVec2 = std::vector<NonLocal2>;
33using NonLocalMap = std::unordered_map<std::string, NonLocalType>;
34using NonLocalMap2 = std::unordered_map<std::string, uint8_t>;
35
Jason Rhinelander5e14aa62017-08-17 11:38:05 -040036PYBIND11_MAKE_OPAQUE(LocalVec);
37PYBIND11_MAKE_OPAQUE(LocalVec2);
38PYBIND11_MAKE_OPAQUE(LocalMap);
39PYBIND11_MAKE_OPAQUE(NonLocalVec);
40//PYBIND11_MAKE_OPAQUE(NonLocalVec2); // same type as LocalVec2
41PYBIND11_MAKE_OPAQUE(NonLocalMap);
42PYBIND11_MAKE_OPAQUE(NonLocalMap2);
43
44
Jason Rhinelander7437c692017-07-28 22:03:44 -040045// Simple bindings (used with the above):
Dean Moldovan7b1de1e2017-09-03 01:31:47 +020046template <typename T, int Adjust = 0, typename... Args>
Jason Rhinelander7437c692017-07-28 22:03:44 -040047py::class_<T> bind_local(Args && ...args) {
48 return py::class_<T>(std::forward<Args>(args)...)
49 .def(py::init<int>())
50 .def("get", [](T &i) { return i.i + Adjust; });
51};
Jason Rhinelander5e14aa62017-08-17 11:38:05 -040052
53// Simulate a foreign library base class (to match the example in the docs):
54namespace pets {
55class Pet {
56public:
57 Pet(std::string name) : name_(name) {}
58 std::string name_;
59 const std::string &name() { return name_; }
60};
61}
62
63struct MixGL { int i; MixGL(int i) : i{i} {} };
64struct MixGL2 { int i; MixGL2(int i) : i{i} {} };