OpenDDS  Snapshot(2023/04/28-20:55)
DurabilityArray.h
Go to the documentation of this file.
1 /*
2  *
3  *
4  * Distributed under the OpenDDS License.
5  * See: http://www.opendds.org/license.html
6  */
7 
8 #ifndef OPENDDS_DCPS_DURABILITYARRAY_H
9 #define OPENDDS_DCPS_DURABILITYARRAY_H
10 
11 #include <ace/Array_Base.h>
12 
13 #include <algorithm>
14 
16 
17 namespace OpenDDS {
18 namespace DCPS {
19 
20 /**
21  * @class DurabilityArray
22  *
23  * @brief Array class that provides a means to reset the
24  * underlying @c ACE_Allocator.
25  *
26  * This class only exists to provide a means to reset the
27  * allocator used by the @c ACE_Array_Base base class. It has a
28  * specific use case, namely to correctly support instances
29  * created by a persistent allocator. The allocator address may
30  * change between process runs, meaning the allocator address
31  * stored in the persistent @c ACE_Array_Base instance will be
32  * invalid. Use the @c set_allocator() method to reset the
33  * allocator address before performing any operations that will
34  * require use of the allocator (e.g. increasing the size of the
35  * array).
36  */
37 template<typename T>
38 class DurabilityArray : public ACE_Array_Base<T> {
39 public:
40 
42  ACE_Allocator * allocator)
43  : ACE_Array_Base<T> (size, allocator)
44  {}
45 
47  T const & default_value,
48  ACE_Allocator * allocator)
49  : ACE_Array_Base<T> (size, default_value, allocator)
50  {}
51 
53  : ACE_Array_Base<T> (rhs.size(), rhs.allocator_)
54  {
55  for (size_t i = 0; i < this->size_; ++i)
56  this->array_[i] = rhs.array_[i];
57  }
58 
60  {}
61 
62  void operator= (DurabilityArray<T> const & rhs)
63  {
64  DurabilityArray tmp(rhs);
65  this->swap(rhs);
66  }
67 
68  /// Reset allocator
69  void set_allocator(ACE_Allocator * allocator)
70  {
71  if (allocator == 0)
72  allocator = ACE_Allocator::instance();
73 
74  this->allocator_ = allocator;
75  }
76 
78  {
79  std::swap(this->max_size_, rhs.max_size_);
80  std::swap(this->cur_size_, rhs.current_size_);
81  std::swap(this->array_, rhs.array_);
82  std::swap(this->allocator_, rhs.allocator_);
83  }
84 };
85 
86 } // namespace DCPS
87 } // namespace OpenDDS
88 
90 
91 #endif /* OPENDDS_DURABILITY_ARRAY_H */
void swap(MessageBlock &lhs, MessageBlock &rhs)
size_type max_size_
void set_allocator(ACE_Allocator *allocator)
Reset allocator.
DurabilityArray(size_t size, ACE_Allocator *allocator)
void operator=(DurabilityArray< T > const &rhs)
void swap(DurabilityArray< T > &rhs)
size_type cur_size_
Array class that provides a means to reset the underlying ACE_Allocator.
static ACE_Allocator * instance(void)
size_type size(void) const
size_t size_
ACE_Allocator * allocator_
DurabilityArray(DurabilityArray< T > const &rhs)
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
DurabilityArray(size_t size, T const &default_value, ACE_Allocator *allocator)
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
value_type * array_