interop: add weak_virtual_ptr for std::weak_ptr - #100
Open
jll63 wants to merge 2 commits into
Open
Conversation
Closes boostorg#73. Add <boost/openmethod/interop/std_weak_ptr.hpp>, providing weak_virtual_ptr<Class>, an alias for virtual_ptr<std::weak_ptr<Class>>. It tracks an object with a std::weak_ptr and remembers its v-table pointer, so that lock() returns a shared_virtual_ptr without a hash table lookup. Remembering the vptr is safe: the weak pointer keeps the control block alive, so once the object is destroyed it stays expired for good. It is a storage facility only. It is constructed from a shared_virtual_ptr (or a std::shared_ptr or std::weak_ptr), converts to a weak_virtual_ptr to a base class, and offers lock(), expired(), use_count(), reset(), pointer() and vptr(). It cannot be dereferenced, and it cannot be used as a virtual parameter - neither as virtual_<std::weak_ptr<T>> nor as weak_virtual_ptr<T> - because the object may no longer exist; validate_method_parameter specializations reject both with "a weak pointer cannot be a virtual parameter; call lock() first". std::weak_ptr does not fit the generic smart-pointer specialization of virtual_ptr, which needs get(), operator* and a conversion to bool, so the specialization is written by hand. virtual_traits is deliberately not specialized for std::weak_ptr: that keeps IsSmartPtr false, which is what lets the hand-written specialization win. In core.hpp, the plain virtual_ptr's converting constructor and assignment from another virtual_ptr now also require the source to have get() (detail::has_get), so a weak source is a clean substitution failure instead of a hard error in the body, and is_constructible reports it correctly. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
An automated preview of the documentation is available at https://100.openmethod.prtest3.cppalliance.org/libs/openmethod/doc/html/index.html If more commits are pushed to the pull request, the docs will rebuild at the same URL. 2026-09-05 20:26:23 UTC |
… test
The dispatch test registered the static member functions of a local class
as override<> template arguments. That is valid C++17, but gcc before 13
rejects it ("has no linkage"), failing the gcc-10/11/12 and Cygwin 32-bit
jobs. Use namespace-scope function templates instead, as test_util.hpp's
poke_bear does.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #100 +/- ##
===========================================
+ Coverage 93.46% 93.77% +0.31%
===========================================
Files 22 23 +1
Lines 1653 1752 +99
Branches 500 512 +12
===========================================
+ Hits 1545 1643 +98
Misses 64 64
- Partials 44 45 +1
... and 3 files with indirect coverage changes Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #73.
Summary
Adds
<boost/openmethod/interop/std_weak_ptr.hpp>, providingweak_virtual_ptr<Class>, an alias forvirtual_ptr<std::weak_ptr<Class>>. It tracks an object with astd::weak_ptrand remembers its v-table pointer, solock()returns ashared_virtual_ptrwith no hash table lookup — it costs exactlystd::weak_ptr::lock(). Remembering the vptr is safe: the weak pointer keeps the control block alive, so once the object is destroyed it stays expired for good.It is a storage facility only:
shared_virtual_ptr(copies the vptr; the class need not be polymorphic), from anotherweak_virtual_ptr(upcasts), or from astd::shared_ptr/std::weak_ptr(acquires the vptr; expired ⇒ null vptr);lock(),expired(),use_count(),reset(),pointer(),vptr();get(),operator*,operator->,cast<>()ormake_weak_virtual;virtual_<std::weak_ptr<T>>nor asweak_virtual_ptr<T>(by value,&,const&):validate_method_parameterspecializations reject both with "a weak pointer cannot be a virtual parameter; call lock() first".Design notes
std::weak_ptrdoes not fit the generic smart-pointer specialization ofvirtual_ptr(it needsget(),operator*and a conversion tobool), so the specialization is written by hand.virtual_traitsis deliberately not specialized forstd::weak_ptr: that keepsIsSmartPtrfalse, which is what lets the hand-written specialization win over the generic one.core.hpp: the plainvirtual_ptr's converting constructor and assignment from anothervirtual_ptrnow also require the source to haveget()(detail::has_get, a partially specializedconstexpr boolvariable template). A weak source is then a clean substitution failure instead of a hard error in the body, andstd::is_constructible_vreports it correctly.Tests and docs
test/test_weak_virtual_ptr.cpp: 7 cases over the 4 direct/indirect × vector/map registries, plusstatic_asserts on the allowed and rejected conversions.test/compile_fail_weak_ptr_parameter.cppandtest/compile_fail_weak_virtual_ptr_parameter.cpp, one per rejected spelling.smart_pointers.adoc,ref_headers.adocentries, andweak_lock/weak_virtual_ptr_aliastags in thesmart_pointers.cppsnippet (referenced by the MrDocs comments).Verified locally: gcc 13.3 Debug
-Werror, full suite 162/162; clang 22 on the new tests, compile-fail tests and snippet. Both compile-fail tests report the intendedstatic_assertas the first error on both compilers. Not verified: a MrDocs build (not installed here).🤖 Generated with Claude Code