25#ifndef _UCOMMON_GENERICS_H_
26#define _UCOMMON_GENERICS_H_
28#ifndef _UCOMMON_CPR_H_
36#ifndef UCOMMON_SYSRUNTIME
37#define THROW(x) throw x
38#if __cplusplus > 199711L
42#define THROWS(x) throw(x)
43#define THROWS_ANY throw()
46#define THROW(x) ::abort()
66 inline void release(
void) {
67 if(counter && --(*counter)==0) {
75 inline void retain(
void) {
80 inline void set(T* ptr) {
83 counter =
new unsigned;
90 if(
object == ref.object)
93 if(counter && --(*counter)==0) {
98 counter = ref.counter;
108 inline explicit pointer(T* ptr = NULL) : object(ptr) {
110 counter =
new unsigned;
119 counter = ref.counter;
129 inline pointer& operator=(T *ptr) {
138 inline T& operator*()
const {
142 inline T* operator->()
const {
146 inline bool operator!()
const {
147 return (counter == NULL);
150 inline operator bool()
const {
151 return counter != NULL;
168 inline void release(
void) {
169 if(counter && --(*counter)==0) {
177 inline void retain(
void) {
182 inline void set(T* ptr) {
185 counter =
new unsigned;
192 if(array == ref.array)
195 if(counter && --(*counter)==0) {
200 counter = ref.counter;
212 counter =
new unsigned;
221 counter = ref.counter;
240 inline T* operator*()
const {
244 inline T& operator[](
size_t offset)
const {
245 return array[offset];
248 inline T* operator()(
size_t offset)
const {
249 return &array[offset];
252 inline bool operator!()
const {
253 return (counter == NULL);
256 inline operator bool()
const {
257 return counter != NULL;
280 original = &object; temp = object;
297inline bool is(T&
object) {
298 return object.operator bool();
309 return (
bool)(
object.operator*() ==
nullptr);
320 return (
bool)(
object->operator*() ==
nullptr);
329inline T*
dup(
const T&
object) {
330 return new T(
object);
334inline void dupfree(T
object) {
339inline char *dup<char>(
const char&
object) {
340 return strdup(&
object);
344inline void dupfree<char*>(
char*
object) {
354 new((caddr_t)&
object) T;
363 memset((
void *)&
object, 0,
sizeof(T));
new((caddr_t)&
object) T;
373 memcpy((
void *)target, (
void *)source,
sizeof(T));
383 memcpy((
void *)&target, (
void *)source,
sizeof(T));
392inline void swap(T& o1, T& o2) {
393 cpr_memswap(&o1, &o2,
sizeof(T));
405inline T&
copy(
const T& src, T& to) {
406 new((caddr_t)&to) T(src);
415 memcpy((
void *)&to, (
void *)&src,
sizeof(T));
416 new((caddr_t)&src) T();
421inline T& clear(T& o) {
423 new((caddr_t)&o) T();
438 if(((
size_t)
pointer) %
sizeof(T))
450inline T& (
max)(T& o1, T& o2) {
451 return o1 > o2 ? o1 : o2;
461inline T& (
min)(T& o1, T& o2) {
462 return o1 < o2 ? o1 : o2;
473inline T& (
limit)(T& value, T& low, T& high) {
474 return (value < low) ? low : ((value > high) ? high : value);
Common namespace for all ucommon objects.
T &() min(T &o1, T &o2)
Convenience function to return min of two objects.
void store_unsafe(T &target, const T *source)
Convenience function to store object pointer into object.
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
bool isnullp(T *object)
Convenience function to test pointer-pointer object.
bool bound(const T *pointer, const T *base, size_t count)
Convenience function to check memory arrays.
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
void reset_unsafe(T &object)
Convenience function to reset an existing object.
T & deref_pointer(T *pointer)
Convert a pointer to a reference with type checking.
void swap(T &o1, T &o2)
Convenience function to swap objects.
T & move(T &src, T &to)
Convenience function to move objects.
void zero_unsafe(T &object)
Convenience function to zero an object and restore type info.
T copy(const T &src)
Convenience function to copy objects.
T &() limit(T &value, T &low, T &high)
Convenience macro to range restrict values.
bool isnull(T &object)
Convenience function to test pointer object.
void copy_unsafe(T *target, const T *source)
Convenience function to copy class.
bool is(T &object)
Convenience function to validate object assuming it is castable to bool.
Generic smart pointer class.
Generic smart array class.
Save and restore global objects in function call stack frames.
~save_restore()
Restore original when stack frame is released.
save_restore(T &object)
Save object into local copy and keep reference to the original object.