Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_SerializationTraitsHelpers.hpp
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 
10 #ifndef TEUCHOS_SERIALIZATION_TRAITS_HELPERS_HPP
11 #define TEUCHOS_SERIALIZATION_TRAITS_HELPERS_HPP
12 
14 #include "Teuchos_ArrayView.hpp"
15 #include "Teuchos_Array.hpp"
16 
17 namespace Teuchos {
18 
37 template <typename Ordinal, typename T>
39 public:
42 
46  return s;
47  }
48 
51  return Teuchos::rcp (new DefaultSerializerType ());
52  }
53 };
54 
62 template <typename Ordinal, typename T, typename Serializer,
63  bool direct = Serializer::supportsDirectSerialization>
65 
73 template <typename Ordinal, typename T, typename Serializer,
74  bool direct = Serializer::supportsDirectSerialization>
76 
85 template <typename Ordinal, typename T, typename Serializer,
86  bool direct = Serializer::supportsDirectSerialization>
88 
97 template <typename Ordinal, typename T, typename Serializer,
98  bool direct = Serializer::supportsDirectSerialization>
100 
106 template <typename Ordinal, typename T, typename Serializer>
108 public:
111  const Ordinal count, T buffer[],
112  const RCP<const Serializer>& serializer
113  );
119  char* getCharBuffer() const;
121  Ordinal getBytes() const;
123  const ArrayView<char> getCharBufferView() const;
124 private:
125  Ordinal count_;
126  T *buffer_;
127  Ordinal bytes_;
128  char *charBuffer_;
129  RCP<const Serializer> serializer_;
130  // Not defined and not to be called
134 };
135 
141 template <typename Ordinal, typename T, typename Serializer>
143 public:
146  const Ordinal count, const T buffer[],
147  const RCP<const Serializer>& serializer
148  );
154  const char* getCharBuffer() const;
156  Ordinal getBytes() const;
158  const ArrayView<const char> getCharBufferView() const;
159 private:
160  Ordinal count_;
161  const T *buffer_;
162  Ordinal bytes_;
163  const char *charBuffer_;
164  RCP<const Serializer> serializer_;
165  // Not defined and not to be called
169 };
170 
177 template <typename Ordinal, typename T, typename Serializer>
179 public:
182  const Ordinal bytes, char charBuffer[],
183  const RCP<const Serializer>& serializer
184  );
190  T* getBuffer() const;
192  Ordinal getCount() const;
193 private:
194  Ordinal bytes_;
195  char *charBuffer_;
196  Ordinal count_;
197  T *buffer_;
198  RCP<const Serializer> serializer_;
199  // Not defined and not to be called
203 };
204 
211 template <typename Ordinal, typename T, typename Serializer>
213 public:
216  const Ordinal bytes, const char charBuffer[],
217  const RCP<const Serializer>& serializer
218  );
224  const T* getBuffer() const;
226  Ordinal getCount() const;
227 private:
228  Ordinal bytes_;
229  const char *charBuffer_;
230  Ordinal count_;
231  const T *buffer_;
232  RCP<const Serializer> serializer_;
233  // Not defined and not to be called
237 };
238 
244 template <typename Ordinal, typename T, typename Serializer>
246 public:
249  const Ordinal count, T buffer[],
250  const RCP<const Serializer>& serializer
251  );
257  char* getCharBuffer() const;
259  Ordinal getBytes() const;
261  const ArrayView<char> getCharBufferView() const;
262 private:
263  Ordinal count_;
264  T *buffer_;
265  Ordinal bytes_;
266  mutable Array<char> charBuffer_;
267  RCP<const Serializer> serializer_;
268  // Not defined and not to be called
272 };
273 
279 template <typename Ordinal, typename T, typename Serializer>
281 public:
284  const Ordinal count, const T buffer[],
285  const RCP<const Serializer>& serializer
286  );
292  const char* getCharBuffer() const;
294  Ordinal getBytes() const;
296  const ArrayView<const char> getCharBufferView() const;
297 private:
298  Ordinal count_;
299  const T *buffer_;
300  Ordinal bytes_;
301  Array<char> charBuffer_;
302  RCP<const Serializer> serializer_;
303  // Not defined and not to be called
307 };
308 
315 template <typename Ordinal, typename T, typename Serializer>
317 public:
320  const Ordinal bytes, char charBuffer[],
321  const RCP<const Serializer>& serializer
322  );
328  T* getBuffer() const;
330  Ordinal getCount() const;
331 private:
332  Ordinal bytes_;
333  char *charBuffer_;
334  Ordinal count_;
335  mutable Array<T> buffer_;
336  RCP<const Serializer> serializer_;
337  // Not defined and not to be called
341 };
342 
349 template <typename Ordinal, typename T, typename Serializer>
351 public:
354  const Ordinal bytes, const char charBuffer[],
355  const RCP<const Serializer>& serializer
356  );
362  const T* getBuffer() const;
364  Ordinal getCount() const;
365 private:
366  Ordinal bytes_;
367  const char *charBuffer_;
368  Ordinal count_;
369  Array<T> buffer_;
370  RCP<const Serializer> serializer_;
371  // Not defined and not to be called
375 };
376 
377 
381 template <typename Ordinal, typename T,
384  public ValueTypeSerializationBufferImp<Ordinal,T,Serializer> {
385 public:
389  const Ordinal count, T buffer[],
390  const RCP<const Serializer>& serializer
391  ) : Base(count,buffer,serializer) {}
392 };
393 
397 template <typename Ordinal, typename T,
398  typename Serializer = typename DefaultSerializer<Ordinal,T>::DefaultSerializerType>
400  public ConstValueTypeSerializationBufferImp<Ordinal,T,Serializer> {
401 public:
405  const Ordinal count, const T buffer[],
406  const RCP<const Serializer>& serializer
407  ) : Base(count,buffer,serializer) {}
408 };
409 
414 template <typename Ordinal, typename T,
415  typename Serializer = typename DefaultSerializer<Ordinal,T>::DefaultSerializerType>
417  public ValueTypeDeserializationBufferImp<Ordinal,T,Serializer> {
418 public:
422  const Ordinal bytes, char charBuffer[],
423  const RCP<const Serializer>& serializer
424  ) : Base(bytes,charBuffer,serializer) {}
425 };
426 
431 template <typename Ordinal, typename T,
432  typename Serializer = typename DefaultSerializer<Ordinal,T>::DefaultSerializerType>
434  public ConstValueTypeDeserializationBufferImp<Ordinal,T,Serializer> {
435 public:
439  const Ordinal bytes, const char charBuffer[],
440  const RCP<const Serializer>& serializer
441  ) : Base(bytes,charBuffer,serializer) {}
442 };
443 
450 template <typename Ordinal, typename T>
451 class ValueTypeSerializationBuffer<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> :
452  public ValueTypeSerializationBufferImp<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> {
453 public:
454  typedef DefaultSerializer<Ordinal,T> DS; // work around for parsing bug in gcc 4.1-4.2
455  typedef typename DS::DefaultSerializerType Serializer;
459  const Ordinal count, T buffer[],
460  const RCP<const Serializer>& serializer = DS::getDefaultSerializerRCP()
461  ) : Base(count,buffer,serializer) {}
462 };
463 
470 template <typename Ordinal, typename T>
471 class ConstValueTypeSerializationBuffer<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> :
472  public ConstValueTypeSerializationBufferImp<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> {
473 public:
474  typedef DefaultSerializer<Ordinal,T> DS; // work around for parsing bug in gcc 4.1-4.2
475  typedef typename DS::DefaultSerializerType Serializer;
479  const Ordinal count, const T buffer[],
480  const RCP<const Serializer>& serializer = DS::getDefaultSerializerRCP()
481  ) : Base(count,buffer,serializer) {}
482 };
483 
491 template <typename Ordinal, typename T>
492 class ValueTypeDeserializationBuffer<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> :
493  public ValueTypeDeserializationBufferImp<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> {
494 public:
495  typedef DefaultSerializer<Ordinal,T> DS; // work around for parsing bug in gcc 4.1-4.2
496  typedef typename DS::DefaultSerializerType Serializer;
500  const Ordinal bytes, char charBuffer[],
501  const RCP<const Serializer>& serializer = DS::getDefaultSerializerRCP()
502  ) : Base(bytes,charBuffer,serializer) {}
503 };
504 
512 template <typename Ordinal, typename T>
513 class ConstValueTypeDeserializationBuffer<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> :
514  public ConstValueTypeDeserializationBufferImp<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> {
515 public:
516  typedef DefaultSerializer<Ordinal,T> DS; // work around for parsing bug in gcc 4.1-4.2
517  typedef typename DS::DefaultSerializerType Serializer;
521  const Ordinal bytes, const char charBuffer[],
522  const RCP<const Serializer>& serializer = DS::getDefaultSerializerRCP()
523  ) : Base(bytes,charBuffer,serializer) {}
524 };
525 
526 // /////////////////////////////////////
527 // Template implementations for direct serialization
528 
529 //
530 // ValueTypeSerializationBufferImp
531 //
532 
533 template <typename Ordinal, typename T, typename Serializer>
534 ValueTypeSerializationBufferImp<Ordinal,T,Serializer,true>::
535 ValueTypeSerializationBufferImp(
536  const Ordinal count, T buffer[], const RCP<const Serializer>& serializer
537  )
538  :count_(count), buffer_(buffer), serializer_(serializer)
539 {
540  bytes_ = serializer_->fromCountToDirectBytes(count_);
541  charBuffer_ = serializer_->convertToCharPtr(buffer_);
542 }
543 
544 template <typename Ordinal, typename T, typename Serializer>
547 {
548  // There is nothing to do since the type uses direct serialization!
549 }
550 
551 template <typename Ordinal, typename T, typename Serializer>
554 {
555  return charBuffer_;
556 }
557 
558 template <typename Ordinal, typename T, typename Serializer>
560 getBytes() const
561 {
562  return bytes_;
563 }
564 
565 
566 template <typename Ordinal, typename T, typename Serializer>
567 const ArrayView<char>
570 {
571  return arrayView(charBuffer_, bytes_);
572 }
573 
574 
575 //
576 // ConstValueTypeSerializationBufferImp
577 //
578 
579 template <typename Ordinal, typename T, typename Serializer>
582  const Ordinal count, const T buffer[], const RCP<const Serializer>& serializer
583  )
584  :count_(count), buffer_(buffer), serializer_(serializer)
585 {
586  bytes_ = serializer_->fromCountToDirectBytes(count_);
587  charBuffer_ = serializer_->convertToCharPtr(buffer_);
588 }
589 
590 template <typename Ordinal, typename T, typename Serializer>
593 {
594  // There is nothing to do since the type uses direct serialization!
595 }
596 
597 template <typename Ordinal, typename T, typename Serializer>
600 {
601  return charBuffer_;
602 }
603 
604 template <typename Ordinal, typename T, typename Serializer>
606 getBytes() const
607 {
608  return bytes_;
609 }
610 
611 template <typename Ordinal, typename T, typename Serializer>
615 {
616  return arrayView(charBuffer_, bytes_);
617 }
618 
619 //
620 // ValueTypeDeserializationBufferImp
621 //
622 
623 template <typename Ordinal, typename T, typename Serializer>
626  const Ordinal bytes, char charBuffer[], const RCP<const Serializer>& serializer
627  )
628  :bytes_(bytes), charBuffer_(charBuffer), serializer_(serializer)
629 {
630  count_ = serializer_->fromDirectBytesToCount(bytes_);
631  buffer_ = serializer_->convertFromCharPtr(charBuffer_);
632 }
633 
634 template <typename Ordinal, typename T, typename Serializer>
637 {
638  // There is nothing to do since the type uses direct serialization!
639 }
640 
641 template <typename Ordinal, typename T, typename Serializer>
643 getBuffer() const
644 {
645  return buffer_;
646 }
647 
648 template <typename Ordinal, typename T, typename Serializer>
650 getCount() const
651 {
652  return count_;
653 }
654 
655 //
656 // ConstValueTypeDeserializationBufferImp
657 //
658 
659 template <typename Ordinal, typename T, typename Serializer>
662  const Ordinal bytes, const char charBuffer[], const RCP<const Serializer>& serializer
663  )
664  :bytes_(bytes), charBuffer_(charBuffer), serializer_(serializer)
665 {
666  count_ = serializer_->fromDirectBytesToCount(bytes_);
667  buffer_ = serializer_->convertFromCharPtr(charBuffer_);
668 }
669 
670 template <typename Ordinal, typename T, typename Serializer>
673 {
674  // There is nothing to do since the type uses direct serialization!
675 }
676 
677 template <typename Ordinal, typename T, typename Serializer>
679 getBuffer() const
680 {
681  return buffer_;
682 }
683 
684 template <typename Ordinal, typename T, typename Serializer>
686 getCount() const
687 {
688  return count_;
689 }
690 
691 
692 // /////////////////////////////////////
693 // Template implementations for indirect specializations
694 
695 //
696 // ValueTypeSerializationBufferImp
697 //
698 
699 template <typename Ordinal, typename T, typename Serializer>
702  const Ordinal count, T buffer[], const RCP<const Serializer>& serializer
703  )
704  :count_(count), buffer_(buffer), serializer_(serializer)
705 {
706  bytes_ = serializer_->fromCountToIndirectBytes(count_, buffer_);
707  charBuffer_.resize(bytes_);
708  serializer_->serialize(count_, buffer_, bytes_, &charBuffer_[0]);
709 }
710 
711 template <typename Ordinal, typename T, typename Serializer>
714 {
715  serializer_->deserialize(bytes_, &charBuffer_[0], count_, buffer_);
716 }
717 
718 template <typename Ordinal, typename T, typename Serializer>
721 {
722  return &charBuffer_[0];
723 }
724 
725 template <typename Ordinal, typename T, typename Serializer>
727 getBytes() const
728 {
729  return bytes_;
730 }
731 
732 template <typename Ordinal, typename T, typename Serializer>
733 const ArrayView<char>
736 {
737  return charBuffer_.view(0, bytes_);
738 }
739 
740 
741 //
742 // ConstValueTypeSerializationBufferImp
743 //
744 
745 template <typename Ordinal, typename T, typename Serializer>
748  const Ordinal count, const T buffer[], const RCP<const Serializer>& serializer
749  )
750  :count_(count), buffer_(buffer), serializer_(serializer)
751 {
752  bytes_ = serializer_->fromCountToIndirectBytes(count_, buffer_);
753  charBuffer_.resize(bytes_);
754  serializer_->serialize(count_, buffer_, bytes_, &charBuffer_[0]);
755 }
756 
757 template <typename Ordinal, typename T, typename Serializer>
760 {
761 }
762 
763 template <typename Ordinal, typename T, typename Serializer>
766 {
767  return &charBuffer_[0];
768 }
769 
770 template <typename Ordinal, typename T, typename Serializer>
772 getBytes() const
773 {
774  return bytes_;
775 }
776 
777 template <typename Ordinal, typename T, typename Serializer>
781 {
782  return charBuffer_.view(0, bytes_);
783 }
784 
785 //
786 // ValueTypeDeserializationBufferImp
787 //
788 
789 template <typename Ordinal, typename T, typename Serializer>
792  const Ordinal bytes, char charBuffer[], const RCP<const Serializer>& serializer
793  )
794  :bytes_(bytes), charBuffer_(charBuffer), serializer_(serializer)
795 {
796  count_ = serializer_->fromIndirectBytesToCount(bytes_, charBuffer_);
797  buffer_.resize(count_);
798  serializer_->deserialize(bytes_, charBuffer_, count_, &buffer_[0]);
799 }
800 
801 template <typename Ordinal, typename T, typename Serializer>
804 {
805  serializer_->serialize(count_, &buffer_[0], bytes_, charBuffer_);
806 }
807 
808 template <typename Ordinal, typename T, typename Serializer>
810 getBuffer() const
811 {
812  return &buffer_[0];
813 }
814 
815 template <typename Ordinal, typename T, typename Serializer>
817 getCount() const
818 {
819  return count_;
820 }
821 
822 //
823 // ConstValueTypeDeserializationBufferImp
824 //
825 
826 template <typename Ordinal, typename T, typename Serializer>
829  const Ordinal bytes, const char charBuffer[], const RCP<const Serializer>& serializer
830  )
831  :bytes_(bytes), charBuffer_(charBuffer), serializer_(serializer)
832 {
833  count_ = serializer_->fromIndirectBytesToCount(bytes_, charBuffer_);
834  buffer_.resize(count_);
835  serializer_->deserialize(bytes_, charBuffer_, count_, &buffer_[0]);
836 }
837 
838 template <typename Ordinal, typename T, typename Serializer>
841 {
842 }
843 
844 template <typename Ordinal, typename T, typename Serializer>
846 getBuffer() const
847 {
848  return &buffer_[0];
849 }
850 
851 template <typename Ordinal, typename T, typename Serializer>
853 getCount() const
854 {
855  return count_;
856 }
857 
858 } // namespace Teuchos
859 
860 #endif // TEUCHOS_SERIALIZATION_TRAITS_HELPERS_HPP
A class for instantiating a default serialization object.
Strategy interface for the indirect serializing and deserializing objects of a given type handled usi...
ConstValueTypeSerializationBuffer(const Ordinal count, const T buffer[], const RCP< const Serializer > &serializer)
Serialize to an internally stored char[] buffer.
ValueTypeSerializationBuffer(const Ordinal count, T buffer[], const RCP< const Serializer > &serializer=DS::getDefaultSerializerRCP())
Serialize to an internally stored char[] buffer.
SerializationTraits< Ordinal, T > DefaultSerializerType
Typename of default serializer.
ValueTypeDeserializationBuffer(const Ordinal bytes, char charBuffer[], const RCP< const Serializer > &serializer)
Serialize to an internally stored char[] buffer.
ValueTypeSerializationBuffer(const Ordinal count, T buffer[], const RCP< const Serializer > &serializer)
Serialize to an internally stored char[] buffer.
Encapsulate how an array of non-const serialized objects with value sematics stored in a char[] array...
static DefaultSerializerType getDefaultSerializer()
Return an instance of the default serializer.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
ValueTypeDeserializationBuffer(const Ordinal bytes, char charBuffer[], const RCP< const Serializer > &serializer=DS::getDefaultSerializerRCP())
Serialize to an internally stored char[] buffer.
Encapsulate how an array of non-const serialized objects with value sematics stored in a char[] array...
Encapsulate how an array of non-const objects with value sematics is serialized into a char[] array...
ConstValueTypeDeserializationBuffer(const Ordinal bytes, const char charBuffer[], const RCP< const Serializer > &serializer=DS::getDefaultSerializerRCP())
Serialize to an internally stored char[] buffer.
Templated array class derived from the STL std::vector.
Serialization traits class for types T that use value semantics.
Nonowning array view.
ConstValueTypeDeserializationBuffer(const Ordinal bytes, const char charBuffer[], const RCP< const Serializer > &serializer)
Serialize to an internally stored char[] buffer.
ConstValueTypeSerializationBuffer(const Ordinal count, const T buffer[], const RCP< const Serializer > &serializer=DS::getDefaultSerializerRCP())
Serialize to an internally stored char[] buffer.
static Teuchos::RCP< DefaultSerializerType > getDefaultSerializerRCP()
Return an RCP of an instance of the default serializer.
Teuchos::SerializationTraits and Teuchos::DirectSerializationTraits definitions.
Encapsulate how an array of const objects with value sematics is serialized into a const char[] array...
Smart reference counting pointer class for automatic garbage collection.
Encapsulate how an array of non-const serialized objects with value sematics stored in a char[] array...
Encapsulate how an array of non-const serialized objects with value sematics stored in a char[] array...
Encapsulate how an array of non-const objects with value sematics is serialized into a char[] array...
Encapsulate how an array of const objects with value sematics is serialized into a const char[] array...