Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
Teuchos_SerializationTraitsHelpers.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 
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 
58 template <typename Ordinal, typename T>
60 public:
61 
64 
66  static DefaultSerializerType& getDefaultSerializer() {
68  defaultSerializer_ = Teuchos::rcp(new DefaultSerializerType);
69  return *defaultSerializer_;
70  }
71 
75  defaultSerializer_ = Teuchos::rcp(new DefaultSerializerType);
76  return defaultSerializer_;
77  }
78 
79 private:
80 
83 };
84 
85 template <typename Ordinal, typename T>
88 
96 template <typename Ordinal, typename T, typename Serializer,
97  bool direct = Serializer::supportsDirectSerialization>
99 
107 template <typename Ordinal, typename T, typename Serializer,
108  bool direct = Serializer::supportsDirectSerialization>
110 
119 template <typename Ordinal, typename T, typename Serializer,
120  bool direct = Serializer::supportsDirectSerialization>
122 
131 template <typename Ordinal, typename T, typename Serializer,
132  bool direct = Serializer::supportsDirectSerialization>
134 
140 template <typename Ordinal, typename T, typename Serializer>
141 class ValueTypeSerializationBufferImp<Ordinal,T,Serializer,true> {
142 public:
145  const Ordinal count, T buffer[],
146  const RCP<const Serializer>& serializer
147  );
153  char* getCharBuffer() const;
155  Ordinal getBytes() const;
157  const ArrayView<char> getCharBufferView() const;
158 private:
159  Ordinal count_;
161  Ordinal bytes_;
162  char *charBuffer_;
164  // Not defined and not to be called
168 };
169 
175 template <typename Ordinal, typename T, typename Serializer>
176 class ConstValueTypeSerializationBufferImp<Ordinal,T,Serializer,true> {
177 public:
180  const Ordinal count, const T buffer[],
181  const RCP<const Serializer>& serializer
182  );
188  const char* getCharBuffer() const;
190  Ordinal getBytes() const;
192  const ArrayView<const char> getCharBufferView() const;
193 private:
194  Ordinal count_;
195  const T *buffer_;
196  Ordinal bytes_;
197  const char *charBuffer_;
199  // Not defined and not to be called
203 };
204 
211 template <typename Ordinal, typename T, typename Serializer>
212 class ValueTypeDeserializationBufferImp<Ordinal,T,Serializer,true> {
213 public:
216  const Ordinal bytes, char charBuffer[],
217  const RCP<const Serializer>& serializer
218  );
224  T* getBuffer() const;
226  Ordinal getCount() const;
227 private:
228  Ordinal bytes_;
229  char *charBuffer_;
230  Ordinal count_;
233  // Not defined and not to be called
237 };
238 
245 template <typename Ordinal, typename T, typename Serializer>
246 class ConstValueTypeDeserializationBufferImp<Ordinal,T,Serializer,true> {
247 public:
250  const Ordinal bytes, const char charBuffer[],
251  const RCP<const Serializer>& serializer
252  );
258  const T* getBuffer() const;
260  Ordinal getCount() const;
261 private:
262  Ordinal bytes_;
263  const char *charBuffer_;
264  Ordinal count_;
265  const T *buffer_;
267  // Not defined and not to be called
271 };
272 
278 template <typename Ordinal, typename T, typename Serializer>
279 class ValueTypeSerializationBufferImp<Ordinal,T,Serializer,false> {
280 public:
283  const Ordinal count, T buffer[],
284  const RCP<const Serializer>& serializer
285  );
291  char* getCharBuffer() const;
293  Ordinal getBytes() const;
295  const ArrayView<char> getCharBufferView() const;
296 private:
297  Ordinal count_;
299  Ordinal bytes_;
302  // Not defined and not to be called
306 };
307 
313 template <typename Ordinal, typename T, typename Serializer>
314 class ConstValueTypeSerializationBufferImp<Ordinal,T,Serializer,false> {
315 public:
318  const Ordinal count, const T buffer[],
319  const RCP<const Serializer>& serializer
320  );
326  const char* getCharBuffer() const;
328  Ordinal getBytes() const;
330  const ArrayView<const char> getCharBufferView() const;
331 private:
332  Ordinal count_;
333  const T *buffer_;
334  Ordinal bytes_;
337  // Not defined and not to be called
341 };
342 
349 template <typename Ordinal, typename T, typename Serializer>
350 class ValueTypeDeserializationBufferImp<Ordinal,T,Serializer,false> {
351 public:
354  const Ordinal bytes, char charBuffer[],
355  const RCP<const Serializer>& serializer
356  );
362  T* getBuffer() const;
364  Ordinal getCount() const;
365 private:
366  Ordinal bytes_;
367  char *charBuffer_;
368  Ordinal count_;
369  mutable Array<T> buffer_;
371  // Not defined and not to be called
375 };
376 
383 template <typename Ordinal, typename T, typename Serializer>
384 class ConstValueTypeDeserializationBufferImp<Ordinal,T,Serializer,false> {
385 public:
388  const Ordinal bytes, const char charBuffer[],
389  const RCP<const Serializer>& serializer
390  );
396  const T* getBuffer() const;
398  Ordinal getCount() const;
399 private:
400  Ordinal bytes_;
401  const char *charBuffer_;
402  Ordinal count_;
405  // Not defined and not to be called
409 };
410 
411 
415 template <typename Ordinal, typename T,
416  typename Serializer = typename DefaultSerializer<Ordinal,T>::DefaultSerializerType>
418  public ValueTypeSerializationBufferImp<Ordinal,T,Serializer> {
419 public:
423  const Ordinal count, T buffer[],
424  const RCP<const Serializer>& serializer
425  ) : Base(count,buffer,serializer) {}
426 };
427 
431 template <typename Ordinal, typename T,
432  typename Serializer = typename DefaultSerializer<Ordinal,T>::DefaultSerializerType>
434  public ConstValueTypeSerializationBufferImp<Ordinal,T,Serializer> {
435 public:
439  const Ordinal count, const T buffer[],
440  const RCP<const Serializer>& serializer
441  ) : Base(count,buffer,serializer) {}
442 };
443 
448 template <typename Ordinal, typename T,
449  typename Serializer = typename DefaultSerializer<Ordinal,T>::DefaultSerializerType>
451  public ValueTypeDeserializationBufferImp<Ordinal,T,Serializer> {
452 public:
456  const Ordinal bytes, char charBuffer[],
457  const RCP<const Serializer>& serializer
458  ) : Base(bytes,charBuffer,serializer) {}
459 };
460 
465 template <typename Ordinal, typename T,
466  typename Serializer = typename DefaultSerializer<Ordinal,T>::DefaultSerializerType>
468  public ConstValueTypeDeserializationBufferImp<Ordinal,T,Serializer> {
469 public:
473  const Ordinal bytes, const char charBuffer[],
474  const RCP<const Serializer>& serializer
475  ) : Base(bytes,charBuffer,serializer) {}
476 };
477 
484 template <typename Ordinal, typename T>
486  public ValueTypeSerializationBufferImp<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> {
487 public:
488  typedef DefaultSerializer<Ordinal,T> DS; // work around for parsing bug in gcc 4.1-4.2
493  const Ordinal count, T buffer[],
494  const RCP<const Serializer>& serializer = DS::getDefaultSerializerRCP()
495  ) : Base(count,buffer,serializer) {}
496 };
497 
504 template <typename Ordinal, typename T>
506  public ConstValueTypeSerializationBufferImp<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> {
507 public:
508  typedef DefaultSerializer<Ordinal,T> DS; // work around for parsing bug in gcc 4.1-4.2
513  const Ordinal count, const T buffer[],
514  const RCP<const Serializer>& serializer = DS::getDefaultSerializerRCP()
515  ) : Base(count,buffer,serializer) {}
516 };
517 
525 template <typename Ordinal, typename T>
527  public ValueTypeDeserializationBufferImp<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> {
528 public:
529  typedef DefaultSerializer<Ordinal,T> DS; // work around for parsing bug in gcc 4.1-4.2
534  const Ordinal bytes, char charBuffer[],
535  const RCP<const Serializer>& serializer = DS::getDefaultSerializerRCP()
536  ) : Base(bytes,charBuffer,serializer) {}
537 };
538 
546 template <typename Ordinal, typename T>
548  public ConstValueTypeDeserializationBufferImp<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> {
549 public:
550  typedef DefaultSerializer<Ordinal,T> DS; // work around for parsing bug in gcc 4.1-4.2
555  const Ordinal bytes, const char charBuffer[],
556  const RCP<const Serializer>& serializer = DS::getDefaultSerializerRCP()
557  ) : Base(bytes,charBuffer,serializer) {}
558 };
559 
560 // /////////////////////////////////////
561 // Template implementations for direct serialization
562 
563 //
564 // ValueTypeSerializationBufferImp
565 //
566 
567 template <typename Ordinal, typename T, typename Serializer>
570  const Ordinal count, T buffer[], const RCP<const Serializer>& serializer
571  )
572  :count_(count), buffer_(buffer), serializer_(serializer)
573 {
574  bytes_ = serializer_->fromCountToDirectBytes(count_);
575  charBuffer_ = serializer_->convertToCharPtr(buffer_);
576 }
577 
578 template <typename Ordinal, typename T, typename Serializer>
581 {
582  // There is nothing to do since the type uses direct serialization!
583 }
584 
585 template <typename Ordinal, typename T, typename Serializer>
588 {
589  return charBuffer_;
590 }
591 
592 template <typename Ordinal, typename T, typename Serializer>
594 getBytes() const
595 {
596  return bytes_;
597 }
598 
599 
600 template <typename Ordinal, typename T, typename Serializer>
601 const ArrayView<char>
604 {
605  return arrayView(charBuffer_, bytes_);
606 }
607 
608 
609 //
610 // ConstValueTypeSerializationBufferImp
611 //
612 
613 template <typename Ordinal, typename T, typename Serializer>
616  const Ordinal count, const T buffer[], const RCP<const Serializer>& serializer
617  )
618  :count_(count), buffer_(buffer), serializer_(serializer)
619 {
620  bytes_ = serializer_->fromCountToDirectBytes(count_);
621  charBuffer_ = serializer_->convertToCharPtr(buffer_);
622 }
623 
624 template <typename Ordinal, typename T, typename Serializer>
627 {
628  // There is nothing to do since the type uses direct serialization!
629 }
630 
631 template <typename Ordinal, typename T, typename Serializer>
634 {
635  return charBuffer_;
636 }
637 
638 template <typename Ordinal, typename T, typename Serializer>
640 getBytes() const
641 {
642  return bytes_;
643 }
644 
645 template <typename Ordinal, typename T, typename Serializer>
649 {
650  return arrayView(charBuffer_, bytes_);
651 }
652 
653 //
654 // ValueTypeDeserializationBufferImp
655 //
656 
657 template <typename Ordinal, typename T, typename Serializer>
660  const Ordinal bytes, char charBuffer[], const RCP<const Serializer>& serializer
661  )
662  :bytes_(bytes), charBuffer_(charBuffer), serializer_(serializer)
663 {
664  count_ = serializer_->fromDirectBytesToCount(bytes_);
665  buffer_ = serializer_->convertFromCharPtr(charBuffer_);
666 }
667 
668 template <typename Ordinal, typename T, typename Serializer>
671 {
672  // There is nothing to do since the type uses direct serialization!
673 }
674 
675 template <typename Ordinal, typename T, typename Serializer>
677 getBuffer() const
678 {
679  return buffer_;
680 }
681 
682 template <typename Ordinal, typename T, typename Serializer>
684 getCount() const
685 {
686  return count_;
687 }
688 
689 //
690 // ConstValueTypeDeserializationBufferImp
691 //
692 
693 template <typename Ordinal, typename T, typename Serializer>
696  const Ordinal bytes, const char charBuffer[], const RCP<const Serializer>& serializer
697  )
698  :bytes_(bytes), charBuffer_(charBuffer), serializer_(serializer)
699 {
700  count_ = serializer_->fromDirectBytesToCount(bytes_);
701  buffer_ = serializer_->convertFromCharPtr(charBuffer_);
702 }
703 
704 template <typename Ordinal, typename T, typename Serializer>
707 {
708  // There is nothing to do since the type uses direct serialization!
709 }
710 
711 template <typename Ordinal, typename T, typename Serializer>
713 getBuffer() const
714 {
715  return buffer_;
716 }
717 
718 template <typename Ordinal, typename T, typename Serializer>
720 getCount() const
721 {
722  return count_;
723 }
724 
725 
726 // /////////////////////////////////////
727 // Template implementations for indirect specializations
728 
729 //
730 // ValueTypeSerializationBufferImp
731 //
732 
733 template <typename Ordinal, typename T, typename Serializer>
736  const Ordinal count, T buffer[], const RCP<const Serializer>& serializer
737  )
738  :count_(count), buffer_(buffer), serializer_(serializer)
739 {
740  bytes_ = serializer_->fromCountToIndirectBytes(count_, buffer_);
741  charBuffer_.resize(bytes_);
742  serializer_->serialize(count_, buffer_, bytes_, &charBuffer_[0]);
743 }
744 
745 template <typename Ordinal, typename T, typename Serializer>
748 {
749  serializer_->deserialize(bytes_, &charBuffer_[0], count_, buffer_);
750 }
751 
752 template <typename Ordinal, typename T, typename Serializer>
755 {
756  return &charBuffer_[0];
757 }
758 
759 template <typename Ordinal, typename T, typename Serializer>
761 getBytes() const
762 {
763  return bytes_;
764 }
765 
766 template <typename Ordinal, typename T, typename Serializer>
767 const ArrayView<char>
770 {
771  return charBuffer_.view(0, bytes_);
772 }
773 
774 
775 //
776 // ConstValueTypeSerializationBufferImp
777 //
778 
779 template <typename Ordinal, typename T, typename Serializer>
782  const Ordinal count, const T buffer[], const RCP<const Serializer>& serializer
783  )
784  :count_(count), buffer_(buffer), serializer_(serializer)
785 {
786  bytes_ = serializer_->fromCountToIndirectBytes(count_, buffer_);
787  charBuffer_.resize(bytes_);
788  serializer_->serialize(count_, buffer_, bytes_, &charBuffer_[0]);
789 }
790 
791 template <typename Ordinal, typename T, typename Serializer>
794 {
795 }
796 
797 template <typename Ordinal, typename T, typename Serializer>
800 {
801  return &charBuffer_[0];
802 }
803 
804 template <typename Ordinal, typename T, typename Serializer>
806 getBytes() const
807 {
808  return bytes_;
809 }
810 
811 template <typename Ordinal, typename T, typename Serializer>
815 {
816  return charBuffer_.view(0, bytes_);
817 }
818 
819 //
820 // ValueTypeDeserializationBufferImp
821 //
822 
823 template <typename Ordinal, typename T, typename Serializer>
826  const Ordinal bytes, char charBuffer[], const RCP<const Serializer>& serializer
827  )
828  :bytes_(bytes), charBuffer_(charBuffer), serializer_(serializer)
829 {
830  count_ = serializer_->fromIndirectBytesToCount(bytes_, charBuffer_);
831  buffer_.resize(count_);
832  serializer_->deserialize(bytes_, charBuffer_, count_, &buffer_[0]);
833 }
834 
835 template <typename Ordinal, typename T, typename Serializer>
838 {
839  serializer_->serialize(count_, &buffer_[0], bytes_, charBuffer_);
840 }
841 
842 template <typename Ordinal, typename T, typename Serializer>
844 getBuffer() const
845 {
846  return &buffer_[0];
847 }
848 
849 template <typename Ordinal, typename T, typename Serializer>
851 getCount() const
852 {
853  return count_;
854 }
855 
856 //
857 // ConstValueTypeDeserializationBufferImp
858 //
859 
860 template <typename Ordinal, typename T, typename Serializer>
863  const Ordinal bytes, const char charBuffer[], const RCP<const Serializer>& serializer
864  )
865  :bytes_(bytes), charBuffer_(charBuffer), serializer_(serializer)
866 {
867  count_ = serializer_->fromIndirectBytesToCount(bytes_, charBuffer_);
868  buffer_.resize(count_);
869  serializer_->deserialize(bytes_, charBuffer_, count_, &buffer_[0]);
870 }
871 
872 template <typename Ordinal, typename T, typename Serializer>
875 {
876 }
877 
878 template <typename Ordinal, typename T, typename Serializer>
880 getBuffer() const
881 {
882  return &buffer_[0];
883 }
884 
885 template <typename Ordinal, typename T, typename Serializer>
887 getCount() const
888 {
889  return count_;
890 }
891 
892 } // namespace Teuchos
893 
894 #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...
ValueTypeDeserializationBufferImp< Ordinal, T, Serializer > Base
static DefaultSerializerType & getDefaultSerializer()
Return the default serializer.
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.
ConstValueTypeSerializationBufferImp< Ordinal, T, Serializer > Base
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...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
static Teuchos::RCP< DefaultSerializerType > defaultSerializer_
RCP to the default serializer.
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...
ValueTypeSerializationBufferImp< Ordinal, T, Serializer > Base
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.
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 RCP to 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...
ConstValueTypeDeserializationBufferImp< Ordinal, T, Serializer > Base
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...