Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / Array_Base.cpp
blob830634f7bf32af3c522d9f7df86f39f21cfa02b8
1 #ifndef ACE_ARRAY_BASE_CPP
2 #define ACE_ARRAY_BASE_CPP
4 #include "ace/Array_Base.h"
6 #if !defined (ACE_LACKS_PRAGMA_ONCE)
7 # pragma once
8 #endif /* ACE_LACKS_PRAGMA_ONCE */
10 #if !defined (__ACE_INLINE__)
11 #include "ace/Array_Base.inl"
12 #endif /* __ACE_INLINE__ */
14 #include "ace/Malloc_Base.h"
15 #include "ace/os_include/os_errno.h"
17 #include <algorithm>
19 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
21 // Dynamically initialize an array.
22 template <class T>
23 ACE_Array_Base<T>::ACE_Array_Base (typename ACE_Array_Base<T>::size_type size,
24 ACE_Allocator *alloc)
25 : max_size_ (size),
26 cur_size_ (size),
27 allocator_ (alloc)
29 if (this->allocator_ == 0)
30 this->allocator_ = ACE_Allocator::instance ();
32 if (size != 0)
34 ACE_ALLOCATOR (this->array_,
35 (T *) this->allocator_->malloc (size * sizeof (T)));
36 for (size_type i = 0; i < size; ++i)
37 new (&array_[i]) T;
39 else
40 this->array_ = 0;
43 template <class T>
44 ACE_Array_Base<T>::ACE_Array_Base (typename ACE_Array_Base<T>::size_type size,
45 const T &default_value,
46 ACE_Allocator *alloc)
47 : max_size_ (size),
48 cur_size_ (size),
49 allocator_ (alloc)
51 if (this->allocator_ == 0)
52 this->allocator_ = ACE_Allocator::instance ();
54 if (size != 0)
56 ACE_ALLOCATOR (this->array_,
57 (T *) this->allocator_->malloc (size * sizeof (T)));
58 for (size_type i = 0; i < size; ++i)
59 new (&array_[i]) T (default_value);
61 else
62 this->array_ = 0;
65 // The copy constructor (performs initialization).
67 template <class T>
68 ACE_Array_Base<T>::ACE_Array_Base (const ACE_Array_Base<T> &s)
69 : max_size_ (s.size ()),
70 cur_size_ (s.size ()),
71 allocator_ (s.allocator_)
73 if (this->allocator_ == 0)
74 this->allocator_ = ACE_Allocator::instance ();
76 ACE_ALLOCATOR (this->array_,
77 (T *) this->allocator_->malloc (s.size () * sizeof (T)));
78 for (size_type i = 0; i < this->size (); ++i)
79 new (&this->array_[i]) T (s.array_[i]);
82 // Assignment operator (performs assignment).
84 template <class T> void
85 ACE_Array_Base<T>::operator= (const ACE_Array_Base<T> &s)
87 // Check for "self-assignment".
89 if (this != &s)
91 if (this->max_size_ < s.size ())
93 // Need to reallocate memory.
95 // Strongly exception-safe assignment.
97 // Note that we're swapping the allocators here, too.
98 // Should we? Probably. "*this" should be a duplicate of
99 // the "right hand side".
100 ACE_Array_Base<T> tmp (s);
101 this->swap (tmp);
103 else
105 // Underlying array is large enough. No need to reallocate
106 // memory.
108 // "*this" still owns the memory for the underlying array.
109 // Do not swap out the allocator.
111 // @@ Why don't we just drop the explicit destructor and
112 // placement operator new() calls with a straight
113 // element-by-element assignment? Is the existing
114 // approach more efficient?
115 // -Ossama
117 ACE_DES_ARRAY_NOFREE (this->array_,
118 s.size (),
121 this->cur_size_ = s.size ();
123 for (size_type i = 0; i < this->size (); ++i)
124 new (&this->array_[i]) T (s.array_[i]);
129 // Set an item in the array at location slot.
131 template <class T> int
132 ACE_Array_Base<T>::set (const T &new_item,
133 typename ACE_Array_Base<T>::size_type slot)
135 if (this->in_range (slot))
137 this->array_[slot] = new_item;
138 return 0;
140 else
141 return -1;
144 // Get an item in the array at location slot.
146 template <class T> int
147 ACE_Array_Base<T>::get (T &item,
148 typename ACE_Array_Base<T>::size_type slot) const
150 if (this->in_range (slot))
152 // Copies the item. If you don't want to copy, use operator []
153 // instead (but then you'll be responsible for range checking).
154 item = this->array_[slot];
155 return 0;
157 else
158 return -1;
161 template<class T> int
162 ACE_Array_Base<T>::max_size (typename ACE_Array_Base<T>::size_type new_size)
164 if (new_size > this->max_size_)
166 T *tmp = 0;
168 ACE_ALLOCATOR_RETURN (tmp,
169 (T *) this->allocator_->malloc (new_size * sizeof (T)),
170 -1);
171 for (size_type i = 0; i < this->cur_size_; ++i)
172 new (&tmp[i]) T (this->array_[i]);
174 // Initialize the new portion of the array that exceeds the
175 // previously allocated section.
176 for (size_type j = this->cur_size_; j < new_size; ++j)
177 new (&tmp[j]) T;
179 ACE_DES_ARRAY_FREE (this->array_,
180 this->max_size_,
181 this->allocator_->free,
183 this->array_ = tmp;
184 this->max_size_ = new_size;
185 this->cur_size_ = new_size;
188 return 0;
191 template<class T> int
192 ACE_Array_Base<T>::size (typename ACE_Array_Base<T>::size_type new_size)
194 int const r = this->max_size (new_size);
196 if (r == 0)
197 this->cur_size_ = new_size;
199 return r;
202 template<class T>
203 void
204 ACE_Array_Base<T>::swap (ACE_Array_Base<T> & rhs)
206 std::swap (this->max_size_ , rhs.max_size_);
207 std::swap (this->cur_size_ , rhs.cur_size_);
208 std::swap (this->array_ , rhs.array_);
209 std::swap (this->allocator_, rhs.allocator_);
212 // ****************************************************************
214 template <class T> int
215 ACE_Array_Iterator<T>::next (T *&item)
217 // ACE_TRACE ("ACE_Array_Iterator<T>::next");
219 if (this->done ())
221 item = 0;
222 return 0;
224 else
226 item = &array_[current_];
227 return 1;
231 ACE_END_VERSIONED_NAMESPACE_DECL
233 #endif /* ACE_ARRAY_BASE_CPP */