Array_Base.cpp

Go to the documentation of this file.
00001 // Array_Base.cpp,v 4.10 2006/04/19 07:53:18 jwillemsen Exp
00002 
00003 #ifndef ACE_ARRAY_BASE_CPP
00004 #define ACE_ARRAY_BASE_CPP
00005 
00006 #include "ace/Array_Base.h"
00007 
00008 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00009 # pragma once
00010 #endif /* ACE_LACKS_PRAGMA_ONCE */
00011 
00012 #if !defined (__ACE_INLINE__)
00013 #include "ace/Array_Base.inl"
00014 #endif /* __ACE_INLINE__ */
00015 
00016 #include "ace/Malloc_Base.h"
00017 #include "ace/os_include/os_errno.h"
00018 
00019 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00020 
00021 // Dynamically initialize an array.
00022 template <class T>
00023 ACE_Array_Base<T>::ACE_Array_Base (size_t size,
00024                                    ACE_Allocator *alloc)
00025   : max_size_ (size),
00026     cur_size_ (size),
00027     allocator_ (alloc)
00028 {
00029   if (this->allocator_ == 0)
00030     this->allocator_ = ACE_Allocator::instance ();
00031 
00032   if (size != 0)
00033     {
00034       ACE_ALLOCATOR (this->array_,
00035                      (T *) this->allocator_->malloc (size * sizeof (T)));
00036       for (size_t i = 0; i < size; ++i)
00037         new (&array_[i]) T;
00038     }
00039   else
00040     this->array_ = 0;
00041 }
00042 
00043 template <class T>
00044 ACE_Array_Base<T>::ACE_Array_Base (size_t size,
00045                                    const T &default_value,
00046                                    ACE_Allocator *alloc)
00047   : max_size_ (size),
00048     cur_size_ (size),
00049     allocator_ (alloc)
00050 {
00051   if (this->allocator_ == 0)
00052     this->allocator_ = ACE_Allocator::instance ();
00053 
00054   if (size != 0)
00055     {
00056       ACE_ALLOCATOR (this->array_,
00057                      (T *) this->allocator_->malloc (size * sizeof (T)));
00058       for (size_t i = 0; i < size; ++i)
00059         new (&array_[i]) T (default_value);
00060     }
00061   else
00062     this->array_ = 0;
00063 }
00064 
00065 // The copy constructor (performs initialization).
00066 
00067 template <class T>
00068 ACE_Array_Base<T>::ACE_Array_Base (const ACE_Array_Base<T> &s)
00069   : max_size_ (s.size ()),
00070     cur_size_ (s.size ()),
00071     allocator_ (s.allocator_)
00072 {
00073   if (this->allocator_ == 0)
00074     this->allocator_ = ACE_Allocator::instance ();
00075 
00076   ACE_ALLOCATOR (this->array_,
00077                  (T *) this->allocator_->malloc (s.size () * sizeof (T)));
00078   for (size_t i = 0; i < this->size (); i++)
00079     new (&this->array_[i]) T (s.array_[i]);
00080 }
00081 
00082 // Assignment operator (performs assignment).
00083 
00084 template <class T> void
00085 ACE_Array_Base<T>::operator= (const ACE_Array_Base<T> &s)
00086 {
00087   // Check for "self-assignment".
00088 
00089   if (this != &s)
00090     {
00091       if (this->max_size_ < s.size ())
00092         {
00093           ACE_DES_ARRAY_FREE (this->array_,
00094                               this->max_size_,
00095                               this->allocator_->free,
00096                               T);
00097           ACE_ALLOCATOR (this->array_,
00098                          (T *) this->allocator_->malloc (s.size () * sizeof (T)));
00099           this->max_size_ = s.size ();
00100         }
00101       else
00102         {
00103           ACE_DES_ARRAY_NOFREE (this->array_,
00104                                 s.size (),
00105                                 T);
00106         }
00107 
00108       this->cur_size_ = s.size ();
00109 
00110       for (size_t i = 0; i < this->size (); i++)
00111         new (&this->array_[i]) T (s.array_[i]);
00112     }
00113 }
00114 
00115 // Set an item in the array at location slot.
00116 
00117 template <class T> int
00118 ACE_Array_Base<T>::set (const T &new_item, size_t slot)
00119 {
00120   if (this->in_range (slot))
00121     {
00122       this->array_[slot] = new_item;
00123       return 0;
00124     }
00125   else
00126     return -1;
00127 }
00128 
00129 // Get an item in the array at location slot.
00130 
00131 template <class T> int
00132 ACE_Array_Base<T>::get (T &item, size_t slot) const
00133 {
00134   if (this->in_range (slot))
00135     {
00136       // Copies the item.  If you don't want to copy, use operator []
00137       // instead (but then you'll be responsible for range checking).
00138       item = this->array_[slot];
00139       return 0;
00140     }
00141   else
00142     return -1;
00143 }
00144 
00145 template<class T> int
00146 ACE_Array_Base<T>::max_size (size_t new_size)
00147 {
00148   if (new_size > this->max_size_)
00149     {
00150       T *tmp = 0;
00151 
00152       ACE_ALLOCATOR_RETURN (tmp,
00153                             (T *) this->allocator_->malloc (new_size * sizeof (T)),
00154                             -1);
00155       for (size_t i = 0; i < this->cur_size_; ++i)
00156         new (&tmp[i]) T (this->array_[i]);
00157 
00158       // Initialize the new portion of the array that exceeds the
00159       // previously allocated section.
00160       for (size_t j = this->cur_size_; j < new_size; j++)
00161         new (&tmp[j]) T;
00162 
00163       ACE_DES_ARRAY_FREE (this->array_,
00164                           this->max_size_,
00165                           this->allocator_->free,
00166                           T);
00167       this->array_ = tmp;
00168       this->max_size_ = new_size;
00169       this->cur_size_ = new_size;
00170     }
00171 
00172   return 0;
00173 }
00174 
00175 template<class T> int
00176 ACE_Array_Base<T>::size (size_t new_size)
00177 {
00178   int r = this->max_size (new_size);
00179   if (r != 0)
00180     return r;
00181   this->cur_size_ = new_size;
00182   return 0;
00183 }
00184 
00185 // ****************************************************************
00186 
00187 template <class T> int
00188 ACE_Array_Iterator<T>::next (T *&item)
00189 {
00190   // ACE_TRACE ("ACE_Array_Iterator<T>::next");
00191 
00192   if (this->done ())
00193     {
00194       item = 0;
00195       return 0;
00196     }
00197   else
00198     {
00199       item = &array_[current_];
00200       return 1;
00201     }
00202 }
00203 
00204 ACE_END_VERSIONED_NAMESPACE_DECL
00205 
00206 #endif /* ACE_ARRAY_BASE_CPP */

Generated on Thu Nov 9 09:41:45 2006 for ACE by doxygen 1.3.6