7namespace Stroika::Foundation::Memory {
10 template <
typename T,
typename SHARED_IMPL,
typename COPIER>
11 [[deprecated (
"Since Stroika v3.0d23 - not used - doesnt do anything")]]
constexpr bool SharedByValue_IsCopier ()
16 template <
typename T,
typename SHARED_IMPL = shared_ptr<T>,
typename COPIER = SharedByValueSupport::DefaultValueCopier_FunctionObject<T, SHARED_IMPL>>
17 using SharedByValue_Traits [[deprecated (
"Since Stroika v3.0d23 - use DefaultTraits directly")]] =
20 using SharedByValue_State [[deprecated (
"Since Stroika v3.0d23 - use SharedByValueSupport::SharingState directly")]] =
28 template <
typename T,
typename SHARED_IMPL>
31 if constexpr (same_as<SHARED_IMPL, shared_ptr<T>>) {
32 return Memory::MakeSharedPtr<T> (t);
35 return SHARED_IMPL{
new T{t}};
38 template <
typename T,
typename SHARED_IMPL>
39#if __cplusplus >= kStrokia_Foundation_Common_cplusplus_23 || _HAS_CXX23
45 return DefaultValueCopier (t);
53#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
54 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
56 requires (not same_as<default_copier_type, MissingCopierTypeSentinel>)
59 if constexpr (not same_as<instance_defined_copier_type, MissingCopierTypeSentinel>) {
60 fCopier_ = TRAITS::kDefaultCopier;
63 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
65 requires (not same_as<default_copier_type, MissingCopierTypeSentinel>)
68 if constexpr (not same_as<instance_defined_copier_type, MissingCopierTypeSentinel>) {
69 fCopier_ = TRAITS::kDefaultCopier;
72 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
74 requires (not same_as<default_copier_type, MissingCopierTypeSentinel>)
77 if constexpr (not same_as<instance_defined_copier_type, MissingCopierTypeSentinel>) {
78 fCopier_ = TRAITS::kDefaultCopier;
81 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
83 requires (not same_as<default_copier_type, MissingCopierTypeSentinel>)
84 : fSharedImpl_{TRAITS::kDefaultCopier (from)}
86 if constexpr (not same_as<instance_defined_copier_type, MissingCopierTypeSentinel>) {
87 fCopier_ = TRAITS::kDefaultCopier;
90 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
92 requires (not same_as<default_copier_type, MissingCopierTypeSentinel>)
93 : fSharedImpl_{move (from)}
95 if constexpr (not same_as<instance_defined_copier_type, MissingCopierTypeSentinel>) {
96 fCopier_ = TRAITS::kDefaultCopier;
99 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
101 requires (not same_as<instance_defined_copier_type, MissingCopierTypeSentinel>)
106 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
108 requires (not same_as<instance_defined_copier_type, MissingCopierTypeSentinel>)
110 , fSharedImpl_{copier (from)}
113 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
115 requires (not same_as<instance_defined_copier_type, MissingCopierTypeSentinel>)
116 : fCopier_{move (copier)}
117 , fSharedImpl_{move (from)}
121 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
122 inline SharedByValue<T, TRAITS>& SharedByValue<T, TRAITS>::operator= (
const shared_ptr_type& from)
noexcept
126 if (fSharedImpl_ != from) [[likely]] {
131 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
134 return static_cast<bool> (fSharedImpl_);
136 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
141 if (fSharedImpl_ != from) [[likely]] {
142 fSharedImpl_ = move (from);
146 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
149 return fSharedImpl_.get ();
151 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
156 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
159 if constexpr (same_as<instance_defined_copier_type, MissingCopierTypeSentinel>) {
160 return rwget_ptr (TRAITS::kDefaultCopier);
163 return rwget_ptr (fCopier_);
166 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
167 template <
typename COPIER>
170 if (fSharedImpl_ !=
nullptr) [[likely]] {
171 auto result = forward<COPIER> (copier) (fSharedImpl_);
172 Assert (result.use_count () == 1);
177 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
180 if constexpr (same_as<instance_defined_copier_type, MissingCopierTypeSentinel>) {
181 return rwget (TRAITS::kDefaultCopier);
184 return rwget (fCopier_);
187 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
188 template <
typename COPIER>
197 if (fSharedImpl_ !=
nullptr) [[likely]] {
198 AssureNOrFewerReferences (forward<COPIER> (copier));
199 Ensure (fSharedImpl_.use_count () == 1);
200 return fSharedImpl_.get ();
204 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
207 return fSharedImpl_.get ();
209 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
214 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
217 const element_type* ptr = cget ();
221 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
224 return fSharedImpl_ ==
nullptr;
226 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
227 inline auto SharedByValue<T, TRAITS>::GetElementCopier () const -> element_copier_type
229 if constexpr (same_as<instance_defined_copier_type, MissingCopierTypeSentinel>) {
230 return TRAITS::kDefaultCopier;
236 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
239 switch (fSharedImpl_.use_count ()) {
241 Assert (fSharedImpl_.get () ==
nullptr);
242 return SharingState::eNull;
244 Assert (fSharedImpl_.get () !=
nullptr);
245 return SharingState::eSolo;
247 Assert (fSharedImpl_.get () !=
nullptr);
253 return SharingState::eShared;
256 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
259 return fSharedImpl_.use_count () == 1;
261 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
264 return fSharedImpl_.use_count ();
266 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
267 template <
typename COPIER>
271 if (
static_cast<unsigned int> (fSharedImpl_.use_count ()) > n) [[unlikely]] {
272 BreakReferences_ (forward<COPIER> (copier));
273 Assert (this->use_count () == 1);
276 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
279 if constexpr (same_as<instance_defined_copier_type, MissingCopierTypeSentinel>) {
280 AssureNOrFewerReferences (TRAITS::kDefaultCopier, n);
283 AssureNOrFewerReferences (fCopier_, n);
286 template <
typename T, SharedByValueSupport::ITraits<T> TRAITS>
287 template <
typename COPIER>
288 void SharedByValue<T, TRAITS>::BreakReferences_ (COPIER&& copier)
312 fSharedImpl_ = forward<COPIER> (copier) (*fSharedImpl_);
#define RequireNotNull(p)
SharedByValue is a utility class to implement Copy-On-Write (aka COW) - sort of halfway between uniqu...
nonvirtual bool unique() const
nonvirtual unsigned int use_count() const
nonvirtual shared_ptr_type cget_ptr() const
access te underlying shared_ptr stored in the SharedByValue. This should be treated as readonly and o...
nonvirtual shared_ptr_type rwget_ptr()
forced copy of the underlying shared_ptr data
nonvirtual const element_type * cget() const noexcept
nonvirtual const element_type * operator->() const
nonvirtual element_type * rwget()
nonvirtual void AssureNOrFewerReferences(COPIER &&copier, unsigned int n=1u)
nonvirtual SharingState GetSharingState() const
nonvirtual const element_type & operator*() const
DefaultTraits_NoInstanceCopier< T, COPIER_INSTANCE > DefaultTraits
DefaultTraits is a utility struct to provide parameterized support for SharedByValue<>
SHARED_IMPL DefaultValueCopier(const T &t)
DefaultValueCopier is the default value-copier for copying SharedByValue (thing that clones a shared_...
DefaultValueCopier_FunctionObject same as DefaultValueCopier, but as a function object.