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 #ifndef TEUCHOS_SERIALIZATION_TRAITS_HPP
00030 #define TEUCHOS_SERIALIZATION_TRAITS_HPP
00031 
00032 #include "Teuchos_ConfigDefs.hpp"
00033 
00034 namespace Teuchos {
00035 
00036 template<typename T>
00037 struct UndefinedSerializationTraits {
00039   static inline T notDefined() {return(T::this_type_is_missing_a_specialization());}
00040 };
00041 
00048 template <typename Ordinal, typename T>
00049 class SerializationTraits {
00050 public:
00051   
00053 
00054 
00056   static const bool supportsDirectSerialization = false;
00057 
00059 
00061 
00062 
00064   static Ordinal fromCountToDirectBytes(const Ordinal count) { UndefinedSerializationTraits<T>::notDefined(); return 0; }
00065 
00067   static char* convertToCharPtr( T* ptr ) { UndefinedSerializationTraits<T>::notDefined(); return 0; }
00068 
00070   static const char* convertToCharPtr( const T* ptr ) { UndefinedSerializationTraits<T>::notDefined(); return 0; }
00071 
00073   static Ordinal fromDirectBytesToCount(const Ordinal bytes) { UndefinedSerializationTraits<T>::notDefined(); return 0; }
00074 
00076   static T* convertFromCharPtr( char* ptr ) { UndefinedSerializationTraits<T>::notDefined(); return 0; }
00077 
00079   static const T* convertFromCharPtr( const char* ptr ) { UndefinedSerializationTraits<T>::notDefined(); return 0; }
00080 
00082 
00084 
00085 
00087   static Ordinal fromCountToIndirectBytes(const Ordinal count) { UndefinedSerializationTraits<T>::notDefined(); return 0; }
00088 
00104   static void serialize(
00105     const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[]
00106     )
00107     { UndefinedSerializationTraits<T>::notDefined(); }
00108 
00110   static Ordinal fromIndirectBytesToCount(const Ordinal bytes) { UndefinedSerializationTraits<T>::notDefined(); return 0; }
00111 
00127   static void deserialize(
00128     const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[]
00129     )
00130     { UndefinedSerializationTraits<T>::notDefined(); }
00131   
00133 
00134 };
00135 
00136 template <typename Ordinal, typename T>
00137 class DirectSerializationTraits {
00138 public:
00139   static const bool supportsDirectSerialization = true;
00140   // Direct serialization
00141   static Ordinal fromCountToDirectBytes(const Ordinal count)
00142     { return sizeof(T)*count; }
00143   static char* convertToCharPtr( T* ptr )
00144     { return reinterpret_cast<char*>(ptr); }
00145   static const char* convertToCharPtr( const T* ptr )
00146     { return reinterpret_cast<const char*>(ptr); }
00147   static Ordinal fromDirectBytesToCount(const Ordinal count)
00148     { return count/sizeof(T); }
00149   static T* convertFromCharPtr( char* ptr )
00150     { return reinterpret_cast<T*>(ptr); }
00151   static const T* convertFromCharPtr( const char* ptr )
00152     { return reinterpret_cast<const T*>(ptr); }
00153   // Indirect serialization
00154   static Ordinal fromCountToIndirectBytes(const Ordinal count)
00155     { return fromCountToDirectBytes(count); }
00156   static void serialize(
00157     const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[]
00158     )
00159     {
00160 #ifdef TEUCHOS_DEBUG
00161       TEST_FOR_EXCEPT(bytes!=fromCountToIndirectBytes(count));
00162 #endif
00163       const char *_buffer = convertToCharPtr(buffer);
00164       std::copy(_buffer,_buffer+bytes,charBuffer);
00165     }
00166   static Ordinal fromIndirectBytesToCount(const Ordinal bytes) 
00167     { return fromDirectBytesToCount(bytes); }
00168   static void deserialize(
00169     const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[]
00170     )
00171     {
00172 #ifdef TEUCHOS_DEBUG
00173       TEST_FOR_EXCEPT(count!=fromIndirectBytesToCount(bytes));
00174 #endif
00175       char *_buffer = convertToCharPtr(buffer);
00176       std::copy(charBuffer,charBuffer+bytes,_buffer);
00177     }
00178 };
00179 
00180 template<typename Ordinal>
00181 class SerializationTraits<Ordinal,char>
00182   : public DirectSerializationTraits<Ordinal,char>
00183 {};
00184 
00185 template<typename Ordinal>
00186 class SerializationTraits<Ordinal,int>
00187   : public DirectSerializationTraits<Ordinal,int>
00188 {};
00189 
00190 template<typename Ordinal>
00191 class SerializationTraits<Ordinal,float>
00192   : public DirectSerializationTraits<Ordinal,float>
00193 {};
00194 
00195 template<typename Ordinal>
00196 class SerializationTraits<Ordinal,double>
00197   : public DirectSerializationTraits<Ordinal,double>
00198 {};
00199 
00200 #if defined(HAVE_COMPLEX) && defined(HAVE_TEUCHOS_COMPLEX)
00201 
00202 template<typename Ordinal>
00203 class SerializationTraits<Ordinal,std::complex<float> >
00204   : public DirectSerializationTraits<Ordinal,std::complex<float> >
00205 {};
00206 
00207 template<typename Ordinal>
00208 class SerializationTraits<Ordinal,std::complex<double> >
00209   : public DirectSerializationTraits<Ordinal,std::complex<double> >
00210 {};
00211 
00212 #endif // defined(HAVE_COMPLEX) && defined(HAVE_TEUCHOS_COMPLEX)
00213 
00214 } // namespace Teuchos
00215 
00216 #endif // TEUCHOS_SERIALIZATION_TRAITS_HPP

Generated on Tue Oct 20 12:45:26 2009 for Teuchos - Trilinos Tools Package by doxygen 1.4.7