1 #ifndef EBML_NG_FLEXPTR_H 2 #define EBML_NG_FLEXPTR_H 39 void _init_ptr(
const ptr<T>& p) {
40 new (&this->
sp) ptr<T>(p);
44 void _init_ptr(ptr<T>&& p) {
45 new (&this->
sp) ptr<T>(std::move(p));
49 void _init_ptr(
const wptr<T>& p) {
50 new (&this->
wp) wptr<T>(p);
54 void _init_ptr(wptr<T>&& p) {
55 new (&this->
wp) wptr<T>(std::move(p));
59 void _init_ptr(
const ptr<const T>& p) {
60 new (&this->
const_sp) ptr<const T>(p);
64 void _init_ptr(ptr<const T>&& p) {
65 new (&this->
const_sp) ptr<const T>(std::move(p));
69 void _init_ptr(
const wptr<const T>& p) {
70 new (&this->
const_wp) wptr<const T>(p);
74 void _init_ptr(wptr<const T>&& p) {
75 new (&this->
const_wp) wptr<const T>(std::move(p));
82 if (this->_flags == 0x00) {
95 void _set_ptr(
const ptr<T>& p) {
99 void _set_ptr(ptr<T>&& p) {
100 this->
sp = std::move(p);
103 void _set_ptr(
const ptr<const T>& p) {
107 void _set_ptr(ptr<const T>&& p) {
111 void _set_ptr(
const wptr<T>& p) {
115 void _set_ptr(wptr<T>&& p) {
116 this->
wp = std::move(p);
119 void _set_ptr(
const wptr<const T>& p) {
123 void _set_ptr(wptr<const T>&& p) {
129 void _destroy_ptr() {
130 unsigned char flags = this->_flags & 0x03;
143 using p = ptr<const T>;
148 using p = wptr<const T>;
174 unsigned char flags = other._flags & 0x03;
177 this->_init_ptr(other.
sp);
181 this->_init_ptr(other.
wp);
203 unsigned char flags = other._flags & 0x03;
206 this->_init_ptr(std::move(other.sp));
210 this->_init_ptr(std::move(other.wp));
215 this->_init_ptr(std::move(other.const_sp));
220 this->_init_ptr(std::move(other.const_wp));
242 this->_init_ptr(std::move(p));
260 this->_init_ptr(std::move(p));
278 this->_init_ptr(std::move(p));
296 this->_init_ptr(std::move(p));
305 this->_destroy_ptr();
317 if (
this == &other) {
321 if (this->_flags == other._flags) {
322 switch (this->_flags) {
324 this->_set_ptr(other.
sp);
328 this->_set_ptr(other.
wp);
343 this->_destroy_ptr();
344 switch (other._flags) {
346 this->_init_ptr(other.
sp);
350 this->_init_ptr(other.
wp);
375 if (
this == &other) {
379 if (this->_flags == other._flags) {
380 switch (this->_flags) {
382 this->_set_ptr(std::move(other.sp));
386 this->_set_ptr(std::move(other.wp));
391 this->_set_ptr(std::move(other.const_sp));
396 this->_set_ptr(std::move(other.const_wp));
404 this->_destroy_ptr();
405 switch (other._flags) {
407 this->_init_ptr(std::move(other.sp));
411 this->_init_ptr(std::move(other.wp));
416 this->_init_ptr(std::move(other.const_sp));
421 this->_init_ptr(std::move(other.const_wp));
437 if (this->_flags == 0x00) {
438 this->_set_ptr(other);
441 this->_destroy_ptr();
442 this->_init_ptr(other);
453 if (this->_flags == 0x00) {
454 this->_set_ptr(std::move(other));
457 this->_destroy_ptr();
458 this->_init_ptr(std::move(other));
469 if (this->_flags == 0x02) {
470 this->_set_ptr(other);
473 this->_destroy_ptr();
474 this->_init_ptr(other);
485 if (this->_flags == 0x01) {
486 this->_set_ptr(std::move(other));
489 this->_destroy_ptr();
490 this->_init_ptr(std::move(other));
501 if (this->_flags == 0x02) {
502 this->_set_ptr(other);
505 this->_destroy_ptr();
506 this->_init_ptr(other);
517 if (this->_flags == 0x02) {
518 this->_set_ptr(std::move(other));
521 this->_destroy_ptr();
522 this->_init_ptr(std::move(other));
533 if (this->_flags == 0x03) {
534 this->_set_ptr(other);
537 this->_destroy_ptr();
538 this->_init_ptr(other);
549 if (this->_flags == 0x03) {
550 this->_set_ptr(std::move(other));
553 this->_destroy_ptr();
554 this->_init_ptr(std::move(other));
576 return (this->_flags & 0x02);
585 return (this->_flags & 0x01);
598 throw std::runtime_error(
"non-const access to const object");
600 unsigned char flags = this->_flags & 0x03;
606 return this->
wp.lock();
609 throw std::runtime_error(
"this should never happen");
621 unsigned char flags = this->_flags & 0x03;
636 throw std::runtime_error(
"this should never happen");
648 unsigned char flags = this->_flags & 0x03;
651 return this->
sp !=
nullptr;
654 return not this->
wp.expired();
660 return not this->
const_wp.expired();
663 throw std::runtime_error(
"this should never happen");
flexible_ptr(ptr< T > &&p)
Constructs a flexible_ptr from a movable strong pointer to T.
Definition: flexptr.h:241
flexible_ptr(const ptr< T > &p)
Constructs a flexible_ptr from a strong pointer to T.
Definition: flexptr.h:232
~flexible_ptr()
Destructor.
Definition: flexptr.h:304
bool is_weak() const
Checks whether the stored pointer is a weak pointer.
Definition: flexptr.h:584
flexible_ptr< T > & operator=(wptr< const T > &&other)
Assignment operator from a movable weak pointer of type wptr<const T>.
Definition: flexptr.h:548
flexible_ptr(ptr< const T > &&p)
Constructs a flexible_ptr from a movable strong pointer to a const T.
Definition: flexptr.h:259
ptr< const T > const_sp
Stored strong pointer for const T.
Definition: flexptr.h:28
flexible_ptr(const flexible_ptr< T > &other)
Copy constructor.
Definition: flexptr.h:173
wptr< T > wp
Stored weak pointer for mutable T.
Definition: flexptr.h:29
flexible_ptr(wptr< T > &&p)
Constructs a flexible_ptr from a movable weak pointer to T.
Definition: flexptr.h:277
flexible_ptr(const ptr< const T > &p)
Constructs a flexible_ptr from a strong pointer to a const T.
Definition: flexptr.h:250
A drop-in replacement for std::weak_ptr tailored for EBML objects.
Definition: ptrs.h:30
flexible_ptr< T > & operator=(const ptr< T > &other)
Assignment operator from a strong pointer of type ptr<T>.
Definition: flexptr.h:436
ptr< const T > cget() const
Retrieves the stored strong pointer to a const T.
Definition: flexptr.h:620
flexible_ptr< T > & operator=(const flexible_ptr< T > &other)
Copy assignment operator.
Definition: flexptr.h:316
Definition: basictypes.h:40
flexible_ptr()
Default constructor. Initializes with an empty strong pointer.
Definition: flexptr.h:157
A drop-in replacement for std::shared_ptr tailored for EBML objects.
Definition: ptrs.h:27
flexible_ptr(const wptr< const T > &p)
Constructs a flexible_ptr from a weak pointer to a const T.
Definition: flexptr.h:286
flexible_ptr< T > & operator=(ptr< const T > &&other)
Assignment operator from a movable strong pointer of type ptr<const T>.
Definition: flexptr.h:516
wptr< const T > const_wp
Stored weak pointer for const T.
Definition: flexptr.h:30
flexible_ptr< T > & operator=(wptr< T > &&other)
Assignment operator from a movable weak pointer of type wptr<T>.
Definition: flexptr.h:484
flexible_ptr< T > & operator=(flexible_ptr< T > &&other)
Move assignment operator.
Definition: flexptr.h:374
flexible_ptr< T > & operator=(const ptr< const T > &other)
Assignment operator from a strong pointer of type ptr<const T>.
Definition: flexptr.h:500
A flexible pointer that can store either a strong (shared) or weak pointer.
Definition: flexptr.h:21
flexible_ptr< T > & operator=(const wptr< const T > &other)
Assignment operator from a weak pointer of type wptr<const T>.
Definition: flexptr.h:532
flexible_ptr< T > & operator=(ptr< T > &&other)
Assignment operator from a movable strong pointer of type ptr<T>.
Definition: flexptr.h:452
ptr< T > sp
Stored strong pointer for mutable T.
Definition: flexptr.h:27
flexible_ptr< T > & operator=(const std::nullptr_t &)
Assignment operator from nullptr.
Definition: flexptr.h:565
bool is_const() const
Checks whether the stored pointer is pointing to a const object.
Definition: flexptr.h:575
flexible_ptr(const std::nullptr_t &)
Constructs a flexible_ptr that is null.
Definition: flexptr.h:162
flexible_ptr< T > & operator=(const wptr< T > &other)
Assignment operator from a weak pointer of type wptr<T>.
Definition: flexptr.h:468
flexible_ptr(wptr< const T > &&p)
Constructs a flexible_ptr from a movable weak pointer to a const T.
Definition: flexptr.h:295
flexible_ptr(const wptr< T > &p)
Constructs a flexible_ptr from a weak pointer to T.
Definition: flexptr.h:268
ebml_shared_ptr< T > ebml_const_pointer_cast(const ebml_shared_ptr< U > &)
Definition: ptrs.h:777
bool notnull() const
Checks whether the stored pointer is not null.
Definition: flexptr.h:647
flexible_ptr(flexible_ptr< T > &&other)
Move constructor.
Definition: flexptr.h:202