Teuchos - Trilinos Tools Package Version of the Day
Teuchos_SerializationTraitsHelpers.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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #ifndef TEUCHOS_SERIALIZATION_TRAITS_HELPERS_HPP
00043 #define TEUCHOS_SERIALIZATION_TRAITS_HELPERS_HPP
00044 
00045 #include "Teuchos_SerializationTraits.hpp"
00046 #include "Teuchos_ArrayView.hpp"
00047 
00048 namespace Teuchos {
00049 
00053 template <typename Ordinal, typename T>
00054 class ValueTypeSerializationBuffer {
00055 public:
00057   ValueTypeSerializationBuffer(
00058     const Ordinal count, T buffer[] 
00059     );
00063   ~ValueTypeSerializationBuffer();
00065   char* getCharBuffer() const;
00067   Ordinal getBytes() const;
00069   const ArrayView<char> getCharBufferView() const;
00070 private:
00071   Ordinal    count_;
00072   T          *buffer_;
00073   Ordinal    bytes_;
00074   char       *charBuffer_;
00075   // Not defined and not to be called
00076   ValueTypeSerializationBuffer();
00077   ValueTypeSerializationBuffer(const ValueTypeSerializationBuffer&);
00078   ValueTypeSerializationBuffer& operator=(const ValueTypeSerializationBuffer&);
00079 };
00080 
00084 template <typename Ordinal, typename T>
00085 class ConstValueTypeSerializationBuffer {
00086 public:
00088   ConstValueTypeSerializationBuffer(
00089     const Ordinal count, const T buffer[]
00090     );
00094   ~ConstValueTypeSerializationBuffer();
00096   const char* getCharBuffer() const;
00098   Ordinal getBytes() const;
00100   const ArrayView<const char> getCharBufferView() const;
00101 private:
00102   Ordinal    count_;
00103   const T    *buffer_;
00104   Ordinal    bytes_;
00105   const char *charBuffer_;
00106   // Not defined and not to be called
00107   ConstValueTypeSerializationBuffer();
00108   ConstValueTypeSerializationBuffer(const ConstValueTypeSerializationBuffer&);
00109   ConstValueTypeSerializationBuffer& operator=(const ConstValueTypeSerializationBuffer&);
00110 };
00111 
00116 template <typename Ordinal, typename T>
00117 class ValueTypeDeserializationBuffer {
00118 public:
00120   ValueTypeDeserializationBuffer(
00121     const Ordinal bytes, char charBuffer[] 
00122     );
00126   ~ValueTypeDeserializationBuffer();
00128   T* getBuffer() const;
00130   Ordinal getCount() const;
00131 private:
00132   Ordinal    bytes_;
00133   char       *charBuffer_;
00134   Ordinal    count_;
00135   T          *buffer_;
00136   // Not defined and not to be called
00137   ValueTypeDeserializationBuffer();
00138   ValueTypeDeserializationBuffer(const ValueTypeDeserializationBuffer&);
00139   ValueTypeDeserializationBuffer& operator=(const ValueTypeDeserializationBuffer&);
00140 };
00141 
00146 template <typename Ordinal, typename T>
00147 class ConstValueTypeDeserializationBuffer {
00148 public:
00150   ConstValueTypeDeserializationBuffer(
00151     const Ordinal bytes, const char charBuffer[]
00152     );
00156   ~ConstValueTypeDeserializationBuffer();
00158   const T* getBuffer() const;
00160   Ordinal getCount() const;
00161 private:
00162   Ordinal    bytes_;
00163   const char *charBuffer_;
00164   Ordinal    count_;
00165   const T    *buffer_;
00166   // Not defined and not to be called
00167   ConstValueTypeDeserializationBuffer();
00168   ConstValueTypeDeserializationBuffer(const ConstValueTypeDeserializationBuffer&);
00169   ConstValueTypeDeserializationBuffer& operator=(const ConstValueTypeDeserializationBuffer&);
00170 };
00171 
00172 // /////////////////////////////////////
00173 // Template implementations
00174 
00175 //
00176 // ValueTypeSerializationBuffer
00177 //
00178 // ToDo: Update this implementation to handle objects with indirect
00179 // serialization when needed!
00180 //
00181 
00182 template <typename Ordinal, typename T>
00183 ValueTypeSerializationBuffer<Ordinal,T>::ValueTypeSerializationBuffer(
00184   const Ordinal count, T buffer[]
00185   )
00186   :count_(count), buffer_(buffer)
00187 {
00188   typedef SerializationTraits<Ordinal,T> SerT;
00189   bytes_ = SerT::fromCountToDirectBytes(count_);
00190   charBuffer_ = SerT::convertToCharPtr(buffer_);
00191   // ToDo: Handle indirect serailization!
00192 }
00193 
00194 template <typename Ordinal, typename T>
00195 ValueTypeSerializationBuffer<Ordinal,T>::~ValueTypeSerializationBuffer()
00196 {
00197   // There is nothing to do since the type uses direct serialization!
00198   // ToDo: Handle indirect serailization!
00199 }
00200 
00201 template <typename Ordinal, typename T>
00202 char* ValueTypeSerializationBuffer<Ordinal,T>::getCharBuffer() const
00203 {
00204   return charBuffer_;
00205 }
00206 
00207 template <typename Ordinal, typename T>
00208 Ordinal ValueTypeSerializationBuffer<Ordinal,T>::getBytes() const
00209 {
00210   return bytes_;
00211 }
00212 
00213 
00214 template <typename Ordinal, typename T>
00215 const ArrayView<char>
00216 ValueTypeSerializationBuffer<Ordinal,T>::getCharBufferView() const
00217 {
00218   return arrayView(charBuffer_, bytes_);
00219 }
00220 
00221 
00222 //
00223 // ConstValueTypeSerializationBuffer
00224 //
00225 // ToDo: Update this implementation to handle objects with indirect
00226 // serialization when needed!
00227 //
00228 
00229 template <typename Ordinal, typename T>
00230 ConstValueTypeSerializationBuffer<Ordinal,T>::ConstValueTypeSerializationBuffer(
00231   const Ordinal count, const T buffer[]
00232   )
00233   :count_(count), buffer_(buffer)
00234 {
00235   typedef SerializationTraits<Ordinal,T> SerT;
00236   bytes_ = SerT::fromCountToDirectBytes(count_);
00237   charBuffer_ = SerT::convertToCharPtr(buffer_);
00238   // ToDo: Handle indirect serailization!
00239 }
00240 
00241 template <typename Ordinal, typename T>
00242 ConstValueTypeSerializationBuffer<Ordinal,T>::~ConstValueTypeSerializationBuffer()
00243 {
00244   // There is nothing to do since the type uses direct serialization!
00245   // ToDo: Handle indirect serailization!
00246 }
00247 
00248 template <typename Ordinal, typename T>
00249 const char* ConstValueTypeSerializationBuffer<Ordinal,T>::getCharBuffer() const
00250 {
00251   return charBuffer_;
00252 }
00253 
00254 template <typename Ordinal, typename T>
00255 Ordinal ConstValueTypeSerializationBuffer<Ordinal,T>::getBytes() const
00256 {
00257   return bytes_;
00258 }
00259 
00260 template <typename Ordinal, typename T>
00261 const ArrayView<const char>
00262 ConstValueTypeSerializationBuffer<Ordinal,T>::getCharBufferView() const
00263 {
00264   return arrayView(charBuffer_, bytes_);
00265 }
00266 
00267 //
00268 // ValueTypeDeserializationBuffer
00269 //
00270 // ToDo: Update this implementation to handle objects with indirect
00271 // serialization when needed!
00272 //
00273 
00274 template <typename Ordinal, typename T>
00275 ValueTypeDeserializationBuffer<Ordinal,T>::ValueTypeDeserializationBuffer(
00276   const Ordinal bytes, char charBuffer[]
00277   )
00278   :bytes_(bytes), charBuffer_(charBuffer)
00279 {
00280   typedef SerializationTraits<Ordinal,T> SerT;
00281   count_ = SerT::fromDirectBytesToCount(bytes_);
00282   buffer_ = SerT::convertFromCharPtr(charBuffer_);
00283   // ToDo: Handle indirect serailization!
00284 }
00285 
00286 template <typename Ordinal, typename T>
00287 ValueTypeDeserializationBuffer<Ordinal,T>::~ValueTypeDeserializationBuffer()
00288 {
00289   // There is nothing to do since the type uses direct serialization!
00290   // ToDo: Handle indirect serailization!
00291 }
00292 
00293 template <typename Ordinal, typename T>
00294 T* ValueTypeDeserializationBuffer<Ordinal,T>::getBuffer() const
00295 {
00296   return buffer_;
00297 }
00298 
00299 template <typename Ordinal, typename T>
00300 Ordinal ValueTypeDeserializationBuffer<Ordinal,T>::getCount() const
00301 {
00302   return count_;
00303 }
00304 
00305 //
00306 // ConstValueTypeDeserializationBuffer
00307 //
00308 // ToDo: Update this implementation to handle objects with indirect
00309 // serialization when needed!
00310 //
00311 
00312 template <typename Ordinal, typename T>
00313 ConstValueTypeDeserializationBuffer<Ordinal,T>::ConstValueTypeDeserializationBuffer(
00314   const Ordinal bytes, const char charBuffer[]
00315   )
00316   :bytes_(bytes), charBuffer_(charBuffer)
00317 {
00318   typedef SerializationTraits<Ordinal,T> SerT;
00319   count_ = SerT::fromDirectBytesToCount(bytes_);
00320   buffer_ = SerT::convertFromCharPtr(charBuffer_);
00321   // ToDo: Handle indirect serailization!
00322 }
00323 
00324 template <typename Ordinal, typename T>
00325 ConstValueTypeDeserializationBuffer<Ordinal,T>::~ConstValueTypeDeserializationBuffer()
00326 {
00327   // There is nothing to do since the type uses direct serialization!
00328   // ToDo: Handle indirect serailization!
00329 }
00330 
00331 template <typename Ordinal, typename T>
00332 const T* ConstValueTypeDeserializationBuffer<Ordinal,T>::getBuffer() const
00333 {
00334   return buffer_;
00335 }
00336 
00337 template <typename Ordinal, typename T>
00338 Ordinal ConstValueTypeDeserializationBuffer<Ordinal,T>::getCount() const
00339 {
00340   return count_;
00341 }
00342 
00343 } // namespace Teuchos
00344 
00345 #endif // TEUCHOS_SERIALIZATION_TRAITS_HELPERS_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines