ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

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:
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:
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
ucommon::temporary< char * >::~temporary
~temporary()
Definition: temporary.h:249
ucommon::temporary< char * >::read
size_t read(FILE *fp)
Definition: temporary.h:256
ucommon::fsys::write
ssize_t write(const void *buffer, size_t count)
Definition: fsys.cpp:769
ucommon::temporary< char * >::operator!
bool operator!() const
Definition: temporary.h:238
ucommon::temporary
Definition: temporary.h:80
ucommon::temporary::operator->
T * operator->() const
Definition: temporary.h:134
ucommon::temporary< char * >::size
size_t size() const
Definition: temporary.h:222
ucommon::temporary< char * >::release
void release()
Definition: temporary.h:242
ucommon::temporary< uint8_t * >::temporary
temporary(const temporary< uint8_t * > &)
Definition: temporary.h:276
ucommon::fsys::read
ssize_t read(void *buffer, size_t count)
Definition: fsys.cpp:750
ucommon::temporary::operator*
T & operator*() const
Definition: temporary.h:126
ucommon::temporary< uint8_t * >::~temporary
~temporary()
Definition: temporary.h:351
ucommon
Definition: access.cpp:23
ucommon::temporary::temporary
temporary(size_t size, const T initial)
Definition: temporary.h:98
ucommon::temporary::__DELETE_COPY
__DELETE_COPY(temporary)
protocols.h
ucommon::temporary< char * >::operator*
char * operator*() const
Definition: temporary.h:230
ucommon::temporary::operator=
temporary & operator=(const T initial)
Definition: temporary.h:146
ucommon::temporary< uint8_t * >::operator!
bool operator!() const
Definition: temporary.h:311
ucommon::temporary< char * >::used
size_t used
Definition: temporary.h:207
result
static void result(const char *path, int code)
Definition: mdsum.cpp:35
ucommon::temporary::~temporary
~temporary()
Definition: temporary.h:111
ucommon::temporary< char * >::seek
size_t seek(FILE *fp, long pos)
Definition: temporary.h:266
ucommon::temporary< char * >::write
size_t write(FILE *fp)
Definition: temporary.h:261
ucommon::temporary::release
void release()
Definition: temporary.h:151
ucommon::temporary::operator()
void operator()(size_t index, const T value)
Definition: temporary.h:168
ucommon::temporary< uint8_t * >::write
size_t write(fsys &fs)
Definition: temporary.h:344
ucommon::temporary< uint8_t * >::seek
size_t seek(FILE *fp, long pos)
Definition: temporary.h:332
ucommon::temporary::seek
size_t seek(FILE *fp, long pos)
Definition: temporary.h:193
thread.h
ucommon::temporary< uint8_t * >::operator*
uint8_t * operator*() const
Definition: temporary.h:303
ucommon::temporary::value
T & value(size_t index) const
Definition: temporary.h:173
ucommon::temporary< uint8_t * >::release
void release()
Definition: temporary.h:315
ucommon::temporary::used
size_t used
Definition: temporary.h:87
crit
#define crit(x, text)
Definition: platform.h:541
ucommon::temporary< char * >::object
char * object
Definition: temporary.h:206
ucommon::temporary< uint8_t * >::write
size_t write(FILE *fp)
Definition: temporary.h:327
ucommon::temporary< uint8_t * >::read
size_t read(FILE *fp)
Definition: temporary.h:322
__DELETE_COPY
#define __DELETE_COPY(x)
Definition: platform.h:160
platform.h
ucommon::temporary::read
size_t read(FILE *fp)
Definition: temporary.h:183
ucommon::temporary::operator()
T * operator()(size_t index) const
Definition: temporary.h:163
fsys.h
ucommon::temporary< char * >::temporary
temporary(size_t size)
Definition: temporary.h:213
ucommon::temporary::write
size_t write(FILE *fp)
Definition: temporary.h:188
socksize_t
size_t socksize_t
Definition: platform.h:296
ucommon::fsys
Definition: fsys.h:125
string.h
ucommon::temporary< uint8_t * >::read
size_t read(fsys &fs)
Definition: temporary.h:337
ucommon::temporary::temporary
temporary(const T initial)
Definition: temporary.h:105
ucommon::temporary::operator[]
T & operator[](size_t index) const
Definition: temporary.h:158
ucommon::temporary::temporary
temporary(size_t size=1)
Definition: temporary.h:93
ucommon::temporary< uint8_t * >::size
size_t size() const
Definition: temporary.h:295
ucommon::temporary< uint8_t * >::used
size_t used
Definition: temporary.h:280
ucommon::temporary< uint8_t * >
Definition: temporary.h:273
ucommon::String::count
size_t count(void) const
Definition: string.cpp:618
memory.h
ucommon::temporary< uint8_t * >::temporary
temporary(size_t size)
Definition: temporary.h:286
ucommon::temporary::value
void value(size_t index, const T value)
Definition: temporary.h:178
ucommon::temporary::array
T * array
Definition: temporary.h:86
ucommon::temporary::operator!
bool operator!() const
Definition: temporary.h:142