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