Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_SerializationTraits.hpp
Go to the documentation of this file.
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 
00045 #ifndef TEUCHOS_SERIALIZATION_TRAITS_HPP
00046 #define TEUCHOS_SERIALIZATION_TRAITS_HPP
00047 
00048 #include "Teuchos_ConfigDefs.hpp"
00049 
00050 #ifdef HAVE_TEUCHOS_QD
00051 #include <qd/dd_real.h>
00052 #include <qd/qd_real.h>
00053 #endif
00054 
00055 namespace Teuchos {
00056 
00063 template<typename T>
00064 struct UndefinedSerializationTraits {
00066   static inline T notDefined() {return(T::this_type_is_missing_a_specialization());}
00067 };
00068 
00069 
00123 template <typename Ordinal, typename T>
00124 class SerializationTraits {
00125 public:
00126   
00128 
00129 
00134   static const bool supportsDirectSerialization = false;
00135 
00137 
00139 
00140 
00142   static Ordinal fromCountToDirectBytes(const Ordinal count) { 
00143     (void)count; 
00144     UndefinedSerializationTraits<T>::notDefined(); 
00145     return 0; 
00146   }
00147 
00149   static char* convertToCharPtr( T* ptr ) { 
00150     (void)ptr; 
00151     UndefinedSerializationTraits<T>::notDefined(); 
00152     return 0; 
00153   }
00154 
00156   static const char* convertToCharPtr( const T* ptr ) { 
00157     (void)ptr; 
00158     UndefinedSerializationTraits<T>::notDefined(); 
00159     return 0; 
00160   }
00161 
00163   static Ordinal fromDirectBytesToCount(const Ordinal bytes) { 
00164     (void)bytes; 
00165     UndefinedSerializationTraits<T>::notDefined(); 
00166     return 0; 
00167   }
00168 
00170   static T* convertFromCharPtr( char* ptr ) { 
00171     (void)ptr; 
00172     UndefinedSerializationTraits<T>::notDefined(); 
00173     return 0; 
00174   }
00175 
00177   static const T* convertFromCharPtr( const char* ptr ) { 
00178     (void)ptr; 
00179     UndefinedSerializationTraits<T>::notDefined(); 
00180     return 0; 
00181   }
00182 
00184 
00186 
00187 
00189   static Ordinal fromCountToIndirectBytes(const Ordinal count, 
00190             const T buffer[]) { 
00191     (void)count; (void)buffer; 
00192     UndefinedSerializationTraits<T>::notDefined(); 
00193     return 0; 
00194   }
00195 
00211   static void serialize (const Ordinal count, 
00212        const T buffer[], 
00213        const Ordinal bytes, 
00214        char charBuffer[])
00215   { 
00216     (void)count; (void)buffer; (void)bytes; (void)charBuffer; 
00217     UndefinedSerializationTraits<T>::notDefined(); 
00218   }
00219 
00221   static Ordinal fromIndirectBytesToCount(const Ordinal bytes, 
00222             const char charBuffer[]) { 
00223     (void)bytes; (void)charBuffer; 
00224     UndefinedSerializationTraits<T>::notDefined(); 
00225     return 0; 
00226   }
00227 
00243   static void deserialize (const Ordinal bytes, 
00244          const char charBuffer[], 
00245          const Ordinal count, 
00246          T buffer[])
00247   { 
00248     (void)bytes; (void)charBuffer; (void)count; (void)buffer;
00249     UndefinedSerializationTraits<T>::notDefined(); 
00250   }
00251   
00253 
00254 };
00255 
00269 template <typename Ordinal, typename T>
00270 class ValueTypeSerializer : public Teuchos::SerializationTraits<Ordinal,T> {};
00271 
00304 template <typename Ordinal, typename T>
00305 class DirectSerializationTraits {
00306 public:
00307   static const bool supportsDirectSerialization = true;
00308   // Direct serialization
00309   static Ordinal fromCountToDirectBytes(const Ordinal count)
00310     { return sizeof(T)*count; }
00311   static char* convertToCharPtr( T* ptr )
00312     { return reinterpret_cast<char*>(ptr); }
00313   static const char* convertToCharPtr( const T* ptr )
00314     { return reinterpret_cast<const char*>(ptr); }
00315   static Ordinal fromDirectBytesToCount(const Ordinal count)
00316     { return count/sizeof(T); }
00317   static T* convertFromCharPtr( char* ptr )
00318     { return reinterpret_cast<T*>(ptr); }
00319   static const T* convertFromCharPtr( const char* ptr )
00320     { return reinterpret_cast<const T*>(ptr); }
00321   // Indirect serialization
00322   static Ordinal fromCountToIndirectBytes(const Ordinal count, const T buffer[])
00323     { return fromCountToDirectBytes(count); }
00324   static void serialize(
00325     const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[]
00326     )
00327     {
00328 #ifdef TEUCHOS_DEBUG
00329       TEUCHOS_TEST_FOR_EXCEPT(bytes!=fromCountToDirectBytes(count));
00330 #endif
00331       const char *_buffer = convertToCharPtr(buffer);
00332       std::copy(_buffer,_buffer+bytes,charBuffer);
00333     }
00334   static Ordinal fromIndirectBytesToCount(const Ordinal bytes, 
00335             const char charBuffer[]) 
00336     { return fromDirectBytesToCount(bytes); }
00337   static void deserialize(
00338     const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[]
00339     )
00340     {
00341 #ifdef TEUCHOS_DEBUG
00342       TEUCHOS_TEST_FOR_EXCEPT(count!=fromDirectBytesToCount(bytes));
00343 #endif
00344       char *_buffer = convertToCharPtr(buffer);
00345       std::copy(charBuffer,charBuffer+bytes,_buffer);
00346     }
00347 };
00348 
00349 template<typename Ordinal>
00350 class SerializationTraits<Ordinal,char>
00351   : public DirectSerializationTraits<Ordinal,char>
00352 {};
00353 
00354 template<typename Ordinal>
00355 class SerializationTraits<Ordinal,short int>
00356   : public DirectSerializationTraits<Ordinal,short int>
00357 {};
00358 
00359 template<typename Ordinal>
00360 class SerializationTraits<Ordinal,int>
00361   : public DirectSerializationTraits<Ordinal,int>
00362 {};
00363 
00364 template<typename Ordinal>
00365 class SerializationTraits<Ordinal,unsigned int>
00366   : public DirectSerializationTraits<Ordinal,unsigned int>
00367 {};
00368 
00369 template<typename Ordinal>
00370 class SerializationTraits<Ordinal,long int>
00371   : public DirectSerializationTraits<Ordinal,long int>
00372 {};
00373 
00374 template<typename Ordinal>
00375 class SerializationTraits<Ordinal,long unsigned int>
00376   : public DirectSerializationTraits<Ordinal,long unsigned int>
00377 {};
00378 
00379 template<typename Ordinal>
00380 class SerializationTraits<Ordinal,float>
00381   : public DirectSerializationTraits<Ordinal,float>
00382 {};
00383 
00384 template<typename Ordinal>
00385 class SerializationTraits<Ordinal,double>
00386   : public DirectSerializationTraits<Ordinal,double>
00387 {};
00388 
00389 template<typename Ordinal, typename P1, typename P2>
00390 class SerializationTraits<Ordinal,std::pair<P1,P2> >
00391   : public DirectSerializationTraits<Ordinal,std::pair<P1,P2> >
00392 {};
00393 
00394 #ifdef HAVE_TEUCHOS_QD
00395 template<typename Ordinal>
00396 class SerializationTraits<Ordinal,dd_real>
00397   : public DirectSerializationTraits<Ordinal,dd_real>
00398 {};
00399 
00400 template<typename Ordinal>
00401 class SerializationTraits<Ordinal,qd_real>
00402   : public DirectSerializationTraits<Ordinal,qd_real>
00403 {};
00404 #endif
00405 
00406 #ifdef HAVE_TEUCHOS_COMPLEX
00407 
00408 template<typename Ordinal>
00409 class SerializationTraits<Ordinal,std::complex<float> >
00410   : public DirectSerializationTraits<Ordinal,std::complex<float> >
00411 {};
00412 
00413 template<typename Ordinal>
00414 class SerializationTraits<Ordinal,std::complex<double> >
00415   : public DirectSerializationTraits<Ordinal,std::complex<double> >
00416 {};
00417 
00418 #endif // HAVE_TEUCHOS_COMPLEX
00419 
00420 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00421 
00422 template<typename Ordinal>
00423 class SerializationTraits<Ordinal, long long int>
00424   : public DirectSerializationTraits<Ordinal, long long int>
00425 {};
00426 
00427 #endif // HAVE_TEUCHOS_LONG_LONG_INT
00428 
00429 } // namespace Teuchos
00430 
00431 #endif // TEUCHOS_SERIALIZATION_TRAITS_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines