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
10
29
30namespace original {
31 // Forward declaration for friend class
32 template<typename TYPE, typename DELETER>
33 class refCount;
34
51 template<typename TYPE, typename DERIVED, typename DELETER>
52 class autoPtr : public printable,
53 public comparable<autoPtr<TYPE, DERIVED, DELETER>>,
54 public hashable<autoPtr<TYPE, DERIVED, DELETER>> {
55 protected:
57
63 explicit autoPtr(TYPE* p);
64
70 void setPtr(TYPE* p);
71
77
82 void addWeakRef();
83
89
95
100 void destroyRefCnt() noexcept;
101
106 void clean() noexcept;
107
113 static refCount<TYPE, DELETER>* newRefCount(TYPE* p = nullptr);
114
115 public:
116
122
128
133 bool exist() const;
134
139 bool expired() const;
140
145 explicit operator bool() const;
146
151 bool operator!() const;
152
158 const TYPE* get() const;
159
165 TYPE* get();
166
172 virtual const TYPE& operator*() const;
173
179 virtual const TYPE* operator->() const;
180
187 virtual const TYPE& operator[](u_integer index) const;
188
189 // Mutable accessors
195 virtual TYPE& operator*();
196
202 virtual TYPE* operator->();
203
210 virtual TYPE& operator[](u_integer index);
211
221 void swap(autoPtr& other) noexcept;
222
228 integer compareTo(const autoPtr& other) const override;
229
234 std::string className() const override;
235
241 std::string toString(bool enter) const override;
242
248 [[nodiscard]] u_integer toHash() const noexcept override;
249
256 bool equals(const autoPtr& other) const noexcept override;
257
261 ~autoPtr() override;
262
263 template<typename T, typename DER, typename DEL>
264 friend bool operator==(const autoPtr<T, DER, DEL>& ptr, const std::nullptr_t& null);
265
266 template<typename T, typename DER, typename DEL>
267 friend bool operator!=(const autoPtr<T, DER, DEL>& ptr, const std::nullptr_t& null);
268
269 template<typename T, typename DER, typename DEL>
270 friend bool operator==(const std::nullptr_t& null, const autoPtr<T, DER, DEL>& ptr);
271
272 template<typename T, typename DER, typename DEL>
273 friend bool operator!=(const std::nullptr_t& null, const autoPtr<T, DER, DEL>& ptr);
274 };
275
286 template<typename T, typename DER, typename DEL>
287 bool operator==(const autoPtr<T, DER, DEL>& ptr, const std::nullptr_t& null);
288
299 template<typename T, typename DER, typename DEL>
300 bool operator!=(const autoPtr<T, DER, DEL>& ptr, const std::nullptr_t& null);
301
312 template<typename T, typename DER, typename DEL>
313 bool operator==(const std::nullptr_t& null, const autoPtr<T, DER, DEL>& ptr);
314
325 template<typename T, typename DER, typename DEL>
326 bool operator!=(const std::nullptr_t& null, const autoPtr<T, DER, DEL>& ptr);
327
338 template<typename TYPE, typename DELETER>
339 class refCount {
340 template <typename, typename, typename>
341 friend class autoPtr;
342
343 TYPE* ptr;
344 u_integer strong_refs;
345 u_integer weak_refs;
346 DELETER deleter;
347
352 explicit refCount(TYPE* p = nullptr);
353
357 void destroyPtr() noexcept;
358
362 ~refCount();
363 };
364}
365
366template<typename TYPE, typename DERIVED, typename DELETER>
369
370
371template<typename TYPE, typename DERIVED, typename DELETER>
373 if (!this->exist()){
374 throw nullPointerError();
375 }
376 this->ref_count->ptr = p;
377}
378
379template<typename TYPE, typename DERIVED, typename DELETER>
383
384template<typename TYPE, typename DERIVED, typename DELETER>
388
389template<typename TYPE, typename DERIVED, typename DELETER>
393
394template<typename TYPE, typename DERIVED, typename DELETER>
398
399template<typename TYPE, typename DERIVED, typename DELETER>
401 delete this->ref_count;
402 this->ref_count = nullptr;
403}
404
405template<typename TYPE, typename DERIVED, typename DELETER>
407 if (!this->exist()){
408 this->destroyRefCnt();
409 }
410 if (this->expired()){
411 this->ref_count->destroyPtr();
412 }
413}
414
415template <typename TYPE, typename DERIVED, typename DELETER>
420
421template<typename TYPE, typename DERIVED, typename DELETER>
425
426template<typename TYPE, typename DERIVED, typename DELETER>
430
431template<typename TYPE, typename DERIVED, typename DELETER>
433 return this->ref_count && (this->strongRefs() > 0 || this->weakRefs() > 0);
434}
435
436template<typename TYPE, typename DERIVED, typename DELETER>
438 return this->ref_count && this->strongRefs() == 0;
439}
440
441template<typename TYPE, typename DERIVED, typename DELETER>
443 return this->exist() && this->get();
444}
445
446template <typename TYPE, typename DERIVED, typename DELETER>
448 return !this->operator bool();
449}
450
451template<typename TYPE, typename DERIVED, typename DELETER>
453 if (!this->exist()){
454 throw nullPointerError();
455 }
456 return this->ref_count->ptr;
457}
458
459template<typename TYPE, typename DERIVED, typename DELETER>
461 if (!this->exist()){
462 throw nullPointerError();
463 }
464 return this->ref_count->ptr;
465}
466
467template<typename TYPE, typename DERIVED, typename DELETER>
469 const auto ptr = this->get();
470 if (!ptr)
471 throw nullPointerError();
472 return *ptr;
473}
474
475template<typename TYPE, typename DERIVED, typename DELETER>
476const TYPE*
478 const auto ptr = this->get();
479 if (!ptr)
480 throw nullPointerError();
481 return ptr;
482}
483
484template<typename TYPE, typename DERIVED, typename DELETER>
486 const auto ptr = this->get();
487 if (!ptr)
488 throw nullPointerError();
489 return ptr[index];
490}
491
492template<typename TYPE, typename DERIVED, typename DELETER>
494 auto ptr = this->get();
495 if (!ptr)
496 throw nullPointerError();
497 return *ptr;
498}
499
500template<typename TYPE, typename DERIVED, typename DELETER>
501TYPE*
503 auto ptr = this->get();
504 if (!ptr)
505 throw nullPointerError();
506 return ptr;
507}
508
509template<typename TYPE, typename DERIVED, typename DELETER>
511 auto ptr = this->get();
512 if (!ptr)
513 throw nullPointerError();
514 return ptr[index];
515}
516
517template<typename TYPE, typename DERIVED, typename DELETER>
519 auto other_ref_count = other.ref_count;
520 other.ref_count = this->ref_count;
521 this->ref_count = other_ref_count;
522}
523
524template<typename TYPE, typename DERIVED, typename DELETER>
528
529template<typename TYPE, typename DERIVED, typename DELETER>
531 return "autoPtr";
532}
533
534template<typename TYPE, typename DERIVED, typename DELETER>
535std::string original::autoPtr<TYPE, DERIVED, DELETER>::toString(const bool enter) const {
536 std::stringstream ss;
537 ss << this->className() << "(";
538 ss << formatString(this->get());
539 ss << ")";
540 if (enter)
541 ss << "\n";
542 return ss.str();
543}
544
545template<typename TYPE, typename DERIVED, typename DELETER>
549
550template<typename TYPE, typename DERIVED, typename DELETER>
552 return *this == other;
553}
554
555template<typename TYPE, typename DERIVED, typename DELETER>
559
560template<typename T, typename DER, typename DEL>
561bool original::operator==(const autoPtr<T, DER, DEL>& ptr, const std::nullptr_t&) {
562 return !ptr.operator bool();
563}
564
565template<typename T, typename DER, typename DEL>
566bool original::operator!=(const autoPtr<T, DER, DEL>& ptr, const std::nullptr_t&) {
567 return ptr.operator bool();
568}
569
570template<typename T, typename DER, typename DEL>
571bool original::operator==(const std::nullptr_t&, const autoPtr<T, DER, DEL>& ptr) {
572 return !ptr.operator bool();
573}
574
575template<typename T, typename DER, typename DEL>
576bool original::operator!=(const std::nullptr_t&, const autoPtr<T, DER, DEL>& ptr) {
577 return ptr.operator bool();
578}
579
580template<typename TYPE, typename DELETER>
581original::refCount<TYPE, DELETER>::refCount(TYPE *p)
582 : ptr(p), strong_refs(0), weak_refs(0) {}
583
584template<typename TYPE, typename DELETER>
585void original::refCount<TYPE, DELETER>::destroyPtr() noexcept {
586 TYPE* tmp = this->ptr;
587 this->ptr = nullptr;
588 this->deleter(tmp);
589}
590
591template<typename TYPE, typename DELETER>
592original::refCount<TYPE, DELETER>::~refCount() {
593 this->destroyPtr();
594}
595
596#endif //AUTOPTR_H
Base smart pointer with reference counting.
Definition autoPtr.h:54
static refCount< TYPE, deleter< TYPE > > * newRefCount(TYPE *p=nullptr)
Definition autoPtr.h:416
integer compareTo(const autoPtr &other) const override
Definition autoPtr.h:525
void destroyRefCnt() noexcept
Destroy reference counter.
Definition autoPtr.h:400
void removeStrongRef()
Decrement strong reference count.
Definition autoPtr.h:390
virtual const TYPE & operator[](u_integer index) const
Const array access operator.
Definition autoPtr.h:485
~autoPtr() override
Destructor triggers reference cleanup.
Definition autoPtr.h:556
void addStrongRef()
Increment strong reference count.
Definition autoPtr.h:380
bool equals(const autoPtr &other) const noexcept override
Definition autoPtr.h:551
void setPtr(TYPE *p)
Replace managed pointer.
Definition autoPtr.h:372
void removeWeakRef()
Decrement weak reference count.
Definition autoPtr.h:395
bool operator!() const
Logical NOT operator.
Definition autoPtr.h:447
void addWeakRef()
Increment weak reference count.
Definition autoPtr.h:385
std::string className() const override
Definition autoPtr.h:530
void swap(autoPtr &other) noexcept
Definition autoPtr.h:518
u_integer toHash() const noexcept override
Definition autoPtr.h:546
autoPtr(TYPE *p)
Construct from raw pointer.
Definition autoPtr.h:367
refCount< TYPE, DELETER > * ref_count
Reference counter object.
Definition autoPtr.h:56
virtual const TYPE & operator*() const
Const dereference operator.
Definition autoPtr.h:468
std::string toString(bool enter) const override
Definition autoPtr.h:535
virtual const TYPE * operator->() const
Const member access operator.
Definition autoPtr.h:477
Base class for comparable objects.
Definition comparable.h:31
static u_integer hashFunc(const T &t) noexcept
Default hash function fallback.
Forward declaration of hashable interface template.
Definition hash.h:216
Exception for null pointer dereference attempts.
Definition error.h:110
Base class providing polymorphic string conversion capabilities.
Definition printable.h:29
static std::string formatString(const TYPE &t)
Universal value-to-string conversion.
Reference counting metadata container.
Definition autoPtr.h:339
Interface for objects that can be compared.
Platform-independent integer and floating-point type definitions.
Custom exception classes and callback validation utilities.
Provides a generic hashing utility and a base interface for hashable types.
Main namespace for the project Original.
Definition algorithms.h:21
std::uint32_t u_integer
32-bit unsigned integer type for sizes and indexes
Definition config.h:43
std::int64_t integer
64-bit signed integer type for arithmetic operations
Definition config.h:35
bool operator!=(const autoPtr< T, DER, DEL > &ptr, const std::nullptr_t &null)
Inequality comparison with nullptr.
Definition autoPtr.h:566
bool operator==(const autoPtr< T, DER, DEL > &ptr, const std::nullptr_t &null)
Equality comparison with nullptr.
Definition autoPtr.h:561
Interface for polymorphic string formatting and output.