Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
TPointer.h
Go to the documentation of this file.
1#pragma once
2
3namespace sf
4{
5
12template<typename T>
14{
15 static_assert(!std::is_pointer_v<T>, "Type T must be a non-pointer type.");
16
17 public:
19 {
20 return *P;
21 }
22
23 // ReSharper disable once CppNonExplicitConversionOperator
24 operator T*()
25 {
26 return P;
27 }
28
29 int operator!() const
30 {
31 return P == nullptr;
32 }
33
35 {
36 T* p = P;
37 P = 0;
38 return p;
39 }
40
41 protected:
42 explicit TPointerBase(T* pointer)
43 : P(pointer)
44 {}
45
47 : P(nullptr)
48 {}
49
50 T* P;
51
52 private:
53 // Prohibit use of new
54 void* operator new(size_t) noexcept
55 {
56 return nullptr;
57 }
58
59 // Delete only sets pointer to null.
60 void operator delete(void* p)
61 {
62 static_cast<TPointerBase<T>*>(p)->P = nullptr;
63 }
64};
65
69template<typename T>
70class TPointer : public TPointerBase<T>
71{
72 private:
73 typedef TPointerBase<T> TBase;
74
75 public:
77 : TBase()
78 {}
79
80 explicit TPointer(T* pointer)
81 : TBase(pointer)
82 {}
83
85 {
86 delete TBase::P;
87 }
88
90 {
91 delete TBase::P;
92 TBase::P = src;
93 return *this;
94 }
95
96 // Could throw exception if P==0
98 {
99 return TBase::P;
100 }
101};
102
106template<typename T>
107class TAPointer : public TPointerBase<T>
108{
109 private:
110 typedef TPointerBase<T> TBase;
111
112 public:
114 : TBase()
115 {}
116
117 explicit TAPointer(T array[])
118 : TBase(array)
119 {}
120
122 {
123 delete[] TBase::P;
124 }
125
127 {
128 delete[] TBase::P;
129 TBase::P = src;
130 return *this;
131 }
132
133 T& operator[](int i)
134 {
135 // Could throw exception if P==0
136 return TBase::P[i];
137 }
138};
139
140}// namespace sf
Pointer to an array of type T. Provides an array subscript operator and uses array delete[].
Definition TPointer.h:108
T & operator[](int i)
Definition TPointer.h:133
TAPointer()
Definition TPointer.h:113
TAPointer< T > & operator=(T src[])
Definition TPointer.h:126
~TAPointer()
Definition TPointer.h:121
TAPointer(T array[])
Definition TPointer.h:117
A pair of smart pointer template classes. Provides basic conversion operator to T*,...
Definition TPointer.h:14
TPointerBase(T *pointer)
Definition TPointer.h:42
int operator!() const
Definition TPointer.h:29
TPointerBase()
Definition TPointer.h:46
T * P
Definition TPointer.h:50
T & operator*()
Definition TPointer.h:18
T * Relinquish()
Definition TPointer.h:34
Pointer to a single object. Provides member access operator ->
Definition TPointer.h:71
TPointer(T *pointer)
Definition TPointer.h:80
TPointer()
Definition TPointer.h:76
~TPointer()
Definition TPointer.h:84
T * operator->()
Definition TPointer.h:97
TPointer & operator=(T *src)
Definition TPointer.h:89
Definition Application.h:10