Teuchos_SerializationTraits.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 
00030 // NOTE: Before adding specializations of ScalarTraits, make sure that they do not duplicate 
00031 // specializations already present in PyTrilinos (see packages/PyTrilinos/src/Teuchos_Traits.i)
00032 
00033 
00034 #ifndef TEUCHOS_SERIALIZATION_TRAITS_HPP
00035 #define TEUCHOS_SERIALIZATION_TRAITS_HPP
00036 
00037 #include "Teuchos_ConfigDefs.hpp"
00038 
00039 namespace Teuchos {
00040 
00041 
00042 template<typename T>
00043 struct UndefinedSerializationTraits {
00045   static inline T notDefined() {return(T::this_type_is_missing_a_specialization());}
00046 };
00047 
00048 
00055 template <typename Ordinal, typename T>
00056 class SerializationTraits {
00057 public:
00058   
00060 
00061 
00063   static const bool supportsDirectSerialization = false;
00064 
00066 
00068 
00069 
00071   static Ordinal fromCountToDirectBytes(const Ordinal count) { (void)count; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00072 
00074   static char* convertToCharPtr( T* ptr ) { (void)ptr; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00075 
00077   static const char* convertToCharPtr( const T* ptr ) { (void)ptr; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00078 
00080   static Ordinal fromDirectBytesToCount(const Ordinal bytes) { (void)bytes; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00081 
00083   static T* convertFromCharPtr( char* ptr ) { (void)ptr; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00084 
00086   static const T* convertFromCharPtr( const char* ptr ) { (void)ptr; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00087 
00089 
00091 
00092 
00094   static Ordinal fromCountToIndirectBytes(const Ordinal count) { (void)count; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00095 
00111   static void serialize(
00112     const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[]
00113     )
00114     { (void)count; (void)buffer; (void)bytes; (void)charBuffer; UndefinedSerializationTraits<T>::notDefined(); }
00115 
00117   static Ordinal fromIndirectBytesToCount(const Ordinal bytes) { (void)bytes; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00118 
00134   static void deserialize(
00135     const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[]
00136     )
00137     { (void)bytes; (void)charBuffer; (void)count; (void)buffer;
00138       UndefinedSerializationTraits<T>::notDefined(); }
00139   
00141 
00142 };
00143 
00144 
00145 template <typename Ordinal, typename T>
00146 class DirectSerializationTraits {
00147 public:
00148   static const bool supportsDirectSerialization = true;
00149   // Direct serialization
00150   static Ordinal fromCountToDirectBytes(const Ordinal count)
00151     { return sizeof(T)*count; }
00152   static char* convertToCharPtr( T* ptr )
00153     { return reinterpret_cast<char*>(ptr); }
00154   static const char* convertToCharPtr( const T* ptr )
00155     { return reinterpret_cast<const char*>(ptr); }
00156   static Ordinal fromDirectBytesToCount(const Ordinal count)
00157     { return count/sizeof(T); }
00158   static T* convertFromCharPtr( char* ptr )
00159     { return reinterpret_cast<T*>(ptr); }
00160   static const T* convertFromCharPtr( const char* ptr )
00161     { return reinterpret_cast<const T*>(ptr); }
00162   // Indirect serialization
00163   static Ordinal fromCountToIndirectBytes(const Ordinal count)
00164     { return fromCountToDirectBytes(count); }
00165   static void serialize(
00166     const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[]
00167     )
00168     {
00169 #ifdef TEUCHOS_DEBUG
00170       TEST_FOR_EXCEPT(bytes!=fromCountToIndirectBytes(count));
00171 #endif
00172       const char *_buffer = convertToCharPtr(buffer);
00173       std::copy(_buffer,_buffer+bytes,charBuffer);
00174     }
00175   static Ordinal fromIndirectBytesToCount(const Ordinal bytes) 
00176     { return fromDirectBytesToCount(bytes); }
00177   static void deserialize(
00178     const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[]
00179     )
00180     {
00181 #ifdef TEUCHOS_DEBUG
00182       TEST_FOR_EXCEPT(count!=fromIndirectBytesToCount(bytes));
00183 #endif
00184       char *_buffer = convertToCharPtr(buffer);
00185       std::copy(charBuffer,charBuffer+bytes,_buffer);
00186     }
00187 };
00188 
00189 template<typename Ordinal>
00190 class SerializationTraits<Ordinal,char>
00191   : public DirectSerializationTraits<Ordinal,char>
00192 {};
00193 
00194 template<typename Ordinal>
00195 class SerializationTraits<Ordinal,short int>
00196   : public DirectSerializationTraits<Ordinal,short int>
00197 {};
00198 
00199 template<typename Ordinal>
00200 class SerializationTraits<Ordinal,int>
00201   : public DirectSerializationTraits<Ordinal,int>
00202 {};
00203 
00204 template<typename Ordinal>
00205 class SerializationTraits<Ordinal,long int>
00206   : public DirectSerializationTraits<Ordinal,long int>
00207 {};
00208 
00209 template<typename Ordinal>
00210 class SerializationTraits<Ordinal,float>
00211   : public DirectSerializationTraits<Ordinal,float>
00212 {};
00213 
00214 template<typename Ordinal>
00215 class SerializationTraits<Ordinal,double>
00216   : public DirectSerializationTraits<Ordinal,double>
00217 {};
00218 
00219 template<typename Ordinal, typename Packet>
00220 class SerializationTraits<Ordinal,std::pair<Packet,Packet> >
00221   : public DirectSerializationTraits<Ordinal,std::pair<Packet,Packet> >
00222 {};
00223 
00224 #if defined(HAVE_COMPLEX) && defined(HAVE_TEUCHOS_COMPLEX)
00225 
00226 template<typename Ordinal>
00227 class SerializationTraits<Ordinal,std::complex<float> >
00228   : public DirectSerializationTraits<Ordinal,std::complex<float> >
00229 {};
00230 
00231 template<typename Ordinal>
00232 class SerializationTraits<Ordinal,std::complex<double> >
00233   : public DirectSerializationTraits<Ordinal,std::complex<double> >
00234 {};
00235 
00236 #endif // defined(HAVE_COMPLEX) && defined(HAVE_TEUCHOS_COMPLEX)
00237 
00238 } // namespace Teuchos
00239 
00240 #endif // TEUCHOS_SERIALIZATION_TRAITS_HPP

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