Skip to content

Commit d2b49ef

Browse files
committed
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 4c80357 commit d2b49ef

File tree

3 files changed

+79
-95
lines changed

3 files changed

+79
-95
lines changed

include/pybind11/detail/struct_smart_holder.h

Lines changed: 1 addition & 54 deletions
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;

tests/pure_cpp/smart_holder_poc.h

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,50 @@
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

5-
// Proof-of-Concept for smart pointer interoperability.
6-
75
#pragma once
86

97
#include "pybind11/detail/struct_smart_holder.h"
108

119
namespace pybindit {
1210
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+
}
1348

49+
} // namespace smart_holder_poc
1450
} // namespace memory
1551
} // namespace pybindit

0 commit comments

Comments
 (0)