UCommon
temporary.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 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 #define THROWS(x) throw(x)
59 #define THROWS_ANY throw()
60 #else
61 #define THROW(x) ::abort()
62 #define THROWS(x)
63 #define THROWS_ANY
64 #endif
65 
66 namespace ucommon {
67 
79 template <typename T>
80 class temporary
81 {
82 private:
83  __DELETE_COPY(temporary);
84 
85 protected:
86  T *array;
87  size_t used;
88 
89 public:
93  inline temporary(size_t size = 1) {
94  array = new T[size];
95  used = size;
96  }
97 
98  inline temporary(size_t size, const T initial) {
99  array = new T[size];
100  used = size;
101  for(size_t p = 0; p < size; ++p)
102  array[p] = initial;
103  }
104 
105  inline explicit temporary(const T initial) {
106  array = new T[1];
107  used = 1;
108  array[0] = initial;
109  }
110 
111  inline ~temporary() {
112  if(array) {
113  delete[] array;
114  array = NULL;
115  }
116  }
117 
118  inline operator T&() const {
119  return array[0];
120  }
121 
126  inline T& operator*() const {
127  return array[0];
128  }
129 
134  inline T* operator->() const {
135  return &array[0];
136  }
137 
138  inline operator bool() const {
139  return array != NULL;
140  }
141 
142  inline bool operator!() const {
143  return array == NULL;
144  }
145 
146  inline temporary& operator=(const T initial) {
147  array[0] = initial;
148  return *this;
149  }
150 
151  inline void release() {
152  if(array) {
153  delete[] array;
154  array = NULL;
155  }
156  }
157 
158  inline T& operator[](size_t index) const {
159  crit(index < used, "array out of bound");
160  return array[index];
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 void operator()(size_t index, const T value) {
169  crit(index < used, "array out of bound");
170  array[index] = value;
171  }
172 
173  inline T& value(size_t index) const {
174  crit(index < used, "array out of bound");
175  return array[index];
176  }
177 
178  inline void value(size_t index, const T value) {
179  crit(index < used, "array out of bound");
180  array[index] = value;
181  }
182 
183  inline size_t read(FILE *fp) {
184  return (fp == NULL) || (array == NULL) ?
185  0 : fread(array, sizeof(T), used, fp);
186  }
187 
188  inline size_t write(FILE *fp) {
189  return (fp == NULL) || (array == NULL) ?
190  0 : fwrite(array, sizeof(T), used, fp);
191  }
192 
193  inline size_t seek(FILE *fp, long pos) {
194  return (fp == NULL) ?
195  0 : (fseek(fp, sizeof(T) * pos, SEEK_CUR) / sizeof(T));
196  }
197 };
198 
199 template<>
200 class temporary<char *>
201 {
202 private:
203  __DELETE_COPY(temporary);
204 
205 protected:
206  char *object;
207  size_t used;
208 
209 public:
213  inline temporary(size_t size) {
214  object = (char *)::malloc(size);
215  used = size;
216  }
217 
218  inline operator char *() const {
219  return object;
220  }
221 
222  inline size_t size() const {
223  return used;
224  }
225 
230  inline char *operator*() const {
231  return object;
232  }
233 
234  inline operator bool() const {
235  return object != NULL;
236  }
237 
238  inline bool operator!() const {
239  return object == NULL;
240  }
241 
242  inline void release() {
243  if(object) {
244  ::free(object);
245  object = NULL;
246  }
247  }
248 
249  inline ~temporary() {
250  if(object) {
251  ::free(object);
252  object = NULL;
253  }
254  }
255 
256  inline size_t read(FILE *fp) {
257  return (fp == NULL) || (object == NULL) ?
258  0 : String::count(fgets(object, (socksize_t)used, fp));
259  }
260 
261  inline size_t write(FILE *fp) {
262  return (fp == NULL) || (object == NULL) ?
263  0 : fputs(object, fp);
264  }
265 
266  inline size_t seek(FILE *fp, long pos) {
267  return (fp == NULL) ?
268  0 : fseek(fp, pos, SEEK_CUR);
269  }
270 };
271 
272 template<>
273 class temporary<uint8_t *>
274 {
275 private:
276  inline temporary(const temporary<uint8_t *>&) {};
277 
278 protected:
279  uint8_t *object;
280  size_t used;
281 
282 public:
286  inline temporary(size_t size) {
287  object = (uint8_t *)::malloc(size);
288  used = size;
289  }
290 
291  inline operator uint8_t *() const {
292  return object;
293  }
294 
295  inline size_t size() const {
296  return used;
297  }
298 
303  inline uint8_t *operator*() const {
304  return object;
305  }
306 
307  inline operator bool() const {
308  return object != NULL;
309  }
310 
311  inline bool operator!() const {
312  return object == NULL;
313  }
314 
315  inline void release() {
316  if(object) {
317  ::free(object);
318  object = NULL;
319  }
320  }
321 
322  inline size_t read(FILE *fp) {
323  return (fp == NULL) || (object == NULL) ?
324  0 : fread(object, 1, used, fp);
325  }
326 
327  inline size_t write(FILE *fp) {
328  return (fp == NULL) || (object == NULL) ?
329  0 : fwrite(object, 1, used, fp);
330  }
331 
332  inline size_t seek(FILE *fp, long pos) {
333  return (fp == NULL) ?
334  0 : fseek(fp, pos, SEEK_CUR);
335  }
336 
337  inline size_t read(fsys& fs) {
338  ssize_t result;
339  if(!object || (result = fs.read(object, used)) < 0)
340  return 0;
341  return (size_t)result;
342  }
343 
344  inline size_t write(fsys& fs) {
345  ssize_t result;
346  if(!object || (result = fs.write(object, used)) < 0)
347  return 0;
348  return (size_t)result;
349  }
350 
351  inline ~temporary() {
352  if(object) {
353  ::free(object);
354  object = NULL;
355  }
356  }
357 };
358 
359 } // namespace ucommon
360 
361 #endif
size_t count(void) const
Count all characters in the string (strlen).
A common string class and character string support functions.
T * operator->() const
Access members of our heap object through our temporary.
Definition: temporary.h:134
Manage temporary object stored on the heap.
Definition: temporary.h:80
Thread classes and sychronization objects.
Private heaps, pools, and associations.
T & operator *() const
Access heap object through our temporary directly.
Definition: temporary.h:126
Various miscellaneous platform specific headers and defines.
Common namespace for all ucommon objects.
Definition: access.h:47
temporary(size_t size=1)
Construct a temporary object, create our stack frame reference.
Definition: temporary.h:93
Thread-aware file system manipulation class.
Abstract interfaces and support.