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 
00042 
00043 // NOTE: Before adding specializations of ScalarTraits, make sure that they do not duplicate 
00044 // specializations already present in PyTrilinos (see packages/PyTrilinos/src/Teuchos_Traits.i)
00045 
00046 
00047 #ifndef TEUCHOS_SERIALIZATION_TRAITS_HPP
00048 #define TEUCHOS_SERIALIZATION_TRAITS_HPP
00049 
00050 #include "Teuchos_ConfigDefs.hpp"
00051 
00052 #ifdef HAVE_TEUCHOS_QD
00053 #include <qd/dd_real.h>
00054 #include <qd/qd_real.h>
00055 #endif
00056 
00057 namespace Teuchos {
00058 
00059 
00060 template<typename T>
00061 struct UndefinedSerializationTraits {
00063   static inline T notDefined() {return(T::this_type_is_missing_a_specialization());}
00064 };
00065 
00066 
00073 template <typename Ordinal, typename T>
00074 class SerializationTraits {
00075 public:
00076   
00078 
00079 
00081   static const bool supportsDirectSerialization = false;
00082 
00084 
00086 
00087 
00089   static Ordinal fromCountToDirectBytes(const Ordinal count) { (void)count; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00090 
00092   static char* convertToCharPtr( T* ptr ) { (void)ptr; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00093 
00095   static const char* convertToCharPtr( const T* ptr ) { (void)ptr; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00096 
00098   static Ordinal fromDirectBytesToCount(const Ordinal bytes) { (void)bytes; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00099 
00101   static T* convertFromCharPtr( char* ptr ) { (void)ptr; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00102 
00104   static const T* convertFromCharPtr( const char* ptr ) { (void)ptr; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00105 
00107 
00109 
00110 
00112   static Ordinal fromCountToIndirectBytes(const Ordinal count) { (void)count; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00113 
00129   static void serialize(
00130     const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[]
00131     )
00132     { (void)count; (void)buffer; (void)bytes; (void)charBuffer; UndefinedSerializationTraits<T>::notDefined(); }
00133 
00135   static Ordinal fromIndirectBytesToCount(const Ordinal bytes) { (void)bytes; UndefinedSerializationTraits<T>::notDefined(); return 0; }
00136 
00152   static void deserialize(
00153     const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[]
00154     )
00155     { (void)bytes; (void)charBuffer; (void)count; (void)buffer;
00156       UndefinedSerializationTraits<T>::notDefined(); }
00157   
00159 
00160 };
00161 
00162 
00163 template <typename Ordinal, typename T>
00164 class DirectSerializationTraits {
00165 public:
00166   static const bool supportsDirectSerialization = true;
00167   // Direct serialization
00168   static Ordinal fromCountToDirectBytes(const Ordinal count)
00169     { return sizeof(T)*count; }
00170   static char* convertToCharPtr( T* ptr )
00171     { return reinterpret_cast<char*>(ptr); }
00172   static const char* convertToCharPtr( const T* ptr )
00173     { return reinterpret_cast<const char*>(ptr); }
00174   static Ordinal fromDirectBytesToCount(const Ordinal count)
00175     { return count/sizeof(T); }
00176   static T* convertFromCharPtr( char* ptr )
00177     { return reinterpret_cast<T*>(ptr); }
00178   static const T* convertFromCharPtr( const char* ptr )
00179     { return reinterpret_cast<const T*>(ptr); }
00180   // Indirect serialization
00181   static Ordinal fromCountToIndirectBytes(const Ordinal count)
00182     { return fromCountToDirectBytes(count); }
00183   static void serialize(
00184     const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[]
00185     )
00186     {
00187 #ifdef TEUCHOS_DEBUG
00188       TEST_FOR_EXCEPT(bytes!=fromCountToIndirectBytes(count));
00189 #endif
00190       const char *_buffer = convertToCharPtr(buffer);
00191       std::copy(_buffer,_buffer+bytes,charBuffer);
00192     }
00193   static Ordinal fromIndirectBytesToCount(const Ordinal bytes) 
00194     { return fromDirectBytesToCount(bytes); }
00195   static void deserialize(
00196     const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[]
00197     )
00198     {
00199 #ifdef TEUCHOS_DEBUG
00200       TEST_FOR_EXCEPT(count!=fromIndirectBytesToCount(bytes));
00201 #endif
00202       char *_buffer = convertToCharPtr(buffer);
00203       std::copy(charBuffer,charBuffer+bytes,_buffer);
00204     }
00205 };
00206 
00207 template<typename Ordinal>
00208 class SerializationTraits<Ordinal,char>
00209   : public DirectSerializationTraits<Ordinal,char>
00210 {};
00211 
00212 template<typename Ordinal>
00213 class SerializationTraits<Ordinal,short int>
00214   : public DirectSerializationTraits<Ordinal,short int>
00215 {};
00216 
00217 template<typename Ordinal>
00218 class SerializationTraits<Ordinal,int>
00219   : public DirectSerializationTraits<Ordinal,int>
00220 {};
00221 
00222 template<typename Ordinal>
00223 class SerializationTraits<Ordinal,unsigned int>
00224   : public DirectSerializationTraits<Ordinal,unsigned int>
00225 {};
00226 
00227 template<typename Ordinal>
00228 class SerializationTraits<Ordinal,long int>
00229   : public DirectSerializationTraits<Ordinal,long int>
00230 {};
00231 
00232 template<typename Ordinal>
00233 class SerializationTraits<Ordinal,long unsigned int>
00234   : public DirectSerializationTraits<Ordinal,long unsigned int>
00235 {};
00236 
00237 template<typename Ordinal>
00238 class SerializationTraits<Ordinal,float>
00239   : public DirectSerializationTraits<Ordinal,float>
00240 {};
00241 
00242 template<typename Ordinal>
00243 class SerializationTraits<Ordinal,double>
00244   : public DirectSerializationTraits<Ordinal,double>
00245 {};
00246 
00247 template<typename Ordinal, typename P1, typename P2>
00248 class SerializationTraits<Ordinal,std::pair<P1,P2> >
00249   : public DirectSerializationTraits<Ordinal,std::pair<P1,P2> >
00250 {};
00251 
00252 #ifdef HAVE_TEUCHOS_QD
00253 template<typename Ordinal>
00254 class SerializationTraits<Ordinal,dd_real>
00255   : public DirectSerializationTraits<Ordinal,dd_real>
00256 {};
00257 
00258 template<typename Ordinal>
00259 class SerializationTraits<Ordinal,qd_real>
00260   : public DirectSerializationTraits<Ordinal,qd_real>
00261 {};
00262 #endif
00263 
00264 #ifdef HAVE_TEUCHOS_COMPLEX
00265 
00266 template<typename Ordinal>
00267 class SerializationTraits<Ordinal,std::complex<float> >
00268   : public DirectSerializationTraits<Ordinal,std::complex<float> >
00269 {};
00270 
00271 template<typename Ordinal>
00272 class SerializationTraits<Ordinal,std::complex<double> >
00273   : public DirectSerializationTraits<Ordinal,std::complex<double> >
00274 {};
00275 
00276 #endif // HAVE_TEUCHOS_COMPLEX
00277 
00278 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00279 
00280 template<typename Ordinal>
00281 class SerializationTraits<Ordinal, long long int>
00282   : public DirectSerializationTraits<Ordinal, long long int>
00283 {};
00284 
00285 #endif // HAVE_TEUCHOS_LONG_LONG_INT
00286 
00287 } // namespace Teuchos
00288 
00289 #endif // TEUCHOS_SERIALIZATION_TRAITS_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines