@@ -193,6 +193,20 @@ class MyObject5 { // managed by huge_unique_ptr
193
193
int value;
194
194
};
195
195
196
+ // test const_only_shared_ptr
197
+ class MyObject6 {
198
+ public:
199
+ static const_only_shared_ptr<MyObject6> createObject (std::string value) {
200
+ return const_only_shared_ptr<MyObject6>(new MyObject6 (std::move (value)));
201
+ }
202
+
203
+ const std::string &value () const { return value_; }
204
+
205
+ private:
206
+ explicit MyObject6 (std::string &&value) : value_{std::move (value)} {}
207
+ std::string value_;
208
+ };
209
+
196
210
// test_shared_ptr_and_references
197
211
struct SharedPtrRef {
198
212
struct A {
@@ -412,11 +426,6 @@ TEST_SUBMODULE(smart_ptr, m) {
412
426
m.def (" print_myobject2_4" ,
413
427
[](const std::shared_ptr<MyObject2> *obj) { py::print ((*obj)->toString ()); });
414
428
415
- m.def (" make_myobject2_3" ,
416
- [](int val) { return const_only_shared_ptr<MyObject2>(new MyObject2 (val)); });
417
- m.def (" print_myobject2_5" ,
418
- [](const const_only_shared_ptr<MyObject2> &obj) { py::print (obj.get ()->toString ()); });
419
-
420
429
py::class_<MyObject3, std::shared_ptr<MyObject3>>(m, " MyObject3" ).def (py::init<int >());
421
430
m.def (" make_myobject3_1" , []() { return new MyObject3 (8 ); });
422
431
m.def (" make_myobject3_2" , []() { return std::make_shared<MyObject3>(9 ); });
@@ -459,6 +468,10 @@ TEST_SUBMODULE(smart_ptr, m) {
459
468
.def (py::init<int >())
460
469
.def_readwrite (" value" , &MyObject5::value);
461
470
471
+ py::class_<MyObject6, const_only_shared_ptr<MyObject6>>(m, " MyObject6" )
472
+ .def (py::init ([](const std::string &value) { return MyObject6::createObject (value); }))
473
+ .def_property_readonly (" value" , &MyObject6::value);
474
+
462
475
// test_shared_ptr_and_references
463
476
using A = SharedPtrRef::A;
464
477
py::class_<A, std::shared_ptr<A>>(m, " A" );
0 commit comments