2017-07-29 02:03:44 +00:00
|
|
|
#pragma once
|
|
|
|
#include "pybind11_tests.h"
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2017-07-29 02:03:44 +00:00
|
|
|
/// Simple class used to test py::local:
|
2022-02-10 20:17:07 +00:00
|
|
|
template <int>
|
|
|
|
class LocalBase {
|
2017-07-29 02:03:44 +00:00
|
|
|
public:
|
2022-02-10 20:17:07 +00:00
|
|
|
explicit LocalBase(int i) : i(i) {}
|
2017-07-29 02:03:44 +00:00
|
|
|
int i = -1;
|
|
|
|
};
|
|
|
|
|
2017-09-02 23:31:47 +00:00
|
|
|
/// Registered with py::module_local in both main and secondary modules:
|
2017-07-29 02:03:44 +00:00
|
|
|
using LocalType = LocalBase<0>;
|
2017-09-02 23:31:47 +00:00
|
|
|
/// Registered without py::module_local in both modules:
|
2017-07-29 02:03:44 +00:00
|
|
|
using NonLocalType = LocalBase<1>;
|
|
|
|
/// A second non-local type (for stl_bind tests):
|
|
|
|
using NonLocal2 = LocalBase<2>;
|
|
|
|
/// Tests within-module, different-compilation-unit local definition conflict:
|
|
|
|
using LocalExternal = LocalBase<3>;
|
2017-08-04 17:05:12 +00:00
|
|
|
/// Mixed: registered local first, then global
|
|
|
|
using MixedLocalGlobal = LocalBase<4>;
|
2017-08-17 15:38:05 +00:00
|
|
|
/// Mixed: global first, then local
|
2017-08-04 17:05:12 +00:00
|
|
|
using MixedGlobalLocal = LocalBase<5>;
|
2017-07-29 02:03:44 +00:00
|
|
|
|
2017-09-02 23:31:47 +00:00
|
|
|
/// Registered with py::module_local only in the secondary module:
|
|
|
|
using ExternalType1 = LocalBase<6>;
|
|
|
|
using ExternalType2 = LocalBase<7>;
|
|
|
|
|
2017-08-13 01:03:06 +00:00
|
|
|
using LocalVec = std::vector<LocalType>;
|
|
|
|
using LocalVec2 = std::vector<NonLocal2>;
|
|
|
|
using LocalMap = std::unordered_map<std::string, LocalType>;
|
|
|
|
using NonLocalVec = std::vector<NonLocalType>;
|
|
|
|
using NonLocalVec2 = std::vector<NonLocal2>;
|
|
|
|
using NonLocalMap = std::unordered_map<std::string, NonLocalType>;
|
|
|
|
using NonLocalMap2 = std::unordered_map<std::string, uint8_t>;
|
|
|
|
|
2021-07-21 12:22:18 +00:00
|
|
|
// Exception that will be caught via the module local translator.
|
|
|
|
class LocalException : public std::exception {
|
|
|
|
public:
|
2022-02-10 20:17:07 +00:00
|
|
|
explicit LocalException(const char *m) : message{m} {}
|
|
|
|
const char *what() const noexcept override { return message.c_str(); }
|
|
|
|
|
2021-07-21 12:22:18 +00:00
|
|
|
private:
|
|
|
|
std::string message = "";
|
|
|
|
};
|
|
|
|
|
|
|
|
// Exception that will be registered with register_local_exception_translator
|
|
|
|
class LocalSimpleException : public std::exception {
|
|
|
|
public:
|
2022-02-10 20:17:07 +00:00
|
|
|
explicit LocalSimpleException(const char *m) : message{m} {}
|
|
|
|
const char *what() const noexcept override { return message.c_str(); }
|
|
|
|
|
2021-07-21 12:22:18 +00:00
|
|
|
private:
|
|
|
|
std::string message = "";
|
|
|
|
};
|
|
|
|
|
2024-08-22 04:27:50 +00:00
|
|
|
PYBIND11_MAKE_OPAQUE(LocalVec)
|
|
|
|
PYBIND11_MAKE_OPAQUE(LocalVec2)
|
|
|
|
PYBIND11_MAKE_OPAQUE(LocalMap)
|
|
|
|
PYBIND11_MAKE_OPAQUE(NonLocalVec)
|
|
|
|
// PYBIND11_MAKE_OPAQUE(NonLocalVec2) // same type as LocalVec2
|
|
|
|
PYBIND11_MAKE_OPAQUE(NonLocalMap)
|
|
|
|
PYBIND11_MAKE_OPAQUE(NonLocalMap2)
|
2017-08-17 15:38:05 +00:00
|
|
|
|
2017-07-29 02:03:44 +00:00
|
|
|
// Simple bindings (used with the above):
|
2017-09-02 23:31:47 +00:00
|
|
|
template <typename T, int Adjust = 0, typename... Args>
|
2022-02-10 20:17:07 +00:00
|
|
|
py::class_<T> bind_local(Args &&...args) {
|
|
|
|
return py::class_<T>(std::forward<Args>(args)...).def(py::init<int>()).def("get", [](T &i) {
|
|
|
|
return i.i + Adjust;
|
|
|
|
});
|
2024-08-22 04:27:50 +00:00
|
|
|
}
|
2017-08-17 15:38:05 +00:00
|
|
|
|
|
|
|
// Simulate a foreign library base class (to match the example in the docs):
|
|
|
|
namespace pets {
|
|
|
|
class Pet {
|
|
|
|
public:
|
CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250)
* Adding google-explicit-constructor to .clang-tidy
* clang-tidy explicit attr.h (all automatic)
* clang-tidy explicit cast.h (all automatic)
* clang-tidy detail/init.h (1 NOLINT)
* clang-tidy detail/type_caster_base.h (2 NOLINT)
* clang-tidy pybind11.h (7 NOLINT)
* clang-tidy detail/common.h (3 NOLINT)
* clang-tidy detail/descr.h (2 NOLINT)
* clang-tidy pytypes.h (23 NOLINT, only 1 explicit)
* clang-tidy eigen.h (7 NOLINT, 0 explicit)
* Adding 2 explicit in functional.h
* Adding 4 explicit in iostream.h
* clang-tidy numpy.h (1 NOLINT, 1 explicit)
* clang-tidy embed.h (0 NOLINT, 1 explicit)
* clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit)
* clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit)
* clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit)
* clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit)
* clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit)
* clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit)
* clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/object.h (0 NOLINT, 2 explicit)
* clang-tidy batch of fully automatic fixes.
* Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error.
2021-09-09 01:53:38 +00:00
|
|
|
explicit Pet(std::string name) : name_(std::move(name)) {}
|
2017-08-17 15:38:05 +00:00
|
|
|
std::string name_;
|
2021-06-21 14:37:48 +00:00
|
|
|
const std::string &name() const { return name_; }
|
2017-08-17 15:38:05 +00:00
|
|
|
};
|
2020-09-11 01:16:40 +00:00
|
|
|
} // namespace pets
|
2017-08-17 15:38:05 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
struct MixGL {
|
|
|
|
int i;
|
|
|
|
explicit MixGL(int i) : i{i} {}
|
|
|
|
};
|
|
|
|
struct MixGL2 {
|
|
|
|
int i;
|
|
|
|
explicit MixGL2(int i) : i{i} {}
|
|
|
|
};
|