pybind11/tests/test_class_sh_property_non_owning.cpp
Ralf W. Grosse-Kunstleve bd8985aa0f
[smart_holder] Introduce PYBIND11_SMART_HOLDER_DISABLE option. (#5348)
* Step 1: Establish new `PYBIND11_SMART_HOLDER_ENABLED` macro, but only under include/pybind11/

At the stage `PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT` and `PYBIND11_SMART_HOLDER_ENABLED` are still equivalent.

* Systematically replace all `PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT` with `PYBIND11_SMART_HOLDER_ENABLED` under tests/ and ubench/

As at the previous stage, `PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT` and `PYBIND11_SMART_HOLDER_ENABLED` are still equivalent.

* Introduce `PYBIND11_SMART_HOLDER_DISABLE` option.

* `#ifdef` out entire `wrap()` function to avoid `unused-parameter` warning-as-error under macos-13

```
/Users/runner/work/pybind11/pybind11/tests/test_class_sh_trampoline_basic.cpp:67:23: error: unused parameter 'm' [-Werror,-Wunused-parameter]
void wrap(py::module_ m, const char *py_class_name) {
                      ^
/Users/runner/work/pybind11/pybind11/tests/test_class_sh_trampoline_basic.cpp:67:38: error: unused parameter 'py_class_name' [-Werror,-Wunused-parameter]
void wrap(py::module_ m, const char *py_class_name) {
                                     ^
2 errors generated.
```
2024-09-01 14:34:36 -07:00

76 lines
2.4 KiB
C++

#include "pybind11/smart_holder.h"
#include "pybind11_tests.h"
#include <cstddef>
#include <vector>
namespace test_class_sh_property_non_owning {
struct CoreField {
explicit CoreField(int int_value = -99) : int_value{int_value} {}
int int_value;
};
struct DataField {
DataField(int i_value, int i_shared, int i_unique)
: core_fld_value{i_value}, core_fld_shared_ptr{new CoreField{i_shared}},
core_fld_raw_ptr{core_fld_shared_ptr.get()},
core_fld_unique_ptr{new CoreField{i_unique}} {}
CoreField core_fld_value;
std::shared_ptr<CoreField> core_fld_shared_ptr;
CoreField *core_fld_raw_ptr;
std::unique_ptr<CoreField> core_fld_unique_ptr;
};
struct DataFieldsHolder {
private:
std::vector<DataField> vec;
public:
explicit DataFieldsHolder(std::size_t vec_size) {
for (std::size_t i = 0; i < vec_size; i++) {
int i11 = static_cast<int>(i) * 11;
vec.emplace_back(13 + i11, 14 + i11, 15 + i11);
}
}
DataField *vec_at(std::size_t index) {
if (index >= vec.size()) {
return nullptr;
}
return &vec[index];
}
};
} // namespace test_class_sh_property_non_owning
using namespace test_class_sh_property_non_owning;
PYBIND11_SMART_HOLDER_TYPE_CASTERS(CoreField)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(DataField)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(DataFieldsHolder)
TEST_SUBMODULE(class_sh_property_non_owning, m) {
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
#ifndef PYBIND11_SMART_HOLDER_ENABLED
false;
#else
true;
py::classh<CoreField>(m, "CoreField").def_readwrite("int_value", &CoreField::int_value);
py::classh<DataField>(m, "DataField")
.def_readonly("core_fld_value_ro", &DataField::core_fld_value)
.def_readwrite("core_fld_value_rw", &DataField::core_fld_value)
.def_readonly("core_fld_shared_ptr_ro", &DataField::core_fld_shared_ptr)
.def_readwrite("core_fld_shared_ptr_rw", &DataField::core_fld_shared_ptr)
.def_readonly("core_fld_raw_ptr_ro", &DataField::core_fld_raw_ptr)
.def_readwrite("core_fld_raw_ptr_rw", &DataField::core_fld_raw_ptr)
.def_readwrite("core_fld_unique_ptr_rw", &DataField::core_fld_unique_ptr);
py::classh<DataFieldsHolder>(m, "DataFieldsHolder")
.def(py::init<std::size_t>())
.def("vec_at", &DataFieldsHolder::vec_at, py::return_value_policy::reference_internal);
#endif // PYBIND11_SMART_HOLDER_ENABLED
}