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
19namespace original {
20 // Forward declaration for friend class
21 template<typename TYPE, typename DELETER>
22 class refCount;
23
38 template<typename TYPE, typename DERIVED, typename DELETER>
39 class autoPtr : public printable,
40 public comparable<autoPtr<TYPE, DERIVED, DELETER>>,
41 public hashable<autoPtr<TYPE, DERIVED, DELETER>> {
42 protected:
44
50 explicit autoPtr(TYPE* p);
51
57 void setPtr(TYPE* p);
58
64
69 void addWeakRef();
70
76
82
87 void destroyRefCnt() noexcept;
88
93 void clean() noexcept;
94
100 static refCount<TYPE, DELETER>* newRefCount(TYPE* p = nullptr);
101
102 public:
103
109
115
120 bool exist() const;
121
126 bool expired() const;
127
132 explicit operator bool() const;
133
139 const TYPE* get() const;
140
146 TYPE* get();
147
153 virtual const TYPE& operator*() const;
154
160 virtual const TYPE* operator->() const;
161
168 virtual const TYPE& operator[](u_integer index) const;
169
170 // Mutable accessors
176 virtual TYPE& operator*();
177
183 virtual TYPE* operator->();
184
191 virtual TYPE& operator[](u_integer index);
192
202 void swap(autoPtr& other) noexcept;
203
209 integer compareTo(const autoPtr& other) const override;
210
215 std::string className() const override;
216
222 std::string toString(bool enter) const override;
223
224 [[nodiscard]] u_integer toHash() const noexcept override;
225
226 bool equals(const autoPtr& other) const noexcept override;
227
231 ~autoPtr() override;
232
233 template<typename T, typename DER, typename DEL>
234 friend bool operator==(const autoPtr<T, DER, DEL>& ptr, const std::nullptr_t& null);
235
236 template<typename T, typename DER, typename DEL>
237 friend bool operator!=(const autoPtr<T, DER, DEL>& ptr, const std::nullptr_t& null);
238
239 template<typename T, typename DER, typename DEL>
240 friend bool operator==(const std::nullptr_t& null, const autoPtr<T, DER, DEL>& ptr);
241
242 template<typename T, typename DER, typename DEL>
243 friend bool operator!=(const std::nullptr_t& null, const autoPtr<T, DER, DEL>& ptr);
244 };
245
256 template<typename T, typename DER, typename DEL>
257 bool operator==(const autoPtr<T, DER, DEL>& ptr, const std::nullptr_t& null);
258
269 template<typename T, typename DER, typename DEL>
270 bool operator!=(const autoPtr<T, DER, DEL>& ptr, const std::nullptr_t& null);
271
282 template<typename T, typename DER, typename DEL>
283 bool operator==(const std::nullptr_t& null, const autoPtr<T, DER, DEL>& ptr);
284
295 template<typename T, typename DER, typename DEL>
296 bool operator!=(const std::nullptr_t& null, const autoPtr<T, DER, DEL>& ptr);
297
308 template<typename TYPE, typename DELETER>
309 class refCount {
310 template <typename, typename, typename>
311 friend class autoPtr;
312
313 TYPE* ptr;
314 u_integer strong_refs;
315 u_integer weak_refs;
316 DELETER deleter;
317
322 explicit refCount(TYPE* p = nullptr);
323
327 void destroyPtr() noexcept;
328
332 ~refCount();
333 };
334}
335
336template<typename TYPE, typename DERIVED, typename DELETER>
338 : ref_count(newRefCount(p)) {}
339
340
341template<typename TYPE, typename DERIVED, typename DELETER>
343 if (!this->exist()){
344 throw nullPointerError();
345 }
346 this->ref_count->ptr = p;
347}
348
349template<typename TYPE, typename DERIVED, typename DELETER>
351 this->ref_count->strong_refs += 1;
352}
353
354template<typename TYPE, typename DERIVED, typename DELETER>
356 this->ref_count->weak_refs += 1;
357}
358
359template<typename TYPE, typename DERIVED, typename DELETER>
361 this->ref_count->strong_refs -= 1;
362}
363
364template<typename TYPE, typename DERIVED, typename DELETER>
366 this->ref_count->weak_refs -= 1;
367}
368
369template<typename TYPE, typename DERIVED, typename DELETER>
371 delete this->ref_count;
372 this->ref_count = nullptr;
373}
374
375template<typename TYPE, typename DERIVED, typename DELETER>
377 if (!this->exist()){
378 this->destroyRefCnt();
379 }
380 if (this->expired()){
381 this->ref_count->destroyPtr();
382 }
383}
384
385template <typename TYPE, typename DERIVED, typename DELETER>
390
391template<typename TYPE, typename DERIVED, typename DELETER>
393 return this->ref_count->strong_refs;
394}
395
396template<typename TYPE, typename DERIVED, typename DELETER>
398 return this->ref_count->weak_refs;
399}
400
401template<typename TYPE, typename DERIVED, typename DELETER>
403 return this->ref_count && (this->strongRefs() > 0 || this->weakRefs() > 0);
404}
405
406template<typename TYPE, typename DERIVED, typename DELETER>
408 return this->ref_count && this->strongRefs() == 0;
409}
410
411template<typename TYPE, typename DERIVED, typename DELETER>
413 return this->exist() && this->get();
414}
415
416template<typename TYPE, typename DERIVED, typename DELETER>
418 if (!this->exist()){
419 throw nullPointerError();
420 }
421 return this->ref_count->ptr;
422}
423
424template<typename TYPE, typename DERIVED, typename DELETER>
426 if (!this->exist()){
427 throw nullPointerError();
428 }
429 return this->ref_count->ptr;
430}
431
432template<typename TYPE, typename DERIVED, typename DELETER>
434 const auto ptr = this->get();
435 if (!ptr)
436 throw nullPointerError();
437 return *ptr;
438}
439
440template<typename TYPE, typename DERIVED, typename DELETER>
441const TYPE*
443 const auto ptr = this->get();
444 if (!ptr)
445 throw nullPointerError();
446 return ptr;
447}
448
449template<typename TYPE, typename DERIVED, typename DELETER>
451 const auto ptr = this->get();
452 if (!ptr)
453 throw nullPointerError();
454 return ptr[index];
455}
456
457template<typename TYPE, typename DERIVED, typename DELETER>
459 auto ptr = this->get();
460 if (!ptr)
461 throw nullPointerError();
462 return *ptr;
463}
464
465template<typename TYPE, typename DERIVED, typename DELETER>
466TYPE*
468 auto ptr = this->get();
469 if (!ptr)
470 throw nullPointerError();
471 return ptr;
472}
473
474template<typename TYPE, typename DERIVED, typename DELETER>
476 auto ptr = this->get();
477 if (!ptr)
478 throw nullPointerError();
479 return ptr[index];
480}
481
482template<typename TYPE, typename DERIVED, typename DELETER>
484 auto other_ref_count = other.ref_count;
485 other.ref_count = this->ref_count;
486 this->ref_count = other_ref_count;
487}
488
489template<typename TYPE, typename DERIVED, typename DELETER>
491 return this->ref_count - other.ref_count;
492}
493
494template<typename TYPE, typename DERIVED, typename DELETER>
496 return "autoPtr";
497}
498
499template<typename TYPE, typename DERIVED, typename DELETER>
500std::string original::autoPtr<TYPE, DERIVED, DELETER>::toString(const bool enter) const {
501 std::stringstream ss;
502 ss << this->className() << "(";
503 ss << formatString(this->get());
504 ss << ")";
505 if (enter)
506 ss << "\n";
507 return ss.str();
508}
509
510template<typename TYPE, typename DERIVED, typename DELETER>
514
515template<typename TYPE, typename DERIVED, typename DELETER>
516bool original::autoPtr<TYPE, DERIVED, DELETER>::equals(const autoPtr& other) const noexcept {
517 return *this == other;
518}
519
520template<typename TYPE, typename DERIVED, typename DELETER>
524
525template<typename T, typename DER, typename DEL>
526bool original::operator==(const autoPtr<T, DER, DEL>& ptr, const std::nullptr_t&) {
527 return !ptr.operator bool();
528}
529
530template<typename T, typename DER, typename DEL>
531bool original::operator!=(const autoPtr<T, DER, DEL>& ptr, const std::nullptr_t&) {
532 return ptr.operator bool();
533}
534
535template<typename T, typename DER, typename DEL>
536bool original::operator==(const std::nullptr_t&, const autoPtr<T, DER, DEL>& ptr) {
537 return !ptr.operator bool();
538}
539
540template<typename T, typename DER, typename DEL>
541bool original::operator!=(const std::nullptr_t&, const autoPtr<T, DER, DEL>& ptr) {
542 return ptr.operator bool();
543}
544
545template<typename TYPE, typename DELETER>
547 : ptr(p), strong_refs(0), weak_refs(0) {}
548
549template<typename TYPE, typename DELETER>
551 TYPE* tmp = this->ptr;
552 this->ptr = nullptr;
553 this->deleter(tmp);
554}
555
556template<typename TYPE, typename DELETER>
558 this->destroyPtr();
559}
560
561#endif //AUTOPTR_H
Base smart pointer with reference counting.
Definition autoPtr.h:41
static refCount< TYPE, DELETER > * newRefCount(TYPE *p=nullptr)
Create new reference counter.
Definition autoPtr.h:386
integer compareTo(const autoPtr &other) const override
Compare reference counters.
Definition autoPtr.h:490
void destroyRefCnt() noexcept
Destroy reference counter.
Definition autoPtr.h:370
void removeStrongRef()
Decrement strong reference count.
Definition autoPtr.h:360
virtual const TYPE & operator[](u_integer index) const
Const array access operator.
Definition autoPtr.h:450
u_integer strongRefs() const
Get strong reference count.
Definition autoPtr.h:392
~autoPtr() override
Destructor triggers reference cleanup.
Definition autoPtr.h:521
void addStrongRef()
Increment strong reference count.
Definition autoPtr.h:350
bool exist() const
Check active ownership.
Definition autoPtr.h:402
void setPtr(TYPE *p)
Replace managed pointer.
Definition autoPtr.h:342
void removeWeakRef()
Decrement weak reference count.
Definition autoPtr.h:365
void addWeakRef()
Increment weak reference count.
Definition autoPtr.h:355
std::string className() const override
Get class name string.
Definition autoPtr.h:495
void swap(autoPtr &other) noexcept
Swaps the reference counters between two autoPtr instances.
Definition autoPtr.h:483
u_integer toHash() const noexcept override
Computes the hash of the object.
Definition autoPtr.h:511
bool expired() const
Check resource validity.
Definition autoPtr.h:407
void clean() noexcept
Cleanup resources when expired.
Definition autoPtr.h:376
u_integer weakRefs() const
Get weak reference count.
Definition autoPtr.h:397
autoPtr(TYPE *p)
Construct from raw pointer.
Definition autoPtr.h:337
refCount< TYPE, DELETER > * ref_count
Reference counter object.
Definition autoPtr.h:43
virtual const TYPE & operator*() const
Const dereference operator.
Definition autoPtr.h:433
std::string toString(bool enter) const override
String representation formatter.
Definition autoPtr.h:500
const TYPE * get() const
Get managed pointer const version.
Definition autoPtr.h:417
virtual const TYPE * operator->() const
Const member access operator.
Definition autoPtr.h:442
Base class for comparable objects.
Definition comparable.h:31
Default deletion policy for single objects.
Definition deleter.h:79
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
Reference counting metadata container.
Definition autoPtr.h:309
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
bool operator!=(const autoPtr< T, DER, DEL > &ptr, const std::nullptr_t &null)
Inequality comparison with nullptr.
Definition autoPtr.h:531
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)
Equality comparison with nullptr.
Definition autoPtr.h:526
Interface for polymorphic string formatting and output.