Teuchos_SerializerHelpers.hpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef TEUCHOS_SERIALIZER_HELPERS_HPP
00030 #define TEUCHOS_SERIALIZER_HELPERS_HPP
00031 
00032 #include "Teuchos_Serializer.hpp"
00033 #include "Teuchos_Array.hpp"
00034 
00035 namespace Teuchos {
00036 
00040 template <typename Ordinal, typename T>
00041 class ReferenceTypeSerializationBuffer {
00042 public:
00044   ReferenceTypeSerializationBuffer(
00045     const Serializer<Ordinal,T> &serializer
00046     ,const Ordinal count, T*const buffer[]
00047     );
00051   ~ReferenceTypeSerializationBuffer();
00053   char* getCharBuffer() const;
00055   Ordinal getBytes() const;
00056 private:
00057   const Serializer<Ordinal,T>  &serializer_;
00058   Ordinal                      count_;
00059   T*const                      *buffer_;
00060   Array<char>                  charBuffer_;
00061   // Not defined and not to be called
00062   ReferenceTypeSerializationBuffer();
00063   ReferenceTypeSerializationBuffer(const ReferenceTypeSerializationBuffer&);
00064   ReferenceTypeSerializationBuffer& operator=(const ReferenceTypeSerializationBuffer&);
00065 };
00066 
00070 template <typename Ordinal, typename T>
00071 class ConstReferenceTypeSerializationBuffer {
00072 public:
00074   ConstReferenceTypeSerializationBuffer(
00075     const Serializer<Ordinal,T> &serializer
00076     ,const Ordinal count, const T*const buffer[]
00077     );
00081   ~ConstReferenceTypeSerializationBuffer();
00083   const char* getCharBuffer() const;
00085   Ordinal getBytes() const;
00086 private:
00087   const Serializer<Ordinal,T>  &serializer_;
00088   Ordinal                      count_;
00089   const T*const                *buffer_;
00090   Ordinal                      bytes_;
00091   Array<char>                  charBuffer_;
00092   // Not defined and not to be called
00093   ConstReferenceTypeSerializationBuffer();
00094   ConstReferenceTypeSerializationBuffer(const ConstReferenceTypeSerializationBuffer&);
00095   ConstReferenceTypeSerializationBuffer& operator=(const ConstReferenceTypeSerializationBuffer&);
00096 };
00097 
00102 template <typename Ordinal, typename T>
00103 class ReferenceTypeDeserializationBuffer {
00104 public:
00106   ReferenceTypeDeserializationBuffer(
00107     const Serializer<Ordinal,T> &serializer
00108     ,const Ordinal bytes, char charBuffer[]
00109     );
00113   ~ReferenceTypeDeserializationBuffer();
00115   T*const* getBuffer() const;
00117   Ordinal getCount() const;
00118 private:
00119   typedef Array<RCP<T> >  buffer_ptr_t;
00120   typedef Array<T*>               buffer_t;
00121   const Serializer<Ordinal,T>  &serializer_;
00122   Ordinal                      bytes_;
00123   char                         *charBuffer_;
00124   buffer_ptr_t                 buffer_ptr_;
00125   buffer_t                     buffer_;
00126   // Not defined and not to be called
00127   ReferenceTypeDeserializationBuffer();
00128   ReferenceTypeDeserializationBuffer(const ReferenceTypeDeserializationBuffer&);
00129   ReferenceTypeDeserializationBuffer& operator=(const ReferenceTypeDeserializationBuffer&);
00130 };
00131 
00136 template <typename Ordinal, typename T>
00137 class ConstReferenceTypeDeserializationBuffer {
00138 public:
00140   ConstReferenceTypeDeserializationBuffer(
00141     const Serializer<Ordinal,T> &serializer
00142     ,const Ordinal bytes, const char charBuffer[]
00143     );
00147   ~ConstReferenceTypeDeserializationBuffer();
00149   const T*const* getBuffer() const;
00151   Ordinal getCount() const;
00152 private:
00153   typedef Array<RCP<T> >  buffer_ptr_t;
00154   typedef Array<T*>               buffer_t;
00155   const Serializer<Ordinal,T>  &serializer_;
00156   Ordinal                      bytes_;
00157   const char                   *charBuffer_;
00158   buffer_ptr_t                 buffer_ptr_;
00159   buffer_t                     buffer_;
00160   // Not defined and not to be called
00161   ConstReferenceTypeDeserializationBuffer();
00162   ConstReferenceTypeDeserializationBuffer(const ConstReferenceTypeDeserializationBuffer&);
00163   ConstReferenceTypeDeserializationBuffer& operator=(const ConstReferenceTypeDeserializationBuffer&);
00164 };
00165 
00166 // /////////////////////////////////////
00167 // Template implementations
00168 
00169 //
00170 // ReferenceTypeSerializationBuffer
00171 //
00172 
00173 template <typename Ordinal, typename T>
00174 ReferenceTypeSerializationBuffer<Ordinal,T>::ReferenceTypeSerializationBuffer(
00175   const Serializer<Ordinal,T> &serializer
00176   ,const Ordinal count, T*const buffer[]
00177   )
00178   :serializer_(serializer), count_(count), buffer_(buffer)
00179 {
00180   const Ordinal bytes = serializer_.getBufferSize(count_);
00181   charBuffer_.resize(bytes);
00182   serializer_.serialize(count_,buffer_,bytes,&charBuffer_[0]);
00183 }
00184 
00185 template <typename Ordinal, typename T>
00186 ReferenceTypeSerializationBuffer<Ordinal,T>::~ReferenceTypeSerializationBuffer()
00187 {
00188   serializer_.deserialize(charBuffer_.size(),&charBuffer_[0],count_,buffer_);
00189 }
00190 
00191 template <typename Ordinal, typename T>
00192 char* ReferenceTypeSerializationBuffer<Ordinal,T>::getCharBuffer() const
00193 {
00194   typedef ReferenceTypeSerializationBuffer<Ordinal,T>* this_ptr_t;
00195   return &(const_cast<this_ptr_t>(this)->charBuffer_)[0];
00196   // The above const_cast is a better alternative to declaring charBuffer_ to
00197   // be mutable, in my opinion.
00198 }
00199 
00200 template <typename Ordinal, typename T>
00201 Ordinal ReferenceTypeSerializationBuffer<Ordinal,T>::getBytes() const
00202 {
00203   return charBuffer_.size();
00204 }
00205 
00206 //
00207 // ConstReferenceTypeSerializationBuffer
00208 //
00209 
00210 template <typename Ordinal, typename T>
00211 ConstReferenceTypeSerializationBuffer<Ordinal,T>::ConstReferenceTypeSerializationBuffer(
00212   const Serializer<Ordinal,T> &serializer
00213   ,const Ordinal count, const T*const buffer[]
00214   )
00215   :serializer_(serializer), count_(count), buffer_(buffer)
00216 {
00217   const Ordinal bytes = serializer_.getBufferSize(count_);
00218   charBuffer_.resize(bytes);
00219   serializer_.serialize(count_,buffer_,bytes,&charBuffer_[0]);
00220 }
00221 
00222 template <typename Ordinal, typename T>
00223 ConstReferenceTypeSerializationBuffer<Ordinal,T>::~ConstReferenceTypeSerializationBuffer()
00224 {
00225   // No need to copy back from the char[] buffer!
00226 }
00227 
00228 template <typename Ordinal, typename T>
00229 const char* ConstReferenceTypeSerializationBuffer<Ordinal,T>::getCharBuffer() const
00230 {
00231   return &charBuffer_[0];
00232 }
00233 
00234 template <typename Ordinal, typename T>
00235 Ordinal ConstReferenceTypeSerializationBuffer<Ordinal,T>::getBytes() const
00236 {
00237   return charBuffer_.size();
00238 }
00239 
00240 //
00241 // ReferenceTypeDeserializationBuffer
00242 //
00243 
00244 template <typename Ordinal, typename T>
00245 ReferenceTypeDeserializationBuffer<Ordinal,T>::ReferenceTypeDeserializationBuffer(
00246   const Serializer<Ordinal,T> &serializer
00247   ,const Ordinal bytes, char charBuffer[]
00248   )
00249   :serializer_(serializer),bytes_(bytes),charBuffer_(charBuffer)
00250 {
00251   const Ordinal extent = serializer_.getBufferSize(1);
00252   const Ordinal count = bytes_ / extent;
00253 #ifdef TEUCHOS_DEBUG
00254   TEST_FOR_EXCEPT( !( bytes_ % extent == 0 ) );
00255 #endif
00256   buffer_ptr_.resize(count);
00257   buffer_.resize(count);
00258   for( int i = 0; i < count; ++i ) {
00259     buffer_ptr_[i] = serializer_.createObj();
00260     buffer_[i] = &*buffer_ptr_[i];
00261   }
00262   serializer_.deserialize(
00263     bytes_,charBuffer_,count,&buffer_[0]
00264     );
00265 }
00266 
00267 template <typename Ordinal, typename T>
00268 ReferenceTypeDeserializationBuffer<Ordinal,T>::~ReferenceTypeDeserializationBuffer()
00269 {
00270   serializer_.serialize(
00271     buffer_.size(),&buffer_[0],bytes_,charBuffer_
00272     );
00273 }
00274 
00275 template <typename Ordinal, typename T>
00276 T*const* ReferenceTypeDeserializationBuffer<Ordinal,T>::getBuffer() const
00277 {
00278   typedef ReferenceTypeDeserializationBuffer<Ordinal,T>* this_ptr_t;
00279   return &(const_cast<this_ptr_t>(this)->buffer_)[0];
00280   // The above const_cast is a better alternative to declaring buffer_ to be
00281   // mutable, in my opinion.
00282 }
00283 
00284 template <typename Ordinal, typename T>
00285 Ordinal ReferenceTypeDeserializationBuffer<Ordinal,T>::getCount() const
00286 {
00287   return buffer_.size();
00288 }
00289 
00290 //
00291 // ConstReferenceTypeDeserializationBuffer
00292 //
00293 
00294 template <typename Ordinal, typename T>
00295 ConstReferenceTypeDeserializationBuffer<Ordinal,T>::ConstReferenceTypeDeserializationBuffer(
00296   const Serializer<Ordinal,T> &serializer
00297   ,const Ordinal bytes, const char charBuffer[]
00298   )
00299   :serializer_(serializer),bytes_(bytes),charBuffer_(charBuffer)
00300 {
00301   const Ordinal extent = serializer_.getBufferSize(1);
00302   const Ordinal count = bytes_ / extent;
00303 #ifdef TEUCHOS_DEBUG
00304   TEST_FOR_EXCEPT( !( bytes_ % extent == 0 ) );
00305 #endif
00306   buffer_ptr_.resize(count);
00307   buffer_.resize(count);
00308   for( int i = 0; i < count; ++i ) {
00309     buffer_ptr_[i] = serializer_.createObj();
00310     buffer_[i] = &*buffer_ptr_[i];
00311   }
00312   serializer_.deserialize(
00313     bytes_,charBuffer_,count,&buffer_[0]
00314     );
00315 }
00316 
00317 template <typename Ordinal, typename T>
00318 ConstReferenceTypeDeserializationBuffer<Ordinal,T>::~ConstReferenceTypeDeserializationBuffer()
00319 {
00320   // We don't need to serialized back into charBuffer_[] since it is constant!
00321 }
00322 
00323 template <typename Ordinal, typename T>
00324 const T*const* ConstReferenceTypeDeserializationBuffer<Ordinal,T>::getBuffer() const
00325 {
00326   return &buffer_[0];
00327 }
00328 
00329 template <typename Ordinal, typename T>
00330 Ordinal ConstReferenceTypeDeserializationBuffer<Ordinal,T>::getCount() const
00331 {
00332   return buffer_.size();
00333 }
00334 
00335 } // namespace Teuchos
00336 
00337 #endif // TEUCHOS_SERIALIZER_HELPERS_HPP

Generated on Wed May 12 21:24:41 2010 for Teuchos - Trilinos Tools Package by  doxygen 1.4.7