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