UCommon
typeref.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 
28 #ifndef _UCOMMON_TYPEREF_H_
29 #define _UCOMMON_TYPEREF_H_
30 
31 #ifndef _UCOMMON_CPR_H_
32 #include <ucommon/cpr.h>
33 #endif
34 
35 #ifndef _UCOMMON_ATOMIC_H_
36 #include <ucommon/atomic.h>
37 #endif
38 
39 #ifndef _UCOMMON_PROTOCOLS_H_
40 #include <ucommon/protocols.h>
41 #endif
42 
43 #ifndef _UCOMMON_GENERICS_H_
44 #include <ucommon/generics.h>
45 #endif
46 
47 #ifndef _UCOMMON_OBJECT_H_
48 #include <ucommon/object.h>
49 #endif
50 
51 namespace ucommon {
52 
61 class __EXPORT TypeRef
62 {
63 protected:
64  friend class ArrayRef;
65  friend class SharedRef;
66  friend class MapRef;
67 
75  class __EXPORT Counted : public ObjectProtocol
76  {
77  private:
78  __DELETE_COPY(Counted);
79 
80  protected:
81  friend class TypeRef;
82 
83  mutable atomic::counter count;
84  size_t size;
85  void *memory;
86 
94  explicit Counted(void *address, size_t size);
95 
101  virtual void dealloc(void);
102 
103  public:
108  inline bool is() const {
109  return (count.get() > 0);
110  }
111 
116  inline unsigned copies() const {
117  return ((unsigned)count.get());
118  }
119 
126  void operator delete(void *address);
127 
132  void retain();
133 
138  void release();
139  };
140 
141  Counted *ref; // heap reference...
142 
147  TypeRef(Counted *object);
148 
154  TypeRef(const TypeRef& pointer);
155 
159  TypeRef();
160 
166  void set(Counted *object);
167 
174  static caddr_t alloc(size_t size);
175 
181  static caddr_t mem(caddr_t address);
182 
183 public:
188  virtual ~TypeRef();
189 
195  void set(const TypeRef& pointer);
196 
200  void clear(void);
201 
206  size_t size(void) const;
207 
212  unsigned copies() const;
213 
218  inline operator bool() const {
219  return ref != NULL;
220  }
221 
226  inline bool operator!() const {
227  return ref == NULL;
228  }
229 
239  inline static void put(TypeRef& target, Counted *object) {
240  target.set(object);
241  }
242 };
243 
244 template<typename T>
245 class typeref : public TypeRef
246 {
247 private:
248  class value : public Counted
249  {
250  private:
251  __DELETE_COPY(value);
252 
253  public:
254  T data;
255 
256  inline value(caddr_t mem) :
257  Counted(mem, sizeof(value)) {};
258 
259  inline value(caddr_t mem, const T& object) :
260  Counted(mem, sizeof(value)) {
261  data = object;
262  }
263  };
264 
265 public:
266  inline typeref() : TypeRef() {};
267 
268  inline typeref(const typeref& copy) : TypeRef(copy) {};
269 
270  inline typeref(const T& object) : TypeRef() {
271  caddr_t p = TypeRef::alloc(sizeof(value));
272  TypeRef::set(new(mem(p)) value(p, object));
273  }
274 
275  inline explicit typeref(Counted *object) : TypeRef(object) {};
276 
277  inline const T* operator->() const {
278  if(!ref)
279  return NULL;
280  value *v = polystatic_cast<value *>(ref);
281  return &(v->data);
282  }
283 
284  inline const T& operator*() const {
285  value *v = polystatic_cast<value*>(ref);
286  return *(&(v->data));
287  }
288 
289  inline const T* operator()() const {
290  if(!ref)
291  return NULL;
292 
293  value *v = polystatic_cast<value*>(ref);
294  return &(v->data);
295  }
296 
297  inline operator const T&() const {
298  value *v = polystatic_cast<value*>(ref);
299  return *(&(v->data));
300  }
301 
302  inline typeref& operator=(const typeref& ptr) {
303  TypeRef::set(ptr);
304  return *this;
305  }
306 
307  inline bool operator==(const typeref& ptr) const {
308  value *v1 = polystatic_cast<value*>(ref);
309  value *v2 = polystatic_cast<value*>(ptr.ref);
310  if(!v1 || !v2)
311  return false;
312  return v1->data == v2->data;
313  }
314 
315  inline bool operator==(const T& obj) const {
316  value *v = polystatic_cast<value *>(ref);
317  if(!v)
318  return false;
319  return v->data == obj;
320  }
321 
322  inline bool operator!=(const typeref& ptr) const {
323  return !(*this == ptr);
324  }
325 
326  inline bool operator!=(const T& obj) const {
327  return !(*this == obj);
328  }
329 
330  inline void set(T& object) {
331  clear();
332  caddr_t p = TypeRef::alloc(sizeof(value));
333  TypeRef::set(new(mem(p)) value(p, object));
334  }
335 
336  inline typeref& operator=(T& object) {
337  set(object);
338  return *this;
339  }
340 };
341 
342 template<>
343 class __EXPORT typeref<const char *> : public TypeRef
344 {
345 public:
346  class value : public Counted
347  {
348  private:
349  __DELETE_COPY(value);
350 
351  protected:
352  friend class typeref;
353 
354  char mem[1];
355 
356  value(caddr_t addr, size_t size, const char *str);
357 
358  void destroy(void);
359 
360  public:
361  inline char *get() {
362  return &mem[0];
363  }
364 
365  inline size_t len() {
366  return strlen(mem);
367  }
368 
369  inline size_t max() {
370  return size;
371  }
372  };
373 
374  typeref();
375 
376  typeref(const typeref& copy);
377 
378  typeref(const char *str);
379 
380  inline explicit typeref(Counted *object) : TypeRef(object) {};
381 
382  const char *operator*() const;
383 
384  inline operator const char *() const {
385  return operator*();
386  }
387 
388  size_t len() const;
389 
390  bool operator==(const typeref& ptr) const;
391 
392  bool operator==(const char *obj) const;
393 
394  bool operator==(value *chars) const;
395 
396  inline bool operator!=(const typeref& ptr) const {
397  return !(*this == ptr);
398  }
399 
400  inline bool operator!=(value *chars) const {
401  return !(*this == chars);
402  }
403 
404  inline bool operator!=(const char *obj) const {
405  return !(*this == obj);
406  }
407 
408  bool operator<(const typeref& ptr) const;
409 
410  inline bool operator>(const typeref& ptr) const {
411  return (ptr < *this);
412  }
413 
414  inline bool operator<=(const typeref& ptr) const {
415  return !(*this > ptr);
416  }
417 
418  inline bool operator>=(const typeref& ptr) const {
419  return !(*this < ptr);
420  }
421 
422  typeref& operator=(const typeref& objref);
423 
424  typeref& operator=(const char *str);
425 
426  typeref& operator=(value *chars);
427 
428  typeref operator+(const char *str) const;
429 
430  typeref operator+(const typeref& ptr) const;
431 
432  const char *operator()(ssize_t offset) const;
433 
434  void set(const char *str);
435 
436  void assign(value *chars);
437 
438  static void expand(value **handle, size_t size);
439 
440  static value *create(size_t size);
441 
442  static void destroy(value *bytes);
443 };
444 
445 template<>
446 class __EXPORT typeref<const uint8_t *> : public TypeRef
447 {
448 public:
449  class value : public Counted
450  {
451  private:
452  __DELETE_COPY(value);
453 
454  protected:
455  friend class typeref;
456 
457  uint8_t mem[1];
458 
459  value(caddr_t addr, size_t size, const uint8_t *data);
460 
461  value(caddr_t addr, size_t size);
462 
463  void destroy(void);
464 
465  public:
466  inline size_t max() {
467  return size;
468  }
469 
470  inline uint8_t *get() {
471  return &mem[0];
472  }
473  };
474 
475  typeref();
476 
477  typeref(const typeref& copy);
478 
479  typeref(uint8_t *str, size_t size);
480 
481  typeref(bool mode, size_t bits);
482 
483  inline explicit typeref(Counted *object) : TypeRef(object) {};
484 
485  const uint8_t *operator*() const;
486 
487  inline operator const uint8_t *() const {
488  return operator*();
489  }
490 
491  typeref& operator=(const typeref& objref);
492 
493  typeref& operator=(value *bytes);
494 
495  bool operator==(const typeref& ptr) const;
496 
497  bool operator==(value *bytes) const;
498 
499  inline bool operator!=(const typeref& ptr) const {
500  return !(*this == ptr);
501  }
502 
503  inline bool operator!=(value *bytes) const {
504  return !(*this == bytes);
505  }
506 
507  typeref operator+(const typeref& ptr) const;
508 
509  void set(const uint8_t *str, size_t size);
510 
511  size_t set(bool bit, size_t offset, size_t bits = 1);
512 
513  bool get(size_t offset);
514 
515  size_t count(size_t offset, size_t bits = 1);
516 
517  void assign(value *bytes);
518 
519  static value *create(size_t size);
520 
521  static void destroy(value *bytes);
522 };
523 
524 namespace Type {
525  typedef int32_t Integer;
526  typedef double Real;
527  typedef const char *Chars;
528  typedef const uint8_t *Bytes;
529  typedef const uint8_t *Bools;
530 }
531 
532 typedef typeref<Type::Chars>::value *charvalues_t;
533 typedef typeref<Type::Bytes>::value *bytevalues_t;
534 typedef typeref<Type::Chars> stringref_t;
535 typedef typeref<Type::Chars> stringref;
536 typedef typeref<Type::Bytes> byteref_t;
537 typedef typeref<Type::Bytes> byteref;
538 typedef typeref<Type::Bools> boolref_t;
539 
540 template<typename T>
541 inline typeref<T> typeref_cast(T x) {
542  return typeref<T>(x);
543 }
544 
545 } // namespace
546 
547 #endif
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:280
unsigned copies() const
Number of retains (smart pointers) referencing us.
Definition: typeref.h:116
static caddr_t mem(caddr_t address)
Adjust memory pointer to atomic boundry.
static void put(TypeRef &target, Counted *object)
Special weak-public means to copy a container reference.
Definition: typeref.h:239
Smart pointer base class for auto-retained objects.
Definition: typeref.h:61
TypeRef(Counted *object)
Create a smart pointer referencing an existing heap object.
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:510
bool operator!() const
Check if we are currently not pointing to anything.
Definition: typeref.h:226
A common base class for all managed objects.
Definition: protocols.h:545
void set(Counted *object)
Set our smart pointer to a specific heap container.
A common object base class with auto-pointer support.
struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
Definition: socket.h:2015
Generic smart pointer class.
Definition: generics.h:54
bool is() const
Is this object not empty?
Definition: typeref.h:108
void clear(void)
Manually release the current container.
Common namespace for all ucommon objects.
Definition: access.h:47
void retain(ObjectProtocol *object)
Convenience function to access object retention.
Definition: object.h:492
static caddr_t alloc(size_t size)
Allocate memory from heap.
Atomic counter class.
Definition: atomic.h:65
Heap base-class container for typeref objects.
Definition: typeref.h:75
Atomic pointers and locks.
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
Definition: generics.h:414
Abstract interfaces and support.
Generic templates for C++.
Runtime functions.