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 #ifdef HAVE_TEUCHOS_QD
00040 #include <qd/dd_real.h>
00041 #include <qd/qd_real.h>
00042 #endif
00043 
00044 namespace Teuchos {
00045 
00046 
00047 template<typename T>
00048 struct UndefinedSerializationTraits {
00050   static inline T notDefined() {return(T::this_type_is_missing_a_specialization());}
00051 };
00052 
00053 
00060 template <typename Ordinal, typename T>
00061 class SerializationTraits {
00062 public:
00063   
00065 
00066 
00068   static const bool supportsDirectSerialization = false;
00069 
00071 
00073 
00074 
00076   static Ordinal fromCountToDirectBytes(const Ordinal count) { (void)count; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00077 
00079   static char* convertToCharPtr( T* ptr ) { (void)ptr; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00080 
00082   static const char* convertToCharPtr( const T* ptr ) { (void)ptr; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00083 
00085   static Ordinal fromDirectBytesToCount(const Ordinal bytes) { (void)bytes; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00086 
00088   static T* convertFromCharPtr( char* ptr ) { (void)ptr; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00089 
00091   static const T* convertFromCharPtr( const char* ptr ) { (void)ptr; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00092 
00094 
00096 
00097 
00099   static Ordinal fromCountToIndirectBytes(const Ordinal count) { (void)count; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00100 
00116   static void serialize(
00117     const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[]
00118     )
00119     { (void)count; (void)buffer; (void)bytes; (void)charBuffer; UndefinedSerializationTraits<T>::notDefined(); }
00120 
00122   static Ordinal fromIndirectBytesToCount(const Ordinal bytes) { (void)bytes; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00123 
00139   static void deserialize(
00140     const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[]
00141     )
00142     { (void)bytes; (void)charBuffer; (void)count; (void)buffer;
00143       UndefinedSerializationTraits<T>::notDefined(); }
00144   
00146 
00147 };
00148 
00149 
00150 template <typename Ordinal, typename T>
00151 class DirectSerializationTraits {
00152 public:
00153   static const bool supportsDirectSerialization = true;
00154   // Direct serialization
00155   static Ordinal fromCountToDirectBytes(const Ordinal count)
00156     { return sizeof(T)*count; }
00157   static char* convertToCharPtr( T* ptr )
00158     { return reinterpret_cast<char*>(ptr); }
00159   static const char* convertToCharPtr( const T* ptr )
00160     { return reinterpret_cast<const char*>(ptr); }
00161   static Ordinal fromDirectBytesToCount(const Ordinal count)
00162     { return count/sizeof(T); }
00163   static T* convertFromCharPtr( char* ptr )
00164     { return reinterpret_cast<T*>(ptr); }
00165   static const T* convertFromCharPtr( const char* ptr )
00166     { return reinterpret_cast<const T*>(ptr); }
00167   // Indirect serialization
00168   static Ordinal fromCountToIndirectBytes(const Ordinal count)
00169     { return fromCountToDirectBytes(count); }
00170   static void serialize(
00171     const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[]
00172     )
00173     {
00174 #ifdef TEUCHOS_DEBUG
00175       TEST_FOR_EXCEPT(bytes!=fromCountToIndirectBytes(count));
00176 #endif
00177       const char *_buffer = convertToCharPtr(buffer);
00178       std::copy(_buffer,_buffer+bytes,charBuffer);
00179     }
00180   static Ordinal fromIndirectBytesToCount(const Ordinal bytes) 
00181     { return fromDirectBytesToCount(bytes); }
00182   static void deserialize(
00183     const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[]
00184     )
00185     {
00186 #ifdef TEUCHOS_DEBUG
00187       TEST_FOR_EXCEPT(count!=fromIndirectBytesToCount(bytes));
00188 #endif
00189       char *_buffer = convertToCharPtr(buffer);
00190       std::copy(charBuffer,charBuffer+bytes,_buffer);
00191     }
00192 };
00193 
00194 template<typename Ordinal>
00195 class SerializationTraits<Ordinal,char>
00196   : public DirectSerializationTraits<Ordinal,char>
00197 {};
00198 
00199 template<typename Ordinal>
00200 class SerializationTraits<Ordinal,short int>
00201   : public DirectSerializationTraits<Ordinal,short int>
00202 {};
00203 
00204 template<typename Ordinal>
00205 class SerializationTraits<Ordinal,int>
00206   : public DirectSerializationTraits<Ordinal,int>
00207 {};
00208 
00209 template<typename Ordinal>
00210 class SerializationTraits<Ordinal,unsigned int>
00211   : public DirectSerializationTraits<Ordinal,unsigned int>
00212 {};
00213 
00214 template<typename Ordinal>
00215 class SerializationTraits<Ordinal,long int>
00216   : public DirectSerializationTraits<Ordinal,long int>
00217 {};
00218 
00219 template<typename Ordinal>
00220 class SerializationTraits<Ordinal,long unsigned int>
00221   : public DirectSerializationTraits<Ordinal,long unsigned int>
00222 {};
00223 
00224 template<typename Ordinal>
00225 class SerializationTraits<Ordinal,float>
00226   : public DirectSerializationTraits<Ordinal,float>
00227 {};
00228 
00229 template<typename Ordinal>
00230 class SerializationTraits<Ordinal,double>
00231   : public DirectSerializationTraits<Ordinal,double>
00232 {};
00233 
00234 template<typename Ordinal, typename Packet>
00235 class SerializationTraits<Ordinal,std::pair<Packet,Packet> >
00236   : public DirectSerializationTraits<Ordinal,std::pair<Packet,Packet> >
00237 {};
00238 
00239 #ifdef HAVE_TEUCHOS_QD
00240 template<typename Ordinal>
00241 class SerializationTraits<Ordinal,dd_real>
00242   : public DirectSerializationTraits<Ordinal,dd_real>
00243 {};
00244 
00245 template<typename Ordinal>
00246 class SerializationTraits<Ordinal,qd_real>
00247   : public DirectSerializationTraits<Ordinal,qd_real>
00248 {};
00249 #endif
00250 
00251 #ifdef HAVE_TEUCHOS_COMPLEX
00252 
00253 template<typename Ordinal>
00254 class SerializationTraits<Ordinal,std::complex<float> >
00255   : public DirectSerializationTraits<Ordinal,std::complex<float> >
00256 {};
00257 
00258 template<typename Ordinal>
00259 class SerializationTraits<Ordinal,std::complex<double> >
00260   : public DirectSerializationTraits<Ordinal,std::complex<double> >
00261 {};
00262 
00263 #endif // HAVE_TEUCHOS_COMPLEX
00264 
00265 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00266 
00267 template<typename Ordinal>
00268 class SerializationTraits<Ordinal, long long int>
00269   : public DirectSerializationTraits<Ordinal, long long int>
00270 {};
00271 
00272 #endif // HAVE_TEUCHOS_LONG_LONG_INT
00273 
00274 } // namespace Teuchos
00275 
00276 #endif // TEUCHOS_SERIALIZATION_TRAITS_HPP

Generated on Tue Jul 13 09:23:00 2010 for Teuchos - Trilinos Tools Package by  doxygen 1.4.7