DurabilityArray.h

Go to the documentation of this file.
00001 /*
00002  *
00003  *
00004  * Distributed under the OpenDDS License.
00005  * See: http://www.opendds.org/license.html
00006  */
00007 
00008 #ifndef OPENDDS_DURABILITY_ARRAY_H
00009 #define OPENDDS_DURABILITY_ARRAY_H
00010 
00011 #include <ace/Array_Base.h>
00012 
00013 #include <algorithm>
00014 
00015 namespace OpenDDS {
00016 namespace DCPS {
00017 
00018 /**
00019  * @class DurabilityArray
00020  *
00021  * @brief Array class that provides a means to reset the
00022  *        underlying @c ACE_Allocator.
00023  *
00024  * This class only exists to provide a means to reset the
00025  * allocator used by the @c ACE_Array_Base base class.  It has a
00026  * specific use case, namely to correctly support instances
00027  * created by a persistent allocator.  The allocator address may
00028  * change between process runs, meaning the allocator address
00029  * stored in the persistent @c ACE_Array_Base instance will be
00030  * invalid.  Use the @c set_allocator() method to reset the
00031  * allocator address before performing any operations that will
00032  * require use of the allocator (e.g. increasing the size of the
00033  * array).
00034  */
00035 template<typename T>
00036 class DurabilityArray : public ACE_Array_Base<T> {
00037 public:
00038 
00039   DurabilityArray(size_t size,
00040                   ACE_Allocator * allocator)
00041     : ACE_Array_Base<T> (size, allocator)
00042   {}
00043 
00044   DurabilityArray(size_t size,
00045                   T const & default_value,
00046                   ACE_Allocator * allocator)
00047     : ACE_Array_Base<T> (size, default_value, allocator)
00048   {}
00049 
00050   DurabilityArray(DurabilityArray<T> const & rhs)
00051     : ACE_Array_Base<T> (rhs.size(), rhs.allocator_)
00052   {
00053     for (size_t i = 0; i < this->size_; ++i)
00054       this->array_[i] = rhs.array_[i];
00055   }
00056 
00057   ~DurabilityArray()
00058   {}
00059 
00060   void operator= (DurabilityArray<T> const & rhs)
00061   {
00062     DurabilityArray tmp(rhs);
00063     this->swap(rhs);
00064   }
00065 
00066   /// Reset allocator
00067   void set_allocator(ACE_Allocator * allocator)
00068   {
00069     if (allocator == 0)
00070       allocator = ACE_Allocator::instance();
00071 
00072     this->allocator_ = allocator;
00073   }
00074 
00075   void swap(DurabilityArray<T> & rhs)
00076   {
00077     std::swap(this->max_size_, rhs.max_size_);
00078     std::swap(this->cur_size_, rhs.current_size_);
00079     std::swap(this->array_, rhs.array_);
00080     std::swap(this->allocator_, rhs.allocator_);
00081   }
00082 };
00083 
00084 } // namespace DCPS
00085 } // namespace OpenDDS
00086 
00087 #endif  /* OPENDDS_DURABILITY_ARRAY_H */

Generated on Fri Feb 12 20:05:23 2016 for OpenDDS by  doxygen 1.4.7