Teuchos - Trilinos Tools Package 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 #include "Teuchos_TestForException.hpp"
00050 #include <climits> // SIZE_MAX, ULONG_MAX, etc.
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 
00065 template<typename T>
00066 struct UndefinedSerializationTraits {
00068   static inline T notDefined() {return(T::this_type_is_missing_a_specialization());}
00069 };
00070 
00071 
00125 template <typename Ordinal, typename T>
00126 class SerializationTraits {
00127 public:
00128   
00130 
00131 
00136   static const bool supportsDirectSerialization = false;
00137 
00139 
00141 
00142 
00144   static Ordinal fromCountToDirectBytes(const Ordinal count) { 
00145     (void)count; 
00146     UndefinedSerializationTraits<T>::notDefined(); 
00147     return 0; 
00148   }
00149 
00151   static char* convertToCharPtr( T* ptr ) { 
00152     (void)ptr; 
00153     UndefinedSerializationTraits<T>::notDefined(); 
00154     return 0; 
00155   }
00156 
00158   static const char* convertToCharPtr( const T* ptr ) { 
00159     (void)ptr; 
00160     UndefinedSerializationTraits<T>::notDefined(); 
00161     return 0; 
00162   }
00163 
00165   static Ordinal fromDirectBytesToCount(const Ordinal bytes) { 
00166     (void)bytes; 
00167     UndefinedSerializationTraits<T>::notDefined(); 
00168     return 0; 
00169   }
00170 
00172   static T* convertFromCharPtr( char* ptr ) { 
00173     (void)ptr; 
00174     UndefinedSerializationTraits<T>::notDefined(); 
00175     return 0; 
00176   }
00177 
00179   static const T* convertFromCharPtr( const char* ptr ) { 
00180     (void)ptr; 
00181     UndefinedSerializationTraits<T>::notDefined(); 
00182     return 0; 
00183   }
00184 
00186 
00188 
00189 
00191   static Ordinal fromCountToIndirectBytes(const Ordinal count, 
00192             const T buffer[]) { 
00193     (void)count; (void)buffer; 
00194     UndefinedSerializationTraits<T>::notDefined(); 
00195     return 0; 
00196   }
00197 
00213   static void serialize (const Ordinal count, 
00214        const T buffer[], 
00215        const Ordinal bytes, 
00216        char charBuffer[])
00217   { 
00218     (void)count; (void)buffer; (void)bytes; (void)charBuffer; 
00219     UndefinedSerializationTraits<T>::notDefined(); 
00220   }
00221 
00223   static Ordinal fromIndirectBytesToCount(const Ordinal bytes, 
00224             const char charBuffer[]) { 
00225     (void)bytes; (void)charBuffer; 
00226     UndefinedSerializationTraits<T>::notDefined(); 
00227     return 0; 
00228   }
00229 
00245   static void deserialize (const Ordinal bytes, 
00246          const char charBuffer[], 
00247          const Ordinal count, 
00248          T buffer[])
00249   { 
00250     (void)bytes; (void)charBuffer; (void)count; (void)buffer;
00251     UndefinedSerializationTraits<T>::notDefined(); 
00252   }
00253   
00255 
00256 };
00257 
00271 template <typename Ordinal, typename T>
00272 class ValueTypeSerializer : public Teuchos::SerializationTraits<Ordinal,T> {};
00273 
00306 template <typename Ordinal, typename T>
00307 class DirectSerializationTraits {
00308 public:
00309   static const bool supportsDirectSerialization = true;
00310   // Direct serialization
00311   static Ordinal fromCountToDirectBytes(const Ordinal count)
00312     { return sizeof(T)*count; }
00313   static char* convertToCharPtr( T* ptr )
00314     { return reinterpret_cast<char*>(ptr); }
00315   static const char* convertToCharPtr( const T* ptr )
00316     { return reinterpret_cast<const char*>(ptr); }
00317   static Ordinal fromDirectBytesToCount(const Ordinal count)
00318     { return count/sizeof(T); }
00319   static T* convertFromCharPtr( char* ptr )
00320     { return reinterpret_cast<T*>(ptr); }
00321   static const T* convertFromCharPtr( const char* ptr )
00322     { return reinterpret_cast<const T*>(ptr); }
00323   // Indirect serialization
00324   static Ordinal fromCountToIndirectBytes(const Ordinal count, const T buffer[])
00325     { return fromCountToDirectBytes(count); }
00326   static void serialize(
00327     const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[]
00328     )
00329     {
00330 #ifdef TEUCHOS_DEBUG
00331       TEUCHOS_TEST_FOR_EXCEPT(bytes!=fromCountToDirectBytes(count));
00332 #endif
00333       const char *_buffer = convertToCharPtr(buffer);
00334       std::copy(_buffer,_buffer+bytes,charBuffer);
00335     }
00336   static Ordinal fromIndirectBytesToCount(const Ordinal bytes, 
00337             const char charBuffer[]) 
00338     { return fromDirectBytesToCount(bytes); }
00339   static void deserialize(
00340     const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[]
00341     )
00342     {
00343 #ifdef TEUCHOS_DEBUG
00344       TEUCHOS_TEST_FOR_EXCEPT(count!=fromDirectBytesToCount(bytes));
00345 #endif
00346       char *_buffer = convertToCharPtr(buffer);
00347       std::copy(charBuffer,charBuffer+bytes,_buffer);
00348     }
00349 };
00350 
00351 // Whether 'char' is signed or unsigned depends on the implementation.
00352 // However, on some systems (e.g., Clang 3.1 on Intel Mac), partially
00353 // specializing for signed char and unsigned char, but not for char,
00354 // does not work.  Thus, we include specializations for all three
00355 // possibilities.
00356 template<typename Ordinal>
00357 class SerializationTraits<Ordinal,char>
00358   : public DirectSerializationTraits<Ordinal,char>
00359 {};
00360 
00361 template<typename Ordinal>
00362 class SerializationTraits<Ordinal,signed char>
00363   : public DirectSerializationTraits<Ordinal,signed char>
00364 {};
00365 
00366 template<typename Ordinal>
00367 class SerializationTraits<Ordinal,unsigned char>
00368   : public DirectSerializationTraits<Ordinal,unsigned char>
00369 {};
00370 
00371 template<typename Ordinal>
00372 class SerializationTraits<Ordinal,short int>
00373   : public DirectSerializationTraits<Ordinal,short int>
00374 {};
00375 
00376 template<typename Ordinal>
00377 class SerializationTraits<Ordinal,unsigned short int>
00378   : public DirectSerializationTraits<Ordinal,unsigned short int>
00379 {};
00380 
00381 template<typename Ordinal>
00382 class SerializationTraits<Ordinal,int>
00383   : public DirectSerializationTraits<Ordinal,int>
00384 {};
00385 
00386 template<typename Ordinal>
00387 class SerializationTraits<Ordinal,unsigned int>
00388   : public DirectSerializationTraits<Ordinal,unsigned int>
00389 {};
00390 
00391 template<typename Ordinal>
00392 class SerializationTraits<Ordinal,long int>
00393   : public DirectSerializationTraits<Ordinal,long int>
00394 {};
00395 
00396 template<typename Ordinal>
00397 class SerializationTraits<Ordinal,unsigned long int>
00398   : public DirectSerializationTraits<Ordinal,long unsigned int>
00399 {};
00400 
00401 template<typename Ordinal>
00402 class SerializationTraits<Ordinal,float>
00403   : public DirectSerializationTraits<Ordinal,float>
00404 {};
00405 
00406 template<typename Ordinal>
00407 class SerializationTraits<Ordinal,double>
00408   : public DirectSerializationTraits<Ordinal,double>
00409 {};
00410 
00411 // FIXME: How do we know that P1 and P2 are directly serializable?
00412 template<typename Ordinal, typename P1, typename P2>
00413 class SerializationTraits<Ordinal,std::pair<P1,P2> >
00414   : public DirectSerializationTraits<Ordinal,std::pair<P1,P2> >
00415 {};
00416 
00417 #ifdef HAVE_TEUCHOS_QD
00418 template<typename Ordinal>
00419 class SerializationTraits<Ordinal,dd_real>
00420   : public DirectSerializationTraits<Ordinal,dd_real>
00421 {};
00422 
00423 template<typename Ordinal>
00424 class SerializationTraits<Ordinal,qd_real>
00425   : public DirectSerializationTraits<Ordinal,qd_real>
00426 {};
00427 #endif
00428 
00429 #ifdef HAVE_TEUCHOS_COMPLEX
00430 
00431 template<typename Ordinal>
00432 class SerializationTraits<Ordinal,std::complex<float> >
00433   : public DirectSerializationTraits<Ordinal,std::complex<float> >
00434 {};
00435 
00436 template<typename Ordinal>
00437 class SerializationTraits<Ordinal,std::complex<double> >
00438   : public DirectSerializationTraits<Ordinal,std::complex<double> >
00439 {};
00440 
00441 #endif // HAVE_TEUCHOS_COMPLEX
00442 
00443 #if defined(HAVE_TEUCHOS_LONG_LONG_INT)
00444 
00445 // Partial specialization for long long.
00446 // On platforms with sizeof(ptrdiff_t) <= sizeof(long long), 
00447 // this should take care of the ptrdiff_t specialization as well,
00448 // since we've covered all built-in signed integer types above 
00449 // with size <= sizeof(long long).
00450 template<typename Ordinal>
00451 class SerializationTraits<Ordinal, long long int>
00452   : public DirectSerializationTraits<Ordinal, long long int>
00453 {};
00454 
00455 // Partial specialization for unsigned long long.
00456 // On platforms with sizeof(size_t) <= sizeof(unsigned long long), 
00457 // this should take care of the size_t specialization as well,
00458 // since we've covered all built-in unsigned integer types above 
00459 // with size <= sizeof(unsigned long long).
00460 template<typename Ordinal>
00461 class SerializationTraits<Ordinal, unsigned long long int>
00462   : public DirectSerializationTraits<Ordinal, unsigned long long int>
00463 {};
00464 
00465 // The C preprocessor does not allow "sizeof(T)" expressions in #if
00466 // statements, even if T is a built-in type.  Otherwise, we could test
00467 // for 'sizeof(size_t) > sizeof(unsigned long int)'.  The constants
00468 // below are defined in the <cstdint> header file.
00469 #elif SIZE_MAX > ULONG_MAX
00470 // We already have an unsigned long int specialization above.  If
00471 // Teuchos support for "long long" is enabled, then we've taken care
00472 // of all possible lengths of size_t: unsigned (char, short, int,
00473 // long, long long).  If "long long" is _not_ enabled, we need to
00474 // check if sizeof(size_t) > sizeof(unsigned long).  If so, then we
00475 // need a specialization for size_t.  Ditto for ptrdiff_t (which is a
00476 // signed type of the same length as size_t).
00477 
00478 template<typename Ordinal>
00479 class SerializationTraits<Ordinal, size_t>
00480   : public DirectSerializationTraits<Ordinal, size_t>
00481 {};
00482 
00483 template<typename Ordinal>
00484 class SerializationTraits<Ordinal, ptrdiff_t>
00485   : public DirectSerializationTraits<Ordinal, ptrdiff_t>
00486 {};
00487 
00488 #endif // HAVE_TEUCHOS_LONG_LONG_INT
00489 
00490 } // namespace Teuchos
00491 
00492 #endif // TEUCHOS_SERIALIZATION_TRAITS_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines