ORIGINAL
Loading...
Searching...
No Matches
refCntPtr.h
Go to the documentation of this file.
1#ifndef REFCNTPTR_H
2#define REFCNTPTR_H
3
4#include "autoPtr.h"
5#include "deleter.h"
6#include "refCntPtr.h"
7
25namespace original{
38 template<typename TYPE, typename DERIVED, typename DELETER>
39 class refCntPtr : public autoPtr<TYPE, DERIVED, DELETER>{
40 template<typename, typename, typename> friend class refCntPtr;
41 protected:
47 explicit refCntPtr(TYPE* p = std::nullptr_t{});
48 public:
49 using autoPtr<TYPE, DERIVED, DELETER>::operator==;
50 using autoPtr<TYPE, DERIVED, DELETER>::operator!=;
51
58 template<typename O_DERIVED>
60
67 template<typename O_DERIVED>
69
74 [[nodiscard]] std::string className() const override;
75
81 [[nodiscard]] std::string toString(bool enter) const override;
82
86 ~refCntPtr() override = default;
87 };
88
89 template<typename TYPE, typename DELETER = deleter<TYPE>>
90 class strongPtr;
91
92 template<typename TYPE, typename DELETER = deleter<TYPE>>
93 class weakPtr;
94
107 template<typename TYPE, typename DELETER>
108 class strongPtr final : public refCntPtr<TYPE, strongPtr<TYPE, DELETER>, DELETER>{
109 template<typename, typename> friend class strongPtr;
110 template<typename, typename> friend class weakPtr;
111
117 strongPtr(refCountBase* cnt, TYPE* alias);
118 public:
124 explicit strongPtr(TYPE* p = std::nullptr_t{});
125
130 strongPtr(const strongPtr& other);
131
138
144 strongPtr(strongPtr&& other) noexcept;
145
153 template<typename U, typename DEL = DELETER::template rebound_deleter<U>>
155
163 template<typename U, typename DEL = DELETER::template rebound_deleter<const U>>
165
173 template<typename U, typename DEL = DELETER::template rebound_deleter<U>>
175
183 template<typename U, typename DEL = DELETER::template rebound_deleter<const U>>
185
193 template<typename U, typename DEL = DELETER::template rebound_deleter<U>>
195
204 void reset() noexcept;
205
212 strongPtr& operator=(strongPtr&& other) noexcept;
213
218 [[nodiscard]] std::string className() const override;
219
223 ~strongPtr() override;
224
225 template <typename T, typename DEL, typename... Args>
226 friend strongPtr<T, DEL> makeStrongPtr(Args&&... args);
227
228 template <typename T, typename DEL, typename... Args>
229 friend strongPtr<T, DEL> makeStrongPtrArray(u_integer size, Args&&... args);
230 };
231
243 template<typename TYPE, typename DELETER>
244 class weakPtr final : public refCntPtr<TYPE, weakPtr<TYPE, DELETER>, DELETER>{
245 template<typename, typename> friend class weakPtr;
246 template<typename, typename> friend class strongPtr;
247
253 weakPtr(refCountBase* cnt, TYPE* alias);
254 public:
255
259 explicit weakPtr();
260
269 explicit weakPtr(const strongPtr<TYPE, DELETER>& other);
270
282
290 weakPtr(const weakPtr& other);
291
302 weakPtr& operator=(const weakPtr& other);
303
309 weakPtr(weakPtr&& other) noexcept;
310
317 weakPtr& operator=(weakPtr&& other) noexcept;
318
326 template<typename U, typename DEL = DELETER::template rebound_deleter<U>>
328
336 template<typename U, typename DEL = DELETER::template rebound_deleter<const U>>
338
346 template<typename U, typename DEL = DELETER::template rebound_deleter<U>>
348
356 template<typename U, typename DEL = DELETER::template rebound_deleter<const U>>
358
366 template<typename U, typename DEL = DELETER::template rebound_deleter<U>>
368
378 strongPtr<TYPE, DELETER> lock() const;
379
385 const TYPE& operator*() const override;
386
392 const TYPE* operator->() const override;
393
400 const TYPE& operator[](u_integer index) const override;
401
407 TYPE& operator*() override;
408
414 TYPE* operator->() override;
415
422 TYPE& operator[](u_integer index) override;
423
428 [[nodiscard]] std::string className() const override;
429
436 ~weakPtr() override;
437 };
438
454 template <typename T, typename DEL = deleter<T>, typename... Args>
455 strongPtr<T, DEL> makeStrongPtr(Args&&... args);
456
473 template <typename T, typename DEL = deleter<T[]>, typename... Args>
474 strongPtr<T, DEL> makeStrongPtrArray(u_integer size, Args&&... args);
475
476 // ----------------- Definitions of refCntPtr.h -----------------
477
478
479 template<typename TYPE, typename DERIVED, typename DELETER>
481 : autoPtr<TYPE, DERIVED, DELETER>::autoPtr(p) {}
482
483 template<typename TYPE, typename DERIVED, typename DELETER>
484 template<typename O_DERIVED>
486 return this->get() == other.get();
487 }
488
489 template<typename TYPE, typename DERIVED, typename DELETER>
490 template<typename O_DERIVED>
492 return this->get() != other.get();
493 }
494
495 template<typename TYPE, typename DERIVED, typename DELETER>
497 return "refCntPtr";
498 }
499
500 template<typename TYPE, typename DERIVED, typename DELETER>
501 std::string refCntPtr<TYPE, DERIVED, DELETER>::toString(const bool enter) const {
502 std::stringstream ss;
503 ss << this->className() << "(";
504 ss << printable::formatString(this->get()) << ", ";
505 ss << "strong ref: " << this->strongRefs() << ", " << "weak ref: " << this->weakRefs();
506 ss << ")";
507 if (enter)
508 ss << "\n";
509 return ss.str();
510 }
511
512 template <typename TYPE, typename DELETER>
514 {
515 this->removeStrongRef();
516 this->clean();
517 this->ref_count = cnt;
518 this->alias_ptr = alias;
519 this->addStrongRef();
520 }
521
522 template<typename TYPE, typename DELETER>
524 : refCntPtr<TYPE, strongPtr, DELETER>(p) {
525 this->addStrongRef();
526 }
527
528 template<typename TYPE, typename DELETER>
530 this->operator=(other);
531 }
532
533 template<typename TYPE, typename DELETER>
535 if (this == &other || *this == other)
536 return *this;
537
538 this->removeStrongRef();
539 this->clean();
540 this->ref_count = *other.ref_count;
541 this->addStrongRef();
542 this->alias_ptr = other.alias_ptr;
543 return *this;
544 }
545
546 template<typename TYPE, typename DELETER>
548 this->operator=(std::move(other));
549 }
550
551 template <typename TYPE, typename DELETER>
552 template <typename U, typename DEL>
554 {
556 strongPtr<U, DEL> res{*this->ref_count, static_cast<U*>(this->get())};
557 return res;
558 }
559
560 template <typename TYPE, typename DELETER>
561 template <typename U, typename DEL>
563 {
565 strongPtr<const U, DEL> res{*this->ref_count, static_cast<const U*>(this->get())};
566 return res;
567 }
568
569 template <typename TYPE, typename DELETER>
570 template <typename U, typename DEL>
572 {
573 auto alias = dynamic_cast<U*>(this->get());
574 if (alias == nullptr) {
575 return strongPtr<U, DEL>{};
576 }
577 return strongPtr<U, DEL>{*this->ref_count, alias};
578 }
579
580 template <typename TYPE, typename DELETER>
581 template <typename U, typename DEL>
583 {
584 auto alias = dynamic_cast<const U*>(this->get());
585 if (alias == nullptr) {
587 }
588 return strongPtr<const U, DEL>{*this->ref_count, alias};
589 }
590
591 template <typename TYPE, typename DELETER>
592 template <typename U, typename DEL>
594 {
595 auto alias = const_cast<U*>(this->get());
596 return strongPtr<U, DEL>{*this->ref_count, alias};
597 }
598
599 template<typename TYPE, typename DELETER>
601 this->removeStrongRef();
602 this->clean();
604 this->addStrongRef();
605 this->alias_ptr = nullptr;
606 }
607
608 template<typename TYPE, typename DELETER>
610 if (this == &other || *this == other)
611 return *this;
612
613 this->removeStrongRef();
614 this->clean();
615 this->ref_count = *other.ref_count;
617 other.addStrongRef();
618 this->alias_ptr = other.alias_ptr;
619 return *this;
620 }
621
622 template<typename TYPE, typename DELETER>
624 return "strongPtr";
625 }
626
627 template<typename TYPE, typename DELETER>
629 this->removeStrongRef();
630 }
631
632 template<typename T, typename DEL, typename ... Args>
634 return strongPtr<T, DEL>(new T(std::forward<Args>(args)...));
635 }
636
637 template<typename T, typename DEL, typename ... Args>
638 strongPtr<T, DEL> makeStrongPtrArray(const u_integer size, Args &&...args) {
639 auto strong_ptr = strongPtr<T, DEL>(new T[size]);
640 for (u_integer i = 0; i < size; i++)
641 {
642 strong_ptr[i] = T(std::forward<Args>(args)...);
643 }
644 return strong_ptr;
645 }
646
647 template <typename TYPE, typename DELETER>
649 {
650 this->removeWeakRef();
651 this->clean();
652 this->ref_count = cnt;
653 this->alias_ptr = alias;
654 this->addWeakRef();
655 }
656
657 template<typename TYPE, typename DELETER>
659 : refCntPtr<TYPE, weakPtr, DELETER>() {
660 this->addWeakRef();
661 }
662
663 template<typename TYPE, typename DELETER>
665 : weakPtr() {
666 this->operator=(other);
667 }
668
669 template<typename TYPE, typename DELETER>
671 if (*this == other)
672 return *this;
673
674 this->removeWeakRef();
675 this->clean();
676 this->ref_count = *other.ref_count;
677 this->addWeakRef();
678 this->alias_ptr = other.alias_ptr;
679 return *this;
680 }
681
682 template<typename TYPE, typename DELETER>
684 : weakPtr() {
685 this->operator=(other);
686 }
687
688 template<typename TYPE, typename DELETER>
690 if (this == &other || *this == other)
691 return *this;
692
693 this->removeWeakRef();
694 this->clean();
695 this->ref_count = *other.ref_count;
696 this->addWeakRef();
697 this->alias_ptr = other.alias_ptr;
698 return *this;
699 }
700
701 template<typename TYPE, typename DELETER>
703 this->operator=(std::move(other));
704 }
705
706 template<typename TYPE, typename DELETER>
708 if (this == &other || *this == other)
709 return *this;
710
711 this->removeWeakRef();
712 this->clean();
713 this->ref_count = *other.ref_count;
715 other.addWeakRef();
716 this->alias_ptr = other.alias_ptr;
717 return *this;
718 }
719
720 template <typename TYPE, typename DELETER>
721 template <typename U, typename DEL>
723 {
725 weakPtr<U, DEL> res{*this->ref_count, static_cast<U*>(this->get())};
726 return res;
727 }
728
729 template <typename TYPE, typename DELETER>
730 template <typename U, typename DEL>
732 {
734 weakPtr<const U, DEL> res{*this->ref_count, static_cast<const U*>(this->get())};
735 return res;
736 }
737
738 template <typename TYPE, typename DELETER>
739 template <typename U, typename DEL>
741 {
742 auto alias = dynamic_cast<U*>(this->get());
743 if (alias == nullptr) {
744 return weakPtr<U, DEL>{};
745 }
746 return weakPtr<U, DEL>{*this->ref_count, alias};
747 }
748
749 template <typename TYPE, typename DELETER>
750 template <typename U, typename DEL>
752 {
753 auto alias = dynamic_cast<const U*>(this->get());
754 if (alias == nullptr) {
755 return weakPtr<const U, DEL>{};
756 }
757 return weakPtr<const U, DEL>{*this->ref_count, alias};
758 }
759
760 template <typename TYPE, typename DELETER>
761 template <typename U, typename DEL>
763 {
764 auto alias = const_cast<U*>(this->get());
765 return weakPtr<U, DEL>{*this->ref_count, alias};
766 }
767
768 template<typename TYPE, typename DELETER>
770 strongPtr<TYPE, DELETER> strong_ptr;
771 if (!this->expired()){
772 strong_ptr.removeStrongRef();
773 strong_ptr.clean();
774 strong_ptr.ref_count = *this->ref_count;
775 strong_ptr.addStrongRef();
776 strong_ptr.alias_ptr = this->alias_ptr;
777 }
778 return strong_ptr;
779 }
780
781 template<typename TYPE, typename DELETER>
783 return this->lock().operator*();
784 }
785
786 template<typename TYPE, typename DELETER>
788 return this->lock().operator->();
789 }
790
791 template<typename TYPE, typename DELETER>
793 return this->lock().operator[](index);
794 }
795
796 template<typename TYPE, typename DELETER>
798 return this->lock().operator*();
799 }
800
801 template<typename TYPE, typename DELETER>
803 return this->lock().operator->();
804 }
805
806 template<typename TYPE, typename DELETER>
808 return this->lock().operator[](index);
809 }
810
811 template<typename TYPE, typename DELETER>
813 return "weakPtr";
814 }
815
816 template<typename TYPE, typename DELETER>
818 this->removeWeakRef();
819 }
820}
821
822#endif //REFCNTPTR_H
Base class for reference-counted smart pointers.
Base smart pointer with reference counting.
Definition autoPtr.h:57
void addWeakRef() const
Increment weak reference count.
Definition autoPtr.h:434
static refCount< TYPE, DELETER > * newRefCount(TYPE *p=nullptr)
Create new reference counter.
Definition autoPtr.h:494
virtual const TYPE & operator[](u_integer index) const
Const array access operator.
Definition autoPtr.h:583
void removeWeakRef() const
Decrement weak reference count.
Definition autoPtr.h:450
void addStrongRef() const
Increment strong reference count.
Definition autoPtr.h:426
TYPE * alias_ptr
Aliased pointer for type casting scenarios.
Definition autoPtr.h:61
atomic< refCountBase * > ref_count
Reference counter object.
Definition autoPtr.h:60
void removeStrongRef() const
Decrement strong reference count.
Definition autoPtr.h:442
void clean() noexcept
Cleanup resources when expired.
Definition autoPtr.h:474
friend bool operator!=(const autoPtr< T, DER, DEL > &ptr, const std::nullptr_t &null)
Inequality comparison with nullptr.
virtual const TYPE & operator*() const
Const dereference operator.
Definition autoPtr.h:566
const TYPE * get() const
Get managed pointer const version.
Definition autoPtr.h:542
virtual const TYPE * operator->() const
Const member access operator.
Definition autoPtr.h:575
friend bool operator==(const autoPtr< T, DER, DEL > &ptr, const std::nullptr_t &null)
Equality comparison with nullptr.
static std::string formatString(const TYPE &t)
Universal value-to-string conversion.
Definition printable.h:263
Base class for reference-counted pointers.
Definition refCntPtr.h:39
std::string className() const override
Get class identifier.
Definition refCntPtr.h:496
std::string toString(bool enter) const override
Formatted string with reference info.
Definition refCntPtr.h:501
bool operator!=(const refCntPtr< TYPE, O_DERIVED, DELETER > &other) const
Inequality comparison operator.
Definition refCntPtr.h:491
bool operator==(const refCntPtr< TYPE, O_DERIVED, DELETER > &other) const
Equality comparison operator.
Definition refCntPtr.h:485
~refCntPtr() override=default
Default destructor of refCntPtr.
refCntPtr(TYPE *p=std::nullptr_t{})
Construct from raw pointer.
Definition refCntPtr.h:480
Base class for reference counting metadata.
Definition autoPtr.h:339
Compile-time error triggering utility.
Definition error.h:112
Shared ownership smart pointer with strong references.
Definition refCntPtr.h:108
strongPtr(strongPtr &&other) noexcept
Move constructor transfers ownership.
Definition refCntPtr.h:547
void reset() noexcept
Resets the smart pointer and releases the managed object.
Definition refCntPtr.h:600
std::string className() const override
Get class identifier.
Definition refCntPtr.h:623
friend strongPtr< T, DEL > makeStrongPtr(Args &&... args)
Creates a new strongPtr managing a shared object.
Definition refCntPtr.h:633
strongPtr< const U, DEL > staticCastTo() const
Static cast to const pointer type.
Definition refCntPtr.h:562
strongPtr & operator=(const strongPtr &other)
Copy assignment shares ownership.
Definition refCntPtr.h:534
strongPtr< U, DEL > staticCastTo()
Static cast to different pointer type.
Definition refCntPtr.h:553
strongPtr(TYPE *p=std::nullptr_t{})
Construct from raw pointer.
Definition refCntPtr.h:523
strongPtr< const U, DEL > dynamicCastTo() const
Dynamic cast to const pointer type.
Definition refCntPtr.h:582
friend strongPtr< T, DEL > makeStrongPtrArray(u_integer size, Args &&... args)
Creates a new strongPtr managing a shared array.
Definition refCntPtr.h:638
~strongPtr() override
Destructor decreases strong references count.
Definition refCntPtr.h:628
strongPtr< U, DEL > constCastTo() const
Const cast to remove const qualifier.
Definition refCntPtr.h:593
strongPtr< U, DEL > dynamicCastTo()
Dynamic cast to different pointer type.
Definition refCntPtr.h:571
strongPtr(const strongPtr &other)
Copy constructor shares ownership.
Definition refCntPtr.h:529
Non-owning reference to shared resource.
Definition refCntPtr.h:244
const TYPE & operator*() const override
Const dereference via temporary strong reference.
Definition refCntPtr.h:782
weakPtr< U, DEL > staticCastTo()
Static cast to different pointer type.
Definition refCntPtr.h:722
std::string className() const override
Get class type identifier.
Definition refCntPtr.h:812
strongPtr< TYPE, DELETER > lock() const
Attempt to acquire ownership.
Definition refCntPtr.h:769
weakPtr< U, DEL > constCastTo() const
Const cast to remove const qualifier.
Definition refCntPtr.h:762
weakPtr & operator=(const strongPtr< TYPE, DELETER > &other)
Assign observation from strongPtr.
Definition refCntPtr.h:670
weakPtr< U, DEL > dynamicCastTo()
Dynamic cast to different pointer type.
Definition refCntPtr.h:740
~weakPtr() override
Destructor releases weak reference.
Definition refCntPtr.h:817
const TYPE & operator[](u_integer index) const override
Const array element access via temporary strong reference.
Definition refCntPtr.h:792
const TYPE * operator->() const override
Const member access via temporary strong reference.
Definition refCntPtr.h:787
Default deleters for resource management.
std::uint32_t u_integer
32-bit unsigned integer type for sizes and indexes
Definition config.h:263
Main namespace for the project Original.
Definition algorithms.h:21
strongPtr< T, DEL > makeStrongPtrArray(u_integer size, Args &&... args)
Creates a new strongPtr managing a shared array.
Definition refCntPtr.h:638
strongPtr< T, DEL > makeStrongPtr(Args &&... args)
Creates a new strongPtr managing a shared object.
Definition refCntPtr.h:633
Reference-counted smart pointer hierarchy.