Repository navigation
Expand file tree
/
Copy pathCResult.h
More file actions
289 lines (235 loc) · 6.7 KB
/
Copy pathCResult.h
File metadata and controls
289 lines (235 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#ifndef CRESULT_H
#define CRESULT_H
#include <vector>
template <typename T, typename E>
class CResult
{
public:
CResult(const T& c_value);
CResult(E* pc_error);
CResult(std::vector<E*>& v_errors);
CResult(const CResult<T, E>& c_other);
~CResult();
static CResult<T, E> cOk(const T& c_value);
static CResult<T, E> cFail(E* pc_error);
static CResult<T, E> cFail(std::vector<E*>& v_errors);
CResult<T, E>& operator=(const CResult<T, E>& c_other);
bool bIsSuccess() const;
T* cGetValue() const;
const std::vector<E*>& vGetErrors() const;
private:
T *pc_value;
std::vector<E*> v_errors;
};
template <typename E>
class CResult<void, E>
{
public:
CResult();
CResult (E *pc_error);
CResult(std::vector<E*>& v_errors);
CResult(const CResult<void, E>& c_other);
~CResult();
static CResult<void, E> cOk();
static CResult<void, E> cFail (E* pc_error);
static CResult<void, E> cFail (std::vector<E*>& v_errors);
CResult<void, E>& operator=(const CResult<void, E>& c_other);
bool bIsSuccess() const;
const std::vector<E*>& vGetErrors() const;
private:
std::vector<E*> v_errors; // Brak pc_value
};
template <typename T, typename E>
CResult<T, E>::~CResult()
{
if (pc_value != NULL)
{
delete pc_value;
}
// 2. Zwolnij pamięć dla obiektów błędów.
for (int i = 0; i < v_errors.size(); ++i)
{
delete v_errors[i];
}
}
template<typename T, typename E>
CResult<T, E>::CResult(const CResult<T, E> &c_other) {
pc_value = NULL;
// 1. Kopiowanie Wartości T
if (c_other.pc_value != NULL)
{
pc_value = new T(*(c_other.pc_value));
}
// 2. Kopiowanie Błędów E*
for (size_t i = 0; i < c_other.v_errors.size(); ++i)
{
E* pc_new_error = new E(*(c_other.v_errors[i]));
v_errors.push_back(pc_new_error);
}
}
template<typename T, typename E>
CResult<T, E> &CResult<T, E>::operator=(const CResult<T, E> &c_other) {
// 1. Ochrona przed autodeklaracją (c1 = c1)
if (this == &c_other) {
return *this;
}
// 2. Zwolnienie obecnej pamięci
if (pc_value != NULL) {
delete pc_value;
pc_value = NULL;
}
// Zwolnienie obiektów w wektorze
for (size_t i = 0; i < v_errors.size(); ++i) {
delete v_errors[i];
}
v_errors.clear(); // Opróżnienie wektora
// 3. Wykonanie Głębokiej Kopii
// Kopiowanie wartości T
if (c_other.pc_value != NULL) {
pc_value = new T(*(c_other.pc_value));
}
// Kopiowanie wektora błędów
for (size_t i = 0; i < c_other.v_errors.size(); ++i) {
E* pc_new_error = new E(*(c_other.v_errors[i]));
v_errors.push_back(pc_new_error);
}
return *this;
}
template<typename T, typename E>
CResult<T, E>::CResult(const T& c_value) {
pc_value = NULL;
// Alokujemy nową pamięć i kopiujemy wartość.
pc_value = new T(c_value);
// v_errors jest domyślnie pusty.
}
template<typename T, typename E>
CResult<T, E>::CResult(E* pc_error) {
pc_value = NULL;
// Dodajemy wskaźnik do błędu (zakładamy, że został on zaalokowany dynamicznie
v_errors.push_back(pc_error);
}
template<typename T, typename E>
CResult<T, E>::CResult(std::vector<E*>& v_otherErrors)
{
// 1. Inicjalizacja
pc_value = NULL;
// 2. Głęboka Kopia Wskaźników Błędów
for (size_t i = 0; i < v_otherErrors.size(); ++i)
{
E* pc_new_error = new E(*(v_otherErrors[i]));
v_errors.push_back(pc_new_error);
}
}
template<typename T, typename E>
CResult<T, E> CResult<T, E>::cOk(const T& c_value) {
// Statyczna metoda tworząca stan sukcesu
return CResult<T, E>(c_value);
}
template<typename T, typename E>
CResult<T, E> CResult<T, E>::cFail(E* pc_error) {
// Statyczna metoda tworząca stan porażki (pojedynczy błąd)
return CResult<T, E>(pc_error);
}
template<typename T, typename E>
CResult<T, E> CResult<T, E>::cFail(std::vector<E*>& v_errors) {
// Statyczna metoda tworząca stan porażki (wektor błędów)
return CResult<T, E>(v_errors);
}
template<typename T, typename E>
bool CResult<T, E>::bIsSuccess() const {
// Zwraca true, gdy jest wartość i nie ma błędów
return pc_value != NULL && v_errors.empty();
}
template<typename T, typename E>
T* CResult<T, E>::cGetValue() const{
// Użytkownik musi teraz sprawdzić: if (CResult.ptGetValue() != NULL)
return pc_value;
}
template<typename T, typename E>
const std::vector<E*>& CResult<T, E>::vGetErrors() const {
// Zwraca referencję do wewnętrznego wektora błędów
return v_errors;
}
template <typename E>
CResult<void, E>::CResult()
{
// Sukces - pusty wektor błędów
}
template <typename E>
CResult<void, E>::CResult(E *pcError)
{
// Porażka - dodanie pojedynczego błędu
v_errors.push_back(pcError);
}
template <typename E>
CResult<void, E>::CResult(std::vector<E*>& vErrors)
{
// Porażka - głęboka kopia wektora błędów
for (size_t i = 0; i < vErrors.size(); ++i)
{
E* pc_new_error = new E(*(vErrors[i]));
v_errors.push_back(pc_new_error);
}
}
template <typename E>
CResult<void, E>::CResult(const CResult<void, E>& cOther)
{
// Konstruktor kopiujący: głęboka kopia v_errors
for (size_t i = 0; i < cOther.v_errors.size(); ++i)
{
E* pc_new_error = new E(*(cOther.v_errors[i]));
v_errors.push_back(pc_new_error);
}
}
template <typename E>
CResult<void, E>::~CResult()
{
for (size_t i = 0; i < v_errors.size(); ++i)
{
delete v_errors[i];
}
}
template<typename E>
CResult<void, E>& CResult<void, E>::operator=(const CResult<void, E>& cOther)
{
// Operator przypisania: głęboka kopia
if (this == &cOther) return *this;
// 1. Zwolnienie obecnej pamięci
for (size_t i = 0; i < v_errors.size(); ++i) delete v_errors[i];
v_errors.clear();
// 2. Głęboka Kopia
for (size_t i = 0; i < cOther.v_errors.size(); ++i) {
E* pc_new_error = new E(*(cOther.v_errors[i]));
v_errors.push_back(pc_new_error);
}
return *this;
}
template <typename E>
CResult<void, E> CResult<void, E>::cOk()
{
// Statyczna metoda sukcesu
return CResult<void, E>();
}
template <typename E>
CResult<void, E> CResult<void, E>::cFail(E* pcError)
{
return CResult<void, E>(pcError);
}
template <typename E>
CResult<void, E> CResult<void, E>::cFail(std::vector<E*>& vErrors)
{
return CResult<void, E>(vErrors);
}
template <typename E>
bool CResult<void, E>::bIsSuccess() const
{
// Sukces: wektor błędów jest pusty
return v_errors.empty();
}
template <typename E>
const std::vector<E*>& CResult<void, E>::vGetErrors() const
{
// Dostęp do wektora błędów
return v_errors;
}
#endif