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 #ifndef _UCOMMON_FILE_H_
53 #include <ucommon/file.h>
54 #endif
55 
56 #include <cstdlib>
57 #include <cstring>
58 #include <stdexcept>
59 
60 #ifndef UCOMMON_SYSRUNTIME
61 #define THROW(x) throw x
62 #define THROWS(x) throw(x)
63 #define THROWS_ANY throw()
64 #else
65 #define THROW(x) ::abort()
66 #define THROWS(x)
67 #define THROWS_ANY
68 #endif
69 
70 namespace ucommon {
71 
83 template <typename T>
84 class temporary
85 {
86 private:
87  __DELETE_COPY(temporary);
88 
89 protected:
90  T *array;
91  size_t used;
92 
93 public:
97  inline temporary(size_t size = 1) {
98  array = new T[size];
99  used = size;
100  }
101 
102  inline temporary(size_t size, const T initial) {
103  array = new T[size];
104  used = size;
105  for(size_t p = 0; p < size; ++p)
106  array[p] = initial;
107  }
108 
109  inline explicit temporary(const T initial) {
110  array = new T[1];
111  used = 1;
112  array[0] = initial;
113  }
114 
115  inline ~temporary() {
116  if(array) {
117  delete[] array;
118  array = NULL;
119  }
120  }
121 
122  inline operator T&() const {
123  return array[0];
124  }
125 
130  inline T& operator*() const {
131  return array[0];
132  }
133 
138  inline T* operator->() const {
139  return &array[0];
140  }
141 
142  inline operator bool() const {
143  return array != NULL;
144  }
145 
146  inline bool operator!() const {
147  return array == NULL;
148  }
149 
150  inline temporary& operator=(const T initial) {
151  array[0] = initial;
152  return *this;
153  }
154 
155  inline void release() {
156  if(array) {
157  delete[] array;
158  array = NULL;
159  }
160  }
161 
162  inline T& operator[](size_t index) const {
163  crit(index < used, "array out of bound");
164  return array[index];
165  }
166 
167  inline T* operator()(size_t index) const {
168  crit(index < used, "array out of bound");
169  return &array[index];
170  }
171 
172  inline void operator()(size_t index, const T value) {
173  crit(index < used, "array out of bound");
174  array[index] = value;
175  }
176 
177  inline T& value(size_t index) const {
178  crit(index < used, "array out of bound");
179  return array[index];
180  }
181 
182  inline void value(size_t index, const T value) {
183  crit(index < used, "array out of bound");
184  array[index] = value;
185  }
186 
187  inline size_t read(file& fp) {
188  return (*fp == NULL) || (array == NULL) ?
189  0 : fread(array, sizeof(T), used, *fp);
190  }
191 
192  inline size_t write(file& fp) {
193  return (*fp == NULL) || (array == NULL) ?
194  0 : fwrite(array, sizeof(T), used, *fp);
195  }
196 
197  inline size_t seek(file& fp, long pos) {
198  return (*fp == NULL) ?
199  0 : (fseek(*fp, sizeof(T) * pos, SEEK_CUR) / sizeof(T));
200  }
201 };
202 
203 template<>
204 class temporary<char *>
205 {
206 private:
207  __DELETE_COPY(temporary);
208 
209 protected:
210  char *object;
211  size_t used;
212 
213 public:
217  inline temporary(size_t size) {
218  object = (char *)::malloc(size);
219  used = size;
220  }
221 
222  inline operator char *() const {
223  return object;
224  }
225 
226  inline size_t size() const {
227  return used;
228  }
229 
234  inline char *operator*() const {
235  return object;
236  }
237 
238  inline operator bool() const {
239  return object != NULL;
240  }
241 
242  inline bool operator!() const {
243  return object == NULL;
244  }
245 
246  inline void release() {
247  if(object) {
248  ::free(object);
249  object = NULL;
250  }
251  }
252 
253  inline ~temporary() {
254  if(object) {
255  ::free(object);
256  object = NULL;
257  }
258  }
259 
260  inline size_t read(file& fp) {
261  return (*fp == NULL) || (object == NULL) ?
262  0 : String::count(fgets(object, (socksize_t)used, *fp));
263  }
264 
265  inline size_t write(file& fp) {
266  return (*fp == NULL) || (object == NULL) ?
267  0 : fputs(object, *fp);
268  }
269 
270  inline size_t seek(file& fp, long pos) {
271  return (*fp == NULL) ?
272  0 : fseek(*fp, pos, SEEK_CUR);
273  }
274 };
275 
276 template<>
277 class temporary<uint8_t *>
278 {
279 private:
280  inline temporary(const temporary<uint8_t *>&) {};
281 
282 protected:
283  uint8_t *object;
284  size_t used;
285 
286 public:
290  inline temporary(size_t size) {
291  object = (uint8_t *)::malloc(size);
292  used = size;
293  }
294 
295  inline operator uint8_t *() const {
296  return object;
297  }
298 
299  inline size_t size() const {
300  return used;
301  }
302 
307  inline uint8_t *operator*() const {
308  return object;
309  }
310 
311  inline operator bool() const {
312  return object != NULL;
313  }
314 
315  inline bool operator!() const {
316  return object == NULL;
317  }
318 
319  inline void release() {
320  if(object) {
321  ::free(object);
322  object = NULL;
323  }
324  }
325 
326  inline size_t read(file& fp) {
327  return (*fp == NULL) || (object == NULL) ?
328  0 : fread(object, 1, used, *fp);
329  }
330 
331  inline size_t write(file& fp) {
332  return (*fp == NULL) || (object == NULL) ?
333  0 : fwrite(object, 1, used, *fp);
334  }
335 
336  inline size_t seek(file& fp, long pos) {
337  return (*fp == NULL) ?
338  0 : fseek(*fp, pos, SEEK_CUR);
339  }
340 
341  inline size_t read(fsys& fs) {
342  ssize_t result;
343  if(!object || (result = fs.read(object, used)) < 0)
344  return 0;
345  return (size_t)result;
346  }
347 
348  inline size_t write(fsys& fs) {
349  ssize_t result;
350  if(!object || (result = fs.write(object, used)) < 0)
351  return 0;
352  return (size_t)result;
353  }
354 
355  inline ~temporary() {
356  if(object) {
357  ::free(object);
358  object = NULL;
359  }
360  }
361 };
362 
363 } // namespace ucommon
364 
365 #endif
A common string class and character string support functions.
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:280
Thread classes and sychronization objects.
Various miscellaneous platform specific headers and defines.
Thread-aware file system manipulation class.
Adaption of C runtime FILE processing.
Manage temporary object stored on the heap.
Definition: temporary.h:84
T * operator->() const
Access members of our heap object through our temporary.
Definition: temporary.h:138
Private heaps, pools, and associations.
T & operator*() const
Access heap object through our temporary directly.
Definition: temporary.h:130
temporary(size_t size=1)
Construct a temporary object, create our stack frame reference.
Definition: temporary.h:97
Common namespace for all ucommon objects.
Definition: access.h:47
strsize_t count(void) const
Count all characters in the string (strlen).
Abstract interfaces and support.