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)  

arrayref.h
Go to the documentation of this file.
1 // Copyright (C) 2015 Cherokees of Idaho.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
25 #ifndef _UCOMMON_ARRAYREF_H_
26 #define _UCOMMON_ARRAYREF_H_
27 
28 #ifndef _UCOMMON_CPR_H_
29 #include <ucommon/cpr.h>
30 #endif
31 
32 #ifndef _UCOMMON_ATOMIC_H_
33 #include <ucommon/atomic.h>
34 #endif
35 
36 #ifndef _UCOMMON_PROTOCOLS_H_
37 #include <ucommon/protocols.h>
38 #endif
39 
40 #ifndef _UCOMMON_OBJECT_H_
41 #include <ucommon/object.h>
42 #endif
43 
44 #ifndef _UCOMMON_TYPEREF_H_
45 #include <ucommon/typeref.h>
46 #endif
47 
48 #ifndef _UCOMMON_THREAD_H_
49 #include <ucommon/thread.h>
50 #endif
51 
52 namespace ucommon {
53 
54 class __EXPORT ArrayRef : public TypeRef
55 {
56 protected:
57  typedef enum {ARRAY, STACK, QUEUE, FALLBACK} arraytype_t;
58 
59  class __EXPORT Array : public Counted, public ConditionalAccess
60  {
61  private:
63 
64  protected:
65  friend class ArrayRef;
66 
67  size_t head, tail;
68 
70 
71  explicit Array(arraytype_t mode, void *addr, size_t size);
72 
73  void assign(size_t index, Counted *object);
74 
75  Counted *remove(size_t index);
76 
77  size_t count(void);
78 
79  virtual void dealloc() __OVERRIDE;
80 
81  inline Counted **get(void) {
82  return reinterpret_cast<Counted **>(((caddr_t)(this)) + sizeof(Array));
83  }
84 
85  Counted *get(size_t index);
86  };
87 
88  ArrayRef(arraytype_t mode, size_t size);
89  ArrayRef(arraytype_t mode, size_t size, TypeRef& object);
90  ArrayRef(const ArrayRef& copy);
91  ArrayRef();
92 
93  void assign(size_t index, TypeRef& t);
94 
95  void reset(TypeRef& object);
96 
97  void reset(Counted *object);
98 
99  Counted *get(size_t index);
100 
101  bool is(size_t index);
102 
103  static Array *create(arraytype_t type, size_t size);
104 
105 protected:
106  void push(const TypeRef& object);
107 
108  void pull(TypeRef& object);
109 
110  bool push(const TypeRef& object, timeout_t timeout);
111 
112  void pull(TypeRef& object, timeout_t timeout);
113 
114 public:
115  size_t count(void);
116 
117  void resize(size_t size);
118 
119  void realloc(size_t size);
120 
121  void clear(void);
122 
123  void pop(void);
124 };
125 
126 template<typename T>
127 class stackref : public ArrayRef
128 {
129 public:
130  inline stackref() : ArrayRef() {};
131 
132  inline stackref(const stackref& copy) : ArrayRef(copy) {};
133 
134  inline stackref(size_t size) : ArrayRef(STACK, size + 1) {};
135 
136  inline stackref& operator=(const stackref& copy) {
138  return *this;
139  }
140 
141  inline typeref<T> operator[](size_t index) {
142  return typeref<T>(ArrayRef::get(index));
143  }
144 
145  inline typeref<T> operator()(size_t index) {
146  return typeref<T>(ArrayRef::get(index));
147  }
148 
149  inline typeref<T> at(size_t index) {
150  return typeref<T>(ArrayRef::get(index));
151  }
152 
153  inline void release(void) {
154  TypeRef::set(nullptr);
155  }
156 
157  inline typeref<T> pull() {
158  typeref<T> obj;
159  ArrayRef::pull(obj);
160  return obj;
161  }
162 
164  typeref<T> obj;
165  ArrayRef::pull(obj, timeout);
166  return obj;
167  }
168 
169  inline stackref& operator>>(typeref<T>& target) {
170  ArrayRef::pull(target);
171  return *this;
172  }
173 
174  inline void push(const typeref<T>& source) {
175  ArrayRef::push(source);
176  }
177 
178  inline bool push(const typeref<T>& source, timeout_t timeout) {
179  return ArrayRef::push(source, timeout);
180  }
181 
182  inline stackref& operator<<(const typeref<T>& source) {
183  ArrayRef::push(source);
184  return *this;
185  }
186 
187  inline stackref& operator<<(T t) {
188  typeref<T> v(t);
189  ArrayRef::push(v);
190  return *this;
191  }
192 };
193 
194 template<typename T>
195 class queueref : public ArrayRef
196 {
197 public:
198  inline queueref() : ArrayRef() {};
199 
200  inline queueref(const queueref& copy) : ArrayRef(copy) {};
201 
202  inline queueref(size_t size, bool fallback = false) : ArrayRef(fallback ? FALLBACK : QUEUE, size + 1) {};
203 
204  inline queueref& operator=(const queueref& copy) {
206  return *this;
207  }
208 
209  inline typeref<T> operator[](size_t index) {
210  return typeref<T>(ArrayRef::get(index));
211  }
212 
213  inline typeref<T> operator()(size_t index) {
214  return typeref<T>(ArrayRef::get(index));
215  }
216 
217  inline typeref<T> at(size_t index) {
218  return typeref<T>(ArrayRef::get(index));
219  }
220 
221  inline void release(void) {
222  TypeRef::set(nullptr);
223  }
224 
225  inline typeref<T> pull() {
226  typeref<T> obj;
227  ArrayRef::pull(obj);
228  return obj;
229  }
230 
232  typeref<T> obj;
233  ArrayRef::pull(obj, timeout);
234  return obj;
235  }
236 
237  inline queueref& operator>>(typeref<T>& target) {
238  ArrayRef::pull(target);
239  return *this;
240  }
241 
242  inline void push(const typeref<T>& source) {
243  ArrayRef::push(source);
244  }
245 
246  inline bool push(const typeref<T>& source, timeout_t timeout) {
247  return ArrayRef::push(source, timeout);
248  }
249 
250  inline queueref& operator<<(const typeref<T>& source) {
251  ArrayRef::push(source);
252  return *this;
253  }
254 
255  inline queueref& operator<<(T t) {
256  typeref<T> v(t);
257  ArrayRef::push(v);
258  return *this;
259  }
260 };
261 
262 template<typename T>
263 class arrayref : public ArrayRef
264 {
265 public:
266  inline arrayref() : ArrayRef() {};
267 
268  inline arrayref(const arrayref& copy) : ArrayRef(copy) {};
269 
270  inline arrayref(size_t size) : ArrayRef(ARRAY, size) {};
271 
272  inline arrayref(size_t size, typeref<T>& t) : ArrayRef(ARRAY, size, t) {};
273 
274  inline arrayref(size_t size, T t) : ArrayRef(ARRAY, size) {
275  typeref<T> v(t);
276  reset(v);
277  }
278 
279  inline arrayref& operator=(const arrayref& copy) {
281  return *this;
282  }
283 
285  reset(t);
286  return *this;
287  }
288 
289  inline arrayref& operator=(T t) {
290  typeref<T> v(t);
291  reset(v);
292  }
293 
294  inline typeref<T> operator[](size_t index) {
295  return typeref<T>(ArrayRef::get(index));
296  }
297 
298  inline typeref<T> operator()(size_t index) {
299  return typeref<T>(ArrayRef::get(index));
300  }
301 
302  inline typeref<T> at(size_t index) {
303  return typeref<T>(ArrayRef::get(index));
304  }
305 
306  inline typeref<T> value(size_t index) {
307  return typeref<T>(ArrayRef::get(index));
308  }
309 
310  inline void value(size_t index, typeref<T>& t) {
311  ArrayRef::assign(index, t);
312  }
313 
314  inline void put(typeref<T>& target, size_t index) {
315  TypeRef::put(target, ArrayRef::get(index));
316  }
317 
318  inline void operator()(size_t index, typeref<T>& t) {
319  ArrayRef::assign(index, t);
320  }
321 
322  inline void operator()(size_t index, T t) {
323  typeref<T> v(t);
324  ArrayRef::assign(index, v);
325  }
326 
327  inline void release(void) {
328  TypeRef::set(nullptr);
329  }
330 };
331 
334 
335 } // namespace
336 
337 #endif
ucommon::queueref::operator()
typeref< T > operator()(size_t index)
Definition: arrayref.h:213
ucommon::stackref::release
void release(void)
Definition: arrayref.h:153
ucommon::stackref::stackref
stackref()
Definition: arrayref.h:130
ucommon::queueref::pull
typeref< T > pull(timeout_t timeout)
Definition: arrayref.h:231
ucommon::arrayref::operator()
typeref< T > operator()(size_t index)
Definition: arrayref.h:298
ucommon::queueref::queueref
queueref(const queueref &copy)
Definition: arrayref.h:200
ucommon::queueref::release
void release(void)
Definition: arrayref.h:221
ucommon
Definition: access.cpp:23
ucommon::stackref::operator()
typeref< T > operator()(size_t index)
Definition: arrayref.h:145
ucommon::queueref
Definition: arrayref.h:195
ucommon::arrayref::put
void put(typeref< T > &target, size_t index)
Definition: arrayref.h:314
ucommon::queueref::queueref
queueref()
Definition: arrayref.h:198
protocols.h
ucommon::stackref::operator<<
stackref & operator<<(T t)
Definition: arrayref.h:187
object.h
__OVERRIDE
#define __OVERRIDE
Definition: platform.h:158
ucommon::TypeRef::set
void set(Counted *object)
Definition: typeref.cpp:125
ucommon::queueref::push
bool push(const typeref< T > &source, timeout_t timeout)
Definition: arrayref.h:246
timeout_t
unsigned long timeout_t
Definition: platform.h:453
ucommon::arrayref::at
typeref< T > at(size_t index)
Definition: arrayref.h:302
ucommon::ArrayRef::ARRAY
Definition: arrayref.h:57
ucommon::arrayref::arrayref
arrayref()
Definition: arrayref.h:266
ucommon::TypeRef::size
size_t size(void) const
Definition: typeref.cpp:504
ucommon::ArrayRef::push
void push(const TypeRef &object)
Definition: arrayref.cpp:261
ucommon::queueref::push
void push(const typeref< T > &source)
Definition: arrayref.h:242
ucommon::copy
T copy(const T &src)
Definition: generics.h:395
caddr_t
#define caddr_t
Definition: file.h:86
ucommon::ArrayRef
Definition: arrayref.h:54
ucommon::arrayref::operator=
arrayref & operator=(T t)
Definition: arrayref.h:289
ucommon::stackref::operator[]
typeref< T > operator[](size_t index)
Definition: arrayref.h:141
ucommon::stackref::operator>>
stackref & operator>>(typeref< T > &target)
Definition: arrayref.h:169
ucommon::ArrayRef::assign
void assign(size_t index, TypeRef &t)
Definition: arrayref.cpp:368
ucommon::clear
T & clear(T &o)
Definition: generics.h:416
ucommon::queueref::at
typeref< T > at(size_t index)
Definition: arrayref.h:217
ucommon::ArrayRef::STACK
Definition: arrayref.h:57
ucommon::ArrayRef::pull
void pull(TypeRef &object)
Definition: arrayref.cpp:325
ucommon::TypeRef::put
static void put(TypeRef &target, Counted *object)
Definition: typeref.h:259
ucommon::ArrayRef::QUEUE
Definition: arrayref.h:57
ucommon::ConditionalAccess
Definition: condition.h:337
ucommon::queueref::queueref
queueref(size_t size, bool fallback=false)
Definition: arrayref.h:202
__EXPORT
#define __EXPORT
Definition: config.h:49
ucommon::arrayref::operator=
arrayref & operator=(typeref< T > &t)
Definition: arrayref.h:284
ucommon::arrayref::operator[]
typeref< T > operator[](size_t index)
Definition: arrayref.h:294
thread.h
ucommon::bytearray_t
arrayref< Type::Bytes > bytearray_t
Definition: arrayref.h:332
ucommon::queueref::operator>>
queueref & operator>>(typeref< T > &target)
Definition: arrayref.h:237
ucommon::ArrayRef::arraytype_t
arraytype_t
Definition: arrayref.h:57
ucommon::queueref::operator=
queueref & operator=(const queueref &copy)
Definition: arrayref.h:204
ucommon::arrayref::operator()
void operator()(size_t index, T t)
Definition: arrayref.h:322
ucommon::arrayref::arrayref
arrayref(size_t size, T t)
Definition: arrayref.h:274
ucommon::ArrayRef::Array::tail
size_t tail
Definition: arrayref.h:67
ucommon::TypeRef::Counted
Definition: typeref.h:86
ucommon::stackref::pull
typeref< T > pull(timeout_t timeout)
Definition: arrayref.h:163
cpr.h
ucommon::arrayref::value
void value(size_t index, typeref< T > &t)
Definition: arrayref.h:310
ucommon::stackref::pull
typeref< T > pull()
Definition: arrayref.h:157
ucommon::stackref::at
typeref< T > at(size_t index)
Definition: arrayref.h:149
ucommon::queueref::pull
typeref< T > pull()
Definition: arrayref.h:225
ucommon::ArrayRef::Array
Definition: arrayref.h:59
ucommon::queueref::operator[]
typeref< T > operator[](size_t index)
Definition: arrayref.h:209
ucommon::typeref
Definition: typeref.h:326
__DELETE_DEFAULTS
#define __DELETE_DEFAULTS(x)
Definition: platform.h:162
ucommon::stackref
Definition: arrayref.h:127
ucommon::ArrayRef::get
Counted * get(size_t index)
Definition: arrayref.cpp:425
timeout
static shell::numericopt timeout('t', "--timeout", _TEXT("optional keyboard input timeout"), "seconds", 0)
ucommon::TypeRef
Definition: typeref.h:68
ucommon::stackref::operator<<
stackref & operator<<(const typeref< T > &source)
Definition: arrayref.h:182
ucommon::arrayref
Definition: arrayref.h:263
ucommon::stackref::push
void push(const typeref< T > &source)
Definition: arrayref.h:174
atomic.h
ucommon::arrayref::operator=
arrayref & operator=(const arrayref &copy)
Definition: arrayref.h:279
ucommon::stringarray_t
arrayref< Type::Chars > stringarray_t
Definition: arrayref.h:333
ucommon::stackref::stackref
stackref(size_t size)
Definition: arrayref.h:134
ucommon::ArrayRef::reset
void reset(TypeRef &object)
Definition: arrayref.cpp:208
ucommon::arrayref::value
typeref< T > value(size_t index)
Definition: arrayref.h:306
ucommon::arrayref::arrayref
arrayref(size_t size)
Definition: arrayref.h:270
ucommon::stackref::operator=
stackref & operator=(const stackref &copy)
Definition: arrayref.h:136
ucommon::arrayref::arrayref
arrayref(size_t size, typeref< T > &t)
Definition: arrayref.h:272
ucommon::arrayref::release
void release(void)
Definition: arrayref.h:327
ucommon::stackref::stackref
stackref(const stackref &copy)
Definition: arrayref.h:132
ucommon::ArrayRef::FALLBACK
Definition: arrayref.h:57
ucommon::queueref::operator<<
queueref & operator<<(T t)
Definition: arrayref.h:255
ucommon::queueref::operator<<
queueref & operator<<(const typeref< T > &source)
Definition: arrayref.h:250
ucommon::addr
const struct sockaddr * addr(Socket::address &address)
Definition: socket.h:2089
ucommon::arrayref::arrayref
arrayref(const arrayref &copy)
Definition: arrayref.h:268
ucommon::stackref::push
bool push(const typeref< T > &source, timeout_t timeout)
Definition: arrayref.h:178
ucommon::is
bool is(T &object)
Definition: generics.h:292
ucommon::arrayref::operator()
void operator()(size_t index, typeref< T > &t)
Definition: arrayref.h:318
typeref.h
ucommon::ArrayRef::Array::type
arraytype_t type
Definition: arrayref.h:69