UCommon
temporary.h
Go to the documentation of this file.
1// Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2// Copyright (C) 2015-2020 Cherokees of Idaho.
3//
4// This file is part of GNU uCommon C++.
5//
6// GNU uCommon C++ is free software: you can redistribute it and/or modify
7// it under the terms of the GNU Lesser General Public License as published
8// by the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// GNU uCommon C++ is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU Lesser General Public License for more details.
15//
16// You should have received a copy of the GNU Lesser General Public License
17// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18
25#ifndef _UCOMMON_TEMPORARY_H_
26#define _UCOMMON_TEMPORARY_H_
27
28#ifndef _UCOMMON_CONFIG_H_
29#include <ucommon/platform.h>
30#endif
31
32#ifndef _UCOMMON_PROTOCOLS_H_
33#include <ucommon/protocols.h>
34#endif
35
36#ifndef _UCOMMON_THREAD_H_
37#include <ucommon/thread.h>
38#endif
39
40#ifndef _UCOMMON_STRING_H_
41#include <ucommon/string.h>
42#endif
43
44#ifndef _UCOMMON_MEMORY_H_
45#include <ucommon/memory.h>
46#endif
47
48#ifndef _UCOMMON_FSYS_H_
49#include <ucommon/fsys.h>
50#endif
51
52#include <cstdlib>
53#include <cstring>
54#include <stdexcept>
55
56#ifndef UCOMMON_SYSRUNTIME
57#define THROW(x) throw x
58#if __cplusplus > 199711L
59#define THROWS(x)
60#define THROWS_ANY
61#else
62#define THROWS(x) throw(x)
63#define THROWS_ANY throw()
64#endif
65#else
66#define THROW(x) ::abort()
67#define THROWS(x)
68#define THROWS_ANY
69#endif
70
71namespace ucommon {
72
84template <typename T>
86{
87private:
88 __DELETE_COPY(temporary);
89
90protected:
91 T *array;
92 size_t used;
93
94public:
98 inline temporary(size_t size = 1) {
99 array = new T[size];
100 used = size;
101 }
102
103 inline temporary(size_t size, const T initial) {
104 array = new T[size];
105 used = size;
106 for(size_t p = 0; p < size; ++p)
107 array[p] = initial;
108 }
109
110 inline explicit temporary(const T initial) {
111 array = new T[1];
112 used = 1;
113 array[0] = initial;
114 }
115
116 inline ~temporary() {
117 if(array) {
118 delete[] array;
119 array = NULL;
120 }
121 }
122
123 inline operator T&() const {
124 return array[0];
125 }
126
131 inline T& operator*() const {
132 return array[0];
133 }
134
139 inline T* operator->() const {
140 return &array[0];
141 }
142
143 inline operator bool() const {
144 return array != NULL;
145 }
146
147 inline bool operator!() const {
148 return array == NULL;
149 }
150
151 inline temporary& operator=(const T initial) {
152 array[0] = initial;
153 return *this;
154 }
155
156 inline void release() {
157 if(array) {
158 delete[] array;
159 array = NULL;
160 }
161 }
162
163 inline T& operator[](size_t index) const {
164 crit(index < used, "array out of bound");
165 return array[index];
166 }
167
168 inline T* operator()(size_t index) const {
169 crit(index < used, "array out of bound");
170 return &array[index];
171 }
172
173 inline void operator()(size_t index, const T value) {
174 crit(index < used, "array out of bound");
175 array[index] = value;
176 }
177
178 inline T& value(size_t index) const {
179 crit(index < used, "array out of bound");
180 return array[index];
181 }
182
183 inline void value(size_t index, const T value) {
184 crit(index < used, "array out of bound");
185 array[index] = value;
186 }
187
188 inline size_t read(FILE *fp) {
189 return (fp == NULL) || (array == NULL) ?
190 0 : fread(array, sizeof(T), used, fp);
191 }
192
193 inline size_t write(FILE *fp) {
194 return (fp == NULL) || (array == NULL) ?
195 0 : fwrite(array, sizeof(T), used, fp);
196 }
197
198 inline size_t seek(FILE *fp, long pos) {
199 return (fp == NULL) ?
200 0 : (fseek(fp, sizeof(T) * pos, SEEK_CUR) / sizeof(T));
201 }
202};
203
204template<>
205class temporary<char *>
206{
207private:
208 __DELETE_COPY(temporary);
209
210protected:
211 char *object;
212 size_t used;
213
214public:
218 inline temporary(size_t size) {
219 object = (char *)::malloc(size);
220 used = size;
221 }
222
223 inline operator char *() const {
224 return object;
225 }
226
227 inline size_t size() const {
228 return used;
229 }
230
235 inline char *operator*() const {
236 return object;
237 }
238
239 inline operator bool() const {
240 return object != NULL;
241 }
242
243 inline bool operator!() const {
244 return object == NULL;
245 }
246
247 inline void release() {
248 if(object) {
249 ::free(object);
250 object = NULL;
251 }
252 }
253
254 inline ~temporary() {
255 if(object) {
256 ::free(object);
257 object = NULL;
258 }
259 }
260
261 inline size_t read(FILE *fp) {
262 return (fp == NULL) || (object == NULL) ?
263 0 : String::count(fgets(object, (socksize_t)used, fp));
264 }
265
266 inline size_t write(FILE *fp) {
267 return (fp == NULL) || (object == NULL) ?
268 0 : fputs(object, fp);
269 }
270
271 inline size_t seek(FILE *fp, long pos) {
272 return (fp == NULL) ?
273 0 : fseek(fp, pos, SEEK_CUR);
274 }
275};
276
277template<>
278class temporary<uint8_t *>
279{
280private:
281 inline temporary(const temporary<uint8_t *>&) {};
282
283protected:
284 uint8_t *object;
285 size_t used;
286
287public:
291 inline temporary(size_t size) {
292 object = (uint8_t *)::malloc(size);
293 used = size;
294 }
295
296 inline operator uint8_t *() const {
297 return object;
298 }
299
300 inline size_t size() const {
301 return used;
302 }
303
308 inline uint8_t *operator*() const {
309 return object;
310 }
311
312 inline operator bool() const {
313 return object != NULL;
314 }
315
316 inline bool operator!() const {
317 return object == NULL;
318 }
319
320 inline void release() {
321 if(object) {
322 ::free(object);
323 object = NULL;
324 }
325 }
326
327 inline size_t read(FILE *fp) {
328 return (fp == NULL) || (object == NULL) ?
329 0 : fread(object, 1, used, fp);
330 }
331
332 inline size_t write(FILE *fp) {
333 return (fp == NULL) || (object == NULL) ?
334 0 : fwrite(object, 1, used, fp);
335 }
336
337 inline size_t seek(FILE *fp, long pos) {
338 return (fp == NULL) ?
339 0 : fseek(fp, pos, SEEK_CUR);
340 }
341
342 inline size_t read(fsys& fs) {
343 ssize_t result;
344 if(!object || (result = fs.read(object, used)) < 0)
345 return 0;
346 return (size_t)result;
347 }
348
349 inline size_t write(fsys& fs) {
350 ssize_t result;
351 if(!object || (result = fs.write(object, used)) < 0)
352 return 0;
353 return (size_t)result;
354 }
355
356 inline ~temporary() {
357 if(object) {
358 ::free(object);
359 object = NULL;
360 }
361 }
362};
363
364} // namespace ucommon
365
366#endif
Private heaps, pools, and associations.
Various miscellaneous platform specific headers and defines.
Thread-aware file system manipulation class.
Abstract interfaces and support.
Common namespace for all ucommon objects.
Definition access.h:47
Manage temporary object stored on the heap.
Definition temporary.h:86
T * operator->() const
Access members of our heap object through our temporary.
Definition temporary.h:139
T & operator*() const
Access heap object through our temporary directly.
Definition temporary.h:131
temporary(size_t size=1)
Construct a temporary object, create our stack frame reference.
Definition temporary.h:98
A common string class and character string support functions.
Thread classes and sychronization objects.