24 #ifndef _UCOMMON_GENERICS_H_
25 #define _UCOMMON_GENERICS_H_
27 #ifndef _UCOMMON_CPR_H_
35 #ifndef UCOMMON_SYSRUNTIME
36 #define THROW(x) throw x
37 #define THROWS(x) throw(x)
38 #define THROWS_ANY throw()
40 #define THROW(x) ::abort()
60 inline void release(
void) {
61 if(counter && --(*counter)==0) {
69 inline void retain(
void) {
74 inline void set(T* ptr) {
77 counter =
new unsigned;
84 if(
object == ref.object)
87 if(counter && --(*counter)==0) {
92 counter = ref.counter;
102 inline explicit pointer(T* ptr = NULL) : object(ptr) {
104 counter =
new unsigned;
113 counter = ref.counter;
123 inline pointer& operator=(T *ptr) {
131 inline T& operator*()
const
134 inline T* operator->()
const
137 inline bool operator!()
const
138 {
return (counter == NULL);}
140 inline operator bool()
const
141 {
return counter != NULL;}
149 template <
typename T>
157 inline void release(
void) {
158 if(counter && --(*counter)==0) {
166 inline void retain(
void) {
171 inline void set(T* ptr) {
174 counter =
new unsigned;
181 if(array == ref.array)
184 if(counter && --(*counter)==0) {
189 counter = ref.counter;
201 counter =
new unsigned;
210 counter = ref.counter;
228 inline T* operator*()
const
231 inline T& operator[](
size_t offset)
const
232 {
return array[offset];}
234 inline T* operator()(
size_t offset)
const
235 {
return &array[offset];}
237 inline bool operator!()
const
238 {
return (counter == NULL);}
240 inline operator bool()
const
241 {
return counter != NULL;}
255 template <
typename T>
298 inline void set(T *temp) {
318 inline operator bool()
const
319 {
return object != NULL;}
321 inline bool operator!()
const
322 {
return object == NULL;}
342 template <
typename T>
354 {array =
new T[s]; size = s;}
363 for(
size_t p = 0; p < s; ++p)
367 inline void reset(
size_t s)
368 {
delete[] array; array =
new T[s]; size = s;}
370 inline void reset(
const T& initial,
size_t s) {
375 for(
size_t p = 0; p < s; ++p)
379 inline void set(
const T& initial) {
380 for(
size_t p = 0; p < size; ++p)
390 inline operator bool()
const
391 {
return array != NULL;}
393 inline bool operator!()
const
394 {
return array == NULL;}
403 inline T& operator[](
size_t offset)
const {
404 crit(offset < size,
"array out of bound");
405 return array[offset];
408 inline T* operator()(
size_t offset)
const {
409 crit(offset < size,
"array out of bound");
410 return &array[offset];
431 {original = &object; temp = object;}
446 inline bool is(T&
object)
447 {
return object.operator bool();}
457 {
return (
bool)(
object.operator*() == NULL);}
467 {
return (
bool)(
object->operator*() == NULL);}
475 inline T*
dup(
const T&
object)
476 {
return new T(
object);}
479 inline void dupfree(T
object)
483 inline char *dup<char>(
const char& object)
484 {
return strdup(&
object);}
487 inline void dupfree<char*>(
char* object)
496 {
new((caddr_t)&
object) T;}
504 {memset((
void *)&
object, 0,
sizeof(T));
new((caddr_t)&
object) T;}
513 {memcpy((
void *)target, (
void *)source,
sizeof(T));}
522 {memcpy((
void *)&target, (
void *)source,
sizeof(T));}
540 inline T& (
max)(T& o1, T& o2)
542 return o1 > o2 ? o1 : o2;
552 inline T& (
min)(T& o1, T& o2)
554 return o1 < o2 ? o1 : o2;
565 inline T& (
limit)(T& value, T& low, T& high)
567 return (value < low) ? low : ((value > high) ? high : value);
~save_restore()
Restore original when stack frame is released.
T &() limit(T &value, T &low, T &high)
Convenience macro to range restrict values.
temporary(T *ptr)
Construct an assigned pointer.
Save and restore global objects in function call stack frames.
Manage temporary array stored on the heap.
Generic smart array class.
void cpr_memswap(void *mem1, void *mem2, size_t size)
Portable swap code.
temporary()
Construct a temporary object, create our stack frame reference.
void swap(T &o1, T &o2)
Convenience function to swap objects.
void copy_unsafe(T *target, const T *source)
Convenience function to copy class.
Common namespace for all ucommon objects.
temp_array(const T &initial, size_t s)
Construct a temporary object with a copy of some initial value.
Manage temporary object stored on the heap.
void store_unsafe(T &target, const T *source)
Convenience function to store object pointer into object.
Generic smart pointer class.
T * operator->() const
Access members of our heap object through our temporary.
T &() min(T &o1, T &o2)
Convenience function to return min of two objects.
temp_array(size_t s)
Construct a temporary object, create our stack frame reference.
T & operator*() const
Access heap object through our temporary directly.
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
void zero_unsafe(T &object)
Convenience function to zero an object and restore type info.
save_restore(T &object)
Save object into local copy and keep reference to the original object.
void set(T *temp)
Assign a temporary object.
T & operator=(T *temp)
Assign a temporary object.
temp_array(const temp_array< T > &)
Disable copy constructor.
bool isnull(T &object)
Convenience function to test pointer object.
void reset_unsafe(T &object)
Convenience function to reset an existing object.
Automatic integer counting class.
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
bool is(T &object)
Convenience function to validate object assuming it is castable to bool.
bool isnullp(T *object)
Convenience function to test pointer-pointer object.
temporary(const temporary< T > &)
Disable copy constructor.