Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_SerializationTraits.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
13 #ifndef TEUCHOS_SERIALIZATION_TRAITS_HPP
14 #define TEUCHOS_SERIALIZATION_TRAITS_HPP
15 
16 #include "Teuchos_ConfigDefs.hpp"
18 #include <climits> // SIZE_MAX, ULONG_MAX, etc.
19 
20 #ifdef HAVE_TEUCHOSCORE_QUADMATH
21 #include <quadmath.h>
22 #endif // HAVE_TEUCHOSCORE_QUADMATH
23 
24 #ifdef HAVE_TEUCHOS_QD
25 #include <qd/dd_real.h>
26 #include <qd/qd_real.h>
27 #endif
28 
29 namespace Teuchos {
30 
37 template<typename T>
40  static inline T notDefined() {return(T::this_type_is_missing_a_specialization());}
41 };
42 
43 
97 template <typename Ordinal, typename T>
99 public:
100 
102 
103 
108  static const bool supportsDirectSerialization = false;
109 
111 
113 
114 
116  static Ordinal fromCountToDirectBytes(const Ordinal count) {
117  (void)count;
119  return 0;
120  }
121 
123  static char* convertToCharPtr( T* ptr ) {
124  (void)ptr;
126  return 0;
127  }
128 
130  static const char* convertToCharPtr( const T* ptr ) {
131  (void)ptr;
133  return 0;
134  }
135 
137  static Ordinal fromDirectBytesToCount(const Ordinal bytes) {
138  (void)bytes;
140  return 0;
141  }
142 
144  static T* convertFromCharPtr( char* ptr ) {
145  (void)ptr;
147  return 0;
148  }
149 
151  static const T* convertFromCharPtr( const char* ptr ) {
152  (void)ptr;
154  return 0;
155  }
156 
158 
160 
161 
163  static Ordinal fromCountToIndirectBytes(const Ordinal count,
164  const T buffer[]) {
165  (void)count; (void)buffer;
167  return 0;
168  }
169 
185  static void serialize (const Ordinal count,
186  const T buffer[],
187  const Ordinal bytes,
188  char charBuffer[])
189  {
190  (void)count; (void)buffer; (void)bytes; (void)charBuffer;
192  }
193 
195  static Ordinal fromIndirectBytesToCount(const Ordinal bytes,
196  const char charBuffer[]) {
197  (void)bytes; (void)charBuffer;
199  return 0;
200  }
201 
217  static void deserialize (const Ordinal bytes,
218  const char charBuffer[],
219  const Ordinal count,
220  T buffer[])
221  {
222  (void)bytes; (void)charBuffer; (void)count; (void)buffer;
224  }
225 
227 
228 };
229 
243 template <typename Ordinal, typename T>
245 
278 template <typename Ordinal, typename T>
280 public:
281  static const bool supportsDirectSerialization = true;
282  // Direct serialization
283  static Ordinal fromCountToDirectBytes(const Ordinal count)
284  { return sizeof(T)*count; }
285  static char* convertToCharPtr( T* ptr )
286  { return reinterpret_cast<char*>(ptr); }
287  static const char* convertToCharPtr( const T* ptr )
288  { return reinterpret_cast<const char*>(ptr); }
289  static Ordinal fromDirectBytesToCount(const Ordinal count)
290  { return count/sizeof(T); }
291  static T* convertFromCharPtr( char* ptr )
292  { return reinterpret_cast<T*>(ptr); }
293  static const T* convertFromCharPtr( const char* ptr )
294  { return reinterpret_cast<const T*>(ptr); }
295  // Indirect serialization
296  static Ordinal fromCountToIndirectBytes(const Ordinal count, const T buffer[])
297  { return fromCountToDirectBytes(count); }
298  static void serialize(
299  const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[]
300  )
301  {
302 #ifdef TEUCHOS_DEBUG
303  TEUCHOS_TEST_FOR_EXCEPT(bytes!=fromCountToDirectBytes(count));
304 #else
305  (void)count;
306 #endif
307  const char *_buffer = convertToCharPtr(buffer);
308  std::copy(_buffer,_buffer+bytes,charBuffer);
309  }
310  static Ordinal fromIndirectBytesToCount(const Ordinal bytes,
311  const char charBuffer[])
312  { return fromDirectBytesToCount(bytes); }
313  static void deserialize(
314  const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[]
315  )
316  {
317 #ifdef TEUCHOS_DEBUG
318  TEUCHOS_TEST_FOR_EXCEPT(count!=fromDirectBytesToCount(bytes));
319 #endif
320  char *_buffer = convertToCharPtr(buffer);
321  std::copy(charBuffer,charBuffer+bytes,_buffer);
322  }
323 };
324 
325 // Whether 'char' is signed or unsigned depends on the implementation.
326 // However, on some systems (e.g., Clang 3.1 on Intel Mac), partially
327 // specializing for signed char and unsigned char, but not for char,
328 // does not work. Thus, we include specializations for all three
329 // possibilities.
330 template<typename Ordinal>
331 class SerializationTraits<Ordinal,char>
332  : public DirectSerializationTraits<Ordinal,char>
333 {};
334 
335 template<typename Ordinal>
336 class SerializationTraits<Ordinal,signed char>
337  : public DirectSerializationTraits<Ordinal,signed char>
338 {};
339 
340 template<typename Ordinal>
341 class SerializationTraits<Ordinal,unsigned char>
342  : public DirectSerializationTraits<Ordinal,unsigned char>
343 {};
344 
345 template<typename Ordinal>
346 class SerializationTraits<Ordinal,short int>
347  : public DirectSerializationTraits<Ordinal,short int>
348 {};
349 
350 template<typename Ordinal>
351 class SerializationTraits<Ordinal,unsigned short int>
352  : public DirectSerializationTraits<Ordinal,unsigned short int>
353 {};
354 
355 template<typename Ordinal>
356 class SerializationTraits<Ordinal,int>
357  : public DirectSerializationTraits<Ordinal,int>
358 {};
359 
360 template<typename Ordinal>
361 class SerializationTraits<Ordinal,unsigned int>
362  : public DirectSerializationTraits<Ordinal,unsigned int>
363 {};
364 
365 template<typename Ordinal>
366 class SerializationTraits<Ordinal,long int>
367  : public DirectSerializationTraits<Ordinal,long int>
368 {};
369 
370 template<typename Ordinal>
371 class SerializationTraits<Ordinal,unsigned long int>
372  : public DirectSerializationTraits<Ordinal,long unsigned int>
373 {};
374 
375 template<typename Ordinal>
376 class SerializationTraits<Ordinal,float>
377  : public DirectSerializationTraits<Ordinal,float>
378 {};
379 
380 template<typename Ordinal>
381 class SerializationTraits<Ordinal,double>
382  : public DirectSerializationTraits<Ordinal,double>
383 {};
384 
385 #ifdef HAVE_TEUCHOS_LONG_DOUBLE
386 template<typename Ordinal>
387 class SerializationTraits<Ordinal,long double>
388  : public DirectSerializationTraits<Ordinal,long double>
389 {};
390 #endif
391 
392 
393 // FIXME: How do we know that P1 and P2 are directly serializable?
394 template<typename Ordinal, typename P1, typename P2>
395 class SerializationTraits<Ordinal,std::pair<P1,P2> >
396  : public DirectSerializationTraits<Ordinal,std::pair<P1,P2> >
397 {};
398 
399 #ifdef HAVE_TEUCHOSCORE_QUADMATH
400 template<typename Ordinal>
401 class SerializationTraits<Ordinal,__float128>
402  : public DirectSerializationTraits<Ordinal,__float128>
403 {};
404 #endif // HAVE_TEUCHOSCORE_QUADMATH
405 
406 #ifdef HAVE_TEUCHOS_QD
407 template<typename Ordinal>
408 class SerializationTraits<Ordinal,dd_real>
409  : public DirectSerializationTraits<Ordinal,dd_real>
410 {};
411 
412 template<typename Ordinal>
413 class SerializationTraits<Ordinal,qd_real>
414  : public DirectSerializationTraits<Ordinal,qd_real>
415 {};
416 #endif
417 
418 #ifdef HAVE_TEUCHOS_COMPLEX
419 
420 template<typename Ordinal>
421 class SerializationTraits<Ordinal,std::complex<float> >
422  : public DirectSerializationTraits<Ordinal,std::complex<float> >
423 {};
424 
425 template<typename Ordinal>
426 class SerializationTraits<Ordinal,std::complex<double> >
427  : public DirectSerializationTraits<Ordinal,std::complex<double> >
428 {};
429 
430 #endif // HAVE_TEUCHOS_COMPLEX
431 
432 // Partial specialization for long long.
433 // On platforms with sizeof(ptrdiff_t) <= sizeof(long long),
434 // this should take care of the ptrdiff_t specialization as well,
435 // since we've covered all built-in signed integer types above
436 // with size <= sizeof(long long).
437 template<typename Ordinal>
438 class SerializationTraits<Ordinal, long long int>
439  : public DirectSerializationTraits<Ordinal, long long int>
440 {};
441 
442 // Partial specialization for unsigned long long.
443 // On platforms with sizeof(size_t) <= sizeof(unsigned long long),
444 // this should take care of the size_t specialization as well,
445 // since we've covered all built-in unsigned integer types above
446 // with size <= sizeof(unsigned long long).
447 template<typename Ordinal>
448 class SerializationTraits<Ordinal, unsigned long long int>
449  : public DirectSerializationTraits<Ordinal, unsigned long long int>
450 {};
451 
452 } // namespace Teuchos
453 
454 #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.
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*.
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 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.