Skip to content

Commit 01b6ccb

Browse files
authored
Move smart_holder POC code to tests/pure_cpp directory. (#5315)
* Rename detail/smart_holder_poc.h -> struct_smart_holder.h * Establish (empty) tests/pure_cpp/smart_holder_poc.h * Move code guarded by `PYBIND11_TESTS_PURE_CPP_SMART_HOLDER_POC_TEST_CPP` from struct_smart_holder.h to tests/pure_cpp/smart_holder_poc.h
1 parent 205da0d commit 01b6ccb

File tree

7 files changed

+97
-98
lines changed

7 files changed

+97
-98
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ set(PYBIND11_HEADERS
133133
include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h
134134
include/pybind11/detail/init.h
135135
include/pybind11/detail/internals.h
136-
include/pybind11/detail/smart_holder_poc.h
136+
include/pybind11/detail/struct_smart_holder.h
137137
include/pybind11/detail/type_caster_base.h
138138
include/pybind11/detail/typeid.h
139139
include/pybind11/detail/using_smart_holder.h

include/pybind11/detail/smart_holder_poc.h renamed to include/pybind11/detail/struct_smart_holder.h

+1-54
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2021 The Pybind Development Team.
1+
// Copyright (c) 2020-2024 The Pybind Development Team.
22
// All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

@@ -44,16 +44,6 @@ High-level aspects:
4444
* The `void_cast_raw_ptr` option is needed to make the `smart_holder` `vptr`
4545
member invisible to the `shared_from_this` mechanism, in case the lifetime
4646
of a `PyObject` is tied to the pointee.
47-
48-
* Regarding `PYBIND11_TESTS_PURE_CPP_SMART_HOLDER_POC_TEST_CPP` below:
49-
This define serves as a marker for code that is NOT used
50-
from smart_holder_type_casters.h, but is exercised only from
51-
tests/pure_cpp/smart_holder_poc_test.cpp. The marked code was useful
52-
mainly for bootstrapping the smart_holder work. At this stage, with
53-
smart_holder_type_casters.h in production use (at Google) since around
54-
February 2021, it could be moved from here to tests/pure_cpp/ (help welcome).
55-
It will probably be best in most cases to add tests for new functionality
56-
under test/test_class_sh_*.
5747
*/
5848

5949
#pragma once
@@ -256,26 +246,6 @@ struct smart_holder {
256246
return static_cast<T *>(vptr.get());
257247
}
258248

259-
#ifdef PYBIND11_TESTS_PURE_CPP_SMART_HOLDER_POC_TEST_CPP // See comment near top.
260-
template <typename T>
261-
T &as_lvalue_ref() const {
262-
static const char *context = "as_lvalue_ref";
263-
ensure_is_populated(context);
264-
ensure_has_pointee(context);
265-
return *as_raw_ptr_unowned<T>();
266-
}
267-
#endif
268-
269-
#ifdef PYBIND11_TESTS_PURE_CPP_SMART_HOLDER_POC_TEST_CPP // See comment near top.
270-
template <typename T>
271-
T &&as_rvalue_ref() const {
272-
static const char *context = "as_rvalue_ref";
273-
ensure_is_populated(context);
274-
ensure_has_pointee(context);
275-
return std::move(*as_raw_ptr_unowned<T>());
276-
}
277-
#endif
278-
279249
template <typename T>
280250
static smart_holder from_raw_ptr_take_ownership(T *raw_ptr, bool void_cast_raw_ptr = false) {
281251
ensure_pointee_is_destructible<T>("from_raw_ptr_take_ownership");
@@ -319,16 +289,6 @@ struct smart_holder {
319289
release_disowned();
320290
}
321291

322-
#ifdef PYBIND11_TESTS_PURE_CPP_SMART_HOLDER_POC_TEST_CPP // See comment near top.
323-
template <typename T>
324-
T *as_raw_ptr_release_ownership(const char *context = "as_raw_ptr_release_ownership") {
325-
ensure_can_release_ownership(context);
326-
T *raw_ptr = as_raw_ptr_unowned<T>();
327-
release_ownership();
328-
return raw_ptr;
329-
}
330-
#endif
331-
332292
template <typename T, typename D>
333293
static smart_holder from_unique_ptr(std::unique_ptr<T, D> &&unq_ptr,
334294
void *void_ptr = nullptr) {
@@ -351,19 +311,6 @@ struct smart_holder {
351311
return hld;
352312
}
353313

354-
#ifdef PYBIND11_TESTS_PURE_CPP_SMART_HOLDER_POC_TEST_CPP // See comment near top.
355-
template <typename T, typename D = std::default_delete<T>>
356-
std::unique_ptr<T, D> as_unique_ptr() {
357-
static const char *context = "as_unique_ptr";
358-
ensure_compatible_rtti_uqp_del<T, D>(context);
359-
ensure_use_count_1(context);
360-
T *raw_ptr = as_raw_ptr_unowned<T>();
361-
release_ownership();
362-
// KNOWN DEFECT (see PR #4850): Does not copy the deleter.
363-
return std::unique_ptr<T, D>(raw_ptr);
364-
}
365-
#endif
366-
367314
template <typename T>
368315
static smart_holder from_shared_ptr(std::shared_ptr<T> shd_ptr) {
369316
smart_holder hld;

include/pybind11/detail/type_caster_base.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ struct load_helper : value_and_holder_helper {
789789

790790
auto *gd = std::get_deleter<pybindit::memory::guarded_delete>(holder().vptr);
791791
if (gd && gd->use_del_fun) { // Note the ensure_compatible_rtti_uqp_del<T, D>() call above.
792-
// In smart_holder_poc, a custom deleter is always stored in a guarded delete.
792+
// In struct_smart_holder, a custom deleter is always stored in a guarded delete.
793793
// The guarded delete's std::function<void(void*)> actually points at the
794794
// custom_deleter type, so we can verify it is of the custom deleter type and
795795
// finally extract its deleter.

include/pybind11/detail/using_smart_holder.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <type_traits>
1111

1212
#ifdef PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT
13-
# include "smart_holder_poc.h"
13+
# include "struct_smart_holder.h"
1414
#endif
1515

1616
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)

tests/extra_python_package/test_files.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h",
5959
"include/pybind11/detail/init.h",
6060
"include/pybind11/detail/internals.h",
61-
"include/pybind11/detail/smart_holder_poc.h",
61+
"include/pybind11/detail/struct_smart_holder.h",
6262
"include/pybind11/detail/type_caster_base.h",
6363
"include/pybind11/detail/typeid.h",
6464
"include/pybind11/detail/using_smart_holder.h",

tests/pure_cpp/smart_holder_poc.h

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2020-2024 The Pybind Development Team.
2+
// All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
#pragma once
6+
7+
#include "pybind11/detail/struct_smart_holder.h"
8+
9+
namespace pybindit {
10+
namespace memory {
11+
namespace smart_holder_poc { // Proof-of-Concept implementations.
12+
13+
template <typename T>
14+
T &as_lvalue_ref(const smart_holder &hld) {
15+
static const char *context = "as_lvalue_ref";
16+
hld.ensure_is_populated(context);
17+
hld.ensure_has_pointee(context);
18+
return *hld.as_raw_ptr_unowned<T>();
19+
}
20+
21+
template <typename T>
22+
T &&as_rvalue_ref(const smart_holder &hld) {
23+
static const char *context = "as_rvalue_ref";
24+
hld.ensure_is_populated(context);
25+
hld.ensure_has_pointee(context);
26+
return std::move(*hld.as_raw_ptr_unowned<T>());
27+
}
28+
29+
template <typename T>
30+
T *as_raw_ptr_release_ownership(smart_holder &hld,
31+
const char *context = "as_raw_ptr_release_ownership") {
32+
hld.ensure_can_release_ownership(context);
33+
T *raw_ptr = hld.as_raw_ptr_unowned<T>();
34+
hld.release_ownership();
35+
return raw_ptr;
36+
}
37+
38+
template <typename T, typename D = std::default_delete<T>>
39+
std::unique_ptr<T, D> as_unique_ptr(smart_holder &hld) {
40+
static const char *context = "as_unique_ptr";
41+
hld.ensure_compatible_rtti_uqp_del<T, D>(context);
42+
hld.ensure_use_count_1(context);
43+
T *raw_ptr = hld.as_raw_ptr_unowned<T>();
44+
hld.release_ownership();
45+
// KNOWN DEFECT (see PR #4850): Does not copy the deleter.
46+
return std::unique_ptr<T, D>(raw_ptr);
47+
}
48+
49+
} // namespace smart_holder_poc
50+
} // namespace memory
51+
} // namespace pybindit

0 commit comments

Comments
 (0)