diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-07-23 00:11:27 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-07-23 00:11:27 +0200 |
| commit | af3288263359cdce86f3d0c753961eee9560e4cf (patch) | |
| tree | bef3c2f4edc36adc6b79f3b0081b41513741cb59 /libs/kstd | |
| parent | 0de79775bfcc8ed32726072352d4c0f95ea6b539 (diff) | |
| download | kernel-af3288263359cdce86f3d0c753961eee9560e4cf.tar.xz kernel-af3288263359cdce86f3d0c753961eee9560e4cf.zip | |
kstd: add shared_ptr aliasing constructor
Diffstat (limited to 'libs/kstd')
| -rw-r--r-- | libs/kstd/kstd/bits/shared_ptr.hpp | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/libs/kstd/kstd/bits/shared_ptr.hpp b/libs/kstd/kstd/bits/shared_ptr.hpp index 7af0a00e..8d5dc1a8 100644 --- a/libs/kstd/kstd/bits/shared_ptr.hpp +++ b/libs/kstd/kstd/bits/shared_ptr.hpp @@ -360,7 +360,7 @@ namespace kstd } /** - * @brief Constructor a shared_ptr from a weak_ptr. If other is not expired, constructs a shared_ptr which shares + * @brief Construct a shared_ptr from a weak_ptr. If other is not expired, constructs a shared_ptr which shares * ownership of the object managed by other. Otherwise, constructs an empty shared_ptr. * * @param other The weak_ptr to construct from. @@ -386,6 +386,23 @@ namespace kstd } } + //! Aliasing constructor. + //! + //! @tparam U The type of the object managed by other. + //! @param other The shared_ptr to share ownership with. + //! @param pointer The pointer this shared_ptr should own. + template<typename Y> + requires bits::is_shared_pointer_ctor_compatible<Y *, T *>::value + shared_ptr(shared_ptr<Y> const & other, T * pointer) noexcept + : m_storage{pointer} + , m_control_block{other.m_control_block} + { + if (m_control_block != nullptr) + { + ++(m_control_block->strong_count); + } + } + /** * @brief Copy constructor. * @@ -734,6 +751,12 @@ namespace kstd return shared_ptr<T>(new T(std::forward<Args>(args)...)); } + template<typename T, typename U> + [[nodiscard]] auto static_pointer_cast(shared_ptr<U> const & other) noexcept -> shared_ptr<T> + { + return shared_ptr<T>{other, static_cast<T *>(other.get())}; + } + /** * @brief Equality operator for shared_ptr. Two shared_ptr instances are equal if they point to the same object * @tparam T, U Types of the managed objects of the shared_ptr instances being compared. |
