Blame external/pybind11/tests/local_bindings.h

Packit 534379
#pragma once
Packit 534379
#include "pybind11_tests.h"
Packit 534379
Packit 534379
/// Simple class used to test py::local:
Packit 534379
template <int> class LocalBase {
Packit 534379
public:
Packit 534379
    LocalBase(int i) : i(i) { }
Packit 534379
    int i = -1;
Packit 534379
};
Packit 534379
Packit 534379
/// Registered with py::module_local in both main and secondary modules:
Packit 534379
using LocalType = LocalBase<0>;
Packit 534379
/// Registered without py::module_local in both modules:
Packit 534379
using NonLocalType = LocalBase<1>;
Packit 534379
/// A second non-local type (for stl_bind tests):
Packit 534379
using NonLocal2 = LocalBase<2>;
Packit 534379
/// Tests within-module, different-compilation-unit local definition conflict:
Packit 534379
using LocalExternal = LocalBase<3>;
Packit 534379
/// Mixed: registered local first, then global
Packit 534379
using MixedLocalGlobal = LocalBase<4>;
Packit 534379
/// Mixed: global first, then local
Packit 534379
using MixedGlobalLocal = LocalBase<5>;
Packit 534379
Packit 534379
/// Registered with py::module_local only in the secondary module:
Packit 534379
using ExternalType1 = LocalBase<6>;
Packit 534379
using ExternalType2 = LocalBase<7>;
Packit 534379
Packit 534379
using LocalVec = std::vector<LocalType>;
Packit 534379
using LocalVec2 = std::vector<NonLocal2>;
Packit 534379
using LocalMap = std::unordered_map<std::string, LocalType>;
Packit 534379
using NonLocalVec = std::vector<NonLocalType>;
Packit 534379
using NonLocalVec2 = std::vector<NonLocal2>;
Packit 534379
using NonLocalMap = std::unordered_map<std::string, NonLocalType>;
Packit 534379
using NonLocalMap2 = std::unordered_map<std::string, uint8_t>;
Packit 534379
Packit 534379
PYBIND11_MAKE_OPAQUE(LocalVec);
Packit 534379
PYBIND11_MAKE_OPAQUE(LocalVec2);
Packit 534379
PYBIND11_MAKE_OPAQUE(LocalMap);
Packit 534379
PYBIND11_MAKE_OPAQUE(NonLocalVec);
Packit 534379
//PYBIND11_MAKE_OPAQUE(NonLocalVec2); // same type as LocalVec2
Packit 534379
PYBIND11_MAKE_OPAQUE(NonLocalMap);
Packit 534379
PYBIND11_MAKE_OPAQUE(NonLocalMap2);
Packit 534379
Packit 534379
Packit 534379
// Simple bindings (used with the above):
Packit 534379
template <typename T, int Adjust = 0, typename... Args>
Packit 534379
py::class_<T> bind_local(Args && ...args) {
Packit 534379
    return py::class_<T>(std::forward<Args>(args)...)
Packit 534379
        .def(py::init<int>())
Packit 534379
        .def("get", [](T &i) { return i.i + Adjust; });
Packit 534379
};
Packit 534379
Packit 534379
// Simulate a foreign library base class (to match the example in the docs):
Packit 534379
namespace pets {
Packit 534379
class Pet {
Packit 534379
public:
Packit 534379
    Pet(std::string name) : name_(name) {}
Packit 534379
    std::string name_;
Packit 534379
    const std::string &name() { return name_; }
Packit 534379
};
Packit 534379
}
Packit 534379
Packit 534379
struct MixGL { int i; MixGL(int i) : i{i} {} };
Packit 534379
struct MixGL2 { int i; MixGL2(int i) : i{i} {} };