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 //
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 
42 #ifndef TEUCHOS_SERIALIZATION_TRAITS_HELPERS_HPP
43 #define TEUCHOS_SERIALIZATION_TRAITS_HELPERS_HPP
44 
46 #include "Teuchos_ArrayView.hpp"
47 #include "Teuchos_Array.hpp"
48 
49 namespace Teuchos {
50 
69 template <typename Ordinal, typename T>
71 public:
74 
78  return s;
79  }
80 
83  return Teuchos::rcp (new DefaultSerializerType ());
84  }
85 };
86 
94 template <typename Ordinal, typename T, typename Serializer,
95  bool direct = Serializer::supportsDirectSerialization>
97 
105 template <typename Ordinal, typename T, typename Serializer,
106  bool direct = Serializer::supportsDirectSerialization>
108 
117 template <typename Ordinal, typename T, typename Serializer,
118  bool direct = Serializer::supportsDirectSerialization>
120 
129 template <typename Ordinal, typename T, typename Serializer,
130  bool direct = Serializer::supportsDirectSerialization>
132 
138 template <typename Ordinal, typename T, typename Serializer>
140 public:
143  const Ordinal count, T buffer[],
144  const RCP<const Serializer>& serializer
145  );
151  char* getCharBuffer() const;
153  Ordinal getBytes() const;
155  const ArrayView<char> getCharBufferView() const;
156 private:
157  Ordinal count_;
158  T *buffer_;
159  Ordinal bytes_;
160  char *charBuffer_;
161  RCP<const Serializer> serializer_;
162  // Not defined and not to be called
166 };
167 
173 template <typename Ordinal, typename T, typename Serializer>
175 public:
178  const Ordinal count, const T buffer[],
179  const RCP<const Serializer>& serializer
180  );
186  const char* getCharBuffer() const;
188  Ordinal getBytes() const;
190  const ArrayView<const char> getCharBufferView() const;
191 private:
192  Ordinal count_;
193  const T *buffer_;
194  Ordinal bytes_;
195  const char *charBuffer_;
196  RCP<const Serializer> serializer_;
197  // Not defined and not to be called
201 };
202 
209 template <typename Ordinal, typename T, typename Serializer>
211 public:
214  const Ordinal bytes, char charBuffer[],
215  const RCP<const Serializer>& serializer
216  );
222  T* getBuffer() const;
224  Ordinal getCount() const;
225 private:
226  Ordinal bytes_;
227  char *charBuffer_;
228  Ordinal count_;
229  T *buffer_;
230  RCP<const Serializer> serializer_;
231  // Not defined and not to be called
235 };
236 
243 template <typename Ordinal, typename T, typename Serializer>
245 public:
248  const Ordinal bytes, const char charBuffer[],
249  const RCP<const Serializer>& serializer
250  );
256  const T* getBuffer() const;
258  Ordinal getCount() const;
259 private:
260  Ordinal bytes_;
261  const char *charBuffer_;
262  Ordinal count_;
263  const T *buffer_;
264  RCP<const Serializer> serializer_;
265  // Not defined and not to be called
269 };
270 
276 template <typename Ordinal, typename T, typename Serializer>
278 public:
281  const Ordinal count, T buffer[],
282  const RCP<const Serializer>& serializer
283  );
289  char* getCharBuffer() const;
291  Ordinal getBytes() const;
293  const ArrayView<char> getCharBufferView() const;
294 private:
295  Ordinal count_;
296  T *buffer_;
297  Ordinal bytes_;
298  mutable Array<char> charBuffer_;
299  RCP<const Serializer> serializer_;
300  // Not defined and not to be called
304 };
305 
311 template <typename Ordinal, typename T, typename Serializer>
313 public:
316  const Ordinal count, const T buffer[],
317  const RCP<const Serializer>& serializer
318  );
324  const char* getCharBuffer() const;
326  Ordinal getBytes() const;
328  const ArrayView<const char> getCharBufferView() const;
329 private:
330  Ordinal count_;
331  const T *buffer_;
332  Ordinal bytes_;
333  Array<char> charBuffer_;
334  RCP<const Serializer> serializer_;
335  // Not defined and not to be called
339 };
340 
347 template <typename Ordinal, typename T, typename Serializer>
349 public:
352  const Ordinal bytes, char charBuffer[],
353  const RCP<const Serializer>& serializer
354  );
360  T* getBuffer() const;
362  Ordinal getCount() const;
363 private:
364  Ordinal bytes_;
365  char *charBuffer_;
366  Ordinal count_;
367  mutable Array<T> buffer_;
368  RCP<const Serializer> serializer_;
369  // Not defined and not to be called
373 };
374 
381 template <typename Ordinal, typename T, typename Serializer>
383 public:
386  const Ordinal bytes, const char charBuffer[],
387  const RCP<const Serializer>& serializer
388  );
394  const T* getBuffer() const;
396  Ordinal getCount() const;
397 private:
398  Ordinal bytes_;
399  const char *charBuffer_;
400  Ordinal count_;
401  Array<T> buffer_;
402  RCP<const Serializer> serializer_;
403  // Not defined and not to be called
407 };
408 
409 
413 template <typename Ordinal, typename T,
416  public ValueTypeSerializationBufferImp<Ordinal,T,Serializer> {
417 public:
421  const Ordinal count, T buffer[],
422  const RCP<const Serializer>& serializer
423  ) : Base(count,buffer,serializer) {}
424 };
425 
429 template <typename Ordinal, typename T,
430  typename Serializer = typename DefaultSerializer<Ordinal,T>::DefaultSerializerType>
432  public ConstValueTypeSerializationBufferImp<Ordinal,T,Serializer> {
433 public:
437  const Ordinal count, const T buffer[],
438  const RCP<const Serializer>& serializer
439  ) : Base(count,buffer,serializer) {}
440 };
441 
446 template <typename Ordinal, typename T,
447  typename Serializer = typename DefaultSerializer<Ordinal,T>::DefaultSerializerType>
449  public ValueTypeDeserializationBufferImp<Ordinal,T,Serializer> {
450 public:
454  const Ordinal bytes, char charBuffer[],
455  const RCP<const Serializer>& serializer
456  ) : Base(bytes,charBuffer,serializer) {}
457 };
458 
463 template <typename Ordinal, typename T,
464  typename Serializer = typename DefaultSerializer<Ordinal,T>::DefaultSerializerType>
466  public ConstValueTypeDeserializationBufferImp<Ordinal,T,Serializer> {
467 public:
471  const Ordinal bytes, const char charBuffer[],
472  const RCP<const Serializer>& serializer
473  ) : Base(bytes,charBuffer,serializer) {}
474 };
475 
482 template <typename Ordinal, typename T>
483 class ValueTypeSerializationBuffer<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> :
484  public ValueTypeSerializationBufferImp<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> {
485 public:
486  typedef DefaultSerializer<Ordinal,T> DS; // work around for parsing bug in gcc 4.1-4.2
487  typedef typename DS::DefaultSerializerType Serializer;
491  const Ordinal count, T buffer[],
492  const RCP<const Serializer>& serializer = DS::getDefaultSerializerRCP()
493  ) : Base(count,buffer,serializer) {}
494 };
495 
502 template <typename Ordinal, typename T>
503 class ConstValueTypeSerializationBuffer<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> :
504  public ConstValueTypeSerializationBufferImp<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> {
505 public:
506  typedef DefaultSerializer<Ordinal,T> DS; // work around for parsing bug in gcc 4.1-4.2
507  typedef typename DS::DefaultSerializerType Serializer;
511  const Ordinal count, const T buffer[],
512  const RCP<const Serializer>& serializer = DS::getDefaultSerializerRCP()
513  ) : Base(count,buffer,serializer) {}
514 };
515 
523 template <typename Ordinal, typename T>
524 class ValueTypeDeserializationBuffer<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> :
525  public ValueTypeDeserializationBufferImp<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> {
526 public:
527  typedef DefaultSerializer<Ordinal,T> DS; // work around for parsing bug in gcc 4.1-4.2
528  typedef typename DS::DefaultSerializerType Serializer;
532  const Ordinal bytes, char charBuffer[],
533  const RCP<const Serializer>& serializer = DS::getDefaultSerializerRCP()
534  ) : Base(bytes,charBuffer,serializer) {}
535 };
536 
544 template <typename Ordinal, typename T>
545 class ConstValueTypeDeserializationBuffer<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> :
546  public ConstValueTypeDeserializationBufferImp<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> {
547 public:
548  typedef DefaultSerializer<Ordinal,T> DS; // work around for parsing bug in gcc 4.1-4.2
549  typedef typename DS::DefaultSerializerType Serializer;
553  const Ordinal bytes, const char charBuffer[],
554  const RCP<const Serializer>& serializer = DS::getDefaultSerializerRCP()
555  ) : Base(bytes,charBuffer,serializer) {}
556 };
557 
558 // /////////////////////////////////////
559 // Template implementations for direct serialization
560 
561 //
562 // ValueTypeSerializationBufferImp
563 //
564 
565 template <typename Ordinal, typename T, typename Serializer>
566 ValueTypeSerializationBufferImp<Ordinal,T,Serializer,true>::
567 ValueTypeSerializationBufferImp(
568  const Ordinal count, T buffer[], const RCP<const Serializer>& serializer
569  )
570  :count_(count), buffer_(buffer), serializer_(serializer)
571 {
572  bytes_ = serializer_->fromCountToDirectBytes(count_);
573  charBuffer_ = serializer_->convertToCharPtr(buffer_);
574 }
575 
576 template <typename Ordinal, typename T, typename Serializer>
579 {
580  // There is nothing to do since the type uses direct serialization!
581 }
582 
583 template <typename Ordinal, typename T, typename Serializer>
586 {
587  return charBuffer_;
588 }
589 
590 template <typename Ordinal, typename T, typename Serializer>
592 getBytes() const
593 {
594  return bytes_;
595 }
596 
597 
598 template <typename Ordinal, typename T, typename Serializer>
599 const ArrayView<char>
602 {
603  return arrayView(charBuffer_, bytes_);
604 }
605 
606 
607 //
608 // ConstValueTypeSerializationBufferImp
609 //
610 
611 template <typename Ordinal, typename T, typename Serializer>
614  const Ordinal count, const T buffer[], const RCP<const Serializer>& serializer
615  )
616  :count_(count), buffer_(buffer), serializer_(serializer)
617 {
618  bytes_ = serializer_->fromCountToDirectBytes(count_);
619  charBuffer_ = serializer_->convertToCharPtr(buffer_);
620 }
621 
622 template <typename Ordinal, typename T, typename Serializer>
625 {
626  // There is nothing to do since the type uses direct serialization!
627 }
628 
629 template <typename Ordinal, typename T, typename Serializer>
632 {
633  return charBuffer_;
634 }
635 
636 template <typename Ordinal, typename T, typename Serializer>
638 getBytes() const
639 {
640  return bytes_;
641 }
642 
643 template <typename Ordinal, typename T, typename Serializer>
647 {
648  return arrayView(charBuffer_, bytes_);
649 }
650 
651 //
652 // ValueTypeDeserializationBufferImp
653 //
654 
655 template <typename Ordinal, typename T, typename Serializer>
658  const Ordinal bytes, char charBuffer[], const RCP<const Serializer>& serializer
659  )
660  :bytes_(bytes), charBuffer_(charBuffer), serializer_(serializer)
661 {
662  count_ = serializer_->fromDirectBytesToCount(bytes_);
663  buffer_ = serializer_->convertFromCharPtr(charBuffer_);
664 }
665 
666 template <typename Ordinal, typename T, typename Serializer>
669 {
670  // There is nothing to do since the type uses direct serialization!
671 }
672 
673 template <typename Ordinal, typename T, typename Serializer>
675 getBuffer() const
676 {
677  return buffer_;
678 }
679 
680 template <typename Ordinal, typename T, typename Serializer>
682 getCount() const
683 {
684  return count_;
685 }
686 
687 //
688 // ConstValueTypeDeserializationBufferImp
689 //
690 
691 template <typename Ordinal, typename T, typename Serializer>
694  const Ordinal bytes, const char charBuffer[], const RCP<const Serializer>& serializer
695  )
696  :bytes_(bytes), charBuffer_(charBuffer), serializer_(serializer)
697 {
698  count_ = serializer_->fromDirectBytesToCount(bytes_);
699  buffer_ = serializer_->convertFromCharPtr(charBuffer_);
700 }
701 
702 template <typename Ordinal, typename T, typename Serializer>
705 {
706  // There is nothing to do since the type uses direct serialization!
707 }
708 
709 template <typename Ordinal, typename T, typename Serializer>
711 getBuffer() const
712 {
713  return buffer_;
714 }
715 
716 template <typename Ordinal, typename T, typename Serializer>
718 getCount() const
719 {
720  return count_;
721 }
722 
723 
724 // /////////////////////////////////////
725 // Template implementations for indirect specializations
726 
727 //
728 // ValueTypeSerializationBufferImp
729 //
730 
731 template <typename Ordinal, typename T, typename Serializer>
734  const Ordinal count, T buffer[], const RCP<const Serializer>& serializer
735  )
736  :count_(count), buffer_(buffer), serializer_(serializer)
737 {
738  bytes_ = serializer_->fromCountToIndirectBytes(count_, buffer_);
739  charBuffer_.resize(bytes_);
740  serializer_->serialize(count_, buffer_, bytes_, &charBuffer_[0]);
741 }
742 
743 template <typename Ordinal, typename T, typename Serializer>
746 {
747  serializer_->deserialize(bytes_, &charBuffer_[0], count_, buffer_);
748 }
749 
750 template <typename Ordinal, typename T, typename Serializer>
753 {
754  return &charBuffer_[0];
755 }
756 
757 template <typename Ordinal, typename T, typename Serializer>
759 getBytes() const
760 {
761  return bytes_;
762 }
763 
764 template <typename Ordinal, typename T, typename Serializer>
765 const ArrayView<char>
768 {
769  return charBuffer_.view(0, bytes_);
770 }
771 
772 
773 //
774 // ConstValueTypeSerializationBufferImp
775 //
776 
777 template <typename Ordinal, typename T, typename Serializer>
780  const Ordinal count, const T buffer[], const RCP<const Serializer>& serializer
781  )
782  :count_(count), buffer_(buffer), serializer_(serializer)
783 {
784  bytes_ = serializer_->fromCountToIndirectBytes(count_, buffer_);
785  charBuffer_.resize(bytes_);
786  serializer_->serialize(count_, buffer_, bytes_, &charBuffer_[0]);
787 }
788 
789 template <typename Ordinal, typename T, typename Serializer>
792 {
793 }
794 
795 template <typename Ordinal, typename T, typename Serializer>
798 {
799  return &charBuffer_[0];
800 }
801 
802 template <typename Ordinal, typename T, typename Serializer>
804 getBytes() const
805 {
806  return bytes_;
807 }
808 
809 template <typename Ordinal, typename T, typename Serializer>
813 {
814  return charBuffer_.view(0, bytes_);
815 }
816 
817 //
818 // ValueTypeDeserializationBufferImp
819 //
820 
821 template <typename Ordinal, typename T, typename Serializer>
824  const Ordinal bytes, char charBuffer[], const RCP<const Serializer>& serializer
825  )
826  :bytes_(bytes), charBuffer_(charBuffer), serializer_(serializer)
827 {
828  count_ = serializer_->fromIndirectBytesToCount(bytes_, charBuffer_);
829  buffer_.resize(count_);
830  serializer_->deserialize(bytes_, charBuffer_, count_, &buffer_[0]);
831 }
832 
833 template <typename Ordinal, typename T, typename Serializer>
836 {
837  serializer_->serialize(count_, &buffer_[0], bytes_, charBuffer_);
838 }
839 
840 template <typename Ordinal, typename T, typename Serializer>
842 getBuffer() const
843 {
844  return &buffer_[0];
845 }
846 
847 template <typename Ordinal, typename T, typename Serializer>
849 getCount() const
850 {
851  return count_;
852 }
853 
854 //
855 // ConstValueTypeDeserializationBufferImp
856 //
857 
858 template <typename Ordinal, typename T, typename Serializer>
861  const Ordinal bytes, const char charBuffer[], const RCP<const Serializer>& serializer
862  )
863  :bytes_(bytes), charBuffer_(charBuffer), serializer_(serializer)
864 {
865  count_ = serializer_->fromIndirectBytesToCount(bytes_, charBuffer_);
866  buffer_.resize(count_);
867  serializer_->deserialize(bytes_, charBuffer_, count_, &buffer_[0]);
868 }
869 
870 template <typename Ordinal, typename T, typename Serializer>
873 {
874 }
875 
876 template <typename Ordinal, typename T, typename Serializer>
878 getBuffer() const
879 {
880  return &buffer_[0];
881 }
882 
883 template <typename Ordinal, typename T, typename Serializer>
885 getCount() const
886 {
887  return count_;
888 }
889 
890 } // namespace Teuchos
891 
892 #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...