28 #ifndef _UCOMMON_TYPEREF_H_
29 #define _UCOMMON_TYPEREF_H_
31 #ifndef _UCOMMON_CPR_H_
35 #ifndef _UCOMMON_ATOMIC_H_
39 #ifndef _UCOMMON_PROTOCOLS_H_
43 #ifndef _UCOMMON_GENERICS_H_
47 #ifndef _UCOMMON_OBJECT_H_
64 friend class ArrayRef;
65 friend class SharedRef;
94 explicit Counted(
void *address,
size_t size);
101 virtual void dealloc(
void);
108 inline bool is()
const {
109 return (count.get() > 0);
117 return ((
unsigned)count.get());
126 void operator delete(
void *address);
166 void set(Counted *
object);
174 static caddr_t alloc(
size_t size);
181 static caddr_t mem(caddr_t address);
206 size_t size(
void)
const;
212 unsigned copies()
const;
218 inline operator bool()
const {
245 class typeref :
public TypeRef
248 class value :
public Counted
251 __DELETE_COPY(value);
256 inline value(caddr_t mem) :
257 Counted(mem, sizeof(value)) {};
259 inline value(caddr_t mem,
const T&
object) :
260 Counted(mem, sizeof(value)) {
266 inline typeref() :
TypeRef() {};
268 inline typeref(
const typeref&
copy) :
TypeRef(copy) {};
270 inline typeref(
const T&
object) :
TypeRef() {
275 inline explicit typeref(Counted *
object) :
TypeRef(object) {};
277 inline const T* operator->()
const {
280 value *v = polystatic_cast<value *>(ref);
284 inline const T& operator*()
const {
285 value *v = polystatic_cast<value*>(ref);
286 return *(&(v->data));
289 inline const T* operator()()
const {
293 value *v = polystatic_cast<value*>(ref);
297 inline operator const T&()
const {
298 value *v = polystatic_cast<value*>(ref);
299 return *(&(v->data));
302 inline typeref& operator=(
const typeref& ptr) {
307 inline bool operator==(
const typeref& ptr)
const {
308 value *v1 = polystatic_cast<value*>(ref);
309 value *v2 = polystatic_cast<value*>(ptr.ref);
312 return v1->data == v2->data;
315 inline bool operator==(
const T& obj)
const {
316 value *v = polystatic_cast<value *>(ref);
319 return v->data == obj;
322 inline bool operator!=(
const typeref& ptr)
const {
323 return !(*
this == ptr);
326 inline bool operator!=(
const T& obj)
const {
327 return !(*
this == obj);
330 inline void set(T&
object) {
336 inline typeref& operator=(T&
object) {
343 class __EXPORT typeref<const char *> :
public TypeRef
346 class value :
public Counted
349 __DELETE_COPY(value);
352 friend class typeref;
356 value(caddr_t
addr,
size_t size,
const char *str);
365 inline size_t len() {
369 inline size_t max() {
376 typeref(
const typeref&
copy);
378 typeref(
const char *str);
380 inline explicit typeref(Counted *
object) : TypeRef(object) {};
382 const char *operator*()
const;
384 inline operator const char *()
const {
390 bool operator==(
const typeref& ptr)
const;
392 bool operator==(
const char *obj)
const;
394 bool operator==(value *chars)
const;
396 inline bool operator!=(
const typeref& ptr)
const {
397 return !(*
this == ptr);
400 inline bool operator!=(value *chars)
const {
401 return !(*
this == chars);
404 inline bool operator!=(
const char *obj)
const {
405 return !(*
this == obj);
408 bool operator<(
const typeref& ptr)
const;
410 inline bool operator>(
const typeref& ptr)
const {
411 return (ptr < *
this);
414 inline bool operator<=(
const typeref& ptr)
const {
415 return !(*
this > ptr);
418 inline bool operator>=(
const typeref& ptr)
const {
419 return !(*
this < ptr);
422 typeref& operator=(
const typeref& objref);
424 typeref& operator=(
const char *str);
426 typeref& operator=(value *chars);
428 typeref operator+(
const char *str)
const;
430 typeref operator+(
const typeref& ptr)
const;
432 const char *operator()(ssize_t offset)
const;
434 void set(
const char *str);
436 void assign(value *chars);
438 static void expand(value **handle,
size_t size);
440 static value *create(
size_t size);
442 static void destroy(value *bytes);
446 class __EXPORT typeref<const uint8_t *> :
public TypeRef
449 class value :
public Counted
452 __DELETE_COPY(value);
455 friend class typeref;
459 value(caddr_t
addr,
size_t size,
const uint8_t *data);
461 value(caddr_t
addr,
size_t size);
466 inline size_t max() {
470 inline uint8_t *
get() {
477 typeref(
const typeref&
copy);
479 typeref(uint8_t *str,
size_t size);
481 typeref(
bool mode,
size_t bits);
483 inline explicit typeref(Counted *
object) : TypeRef(object) {};
485 const uint8_t *operator*()
const;
487 inline operator const uint8_t *()
const {
491 typeref& operator=(
const typeref& objref);
493 typeref& operator=(value *bytes);
495 bool operator==(
const typeref& ptr)
const;
497 bool operator==(value *bytes)
const;
499 inline bool operator!=(
const typeref& ptr)
const {
500 return !(*
this == ptr);
503 inline bool operator!=(value *bytes)
const {
504 return !(*
this == bytes);
507 typeref operator+(
const typeref& ptr)
const;
509 void set(
const uint8_t *str,
size_t size);
511 size_t set(
bool bit,
size_t offset,
size_t bits = 1);
513 bool get(
size_t offset);
515 size_t count(
size_t offset,
size_t bits = 1);
517 void assign(value *bytes);
519 static value *create(
size_t size);
521 static void destroy(value *bytes);
525 typedef int32_t Integer;
527 typedef const char *Chars;
528 typedef const uint8_t *Bytes;
529 typedef const uint8_t *Bools;
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;
541 inline typeref<T> typeref_cast(T x) {
542 return typeref<T>(x);
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
unsigned copies() const
Number of retains (smart pointers) referencing us.
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.
Smart pointer base class for auto-retained objects.
TypeRef(Counted *object)
Create a smart pointer referencing an existing heap object.
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
bool operator!() const
Check if we are currently not pointing to anything.
A common base class for all managed objects.
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.
Generic smart pointer class.
bool is() const
Is this object not empty?
void clear(void)
Manually release the current container.
Common namespace for all ucommon objects.
void retain(ObjectProtocol *object)
Convenience function to access object retention.
static caddr_t alloc(size_t size)
Allocate memory from heap.
Heap base-class container for typeref objects.
Atomic pointers and locks.
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
Abstract interfaces and support.
Generic templates for C++.