Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
Teuchos_SerializationTraits.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
45 #ifndef TEUCHOS_SERIALIZATION_TRAITS_HPP
46 #define TEUCHOS_SERIALIZATION_TRAITS_HPP
47 
48 #include "Teuchos_ConfigDefs.hpp"
50 #include <climits> // SIZE_MAX, ULONG_MAX, etc.
51 
52 #ifdef HAVE_TEUCHOSCORE_QUADMATH
53 #include <quadmath.h>
54 #endif // HAVE_TEUCHOSCORE_QUADMATH
55 
56 #ifdef HAVE_TEUCHOS_QD
57 #include <qd/dd_real.h>
58 #include <qd/qd_real.h>
59 #endif
60 
61 namespace Teuchos {
62 
69 template<typename T>
72  static inline T notDefined() {return(T::this_type_is_missing_a_specialization());}
73 };
74 
75 
129 template <typename Ordinal, typename T>
131 public:
132 
134 
135 
140  static const bool supportsDirectSerialization = false;
141 
143 
145 
146 
148  static Ordinal fromCountToDirectBytes(const Ordinal count) {
149  (void)count;
151  return 0;
152  }
153 
155  static char* convertToCharPtr( T* ptr ) {
156  (void)ptr;
158  return 0;
159  }
160 
162  static const char* convertToCharPtr( const T* ptr ) {
163  (void)ptr;
165  return 0;
166  }
167 
169  static Ordinal fromDirectBytesToCount(const Ordinal bytes) {
170  (void)bytes;
172  return 0;
173  }
174 
176  static T* convertFromCharPtr( char* ptr ) {
177  (void)ptr;
179  return 0;
180  }
181 
183  static const T* convertFromCharPtr( const char* ptr ) {
184  (void)ptr;
186  return 0;
187  }
188 
190 
192 
193 
195  static Ordinal fromCountToIndirectBytes(const Ordinal count,
196  const T buffer[]) {
197  (void)count; (void)buffer;
199  return 0;
200  }
201 
217  static void serialize (const Ordinal count,
218  const T buffer[],
219  const Ordinal bytes,
220  char charBuffer[])
221  {
222  (void)count; (void)buffer; (void)bytes; (void)charBuffer;
224  }
225 
227  static Ordinal fromIndirectBytesToCount(const Ordinal bytes,
228  const char charBuffer[]) {
229  (void)bytes; (void)charBuffer;
231  return 0;
232  }
233 
249  static void deserialize (const Ordinal bytes,
250  const char charBuffer[],
251  const Ordinal count,
252  T buffer[])
253  {
254  (void)bytes; (void)charBuffer; (void)count; (void)buffer;
256  }
257 
259 
260 };
261 
275 template <typename Ordinal, typename T>
277 
310 template <typename Ordinal, typename T>
312 public:
313  static const bool supportsDirectSerialization = true;
314  // Direct serialization
315  static Ordinal fromCountToDirectBytes(const Ordinal count)
316  { return sizeof(T)*count; }
317  static char* convertToCharPtr( T* ptr )
318  { return reinterpret_cast<char*>(ptr); }
319  static const char* convertToCharPtr( const T* ptr )
320  { return reinterpret_cast<const char*>(ptr); }
321  static Ordinal fromDirectBytesToCount(const Ordinal count)
322  { return count/sizeof(T); }
323  static T* convertFromCharPtr( char* ptr )
324  { return reinterpret_cast<T*>(ptr); }
325  static const T* convertFromCharPtr( const char* ptr )
326  { return reinterpret_cast<const T*>(ptr); }
327  // Indirect serialization
328  static Ordinal fromCountToIndirectBytes(const Ordinal count, const T buffer[])
329  { return fromCountToDirectBytes(count); }
330  static void serialize(
331  const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[]
332  )
333  {
334 #ifdef TEUCHOS_DEBUG
336 #endif
337  const char *_buffer = convertToCharPtr(buffer);
338  std::copy(_buffer,_buffer+bytes,charBuffer);
339  }
340  static Ordinal fromIndirectBytesToCount(const Ordinal bytes,
341  const char charBuffer[])
342  { return fromDirectBytesToCount(bytes); }
343  static void deserialize(
344  const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[]
345  )
346  {
347 #ifdef TEUCHOS_DEBUG
349 #endif
350  char *_buffer = convertToCharPtr(buffer);
351  std::copy(charBuffer,charBuffer+bytes,_buffer);
352  }
353 };
354 
355 // Whether 'char' is signed or unsigned depends on the implementation.
356 // However, on some systems (e.g., Clang 3.1 on Intel Mac), partially
357 // specializing for signed char and unsigned char, but not for char,
358 // does not work. Thus, we include specializations for all three
359 // possibilities.
360 template<typename Ordinal>
361 class SerializationTraits<Ordinal,char>
362  : public DirectSerializationTraits<Ordinal,char>
363 {};
364 
365 template<typename Ordinal>
366 class SerializationTraits<Ordinal,signed char>
367  : public DirectSerializationTraits<Ordinal,signed char>
368 {};
369 
370 template<typename Ordinal>
371 class SerializationTraits<Ordinal,unsigned char>
372  : public DirectSerializationTraits<Ordinal,unsigned char>
373 {};
374 
375 template<typename Ordinal>
376 class SerializationTraits<Ordinal,short int>
377  : public DirectSerializationTraits<Ordinal,short int>
378 {};
379 
380 template<typename Ordinal>
381 class SerializationTraits<Ordinal,unsigned short int>
382  : public DirectSerializationTraits<Ordinal,unsigned short int>
383 {};
384 
385 template<typename Ordinal>
386 class SerializationTraits<Ordinal,int>
387  : public DirectSerializationTraits<Ordinal,int>
388 {};
389 
390 template<typename Ordinal>
391 class SerializationTraits<Ordinal,unsigned int>
392  : public DirectSerializationTraits<Ordinal,unsigned int>
393 {};
394 
395 template<typename Ordinal>
396 class SerializationTraits<Ordinal,long int>
397  : public DirectSerializationTraits<Ordinal,long int>
398 {};
399 
400 template<typename Ordinal>
401 class SerializationTraits<Ordinal,unsigned long int>
402  : public DirectSerializationTraits<Ordinal,long unsigned int>
403 {};
404 
405 template<typename Ordinal>
406 class SerializationTraits<Ordinal,float>
407  : public DirectSerializationTraits<Ordinal,float>
408 {};
409 
410 template<typename Ordinal>
411 class SerializationTraits<Ordinal,double>
412  : public DirectSerializationTraits<Ordinal,double>
413 {};
414 
415 // FIXME: How do we know that P1 and P2 are directly serializable?
416 template<typename Ordinal, typename P1, typename P2>
417 class SerializationTraits<Ordinal,std::pair<P1,P2> >
418  : public DirectSerializationTraits<Ordinal,std::pair<P1,P2> >
419 {};
420 
421 #ifdef HAVE_TEUCHOSCORE_QUADMATH
422 template<typename Ordinal>
423 class SerializationTraits<Ordinal,__float128>
424  : public DirectSerializationTraits<Ordinal,__float128>
425 {};
426 #endif // HAVE_TEUCHOSCORE_QUADMATH
427 
428 #ifdef HAVE_TEUCHOS_QD
429 template<typename Ordinal>
430 class SerializationTraits<Ordinal,dd_real>
431  : public DirectSerializationTraits<Ordinal,dd_real>
432 {};
433 
434 template<typename Ordinal>
435 class SerializationTraits<Ordinal,qd_real>
436  : public DirectSerializationTraits<Ordinal,qd_real>
437 {};
438 #endif
439 
440 #ifdef HAVE_TEUCHOS_COMPLEX
441 
442 template<typename Ordinal>
443 class SerializationTraits<Ordinal,std::complex<float> >
444  : public DirectSerializationTraits<Ordinal,std::complex<float> >
445 {};
446 
447 template<typename Ordinal>
448 class SerializationTraits<Ordinal,std::complex<double> >
449  : public DirectSerializationTraits<Ordinal,std::complex<double> >
450 {};
451 
452 #endif // HAVE_TEUCHOS_COMPLEX
453 
454 #if defined(HAVE_TEUCHOS_LONG_LONG_INT)
455 
456 // Partial specialization for long long.
457 // On platforms with sizeof(ptrdiff_t) <= sizeof(long long),
458 // this should take care of the ptrdiff_t specialization as well,
459 // since we've covered all built-in signed integer types above
460 // with size <= sizeof(long long).
461 template<typename Ordinal>
462 class SerializationTraits<Ordinal, long long int>
463  : public DirectSerializationTraits<Ordinal, long long int>
464 {};
465 
466 // Partial specialization for unsigned long long.
467 // On platforms with sizeof(size_t) <= sizeof(unsigned long long),
468 // this should take care of the size_t specialization as well,
469 // since we've covered all built-in unsigned integer types above
470 // with size <= sizeof(unsigned long long).
471 template<typename Ordinal>
472 class SerializationTraits<Ordinal, unsigned long long int>
473  : public DirectSerializationTraits<Ordinal, unsigned long long int>
474 {};
475 
476 // The C preprocessor does not allow "sizeof(T)" expressions in #if
477 // statements, even if T is a built-in type. Otherwise, we could test
478 // for 'sizeof(size_t) > sizeof(unsigned long int)'. The constants
479 // below are defined in the <cstdint> header file.
480 #elif SIZE_MAX > ULONG_MAX
481 // We already have an unsigned long int specialization above. If
482 // Teuchos support for "long long" is enabled, then we've taken care
483 // of all possible lengths of size_t: unsigned (char, short, int,
484 // long, long long). If "long long" is _not_ enabled, we need to
485 // check if sizeof(size_t) > sizeof(unsigned long). If so, then we
486 // need a specialization for size_t. Ditto for ptrdiff_t (which is a
487 // signed type of the same length as size_t).
488 
489 template<typename Ordinal>
490 class SerializationTraits<Ordinal, size_t>
491  : public DirectSerializationTraits<Ordinal, size_t>
492 {};
493 
494 template<typename Ordinal>
495 class SerializationTraits<Ordinal, ptrdiff_t>
496  : public DirectSerializationTraits<Ordinal, ptrdiff_t>
497 {};
498 
499 #endif // HAVE_TEUCHOS_LONG_LONG_INT
500 
501 } // namespace Teuchos
502 
503 #endif // TEUCHOS_SERIALIZATION_TRAITS_HPP
Serialization traits for objects that support direct serialization.
static char * convertToCharPtr(T *ptr)
Convert the pointer type to char*.
static void serialize(const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[])
static Ordinal fromCountToIndirectBytes(const Ordinal count, const T buffer[])
static const char * convertToCharPtr(const T *ptr)
Convert the pointer type to const char*.
static Ordinal fromCountToDirectBytes(const Ordinal count)
static T notDefined()
This function should not compile if there is an attempt to instantiate!
static void deserialize(const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[])
static Ordinal fromCountToIndirectBytes(const Ordinal count, const T buffer[])
Return the number of bytes for count objects.
static const bool supportsDirectSerialization
Whether the type T supports direct serialization.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
static void deserialize(const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[])
Deserialize from an indirect char[] buffer.
Serialization class for types T that use value semantics.
static Ordinal fromIndirectBytesToCount(const Ordinal bytes, const char charBuffer[])
Return the number of objects for bytes of storage.
static T * convertFromCharPtr(char *ptr)
Convert the pointer type from char*.
static const T * convertFromCharPtr(const char *ptr)
Convert the pointer type from char*.
static const char * convertToCharPtr(const T *ptr)
static const T * convertFromCharPtr(const char *ptr)
static Ordinal fromIndirectBytesToCount(const Ordinal bytes, const char charBuffer[])
Report an error if a specialization of SerializationTraits is missing.
Serialization traits class for types T that use value semantics.
Standard test and throw macros.
static Ordinal fromDirectBytesToCount(const Ordinal count)
static Ordinal fromDirectBytesToCount(const Ordinal bytes)
Return the number of objects for bytes of storage.
static void serialize(const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[])
Serialize to an indirect char[] buffer.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call...
static Ordinal fromCountToDirectBytes(const Ordinal count)
Return the number of bytes for count objects.