10#include <lagrange/internal/smart_ptr/control_block.h>
11#include <lagrange/internal/weak_ptr.h>
32template <typename T, bool = std::is_array<T>::value,
bool = std::is_void<T>::value>
36 using element_type = T;
41 assert(_get() !=
nullptr);
48 assert(_get() !=
nullptr);
53 element_type* _get() const noexcept {
return static_cast<const shared_ptr<T>*
>(
this)->get(); }
63 using element_type =
typename std::remove_extent<T>::type;
66 element_type*
operator[](std::ptrdiff_t i)
const noexcept
68 assert(_get() !=
nullptr);
69 static_assert(!std::extent<T>::value || i < std::extent<T>::value);
74 element_type* _get() const noexcept {
return static_cast<const shared_ptr<T>*
>(
this)->get(); }
84 using element_type = T;
89 assert(_get() !=
nullptr);
94 element_type* _get() const noexcept {
return static_cast<const shared_ptr<T>*
>(
this)->get(); }
112 template <
typename U>
115 template <
typename U>
118 using element_type =
typename shared_ptr_access<T>::element_type;
139 template <
typename U>
148 template <
typename U,
typename D,
typename A>
154 template <
typename D,
typename A>
160 template <
typename U>
163 , _control_block{sp._control_block}
165 if (_control_block) _control_block->inc_ref();
172 , _control_block{sp._control_block}
174 if (_control_block) _control_block->inc_ref();
179 template <
typename U>
182 , _control_block{sp._control_block}
184 if (_control_block) _control_block->inc_ref();
191 : _ptr{std::move(sp._ptr)}
192 , _control_block{std::move(sp._control_block)}
195 sp._control_block =
nullptr;
201 template <
typename U>
204 , _control_block{sp._control_block}
207 sp._control_block =
nullptr;
212 template <
typename U>
215 , _control_block{wp._control_block}
218 throw std::bad_weak_ptr{};
220 _control_block->inc_ref();
228 if (_control_block) _control_block->dec_ref();
241 template <
typename U>
256 template <
typename U>
270 swap(_control_block, sp._control_block);
277 template <
typename U>
285 template <
typename U,
typename D>
293 template <
typename U,
typename D,
typename A>
294 void reset(U* p, D d, A a) =
delete;
299 element_type*
get() const noexcept {
return _ptr; }
314 long use_count() const noexcept {
return (_control_block) ? _control_block->use_count() : 0; }
318 bool unique() const noexcept {
return (_control_block) ? _control_block->unique() :
false; }
321 explicit operator bool() const noexcept {
return (_ptr) ? true :
false; }
331template <
typename T,
typename...
Args>
337template <
typename T,
typename A,
typename...
Args>
338inline shared_ptr<T> allocate_shared(
const A& a,
Args&&... args) =
delete;
343template <
typename T,
typename U>
346 return sp1.
get() == sp2.
get();
350inline bool operator==(
const shared_ptr<T>& sp, std::nullptr_t)
noexcept
356inline bool operator==(std::nullptr_t,
const shared_ptr<T>& sp)
noexcept
362template <
typename T,
typename U>
365 return sp1.
get() != sp2.
get();
369inline bool operator!=(
const shared_ptr<T>& sp, std::nullptr_t)
noexcept
375inline bool operator!=(std::nullptr_t,
const shared_ptr<T>& sp)
noexcept
391template <
typename T,
typename U>
392inline shared_ptr<T> static_pointer_cast(
const shared_ptr<U>& sp)
noexcept
394 using _Sp = shared_ptr<T>;
395 return _Sp(sp,
static_cast<typename _Sp::element_type*
>(sp.get()));
398template <
typename T,
typename U>
399inline shared_ptr<T> const_pointer_cast(
const shared_ptr<U>& sp)
noexcept
401 using _Sp = shared_ptr<T>;
402 return _Sp(sp,
const_cast<typename _Sp::element_type*
>(sp.get()));
405template <
typename T,
typename U>
406inline shared_ptr<T> dynamic_pointer_cast(
const shared_ptr<U>& sp)
noexcept
408 using _Sp = shared_ptr<T>;
409 if (
auto* _p =
dynamic_cast<typename _Sp::element_type*
>(sp.get()))
return _Sp(sp, _p);
414template <
typename T,
typename U>
415inline shared_ptr<T> reinterpret_pointer_cast(
const shared_ptr<U>& sp)
noexcept
417 using _Sp = shared_ptr<T>;
418 return _Sp(sp,
reinterpret_cast<typename _Sp::element_type*
>(sp.get()));
423template <
class E,
class T,
class Y>
424inline std::basic_ostream<E, T>& operator<<(std::basic_ostream<E, T>& os,
const shared_ptr<Y>& sp)
Definition: control_block.h:24
NOT implemented: custom allocator support.
Definition: control_block.h:52
element_type * operator->() const noexcept
Dereferences pointer to the managed object, operator* is not provided.
Definition: shared_ptr.h:87
element_type * operator[](std::ptrdiff_t i) const noexcept
Index operator, dereferencing operators are not provided.
Definition: shared_ptr.h:66
Definition: shared_ptr.h:34
element_type & operator*() const noexcept
Dereferences pointer to the managed object.
Definition: shared_ptr.h:39
element_type * operator->() const noexcept
Dereferences pointer to the managed object.
Definition: shared_ptr.h:46
NOT implemented: custom allocator support.
Definition: shared_ptr.h:110
bool unique() const noexcept
Checks if use_count == 1.
Definition: shared_ptr.h:318
void reset() noexcept
Resets *this to empty.
Definition: shared_ptr.h:274
shared_ptr(const shared_ptr< U > &sp) noexcept
Copy constructor: shares ownership of the object managed by sp Postconditions: use_count() == sp....
Definition: shared_ptr.h:180
void reset(U *p)
Resets *this with p as the pointer to the managed object.
Definition: shared_ptr.h:278
shared_ptr(U *p, D d, A a)=delete
Constructs a shared_ptr with p as the pointer to the managed object, supplied with custom deleter and...
shared_ptr(shared_ptr &&sp) noexcept
Move constructor: Move-constructs a shared_ptr from sp Postconditions: *this shall contain the old va...
Definition: shared_ptr.h:190
shared_ptr & operator=(const shared_ptr< U > &sp) noexcept
Copy assignment.
Definition: shared_ptr.h:242
shared_ptr & operator=(shared_ptr &&sp) noexcept
Move assignment.
Definition: shared_ptr.h:249
shared_ptr & operator=(const shared_ptr &sp) noexcept
Copy assignment.
Definition: shared_ptr.h:234
shared_ptr(std::nullptr_t p, D d, A a)=delete
Constructs a shared_ptr with no managed object, supplied with custom deleter and allocator Postcondit...
long use_count() const noexcept
Gets use_count.
Definition: shared_ptr.h:314
shared_ptr(const shared_ptr &sp) noexcept
Copy constructor: shares ownership of the object managed by sp Postconditions: use_count() == sp....
Definition: shared_ptr.h:170
shared_ptr(const shared_ptr< U > &sp, T *p) noexcept
Aliasing constructor: constructs a shared_ptr instance that stores p and shares ownership with sp Pos...
Definition: shared_ptr.h:161
shared_ptr(shared_ptr< U > &&sp) noexcept
Move constructor: Move-constructs a shared_ptr from sp Postconditions: *this shall contain the old va...
Definition: shared_ptr.h:202
element_type * get() const noexcept
Gets the stored pointer.
Definition: shared_ptr.h:299
shared_ptr & operator=(shared_ptr< U > &&sp) noexcept
Move assignment.
Definition: shared_ptr.h:257
constexpr shared_ptr() noexcept
Default constructor, creates a shared_ptr with no managed object Postconditions: use_count() == 0 && ...
Definition: shared_ptr.h:125
void swap(shared_ptr &sp) noexcept
Exchanges the contents of *this and sp.
Definition: shared_ptr.h:266
constexpr shared_ptr(std::nullptr_t) noexcept
Constructs a shared_ptr with no managed object Postconditions: use_count() == 0 && get() == 0.
Definition: shared_ptr.h:132
shared_ptr(const weak_ptr< U > &wp)
Constructs a shared_ptr object that shares ownership with wp Postconditions: use_count() == wp....
Definition: shared_ptr.h:213
void reset(U *p, D d)
Resets *this with p as the pointer to the managed object, supplied with custom deleter.
Definition: shared_ptr.h:286
shared_ptr(U *p)
Constructs a shared_ptr with p as the pointer to the managed object Postconditions: use_count() == 1 ...
Definition: shared_ptr.h:140
void reset(U *p, D d, A a)=delete
Resets *this with p as the pointer to the managed object, supplied with custom deleter and allocator.
Definition: weak_ptr.h:26
bool expired() const noexcept
Checks if use_count == 0.
Definition: weak_ptr.h:122
constexpr void swap(function_ref< R(Args...)> &lhs, function_ref< R(Args...)> &rhs) noexcept
Swaps the referred callables of lhs and rhs.
Definition: function_ref.h:114
nullptr_t, size_t, ptrdiff_t basic_ostream bad_weak_ptr extent, remove_extent, is_array,...
Definition: attribute_string_utils.h:21
bool operator==(const shared_ptr< T > &sp1, const shared_ptr< U > &sp2)
Operator == overloading.
Definition: shared_ptr.h:344
void swap(shared_ptr< T > &sp1, shared_ptr< T > &sp2)
Swaps with another shared_ptr.
Definition: shared_ptr.h:384
shared_ptr< T > make_shared(Args &&... args)
Creates a shared_ptr that manages a new object.
Definition: shared_ptr.h:332
bool operator!=(const shared_ptr< T > &sp1, const shared_ptr< U > &sp2)
Operator != overloading.
Definition: shared_ptr.h:363
Definition: project.cpp:27