Line data Source code
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 : 15 : OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL 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 : 41 : DurabilityArray(size_t size, 42 : ACE_Allocator * allocator) 43 : : ACE_Array_Base<T> (size, allocator) 44 : {} 45 : 46 0 : DurabilityArray(size_t size, 47 : T const & default_value, 48 : ACE_Allocator * allocator) 49 0 : : ACE_Array_Base<T> (size, default_value, allocator) 50 0 : {} 51 : 52 : DurabilityArray(DurabilityArray<T> const & rhs) 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 : 59 0 : ~DurabilityArray() 60 0 : {} 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 : 77 : void swap(DurabilityArray<T> & rhs) 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 : 89 : OPENDDS_END_VERSIONED_NAMESPACE_DECL 90 : 91 : #endif /* OPENDDS_DURABILITY_ARRAY_H */