Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

CTBrawBuffer.icc

Go to the documentation of this file.
00001 
00006 /*----------------------------------------------------------------------------*/
00007 /* C Tool Box: Designed and implemented by:                                   */
00008 /*    Walter F.J. Mueller   Gesellschaft fuer Schwerionenforschung (GSI)      */
00009 /*                          Planckstrasse 1, D-64291 Darmstadt, Germany       */
00010 /*                  Email:  W.F.J.Mueller@gsi.de                              */
00011 /*                  WWW:    http://www-kp3.gsi.de/www/kp3/people/mueller.html */
00012 /*------------------------------------------+---------------------------------*/
00013 
00014 #include <assert.h>
00015 #include <new.h>
00016 
00017 #include "CTBgrab.hxx"
00018 #include "CTBosFill.hxx"
00019 
00054 //------------------------------------------+-----------------------------------
00056 
00060 template <class T>
00061 inline CTBrawBuffer<T>::CTBrawBuffer()
00062   : mp_data(0),
00063     mi_capacity(0)
00064 {}
00065 
00066 //------------------------------------------+-----------------------------------
00068 
00073 template <class T>
00074 inline CTBrawBuffer<T>::CTBrawBuffer(CTBint i_capacity)
00075   : mp_data(0),
00076     mi_capacity(0)
00077 {
00078   CreateBuffer(i_capacity);
00079 }
00080 
00081 //------------------------------------------+-----------------------------------
00083 
00084 template <class T>
00085 inline CTBrawBuffer<T>::CTBrawBuffer(CTBrawBuffer<T>& rhs,bool )
00086   : mp_data(rhs.mp_data),                   // copy rhs -> lhs
00087     mi_capacity(rhs.mi_capacity)
00088 {
00089   rhs.mp_data     = 0;                      // reset rhs
00090   rhs.mi_capacity = 0;
00091 }
00092 
00093 //------------------------------------------+-----------------------------------
00095 
00101 template <class T>
00102 inline CTBrawBuffer<T>::~CTBrawBuffer()
00103 {
00104   DeleteBuffer();
00105 }
00106 
00107 //------------------------------------------+-----------------------------------
00109 
00114 template <class T>
00115 inline void CTBrawBuffer<T>::Construct(CTBint i_ind)
00116 {
00117   assert(i_ind >= 0 && i_ind < mi_capacity); // !?! index in range
00118   new(mp_data+i_ind) T();                   // use default constructor
00119   return;
00120 }
00121 
00122 //------------------------------------------+-----------------------------------
00124 
00129 template <class T>
00130 inline void CTBrawBuffer<T>::Construct(T* p_ele)
00131 {
00132   assert(mp_data && p_ele >= mp_data && p_ele < mp_data+mi_capacity); // !?!
00133   new(p_ele) T();                           // use default constructor
00134   return;
00135 }
00136 
00137 //------------------------------------------+-----------------------------------
00139 
00144 template <class T>
00145 inline void CTBrawBuffer<T>::ConstructRange(CTBint i_beg, CTBint i_size)
00146 {
00147   T* p_cur = mp_data + i_beg;
00148   
00149   assert(i_size <= 0 || (i_beg >= 0 && i_beg+i_size <= mi_capacity)); // !?!
00150   for (CTBint i = 0; i < i_size; i++) new(p_cur++) T();
00151   return;
00152 }
00153 
00154 //------------------------------------------+-----------------------------------
00156 
00161 template <class T>
00162 inline void CTBrawBuffer<T>::Construct(CTBint i_ind, const T& value)
00163 {
00164   assert(i_ind >= 0 && i_ind < mi_capacity); // !?! index in range
00165   new(mp_data+i_ind) T(value);              // use copy constructor
00166   return;
00167 }
00168 
00169 //------------------------------------------+-----------------------------------
00171 
00176 template <class T>
00177 inline void CTBrawBuffer<T>::Construct(T* p_ele, const T& value)
00178 {
00179   assert(mp_data && p_ele >= mp_data && p_ele < mp_data+mi_capacity); // !?!
00180   new(p_ele) T(value);                      // use copy constructor
00181   return;
00182 }
00183 
00184 //------------------------------------------+-----------------------------------
00186 
00191 template <class T>
00192 inline void CTBrawBuffer<T>::ConstructRange(CTBint i_beg, CTBint i_size,
00193                                             const T& value)
00194 {
00195   T* p_cur = mp_data + i_beg;
00196   
00197   assert(i_size <= 0 || (i_beg >= 0 && i_beg+i_size <= mi_capacity)); // !?!
00198   for (CTBint i = 0; i < i_size; i++) new(p_cur++) T(value);
00199   return;
00200 }
00201 
00202 //------------------------------------------+-----------------------------------
00204 
00210 template <class T>
00211 inline void CTBrawBuffer<T>::ConstructRange(CTBint i_beg, CTBint i_size,
00212                                             const T rhs[])
00213 {
00214   T* p_cur = mp_data + i_beg;
00215   
00216   assert(i_size <= 0 || (i_beg >= 0 && i_beg+i_size <= mi_capacity)); // !?!
00217   for (CTBint i = 0; i < i_size; i++) new(p_cur++) T(rhs[i]);
00218   return;
00219 }
00220 
00221 //------------------------------------------+-----------------------------------
00223 
00224 template <class T>
00225 inline void CTBrawBuffer<T>::Destruct(CTBint i_ind)
00226 {
00227   assert(i_ind >= 0 && i_ind < mi_capacity); // !?! index in range
00228   (mp_data+i_ind)->~T();                    // explicit destructor call
00229 }
00230 
00231 //------------------------------------------+-----------------------------------
00233 
00234 template <class T>
00235 inline void CTBrawBuffer<T>::Destruct(T* p_ele)
00236 {
00237   assert(mp_data && p_ele >= mp_data && p_ele < mp_data+mi_capacity); // !?!
00238   p_ele->~T();                              // explicit destructor call
00239   return;
00240 }
00241 
00242 //------------------------------------------+-----------------------------------
00244 
00245 template <class T>
00246 inline void CTBrawBuffer<T>::DestructRange(CTBint i_beg, CTBint i_size)
00247 {
00248   T* p_cur = mp_data + i_beg;
00249 
00250   assert(i_size <= 0 || (i_beg >= 0 && i_beg+i_size <= mi_capacity)); // !?!
00251   for (CTBint i = 0; i < i_size; i++) (p_cur++)->~T();
00252   return;
00253 }
00254 
00255 //------------------------------------------+-----------------------------------
00257 
00258 template <class T>
00259 inline void CTBrawBuffer<T>::GrabConstruct(CTBint i_ind, T& value)
00260 {
00261   CTBgrab<T> grab;
00262   
00263   assert(i_ind >= 0 && i_ind < mi_capacity); // !?! index in range
00264   grab.Construct(mp_data+i_ind,value);
00265   return;
00266 }
00267 
00268 //------------------------------------------+-----------------------------------
00270 
00276 template <class T>
00277 inline void CTBrawBuffer<T>::GrabConstructRange(CTBint i_beg, 
00278                                                 CTBint i_size, T rhs[])
00279 {
00280   CTBgrab<T> grab;
00281   T* p_cur = mp_data + i_beg;
00282   
00283   assert(i_size <= 0 || (i_beg >= 0 && i_beg+i_size <= mi_capacity)); // !?!
00284   for (CTBint i = 0; i < i_size; i++) grab.Construct(p_cur++,rhs[i]);
00285   return;
00286 }
00287 
00288 //------------------------------------------+-----------------------------------
00290 
00291 template <class T>
00292 inline void CTBrawBuffer<T>::GrabDestruct(CTBint i_ind, T& obj)
00293 {
00294   CTBgrab<T> grab;
00295   
00296   assert(i_ind >= 0 && i_ind < mi_capacity); // !?! index in range
00297   grab.Destruct(obj, mp_data+i_ind);
00298   return;
00299 }
00300 
00301 //------------------------------------------+-----------------------------------
00303 
00309 template <class T>
00310 inline CTBint CTBrawBuffer<T>::Capacity() const
00311 {
00312   return mi_capacity;
00313 }
00314 
00315 //------------------------------------------+-----------------------------------
00317 
00323 template <class T>
00324 inline void CTBrawBuffer<T>::Resize(CTBint i_capacity)
00325 {
00326   DeleteBuffer();
00327   CreateBuffer(i_capacity);
00328   return;
00329 }
00330 
00331 //------------------------------------------+-----------------------------------
00333 
00342 template <class T>
00343 inline void CTBrawBuffer<T>::Grab(CTBrawBuffer<T>& rhs)
00344 {
00345   assert(!mp_data && this != &rhs);         // !?! must be empty; no self grab
00346   mp_data         = rhs.mp_data;            // copy rhs -> lhs
00347   mi_capacity     = rhs.mi_capacity;
00348   rhs.mp_data     = 0;                      // reset rhs
00349   rhs.mi_capacity = 0;
00350   return;
00351 }
00352 //------------------------------------------+-----------------------------------
00354 
00361 template <class T>
00362 inline bool CTBrawBuffer<T>::Empty() const
00363 {
00364   return !mp_data;
00365 }
00366 
00367 //------------------------------------------+-----------------------------------
00369 
00383 template <class T>
00384 inline void CTBrawBuffer<T>::ShiftRight(CTBint i_beg, CTBint i_end)
00385 {
00386   CTBgrab<T> grab;
00387   T*  p_cur = mp_data + i_end+1;
00388 
00389   assert(i_beg > i_end || (i_beg >= 0 && i_end >= 0 && // !?! 
00390                            i_end < mi_capacity));
00391   for (CTBint i_n = i_end - i_beg + 1; i_n > 0; i_n--) {
00392     grab(p_cur[0],p_cur[-1]);               // p_cur[0] = p_cur[-1]; 
00393     p_cur--;
00394   }
00395   
00396   return;
00397 }
00398 
00399 //------------------------------------------+-----------------------------------
00401 
00415 template <class T>
00416 inline void CTBrawBuffer<T>::ShiftLeft(CTBint i_beg, CTBint i_end)
00417 {
00418   CTBgrab<T> grab;
00419   T*  p_cur = mp_data + i_beg-1;
00420 
00421   assert(i_beg > i_end || (i_beg >= 0 && i_end >= 0 && // !?! 
00422                            i_end < mi_capacity));
00423   for (CTBint i_n = i_end - i_beg + 1; i_n > 0; i_n--) {
00424     grab(p_cur[0],p_cur[1]);                // p_cur[0] = p_cur[1];
00425     p_cur++;
00426   }
00427   
00428   return;
00429 }
00430 
00431 //------------------------------------------+-----------------------------------
00433 
00439 template <class T>
00440 inline void CTBrawBuffer<T>::Copy(CTBint i_beg, CTBint i_size, const T& rhs)
00441 {
00442   T*  p_cur = mp_data + i_beg;
00443 
00444   assert(i_size <= 0 || (i_beg >= 0 && i_beg+i_size <= mi_capacity)); // !?!
00445   for (CTBint i = 0; i < i_size; i++) *p_cur++ = rhs;  
00446   return;
00447 }
00448 
00449 //------------------------------------------+-----------------------------------
00451 
00457 template <class T>
00458 inline void CTBrawBuffer<T>::Copy(          // copy array into rawbuffer
00459     CTBint i_beg,                           //   first index target range
00460     CTBint i_size,                          //   number of elements to copy
00461     const T rhs[])                          //   source array
00462 {
00463   T*  p_cur = mp_data + i_beg;
00464 
00465   assert(i_size <= 0 || (i_beg >= 0 && i_beg+i_size <= mi_capacity)); // !?!
00466   for (CTBint i = 0; i < i_size; i++) *p_cur++ = rhs[i];  
00467   return;
00468 }
00469 
00470 //------------------------------------------+-----------------------------------
00472 
00473 template <class T>
00474 inline void CTBrawBuffer<T>::Dump(int i_indent, ostream& os,
00475                                   const char* p_text) const
00476 {
00477   CTBosFill bl(i_indent);
00478 
00479   os << bl << "--CTBrawBuffer<T> ";
00480   if (p_text) os << p_text;
00481   os << " @ " << this << endl;
00482   os << bl << "  mp_data:       " << (void*) mp_data;
00483   if (mp_data) os << " ... " << (void*) (mp_data + (mi_capacity-1));
00484   os << endl;
00485   os << bl << "  mi_capacity:   " << mi_capacity << endl;
00486   return;
00487 }
00488 
00489 //------------------------------------------+-----------------------------------
00491 
00496 template <class T>
00497 inline T& CTBrawBuffer<T>::operator[](CTBint i_ind)
00498 {
00499   return mp_data[i_ind];
00500 }
00501 
00502 //------------------------------------------+-----------------------------------
00504 
00519 template <class T>
00520 inline T* CTBrawBuffer<T>::operator+(CTBint i_ind)
00521 {
00522   return mp_data+i_ind;
00523 }
00524 
00525 //------------------------------------------+-----------------------------------
00527 
00528 template <class T>
00529 inline const T* CTBrawBuffer<T>::operator+(CTBint i_ind) const
00530 {
00531   return mp_data+i_ind;
00532 }
00533 
00534 //------------------------------------------+-----------------------------------
00536 
00541 template <class T>
00542 inline const T& CTBrawBuffer<T>::operator[](CTBint i_ind) const
00543 {
00544   return mp_data[i_ind];
00545 }
00546 
00547 //------------------------------------------+-----------------------------------
00549 
00550 template <class T>
00551 inline bool CTBrawBuffer<T>::operator !() const
00552 {
00553   return mi_capacity == 0;
00554 }
00555 
00556 //------------------------------------------+-----------------------------------
00558 
00572 template <class T>
00573 inline CTBrawBuffer<T>::operator T*()
00574 {
00575   return mp_data;
00576 }
00577 
00578 //------------------------------------------+-----------------------------------
00580 
00584 template <class T>
00585 inline CTBrawBuffer<T>::operator const T*() const
00586 {
00587   return mp_data;
00588 }
00589 
00590 //------------------------------------------+-----------------------------------
00592 
00593 template <class T>
00594 inline void CTBrawBuffer<T>::DeleteBuffer()
00595 {
00596   if (mp_data != 0) {
00597     ::operator delete(mp_data);
00598     mp_data     = 0;
00599     mi_capacity = 0;
00600   }
00601   return;
00602 }
00603 
00604 //------------------------------------------+-----------------------------------
00606 
00607 template <class T>
00608 inline void CTBrawBuffer<T>::CreateBuffer(CTBint i_capacity)
00609 {
00610   assert(!mp_data);                         // !?! must be in empty state
00611   if (i_capacity > 0) {
00612     mp_data = (T*) ::operator new(i_capacity * sizeof(T));
00613   } else {
00614     mp_data = 0;
00615   }
00616   mi_capacity = i_capacity;
00617   return;
00618 }
00619 
00620 

Generated at Fri Oct 24 18:11:29 2003 for CTBbase by doxygen1.2.9-20010812 written by Dimitri van Heesch, © 1997-2001