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_
51#ifndef _UCOMMON_THREAD_H_
71 friend class ArrayRef;
72 friend class SharedRef;
74 friend class TypeRelease;
93 friend class TypeRelease;
112 explicit Counted(
void *address,
size_t size, TypeRelease *ar = NULL);
126 inline bool is()
const {
127 return (count.get() > 0);
135 return ((
unsigned)count.get());
138 inline TypeRelease *getRelease()
const {
148 void operator delete(
void *address);
201 static caddr_t
mem(caddr_t address);
238 inline operator bool()
const {
263 bool is_released(
void);
266class __EXPORT TypeRelease
269 inline TypeRelease() {
273 inline TypeRelease(TypeRelease *target) {
277 virtual unsigned purge();
279 virtual caddr_t allocate(
size_t size);
282 friend class TypeRef::Counted;
284 TypeRelease *delegate;
286 void enlist(TypeRef::Counted **root, TypeRef::Counted *obj);
288 TypeRef::Counted *delist(TypeRef::Counted **root);
290 virtual void release(TypeRef::Counted *obj);
292 void dealloc(TypeRef::Counted *obj);
294 inline size_t size(TypeRef::Counted *obj) {
299extern __EXPORT TypeRelease auto_release;
300extern __EXPORT TypeRelease secure_release;
301extern __EXPORT TypeRelease release_later;
303class __EXPORT typeref_guard :
protected TypeRef
306 friend class TypeRef;
311 inline typeref_guard() : TypeRef() {}
313 inline typeref_guard(
const typeref_guard& copy) : TypeRef(
copy) {}
315 inline typeref_guard(
const TypeRef& pointer) : TypeRef(pointer) {}
317 void set(
const TypeRef& pointer);
319 inline typeref_guard& operator=(
const TypeRef& pointer) {
325template<
typename T, TypeRelease& R = auto_release>
326class typeref :
public TypeRef
329 class value :
public Counted
332 __DELETE_COPY(value);
337 inline value(caddr_t
mem,
const T&
object, TypeRelease *ar = &R) :
344 inline typeref() :
TypeRef() {}
346 inline typeref(
const typeref_guard& global) :
TypeRef() {
352 inline typeref(
const T&
object, TypeRelease *ar = &R) :
TypeRef() {
353 caddr_t p = R.allocate(
sizeof(value));
357 inline explicit typeref(Counted *
object) :
TypeRef(object) {}
359 inline const T* operator->()
const {
362 value *v = polystatic_cast<value *>(ref);
366 inline const T& operator*()
const {
367 value *v = polystatic_cast<value*>(ref);
369 return *(&(v->data));
372 inline const T* operator()()
const {
373 value *v = polystatic_cast<value*>(ref);
380 inline operator const T&()
const {
381 value *v = polystatic_cast<value*>(ref);
383 return *(&(v->data));
386 inline typeref& operator=(
const typeref_guard& ptr) {
391 inline typeref& operator=(
const typeref& ptr) {
396 inline bool operator==(
const typeref& ptr)
const {
397 value *v1 = polystatic_cast<value*>(ref);
398 value *v2 = polystatic_cast<value*>(ptr.ref);
401 return v1->data == v2->data;
404 inline bool operator==(
const T& obj)
const {
405 value *v = polystatic_cast<value *>(ref);
408 return v->data == obj;
411 inline bool operator!=(
const typeref& ptr)
const {
412 return !(*
this == ptr);
415 inline bool operator!=(
const T& obj)
const {
416 return !(*
this == obj);
419 inline void set(T&
object, TypeRelease *pool = &R) {
421 caddr_t p = R.allocate(
sizeof(value));
425 inline typeref& operator=(T&
object) {
437class __EXPORT typeref<const char *> :
public TypeRef
440 class value :
public Counted
443 __DELETE_COPY(value);
446 friend class typeref;
450 value(caddr_t addr,
size_t size,
const char *str, TypeRelease *ar = &auto_release);
459 inline size_t len() {
463 inline size_t max() {
467 inline operator char *() {
474 typeref(
const typeref&
copy);
476 typeref(
const char *str, TypeRelease *ar = &auto_release);
478 typeref(
size_t size, TypeRelease *ar = &auto_release);
480 inline typeref(
const typeref_guard& global) : TypeRef() {
484 inline explicit typeref(Counted *
object) : TypeRef(object) {}
486 inline explicit typeref(value *value) : TypeRef(value) {}
488 const char *operator*()
const;
490 inline operator const char *()
const {
496 bool operator==(
const typeref& ptr)
const;
498 bool operator==(
const char *obj)
const;
500 bool operator==(value *chars)
const;
502 inline bool operator!=(
const typeref& ptr)
const {
503 return !(*
this == ptr);
506 inline bool operator!=(value *chars)
const {
507 return !(*
this == chars);
510 inline bool operator!=(
const char *obj)
const {
511 return !(*
this == obj);
514 bool operator<(
const typeref& ptr)
const;
516 inline bool operator>(
const typeref& ptr)
const {
517 return (ptr < *
this);
520 inline bool operator<=(
const typeref& ptr)
const {
521 return !(*
this > ptr);
524 inline bool operator>=(
const typeref& ptr)
const {
525 return !(*
this < ptr);
528 typeref& operator=(
const typeref& objref);
530 typeref& operator=(
const char *str);
532 typeref& operator=(value *chars);
534 const typeref operator+(
const char *str)
const;
536 const typeref operator+(
const typeref& ptr)
const;
538 const char *operator()(ssize_t offset)
const;
540 void set(
const char *str, TypeRelease *ar = &auto_release);
542 void hex(
const uint8_t *mem,
size_t size, TypeRelease *ar = &auto_release);
544 void b64(
const uint8_t *mem,
size_t size, TypeRelease *ar = &auto_release);
546 void assign(value *chars);
548 static void expand(value **handle,
size_t size);
550 static value *create(
size_t size, TypeRelease *ar = &auto_release);
552 static void destroy(value *bytes);
556class __EXPORT typeref<const uint8_t *> :
public TypeRef
559 class value :
public Counted
562 __DELETE_COPY(value);
565 friend class typeref;
569 value(caddr_t addr,
size_t size,
const uint8_t *data =
nullptr, TypeRelease *ar = &auto_release);
574 inline size_t max() {
578 inline uint8_t *get() {
582 inline operator uint8_t*() {
589 typeref(
const typeref&
copy);
591 typeref(uint8_t *str,
size_t size, TypeRelease *ar = &auto_release);
593 typeref(
size_t size, TypeRelease *ar = &auto_release);
595 typeref(
bool mode,
size_t bits, TypeRelease *ar = &auto_release);
597 inline typeref(
const typeref_guard& global) : TypeRef() {
601 inline explicit typeref(Counted *
object) : TypeRef(object) {}
603 const uint8_t *operator*()
const;
605 inline operator const uint8_t *()
const {
609 typeref& operator=(
const typeref& objref);
611 typeref& operator=(value *bytes);
613 bool operator==(
const typeref& ptr)
const;
615 bool operator==(value *bytes)
const;
617 inline bool operator!=(
const typeref& ptr)
const {
618 return !(*
this == ptr);
621 inline bool operator!=(value *bytes)
const {
622 return !(*
this == bytes);
625 const typeref operator+(
const typeref& ptr)
const;
627 void set(
const uint8_t *str,
size_t size, TypeRelease *ar = &auto_release);
629 size_t set(
bool bit,
size_t offset,
size_t bits = 1);
631 size_t hex(
const char *str,
bool ws =
false, TypeRelease *ar = &auto_release);
633 size_t b64(
const char *str,
bool ws =
false, TypeRelease *ar = &auto_release);
637 bool get(
size_t offset);
639 size_t count(
size_t offset,
size_t bits = 1);
641 void assign(value *bytes);
643 typeref<const char *> hex();
645 typeref<const char *> b64();
647 static value *create(
size_t size, TypeRelease *ar = &auto_release);
649 static void destroy(value *bytes);
655template<TypeRelease& R>
656class stringref :
public typeref<const char *>
659 inline stringref() : typeref<const char *>() {}
661 inline stringref(
const stringref&
copy) : typeref<const char *>(
copy) {}
663 inline stringref(
const char *str) : typeref<const char *>(str, &R) {}
665 inline stringref(
size_t size) : typeref<const char *>(
size, &R) {}
667 inline explicit stringref(Counted *
object) : typeref<const char *>(object) {}
669 inline void set(
const char *str) {
670 typeref<const char *>::set(str, &R);
673 inline static value *create(
size_t size) {
674 return typeref<const char *>::create(
size, &R);
677 inline static stringref promote(typeref<const char *>& str) {
678 stringref result = *str;
683template<TypeRelease& R>
684class byteref :
public typeref<const uint8_t *>
687 inline byteref() : typeref<const uint8_t *>() {}
689 inline byteref(uint8_t *str,
size_t size) : typeref<const uint8_t *>(str,
size, &R) {}
691 inline byteref(
size_t size) : typeref<const uint8_t *>(
size, &R) {}
693 inline byteref(
bool mode,
size_t bits) : typeref<const uint8_t *>(mode, bits, &R) {}
695 inline explicit byteref(Counted *
object) : typeref<const uint8_t *>(object) {}
697 inline void set(
const uint8_t *str,
size_t size) {
698 typeref<const uint8_t *>::set(str,
size, &R);
701 inline size_t hex(
const char *str,
bool ws =
false) {
702 return typeref<const uint8_t *>::hex(str, ws, &R);
705 inline size_t b64(
const char *str,
bool ws =
false) {
706 return typeref<const uint8_t *>::b64(str, ws, &R);
709 inline stringref<R> hex() {
710 typeref<const char *> str = typeref<const uint8_t *>::hex();
711 stringref<R> result = *str;
715 inline stringref<R> b64() {
716 typeref<const char *> str = typeref<const uint8_t *>::b64();
717 stringref<R> result = *str;
721 inline static value *create(
size_t size) {
722 return typeref<const uint8_t *>::create(
size, &R);
725 inline static byteref promote(typeref<const uint8_t *>& str) {
726 byteref result = *str;
734 typedef int32_t Integer;
736 typedef const char *Chars;
737 typedef const uint8_t *Bytes;
738 typedef const uint8_t *Bools;
741typedef typeref<Type::Chars>::value *charvalues_t;
742typedef typeref<Type::Bytes>::value *bytevalues_t;
743typedef typeref<Type::Chars> stringref_t;
744typedef typeref<Type::Bytes> byteref_t;
745typedef typeref<Type::Bools> boolref_t;
748inline typeref<T> typeref_cast(T x) {
749 return typeref<T>(x);
Atomic pointers and locks.
Abstract interfaces and support.
Generic templates for C++.
Common namespace for all ucommon objects.
T & max(T &o1, T &o2)
Convenience function to return max of two objects.
T copy(const T &src)
Convenience function to copy objects.
Generic smart pointer class.
Create a linked list of auto-releasable objects.
A common base class for all managed objects.
Smart pointer base class for auto-retained objects.
bool operator!() const
Check if we are currently not pointing to anything.
void set(Counted *object)
Set our smart pointer to a specific heap container.
static caddr_t mem(caddr_t address)
Adjust memory pointer to atomic boundry.
TypeRef(const TypeRef &pointer)
Create a smart pointer based on another pointer.
void assign(const typeref_guard &ref)
Assign from a guarded typeref.
unsigned copies() const
Get number of references to container.
TypeRef(Counted *object)
Create a smart pointer referencing an existing heap object.
TypeRef()
Create a smart pointer referencing nothing.
size_t size(void) const
Get size of referenced heap object.
void clear(void)
Manually release the current container.
void set(const TypeRef &pointer)
Set our smart pointer based on another pointer instance.
static void put(TypeRef &target, Counted *object)
Special weak-public means to copy a container reference.
virtual ~TypeRef()
Destroy pointer when falling out of scope.
Heap base-class container for typeref objects.
Counted(void *address, size_t size, TypeRelease *ar=NULL)
Construction of aligned container.
unsigned copies() const
Number of retains (smart pointers) referencing us.
void retain()
Retain a copy of this object.
bool is() const
Is this object not empty?
virtual void dealloc(void)
Release memory and delete object when no longer referenced.
void release()
Release a copy of this object.
A common object base class with auto-pointer support.
Thread classes and sychronization objects.