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#define THROWS(x) throw(x)
39#define THROWS_ANY throw()
41#define THROW(x) ::abort()
61 inline void release(
void) {
62 if(counter && --(*counter)==0) {
70 inline void retain(
void) {
75 inline void set(T* ptr) {
78 counter =
new unsigned;
84 inline void set(
const pointer<T> &ref) {
85 if(
object == ref.object)
88 if(counter && --(*counter)==0) {
93 counter = ref.counter;
103 inline explicit pointer(T* ptr = NULL) : object(ptr) {
105 counter =
new unsigned;
112 inline pointer(
const pointer<T> &ref) {
114 counter = ref.counter;
119 inline pointer& operator=(
const pointer<T> &ref) {
124 inline pointer& operator=(T *ptr) {
133 inline T& operator*()
const {
137 inline T* operator->()
const {
141 inline bool operator!()
const {
142 return (counter == NULL);
145 inline operator bool()
const {
146 return counter != NULL;
163 inline void release(
void) {
164 if(counter && --(*counter)==0) {
172 inline void retain(
void) {
177 inline void set(T* ptr) {
180 counter =
new unsigned;
186 inline void set(
const array_pointer<T> &ref) {
187 if(array == ref.array)
190 if(counter && --(*counter)==0) {
195 counter = ref.counter;
200 inline array_pointer() {
205 inline explicit array_pointer(T* ptr = NULL) : array(ptr) {
207 counter =
new unsigned;
214 inline array_pointer(
const array_pointer<T> &ref) {
216 counter = ref.counter;
221 inline array_pointer& operator=(
const array_pointer<T> &ref) {
226 inline array_pointer& operator=(T *ptr) {
231 inline ~array_pointer() {
235 inline T* operator*()
const {
239 inline T& operator[](
size_t offset)
const {
240 return array[offset];
243 inline T* operator()(
size_t offset)
const {
244 return &array[offset];
247 inline bool operator!()
const {
248 return (counter == NULL);
251 inline operator bool()
const {
252 return counter != NULL;
267 save_restore() __DELETED;
275 original = &object; temp = object;
292inline bool is(T&
object) {
293 return object.operator bool();
304 return (
bool)(
object.operator*() ==
nullptr);
315 return (
bool)(
object->operator*() ==
nullptr);
324inline T*
dup(
const T&
object) {
325 return new T(
object);
329inline void dupfree(T
object) {
334inline char *
dup<char>(
const char&
object) {
335 return strdup(&
object);
339inline void dupfree<char*>(
char*
object) {
349 new((caddr_t)&object) T;
358 memset((
void *)&
object, 0,
sizeof(T));
new((caddr_t)&object) T;
368 memcpy((
void *)target, (
void *)source,
sizeof(T));
378 memcpy((
void *)&target, (
void *)source,
sizeof(T));
387inline void swap(T& o1, T& o2) {
400inline T&
copy(
const T& src, T& to) {
401 new((caddr_t)&to) T(src);
410 memcpy((
void *)&to, (
void *)&src,
sizeof(T));
411 new((caddr_t)&src) T();
416inline T& clear(T& o) {
418 new((caddr_t)&o) T();
431 if(pointer < base || pointer >= &base[count])
433 if(((
size_t)
pointer) %
sizeof(T))
445inline T& (
max)(T& o1, T& o2) {
446 return o1 > o2 ? o1 : o2;
456inline T& (
min)(T& o1, T& o2) {
457 return o1 < o2 ? o1 : o2;
468inline T& (
limit)(T& value, T& low, T& high) {
469 return (value < low) ? low : ((value > high) ? high : value);
void cpr_memswap(void *mem1, void *mem2, size_t size)
Portable swap code.
Common namespace for all ucommon 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.
T & max(T &o1, T &o2)
Convenience function to return max of two objects.
T & limit(T &value, T &low, T &high)
Convenience macro to range restrict values.
bool bound(const T *pointer, const T *base, size_t count)
Convenience function to check memory arrays.
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 & min(T &o1, T &o2)
Convenience function to return min of two objects.
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.
~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.