AFF --- A container for numbers (array) by Friederich and Forbriger.
sharedheap_def.h
Go to the documentation of this file.
1 
50 #ifndef AFF_SHAREDHEAP_H_VERSION
51 #error "include this only through sharedheap.h"
52 #endif
53 
54 // include guard
55 #ifndef AFF_SHAREDHEAP_DEF_H_VERSION
56 
57 #define AFF_SHAREDHEAP_DEF_H_VERSION \
58  "AFF_SHAREDHEAP_DEF_H V1.4"
59 
60 namespace aff {
61 
62 #ifdef AFF_PREBUILT
63 namespace prebuilt {
64 #endif
65 
66 #ifdef AFF_PREBUILT
67 #ifndef AFF_COMPILING_LIBRARY
68 #error "definition read in prebuilt mode and not during library compilation"
69 #endif
70 #endif
71 
72 /*======================================================================*/
73 
74 namespace util {
75 
77  template <typename T>
78  inline
79  SHeap<T>::SHeap(const Tsize& size)
80  : Mheap(0), Msize(size), Mnref(1), Mextern(false)
81  {
82  if (size>0) {
83  try { Mheap=new T[size]; }
84  catch (std::bad_alloc) {
85  throw AllocException(size, sizeof(T));
86  }
87  } else {
88  Msize=0;
89  }
90  }
91 
92  /*----------------------------------------------------------------------*/
93 
95  template <typename T>
96  inline
97  SHeap<T>::SHeap(T* pointer, const Tsize& size)
98  : Mheap(pointer), Msize(size), Mnref(1), Mextern(true) { }
99 
100  /*----------------------------------------------------------------------*/
101 
103  template <typename T>
104  inline
106  {
107  if ((Mheap!=0) && (Msize>0) && (!Mextern))
108  {
109  delete[] Mheap;
110  }
111  }
112 
113 } // namespace util
114 
115 /*======================================================================*/
116 
118  template <typename T>
119  inline
121  : Mheapstruct(new Theapstruct(1)) { }
122 
123  /*----------------------------------------------------------------------*/
124 
126  template <typename T>
127  inline
129  : Mheapstruct(new Theapstruct(size)) { }
130 
131  /*----------------------------------------------------------------------*/
132 
134  template <typename T>
135  inline
137  : Mheapstruct(new Theapstruct(pointer, size)) { }
138 
139  /*----------------------------------------------------------------------*/
140 
142  template <typename T>
143  inline
145  : Mheapstruct(sharedheap.Mheapstruct)
146  { ++(Mheapstruct->Mnref); }
147 
148  /*----------------------------------------------------------------------*/
149 
151  template <typename T>
152  inline
154  {
155  if (--(Mheapstruct->Mnref) < 1)
156  {
157  delete Mheapstruct;
158  }
159  }
160 
161  /*----------------------------------------------------------------------*/
162 
164  template <typename T>
165  inline
168  {
169  if (Mheapstruct != sharedheap.Mheapstruct)
170  {
171  if (--(Mheapstruct->Mnref) < 1)
172  {
173  delete Mheapstruct;
174  }
175  Mheapstruct=sharedheap.Mheapstruct;
176  ++(Mheapstruct->Mnref);
177  }
178  return(*this);
179  }
180 
181 #ifdef AFF_PREBUILT
182 } // namespace prebuilt
183 #endif
184 
185 } // namespace aff
186 
187 #endif // AFF_SHAREDHEAP_DEF_H_VERSION (includeguard)
188 
189 /* ----- END OF sharedheap_def.h ----- */
~SHeap()
Free heap memory.
Root namespace of library.
Definition: array.h:148
ConstSharedHeap< T > & operator=(const Tcontainer &sharedheap)
Copy representation to share heap.
Theapstruct * Mheapstruct
shared heap structure
Definition: sharedheap.h:229
Tsize Msize
size (number of elements) of array
Definition: sharedheap.h:113
T * Mheap
shared raw array on heap
Definition: sharedheap.h:112
This is the base class for const elements.
Definition: sharedheap.h:139
SHeap(const Tsize &size)
Allocate memory on heap.
ConstSharedHeap()
Create from nothing.
A structure to be shared between different SharedHeap instances.
Definition: sharedheap.h:102
~ConstSharedHeap()
Deallocate heap memory if this is the last referencing instance.
Exception thrown in case of allocation error.
Definition: error.h:124
size_t Tsize
Type to hold the size of an array dimension.
Definition: types.h:51
Tsize Mnref
number of referencing instances
Definition: sharedheap.h:114