ORIGINAL
Loading...
Searching...
No Matches
autoPtr.h
Go to the documentation of this file.
1#ifndef AUTOPTR_H
2#define AUTOPTR_H
3
4#include "config.h"
5#include "printable.h"
6#include "comparable.h"
7#include "hash.h"
8#include "error.h"
9
29namespace original {
30 // Forward declaration for refCountBase
31 class refCountBase;
32
33 // Forward declaration for refCount
34 template<typename TYPE, typename DELETER>
35 class refCount;
36
53 template<typename TYPE, typename DERIVED, typename DELETER>
54 class autoPtr : public printable,
55 public comparable<autoPtr<TYPE, DERIVED, DELETER>>,
56 public hashable<autoPtr<TYPE, DERIVED, DELETER>> {
57 template<typename, typename, typename> friend class autoPtr;
58 protected:
60 TYPE* alias_ptr;
61
67 explicit autoPtr(TYPE* p);
68
73 void addStrongRef() const;
74
79 void addWeakRef() const;
80
85 void removeStrongRef() const;
86
91 void removeWeakRef() const;
92
99 TYPE* releasePtr() noexcept;
100
105 void destroyRefCnt() noexcept;
106
111 void clean() noexcept;
112
118 static refCount<TYPE, DELETER>* newRefCount(TYPE* p = nullptr);
119
120 public:
121
127
133
138 bool exist() const;
139
144 bool expired() const;
145
150 explicit operator bool() const;
151
156 bool operator!() const;
157
163 const TYPE* get() const;
164
170 TYPE* get();
171
177 virtual const TYPE& operator*() const;
178
184 virtual const TYPE* operator->() const;
185
192 virtual const TYPE& operator[](u_integer index) const;
193
194 // Mutable accessors
200 virtual TYPE& operator*();
201
207 virtual TYPE* operator->();
208
215 virtual TYPE& operator[](u_integer index);
216
226 void swap(autoPtr& other) noexcept;
227
233 integer compareTo(const autoPtr& other) const override;
234
239 std::string className() const override;
240
246 std::string toString(bool enter) const override;
247
253 [[nodiscard]] u_integer toHash() const noexcept override;
254
261 bool equals(const autoPtr& other) const noexcept override;
262
266 ~autoPtr() override;
267
268 template<typename T, typename DER, typename DEL>
269 friend bool operator==(const autoPtr<T, DER, DEL>& ptr, const std::nullptr_t& null);
270
271 template<typename T, typename DER, typename DEL>
272 friend bool operator!=(const autoPtr<T, DER, DEL>& ptr, const std::nullptr_t& null);
273
274 template<typename T, typename DER, typename DEL>
275 friend bool operator==(const std::nullptr_t& null, const autoPtr<T, DER, DEL>& ptr);
276
277 template<typename T, typename DER, typename DEL>
278 friend bool operator!=(const std::nullptr_t& null, const autoPtr<T, DER, DEL>& ptr);
279 };
280
291 template<typename T, typename DER, typename DEL>
292 bool operator==(const autoPtr<T, DER, DEL>& ptr, const std::nullptr_t& null);
293
304 template<typename T, typename DER, typename DEL>
305 bool operator!=(const autoPtr<T, DER, DEL>& ptr, const std::nullptr_t& null);
306
317 template<typename T, typename DER, typename DEL>
318 bool operator==(const std::nullptr_t& null, const autoPtr<T, DER, DEL>& ptr);
319
330 template<typename T, typename DER, typename DEL>
331 bool operator!=(const std::nullptr_t& null, const autoPtr<T, DER, DEL>& ptr);
332
339 template <typename, typename, typename>
340 friend class autoPtr;
341
342 protected:
345
349 refCountBase();
350
355 virtual const void* getPtr() const noexcept = 0;
356
361 virtual void* getPtr() noexcept = 0;
362
368 virtual void* releasePtr() noexcept = 0;
369
373 virtual void destroyPtr() noexcept = 0;
374
375 virtual ~refCountBase() = default;
376 };
377
388 template<typename TYPE, typename DELETER>
389 class refCount final : public refCountBase {
390 template <typename, typename, typename>
391 friend class autoPtr;
392
393 TYPE* ptr;
394 DELETER deleter;
395
400 explicit refCount(TYPE* p = nullptr);
401
402 const void* getPtr() const noexcept override;
403
404 void* getPtr() noexcept override;
405
406 void* releasePtr() noexcept override;
407
411 void destroyPtr() noexcept override;
412
416 ~refCount() override;
417 };
418}
419
420template<typename TYPE, typename DERIVED, typename DELETER>
423
424template<typename TYPE, typename DERIVED, typename DELETER>
426{
427 this->ref_count->strong_refs += 1;
428}
429
430template<typename TYPE, typename DERIVED, typename DELETER>
432{
433 this->ref_count->weak_refs += 1;
434}
435
436template<typename TYPE, typename DERIVED, typename DELETER>
438{
439 this->ref_count->strong_refs -= 1;
440}
441
442template<typename TYPE, typename DERIVED, typename DELETER>
444{
445 this->ref_count->weak_refs -= 1;
446}
447
448template <typename TYPE, typename DERIVED, typename DELETER>
450{
451 return static_cast<TYPE*>(this->ref_count->releasePtr());
452}
453
454template<typename TYPE, typename DERIVED, typename DELETER>
456 delete this->ref_count;
457 this->ref_count = nullptr;
458}
459
460template<typename TYPE, typename DERIVED, typename DELETER>
462 if (!this->exist()){
463 this->destroyRefCnt();
464 }
465 if (this->expired()){
466 this->ref_count->destroyPtr();
467 }
468}
469
470template <typename TYPE, typename DERIVED, typename DELETER>
475
476template<typename TYPE, typename DERIVED, typename DELETER>
480
481template<typename TYPE, typename DERIVED, typename DELETER>
485
486template<typename TYPE, typename DERIVED, typename DELETER>
488 return this->ref_count && (this->strongRefs() > 0 || this->weakRefs() > 0);
489}
490
491template<typename TYPE, typename DERIVED, typename DELETER>
493 return this->ref_count && this->strongRefs() == 0;
494}
495
496template<typename TYPE, typename DERIVED, typename DELETER>
498 return this->exist() && this->get();
499}
500
501template <typename TYPE, typename DERIVED, typename DELETER>
503 return !this->operator bool();
504}
505
506template<typename TYPE, typename DERIVED, typename DELETER>
508 if (!this->exist()){
509 throw nullPointerError();
510 }
511 if (this->alias_ptr) {
512 return this->alias_ptr;
513 }
514 return static_cast<TYPE*>(this->ref_count->getPtr());
515}
516
517template<typename TYPE, typename DERIVED, typename DELETER>
519 if (!this->exist()){
520 throw nullPointerError();
521 }
522 if (this->alias_ptr) {
523 return this->alias_ptr;
524 }
525 return static_cast<TYPE*>(this->ref_count->getPtr());
526}
527
528template<typename TYPE, typename DERIVED, typename DELETER>
530 const auto ptr = this->get();
531 if (!ptr)
532 throw nullPointerError();
533 return *ptr;
534}
535
536template<typename TYPE, typename DERIVED, typename DELETER>
537const TYPE*
539 const auto ptr = this->get();
540 if (!ptr)
541 throw nullPointerError();
542 return ptr;
543}
544
545template<typename TYPE, typename DERIVED, typename DELETER>
547 const auto ptr = this->get();
548 if (!ptr)
549 throw nullPointerError();
550 return ptr[index];
551}
552
553template<typename TYPE, typename DERIVED, typename DELETER>
555 auto ptr = this->get();
556 if (!ptr)
557 throw nullPointerError();
558 return *ptr;
559}
560
561template<typename TYPE, typename DERIVED, typename DELETER>
562TYPE*
564 auto ptr = this->get();
565 if (!ptr)
566 throw nullPointerError();
567 return ptr;
568}
569
570template<typename TYPE, typename DERIVED, typename DELETER>
572 auto ptr = this->get();
573 if (!ptr)
574 throw nullPointerError();
575 return ptr[index];
576}
577
578template<typename TYPE, typename DERIVED, typename DELETER>
580 auto other_ref_count = other.ref_count;
581 other.ref_count = this->ref_count;
582 this->ref_count = other_ref_count;
583}
584
585template<typename TYPE, typename DERIVED, typename DELETER>
587 return this->ref_count - other.ref_count;
588}
589
590template<typename TYPE, typename DERIVED, typename DELETER>
592 return "autoPtr";
593}
594
595template<typename TYPE, typename DERIVED, typename DELETER>
596std::string original::autoPtr<TYPE, DERIVED, DELETER>::toString(const bool enter) const {
597 std::stringstream ss;
598 ss << this->className() << "(";
599 ss << formatString(this->get());
600 ss << ")";
601 if (enter)
602 ss << "\n";
603 return ss.str();
604}
605
606template<typename TYPE, typename DERIVED, typename DELETER>
610
611template<typename TYPE, typename DERIVED, typename DELETER>
613 return *this == other;
614}
615
616template<typename TYPE, typename DERIVED, typename DELETER>
620
621template<typename T, typename DER, typename DEL>
622bool original::operator==(const autoPtr<T, DER, DEL>& ptr, const std::nullptr_t&) {
623 return !ptr.operator bool();
624}
625
626template<typename T, typename DER, typename DEL>
627bool original::operator!=(const autoPtr<T, DER, DEL>& ptr, const std::nullptr_t&) {
628 return ptr.operator bool();
629}
630
631template<typename T, typename DER, typename DEL>
632bool original::operator==(const std::nullptr_t&, const autoPtr<T, DER, DEL>& ptr) {
633 return !ptr.operator bool();
634}
635
636template<typename T, typename DER, typename DEL>
637bool original::operator!=(const std::nullptr_t&, const autoPtr<T, DER, DEL>& ptr) {
638 return ptr.operator bool();
639}
640
641inline original::refCountBase::refCountBase() : strong_refs(0), weak_refs(0) {}
642
643template<typename TYPE, typename DELETER>
645 : ptr(p) {}
646
647template <typename TYPE, typename DELETER>
648const void* original::refCount<TYPE, DELETER>::getPtr() const noexcept
649{
650 return this->ptr;
651}
652
653template <typename TYPE, typename DELETER>
655{
656 if constexpr (std::is_const_v<TYPE>) {
657 return const_cast<std::remove_const_t<TYPE>*>(this->ptr);
658 } else {
659 return this->ptr;
660 }
661}
662
663template <typename TYPE, typename DELETER>
665{
666 if constexpr (std::is_const_v<TYPE>) {
667 auto p = const_cast<std::remove_const_t<TYPE>*>(this->ptr);
668 this->ptr = nullptr;
669 return p;
670 } else {
671 auto p = this->ptr;
672 this->ptr = nullptr;
673 return p;
674 }
675}
676
677template<typename TYPE, typename DELETER>
679 TYPE* tmp = this->ptr;
680 this->ptr = nullptr;
681 this->deleter(tmp);
682}
683
684template<typename TYPE, typename DELETER>
686 this->destroyPtr();
687}
688
689#endif //AUTOPTR_H
Base smart pointer with reference counting.
Definition autoPtr.h:56
void addWeakRef() const
Increment weak reference count.
Definition autoPtr.h:431
static refCount< TYPE, DELETER > * newRefCount(TYPE *p=nullptr)
Create new reference counter.
Definition autoPtr.h:471
integer compareTo(const autoPtr &other) const override
Compare reference counters.
Definition autoPtr.h:586
void destroyRefCnt() noexcept
Destroy reference counter.
Definition autoPtr.h:455
virtual const TYPE & operator[](u_integer index) const
Const array access operator.
Definition autoPtr.h:546
u_integer strongRefs() const
Get strong reference count.
Definition autoPtr.h:477
~autoPtr() override
Destructor triggers reference cleanup.
Definition autoPtr.h:617
bool exist() const
Check active ownership.
Definition autoPtr.h:487
void removeWeakRef() const
Decrement weak reference count.
Definition autoPtr.h:443
void addStrongRef() const
Increment strong reference count.
Definition autoPtr.h:425
TYPE * alias_ptr
Aliased pointer for type casting scenarios.
Definition autoPtr.h:60
bool equals(const autoPtr &other) const noexcept override
Equality comparison.
Definition autoPtr.h:612
TYPE * releasePtr() noexcept
Release ownership of the managed pointer.
Definition autoPtr.h:449
bool operator!() const
Logical NOT operator.
Definition autoPtr.h:502
refCountBase * ref_count
Reference counter object.
Definition autoPtr.h:59
std::string className() const override
Get class name string.
Definition autoPtr.h:591
void removeStrongRef() const
Decrement strong reference count.
Definition autoPtr.h:437
void swap(autoPtr &other) noexcept
Swaps the reference counters between two autoPtr instances.
Definition autoPtr.h:579
u_integer toHash() const noexcept override
Compute hash value for the pointer.
Definition autoPtr.h:607
bool expired() const
Check resource validity.
Definition autoPtr.h:492
void clean() noexcept
Cleanup resources when expired.
Definition autoPtr.h:461
u_integer weakRefs() const
Get weak reference count.
Definition autoPtr.h:482
autoPtr(TYPE *p)
Construct from raw pointer.
Definition autoPtr.h:421
virtual const TYPE & operator*() const
Const dereference operator.
Definition autoPtr.h:529
std::string toString(bool enter) const override
String representation formatter.
Definition autoPtr.h:596
const TYPE * get() const
Get managed pointer const version.
Definition autoPtr.h:507
virtual const TYPE * operator->() const
Const member access operator.
Definition autoPtr.h:538
Base class for comparable objects.
Definition comparable.h:31
Default deletion policy for single objects.
Definition deleter.h:109
static u_integer hashFunc(const T &t) noexcept
Default hash function fallback.
Forward declaration of hashable interface template.
Definition hash.h:240
Exception for null pointer dereference attempts.
Definition error.h:171
Base class providing polymorphic string conversion capabilities.
Definition printable.h:39
Base class for reference counting metadata.
Definition autoPtr.h:338
virtual const void * getPtr() const noexcept=0
Get managed pointer (const version)
refCountBase()
Construct refCountBase object.
Definition autoPtr.h:641
u_integer weak_refs
Weak reference counter.
Definition autoPtr.h:344
u_integer strong_refs
Strong reference counter.
Definition autoPtr.h:343
virtual void destroyPtr() noexcept=0
Destroy managed pointer using deleter.
virtual void * releasePtr() noexcept=0
Release ownership of the managed pointer.
Reference counting metadata container.
Definition autoPtr.h:389
Interface for objects that can be compared.
Platform-independent type definitions and compiler/platform detection.
Custom exception classes and callback validation utilities.
std::uint32_t u_integer
32-bit unsigned integer type for sizes and indexes
Definition config.h:263
std::int64_t integer
64-bit signed integer type for arithmetic operations
Definition config.h:254
Provides a generic hashing utility and interface for hashable types.
Main namespace for the project Original.
Definition algorithms.h:21
bool operator!=(const autoPtr< T, DER, DEL > &ptr, const std::nullptr_t &null)
Inequality comparison with nullptr.
Definition autoPtr.h:627
bool operator==(const autoPtr< T, DER, DEL > &ptr, const std::nullptr_t &null)
Equality comparison with nullptr.
Definition autoPtr.h:622
Interface for polymorphic string formatting and output.