10#include <lagrange/internal/smart_ptr/control_block.h>
11#include <lagrange/internal/weak_ptr.h>
12#include <lagrange/utils/assert.h>
33template <typename T, bool = std::is_array<T>::value,
bool = std::is_void<T>::value>
37 using element_type = T;
54 element_type* _get() const noexcept {
return static_cast<const shared_ptr<T>*
>(
this)->get(); }
64 using element_type =
typename std::remove_extent<T>::type;
67 element_type*
operator[](std::ptrdiff_t i)
const noexcept
70 static_assert(!std::extent<T>::value || i < std::extent<T>::value);
75 element_type* _get() const noexcept {
return static_cast<const shared_ptr<T>*
>(
this)->get(); }
85 using element_type = T;
95 element_type* _get() const noexcept {
return static_cast<const shared_ptr<T>*
>(
this)->get(); }
113 template <
typename U>
114 friend class shared_ptr;
116 template <
typename U>
117 friend class weak_ptr;
119 using element_type =
typename shared_ptr_access<T>::element_type;
120 using weak_type = weak_ptr<T>;
140 template <
typename U>
149 template <
typename U,
typename D,
typename A>
155 template <
typename D,
typename A>
161 template <
typename U>
164 , _control_block{sp._control_block}
166 if (_control_block) _control_block->inc_ref();
173 , _control_block{sp._control_block}
175 if (_control_block) _control_block->inc_ref();
180 template <
typename U>
183 , _control_block{sp._control_block}
185 if (_control_block) _control_block->inc_ref();
192 : _ptr{std::move(sp._ptr)}
193 , _control_block{std::move(sp._control_block)}
196 sp._control_block =
nullptr;
202 template <
typename U>
205 , _control_block{sp._control_block}
208 sp._control_block =
nullptr;
213 template <
typename U>
216 , _control_block{wp._control_block}
219 throw std::bad_weak_ptr{};
221 _control_block->inc_ref();
229 if (_control_block) _control_block->dec_ref();
237 shared_ptr{sp}.swap(*
this);
242 template <
typename U>
245 shared_ptr{sp}.swap(*
this);
252 shared_ptr{std::move(sp)}.swap(*
this);
257 template <
typename U>
260 shared_ptr{std::move(sp)}.swap(*
this);
267 void swap(shared_ptr& sp)
noexcept
271 swap(_control_block, sp._control_block);
275 void reset() noexcept { shared_ptr{}.swap(*
this); }
278 template <
typename U>
281 shared_ptr{p}.swap(*
this);
286 template <
typename U,
typename D>
289 shared_ptr{p, d}.swap(*
this);
294 template <
typename U,
typename D,
typename A>
295 void reset(U* p, D d, A a) =
delete;
300 element_type*
get() const noexcept {
return _ptr; }
315 long use_count() const noexcept {
return (_control_block) ? _control_block->use_count() : 0; }
319 bool unique() const noexcept {
return (_control_block) ? _control_block->unique() :
false; }
322 explicit operator bool() const noexcept {
return (_ptr) ? true :
false; }
332template <
typename T,
typename...
Args>
338template <
typename T,
typename A,
typename...
Args>
339inline shared_ptr<T> allocate_shared(
const A& a,
Args&&... args) =
delete;
344template <
typename T,
typename U>
347 return sp1.
get() == sp2.
get();
351inline bool operator==(
const shared_ptr<T>& sp, std::nullptr_t)
noexcept
357inline bool operator==(std::nullptr_t,
const shared_ptr<T>& sp)
noexcept
363template <
typename T,
typename U>
366 return sp1.
get() != sp2.
get();
370inline bool operator!=(
const shared_ptr<T>& sp, std::nullptr_t)
noexcept
376inline bool operator!=(std::nullptr_t,
const shared_ptr<T>& sp)
noexcept
392template <
typename T,
typename U>
393inline shared_ptr<T> static_pointer_cast(
const shared_ptr<U>& sp)
noexcept
395 using _Sp = shared_ptr<T>;
396 return _Sp(sp,
static_cast<typename _Sp::element_type*
>(sp.get()));
399template <
typename T,
typename U>
400inline shared_ptr<T> const_pointer_cast(
const shared_ptr<U>& sp)
noexcept
402 using _Sp = shared_ptr<T>;
403 return _Sp(sp,
const_cast<typename _Sp::element_type*
>(sp.get()));
406template <
typename T,
typename U>
407inline shared_ptr<T> dynamic_pointer_cast(
const shared_ptr<U>& sp)
noexcept
409 using _Sp = shared_ptr<T>;
410 if (
auto* _p =
dynamic_cast<typename _Sp::element_type*
>(sp.get()))
return _Sp(sp, _p);
415template <
typename T,
typename U>
416inline shared_ptr<T> reinterpret_pointer_cast(
const shared_ptr<U>& sp)
noexcept
418 using _Sp = shared_ptr<T>;
419 return _Sp(sp,
reinterpret_cast<typename _Sp::element_type*
>(sp.get()));
424template <
class E,
class T,
class Y>
425inline 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:88
element_type * operator[](std::ptrdiff_t i) const noexcept
Index operator, dereferencing operators are not provided.
Definition shared_ptr.h:67
Definition shared_ptr.h:35
element_type & operator*() const noexcept
Dereferences pointer to the managed object.
Definition shared_ptr.h:40
element_type * operator->() const noexcept
Dereferences pointer to the managed object.
Definition shared_ptr.h:47
NOT implemented: custom allocator support.
Definition shared_ptr.h:111
bool unique() const noexcept
Checks if use_count == 1.
Definition shared_ptr.h:319
void reset() noexcept
Resets *this to empty.
Definition shared_ptr.h:275
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:181
void reset(U *p)
Resets *this with p as the pointer to the managed object.
Definition shared_ptr.h:279
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:191
shared_ptr & operator=(const shared_ptr< U > &sp) noexcept
Copy assignment.
Definition shared_ptr.h:243
shared_ptr & operator=(shared_ptr &&sp) noexcept
Move assignment.
Definition shared_ptr.h:250
shared_ptr & operator=(const shared_ptr &sp) noexcept
Copy assignment.
Definition shared_ptr.h:235
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:315
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:171
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:162
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:203
element_type * get() const noexcept
Gets the stored pointer.
Definition shared_ptr.h:300
shared_ptr & operator=(shared_ptr< U > &&sp) noexcept
Move assignment.
Definition shared_ptr.h:258
constexpr shared_ptr() noexcept
Default constructor, creates a shared_ptr with no managed object Postconditions: use_count() == 0 && ...
Definition shared_ptr.h:126
void swap(shared_ptr &sp) noexcept
Exchanges the contents of *this and sp.
Definition shared_ptr.h:267
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:133
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:214
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:287
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:141
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.
bool expired() const noexcept
Checks if use_count == 0.
Definition weak_ptr.h:122
#define la_debug_assert(...)
Debug assertion check.
Definition assert.h:194
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:345
void swap(shared_ptr< T > &sp1, shared_ptr< T > &sp2)
Swaps with another shared_ptr.
Definition shared_ptr.h:385
shared_ptr< T > make_shared(Args &&... args)
Creates a shared_ptr that manages a new object.
Definition shared_ptr.h:333
bool operator!=(const shared_ptr< T > &sp1, const shared_ptr< U > &sp2)
Operator != overloading.
Definition shared_ptr.h:364
Definition project.cpp:27