[176] | 1 | /* -*- C++ -*- */ |
---|
| 2 | |
---|
| 3 | #ifndef _UTILITY_H_ |
---|
| 4 | #define _UTILITY_H_ |
---|
| 5 | |
---|
| 6 | /** |
---|
| 7 | * @file utility.h |
---|
| 8 | * @brief Convenient wrappers that let us new up & delete objects from heaps. |
---|
| 9 | * |
---|
| 10 | * |
---|
| 11 | * Example:<BR> |
---|
| 12 | * <TT> |
---|
| 13 | * |
---|
| 14 | * Heap h;<BR> |
---|
| 15 | * <BR> |
---|
| 16 | * Foo * foo;<BR> |
---|
| 17 | * newObject (foo,h); // instead of: foo = new Foo;<BR> |
---|
| 18 | * deleteObject (foo,h); // instead of: delete foo;<BR> |
---|
| 19 | * <BR> |
---|
| 20 | * Foo * foo2;<BR> |
---|
| 21 | * newArray (foo2, 10, h); // instead of: foo2 = new Foo[10];<BR> |
---|
| 22 | * deleteArray (foo2, h); // instead of: delete [] foo2;<BR> |
---|
| 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #include <new.h> |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | // Construct an object on a given heap. |
---|
| 30 | class newObject { |
---|
| 31 | public: |
---|
| 32 | template <class Heap, class Object> |
---|
| 33 | inline void operator() (Object*& o, Heap& h) const { |
---|
| 34 | o = new (h.malloc (sizeof(Object))) Object; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | template <class Heap, class Object, class A1> |
---|
| 38 | inline void operator() (Object*& o, const A1& a1, Heap& h) const { |
---|
| 39 | o = new (h.malloc (sizeof(Object))) Object (a1); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | template <class Heap, class Object, class A1, class A2> |
---|
| 43 | inline void operator() (Object*& o, const A1& a1, const A2& a2, Heap& h) const { |
---|
| 44 | o = new (h.malloc (sizeof(Object))) Object (a1, a2); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | template <class Heap, class Object, class A1, class A2, class A3> |
---|
| 48 | inline void operator() (Object*& o, const A1& a1, const A2& a2, const A3& a3, Heap& h) const { |
---|
| 49 | o = new (h.malloc (sizeof(Object))) Object (a1, a2, a3); |
---|
| 50 | } |
---|
| 51 | }; |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | // Delete an object to a given heap. |
---|
| 55 | class deleteObject { |
---|
| 56 | public: |
---|
| 57 | template <class Heap, class Object> |
---|
| 58 | inline void operator()(Object*& o, Heap& h) { |
---|
| 59 | o->~Object(); |
---|
| 60 | h.free (o); |
---|
| 61 | } |
---|
| 62 | }; |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | class newArray { |
---|
| 66 | public: |
---|
| 67 | template <class Heap, class Object> |
---|
| 68 | inline void operator() (Object*& o, unsigned int n, Heap& h) const { |
---|
| 69 | // Store the number of array elements in the beginning of the space. |
---|
| 70 | double * ptr = (double *) h.malloc (sizeof(Object) * n + sizeof(double)); |
---|
| 71 | *((unsigned int *) ptr) = n; |
---|
| 72 | // Initialize every element. |
---|
| 73 | ptr++; |
---|
| 74 | Object * ptr2 = (Object *) ptr; |
---|
| 75 | // Save the pointer to the start of the array. |
---|
| 76 | o = ptr2; |
---|
| 77 | // Now iterate and construct every object in place. |
---|
| 78 | for (unsigned int i = 0; i < n; i++) { |
---|
| 79 | new ((void *) ptr2) Object; |
---|
| 80 | ptr2++; |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | }; |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | class deleteArray { |
---|
| 87 | public: |
---|
| 88 | template <class Heap, class Object> |
---|
| 89 | inline void operator()(Object*& o, Heap& h) const { |
---|
| 90 | unsigned int n = *((unsigned int *) ((double *) o - 1)); |
---|
| 91 | Object * optr = o; |
---|
| 92 | // Call the destructor on every element in the array. |
---|
| 93 | for (unsigned int i = 0; i < n; i++) { |
---|
| 94 | optr->~Object(); |
---|
| 95 | optr++; |
---|
| 96 | } |
---|
| 97 | // Free the array. |
---|
| 98 | h.free ((void *) ((double *) o - 1)); |
---|
| 99 | } |
---|
| 100 | }; |
---|
| 101 | |
---|
| 102 | #endif |
---|