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 OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
00016 
00017 namespace OpenDDS {
00018 namespace DCPS {
00019 
00020 /**
00021  * @class DurabilityArray
00022  *
00023  * @brief Array class that provides a means to reset the
00024  *        underlying @c ACE_Allocator.
00025  *
00026  * This class only exists to provide a means to reset the
00027  * allocator used by the @c ACE_Array_Base base class.  It has a
00028  * specific use case, namely to correctly support instances
00029  * created by a persistent allocator.  The allocator address may
00030  * change between process runs, meaning the allocator address
00031  * stored in the persistent @c ACE_Array_Base instance will be
00032  * invalid.  Use the @c set_allocator() method to reset the
00033  * allocator address before performing any operations that will
00034  * require use of the allocator (e.g. increasing the size of the
00035  * array).
00036  */
00037 template<typename T>
00038 class DurabilityArray : public ACE_Array_Base<T> {
00039 public:
00040 
00041   DurabilityArray(size_t size,
00042                   ACE_Allocator * allocator)
00043     : ACE_Array_Base<T> (size, allocator)
00044   {}
00045 
00046   DurabilityArray(size_t size,
00047                   T const & default_value,
00048                   ACE_Allocator * allocator)
00049     : ACE_Array_Base<T> (size, default_value, allocator)
00050   {}
00051 
00052   DurabilityArray(DurabilityArray<T> const & rhs)
00053     : ACE_Array_Base<T> (rhs.size(), rhs.allocator_)
00054   {
00055     for (size_t i = 0; i < this->size_; ++i)
00056       this->array_[i] = rhs.array_[i];
00057   }
00058 
00059   ~DurabilityArray()
00060   {}
00061 
00062   void operator= (DurabilityArray<T> const & rhs)
00063   {
00064     DurabilityArray tmp(rhs);
00065     this->swap(rhs);
00066   }
00067 
00068   /// Reset allocator
00069   void set_allocator(ACE_Allocator * allocator)
00070   {
00071     if (allocator == 0)
00072       allocator = ACE_Allocator::instance();
00073 
00074     this->allocator_ = allocator;
00075   }
00076 
00077   void swap(DurabilityArray<T> & rhs)
00078   {
00079     std::swap(this->max_size_, rhs.max_size_);
00080     std::swap(this->cur_size_, rhs.current_size_);
00081     std::swap(this->array_, rhs.array_);
00082     std::swap(this->allocator_, rhs.allocator_);
00083   }
00084 };
00085 
00086 } // namespace DCPS
00087 } // namespace OpenDDS
00088 
00089 OPENDDS_END_VERSIONED_NAMESPACE_DECL
00090 
00091 #endif  /* OPENDDS_DURABILITY_ARRAY_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 10 Aug 2018 for OpenDDS by  doxygen 1.6.1