Teuchos - Trilinos Tools Package  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"
49 #include <climits> // SIZE_MAX, ULONG_MAX, etc.
50 
51 #ifdef HAVE_TEUCHOS_QD
52 #include <qd/dd_real.h>
53 #include <qd/qd_real.h>
54 #endif
55 
56 namespace Teuchos {
57 
64 template<typename T>
67  static inline T notDefined() {return(T::this_type_is_missing_a_specialization());}
68 };
69 
70 
124 template <typename Ordinal, typename T>
126 public:
127 
129 
130 
135  static const bool supportsDirectSerialization = false;
136 
138 
140 
141 
143  static Ordinal fromCountToDirectBytes(const Ordinal count) {
144  (void)count;
146  return 0;
147  }
148 
150  static char* convertToCharPtr( T* ptr ) {
151  (void)ptr;
153  return 0;
154  }
155 
157  static const char* convertToCharPtr( const T* ptr ) {
158  (void)ptr;
160  return 0;
161  }
162 
164  static Ordinal fromDirectBytesToCount(const Ordinal bytes) {
165  (void)bytes;
167  return 0;
168  }
169 
171  static T* convertFromCharPtr( char* ptr ) {
172  (void)ptr;
174  return 0;
175  }
176 
178  static const T* convertFromCharPtr( const char* ptr ) {
179  (void)ptr;
181  return 0;
182  }
183 
185 
187 
188 
190  static Ordinal fromCountToIndirectBytes(const Ordinal count,
191  const T buffer[]) {
192  (void)count; (void)buffer;
194  return 0;
195  }
196 
212  static void serialize (const Ordinal count,
213  const T buffer[],
214  const Ordinal bytes,
215  char charBuffer[])
216  {
217  (void)count; (void)buffer; (void)bytes; (void)charBuffer;
219  }
220 
222  static Ordinal fromIndirectBytesToCount(const Ordinal bytes,
223  const char charBuffer[]) {
224  (void)bytes; (void)charBuffer;
226  return 0;
227  }
228 
244  static void deserialize (const Ordinal bytes,
245  const char charBuffer[],
246  const Ordinal count,
247  T buffer[])
248  {
249  (void)bytes; (void)charBuffer; (void)count; (void)buffer;
251  }
252 
254 
255 };
256 
270 template <typename Ordinal, typename T>
272 
305 template <typename Ordinal, typename T>
307 public:
308  static const bool supportsDirectSerialization = true;
309  // Direct serialization
310  static Ordinal fromCountToDirectBytes(const Ordinal count)
311  { return sizeof(T)*count; }
312  static char* convertToCharPtr( T* ptr )
313  { return reinterpret_cast<char*>(ptr); }
314  static const char* convertToCharPtr( const T* ptr )
315  { return reinterpret_cast<const char*>(ptr); }
316  static Ordinal fromDirectBytesToCount(const Ordinal count)
317  { return count/sizeof(T); }
318  static T* convertFromCharPtr( char* ptr )
319  { return reinterpret_cast<T*>(ptr); }
320  static const T* convertFromCharPtr( const char* ptr )
321  { return reinterpret_cast<const T*>(ptr); }
322  // Indirect serialization
323  static Ordinal fromCountToIndirectBytes(const Ordinal count, const T buffer[])
324  { return fromCountToDirectBytes(count); }
325  static void serialize(
326  const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[]
327  )
328  {
329 #ifdef TEUCHOS_DEBUG
330  TEUCHOS_TEST_FOR_EXCEPT(bytes!=fromCountToDirectBytes(count));
331 #endif
332  const char *_buffer = convertToCharPtr(buffer);
333  std::copy(_buffer,_buffer+bytes,charBuffer);
334  }
335  static Ordinal fromIndirectBytesToCount(const Ordinal bytes,
336  const char charBuffer[])
337  { return fromDirectBytesToCount(bytes); }
338  static void deserialize(
339  const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[]
340  )
341  {
342 #ifdef TEUCHOS_DEBUG
343  TEUCHOS_TEST_FOR_EXCEPT(count!=fromDirectBytesToCount(bytes));
344 #endif
345  char *_buffer = convertToCharPtr(buffer);
346  std::copy(charBuffer,charBuffer+bytes,_buffer);
347  }
348 };
349 
350 // Whether 'char' is signed or unsigned depends on the implementation.
351 // However, on some systems (e.g., Clang 3.1 on Intel Mac), partially
352 // specializing for signed char and unsigned char, but not for char,
353 // does not work. Thus, we include specializations for all three
354 // possibilities.
355 template<typename Ordinal>
356 class SerializationTraits<Ordinal,char>
357  : public DirectSerializationTraits<Ordinal,char>
358 {};
359 
360 template<typename Ordinal>
361 class SerializationTraits<Ordinal,signed char>
362  : public DirectSerializationTraits<Ordinal,signed char>
363 {};
364 
365 template<typename Ordinal>
366 class SerializationTraits<Ordinal,unsigned char>
367  : public DirectSerializationTraits<Ordinal,unsigned char>
368 {};
369 
370 template<typename Ordinal>
371 class SerializationTraits<Ordinal,short int>
372  : public DirectSerializationTraits<Ordinal,short int>
373 {};
374 
375 template<typename Ordinal>
376 class SerializationTraits<Ordinal,unsigned short int>
377  : public DirectSerializationTraits<Ordinal,unsigned short int>
378 {};
379 
380 template<typename Ordinal>
381 class SerializationTraits<Ordinal,int>
382  : public DirectSerializationTraits<Ordinal,int>
383 {};
384 
385 template<typename Ordinal>
386 class SerializationTraits<Ordinal,unsigned int>
387  : public DirectSerializationTraits<Ordinal,unsigned int>
388 {};
389 
390 template<typename Ordinal>
391 class SerializationTraits<Ordinal,long int>
392  : public DirectSerializationTraits<Ordinal,long int>
393 {};
394 
395 template<typename Ordinal>
396 class SerializationTraits<Ordinal,unsigned long int>
397  : public DirectSerializationTraits<Ordinal,long unsigned int>
398 {};
399 
400 template<typename Ordinal>
401 class SerializationTraits<Ordinal,float>
402  : public DirectSerializationTraits<Ordinal,float>
403 {};
404 
405 template<typename Ordinal>
406 class SerializationTraits<Ordinal,double>
407  : public DirectSerializationTraits<Ordinal,double>
408 {};
409 
410 // FIXME: How do we know that P1 and P2 are directly serializable?
411 template<typename Ordinal, typename P1, typename P2>
412 class SerializationTraits<Ordinal,std::pair<P1,P2> >
413  : public DirectSerializationTraits<Ordinal,std::pair<P1,P2> >
414 {};
415 
416 #ifdef HAVE_TEUCHOS_QD
417 template<typename Ordinal>
418 class SerializationTraits<Ordinal,dd_real>
419  : public DirectSerializationTraits<Ordinal,dd_real>
420 {};
421 
422 template<typename Ordinal>
423 class SerializationTraits<Ordinal,qd_real>
424  : public DirectSerializationTraits<Ordinal,qd_real>
425 {};
426 #endif
427 
428 #ifdef HAVE_TEUCHOS_COMPLEX
429 
430 template<typename Ordinal>
431 class SerializationTraits<Ordinal,std::complex<float> >
432  : public DirectSerializationTraits<Ordinal,std::complex<float> >
433 {};
434 
435 template<typename Ordinal>
436 class SerializationTraits<Ordinal,std::complex<double> >
437  : public DirectSerializationTraits<Ordinal,std::complex<double> >
438 {};
439 
440 #endif // HAVE_TEUCHOS_COMPLEX
441 
442 #if defined(HAVE_TEUCHOS_LONG_LONG_INT)
443 
444 // Partial specialization for long long.
445 // On platforms with sizeof(ptrdiff_t) <= sizeof(long long),
446 // this should take care of the ptrdiff_t specialization as well,
447 // since we've covered all built-in signed integer types above
448 // with size <= sizeof(long long).
449 template<typename Ordinal>
450 class SerializationTraits<Ordinal, long long int>
451  : public DirectSerializationTraits<Ordinal, long long int>
452 {};
453 
454 // Partial specialization for unsigned long long.
455 // On platforms with sizeof(size_t) <= sizeof(unsigned long long),
456 // this should take care of the size_t specialization as well,
457 // since we've covered all built-in unsigned integer types above
458 // with size <= sizeof(unsigned long long).
459 template<typename Ordinal>
460 class SerializationTraits<Ordinal, unsigned long long int>
461  : public DirectSerializationTraits<Ordinal, unsigned long long int>
462 {};
463 
464 // The C preprocessor does not allow "sizeof(T)" expressions in #if
465 // statements, even if T is a built-in type. Otherwise, we could test
466 // for 'sizeof(size_t) > sizeof(unsigned long int)'. The constants
467 // below are defined in the <cstdint> header file.
468 #elif SIZE_MAX > ULONG_MAX
469 // We already have an unsigned long int specialization above. If
470 // Teuchos support for "long long" is enabled, then we've taken care
471 // of all possible lengths of size_t: unsigned (char, short, int,
472 // long, long long). If "long long" is _not_ enabled, we need to
473 // check if sizeof(size_t) > sizeof(unsigned long). If so, then we
474 // need a specialization for size_t. Ditto for ptrdiff_t (which is a
475 // signed type of the same length as size_t).
476 
477 template<typename Ordinal>
478 class SerializationTraits<Ordinal, size_t>
479  : public DirectSerializationTraits<Ordinal, size_t>
480 {};
481 
482 template<typename Ordinal>
483 class SerializationTraits<Ordinal, ptrdiff_t>
484  : public DirectSerializationTraits<Ordinal, ptrdiff_t>
485 {};
486 
487 #endif // HAVE_TEUCHOS_LONG_LONG_INT
488 
489 } // namespace Teuchos
490 
491 #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 const char * convertToCharPtr(const T *ptr)
Convert the pointer type to const char*.
static T notDefined()
This function should not compile if there is an attempt to instantiate!
static Ordinal fromCountToIndirectBytes(const Ordinal count, const T buffer[])
Return the number of bytes for count objects.
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*.
Report an error if a specialization of SerializationTraits is missing.
Serialization traits class for types T that use value semantics.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
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.