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)
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"
19 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
21 // Dynamically initialize an array.
23 ACE_Array_Base
<T
>::ACE_Array_Base (typename ACE_Array_Base
<T
>::size_type size
,
29 if (this->allocator_
== 0)
30 this->allocator_
= ACE_Allocator::instance ();
34 ACE_ALLOCATOR (this->array_
,
35 (T
*) this->allocator_
->malloc (size
* sizeof (T
)));
36 for (size_type i
= 0; i
< size
; ++i
)
44 ACE_Array_Base
<T
>::ACE_Array_Base (typename ACE_Array_Base
<T
>::size_type size
,
45 const T
&default_value
,
51 if (this->allocator_
== 0)
52 this->allocator_
= ACE_Allocator::instance ();
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
);
65 // The copy constructor (performs initialization).
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".
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
);
105 // Underlying array is large enough. No need to reallocate
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?
117 ACE_DES_ARRAY_NOFREE (this->array_
,
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
;
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
];
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_
)
168 ACE_ALLOCATOR_RETURN (tmp
,
169 (T
*) this->allocator_
->malloc (new_size
* sizeof (T
)),
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
)
179 ACE_DES_ARRAY_FREE (this->array_
,
181 this->allocator_
->free
,
184 this->max_size_
= new_size
;
185 this->cur_size_
= new_size
;
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
);
197 this->cur_size_
= new_size
;
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");
226 item
= &array_
[current_
];
231 ACE_END_VERSIONED_NAMESPACE_DECL
233 #endif /* ACE_ARRAY_BASE_CPP */