Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
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
49namespace Teuchos {
50
69template <typename Ordinal, typename T>
71public:
74
78 return s;
79 }
80
83 return Teuchos::rcp (new DefaultSerializerType ());
84 }
85};
86
94template <typename Ordinal, typename T, typename Serializer,
95 bool direct = Serializer::supportsDirectSerialization>
97
105template <typename Ordinal, typename T, typename Serializer,
106 bool direct = Serializer::supportsDirectSerialization>
108
117template <typename Ordinal, typename T, typename Serializer,
118 bool direct = Serializer::supportsDirectSerialization>
120
129template <typename Ordinal, typename T, typename Serializer,
130 bool direct = Serializer::supportsDirectSerialization>
132
138template <typename Ordinal, typename T, typename Serializer>
140public:
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;
156private:
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
173template <typename Ordinal, typename T, typename Serializer>
175public:
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;
191private:
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
209template <typename Ordinal, typename T, typename Serializer>
211public:
214 const Ordinal bytes, char charBuffer[],
215 const RCP<const Serializer>& serializer
216 );
222 T* getBuffer() const;
224 Ordinal getCount() const;
225private:
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
243template <typename Ordinal, typename T, typename Serializer>
245public:
248 const Ordinal bytes, const char charBuffer[],
249 const RCP<const Serializer>& serializer
250 );
256 const T* getBuffer() const;
258 Ordinal getCount() const;
259private:
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
276template <typename Ordinal, typename T, typename Serializer>
278public:
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;
294private:
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
311template <typename Ordinal, typename T, typename Serializer>
313public:
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;
329private:
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
347template <typename Ordinal, typename T, typename Serializer>
349public:
352 const Ordinal bytes, char charBuffer[],
353 const RCP<const Serializer>& serializer
354 );
360 T* getBuffer() const;
362 Ordinal getCount() const;
363private:
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
381template <typename Ordinal, typename T, typename Serializer>
383public:
386 const Ordinal bytes, const char charBuffer[],
387 const RCP<const Serializer>& serializer
388 );
394 const T* getBuffer() const;
396 Ordinal getCount() const;
397private:
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
413template <typename Ordinal, typename T,
416 public ValueTypeSerializationBufferImp<Ordinal,T,Serializer> {
417public:
421 const Ordinal count, T buffer[],
422 const RCP<const Serializer>& serializer
423 ) : Base(count,buffer,serializer) {}
424};
425
429template <typename Ordinal, typename T,
430 typename Serializer = typename DefaultSerializer<Ordinal,T>::DefaultSerializerType>
432 public ConstValueTypeSerializationBufferImp<Ordinal,T,Serializer> {
433public:
437 const Ordinal count, const T buffer[],
438 const RCP<const Serializer>& serializer
439 ) : Base(count,buffer,serializer) {}
440};
441
446template <typename Ordinal, typename T,
447 typename Serializer = typename DefaultSerializer<Ordinal,T>::DefaultSerializerType>
449 public ValueTypeDeserializationBufferImp<Ordinal,T,Serializer> {
450public:
454 const Ordinal bytes, char charBuffer[],
455 const RCP<const Serializer>& serializer
456 ) : Base(bytes,charBuffer,serializer) {}
457};
458
463template <typename Ordinal, typename T,
464 typename Serializer = typename DefaultSerializer<Ordinal,T>::DefaultSerializerType>
466 public ConstValueTypeDeserializationBufferImp<Ordinal,T,Serializer> {
467public:
471 const Ordinal bytes, const char charBuffer[],
472 const RCP<const Serializer>& serializer
473 ) : Base(bytes,charBuffer,serializer) {}
474};
475
482template <typename Ordinal, typename T>
483class ValueTypeSerializationBuffer<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> :
484 public ValueTypeSerializationBufferImp<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> {
485public:
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
502template <typename Ordinal, typename T>
503class ConstValueTypeSerializationBuffer<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> :
504 public ConstValueTypeSerializationBufferImp<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> {
505public:
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
523template <typename Ordinal, typename T>
524class ValueTypeDeserializationBuffer<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> :
525 public ValueTypeDeserializationBufferImp<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> {
526public:
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
544template <typename Ordinal, typename T>
545class ConstValueTypeDeserializationBuffer<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> :
546 public ConstValueTypeDeserializationBufferImp<Ordinal,T,typename DefaultSerializer<Ordinal,T>::DefaultSerializerType> {
547public:
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
565template <typename Ordinal, typename T, typename Serializer>
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
576template <typename Ordinal, typename T, typename Serializer>
579{
580 // There is nothing to do since the type uses direct serialization!
581}
582
583template <typename Ordinal, typename T, typename Serializer>
585getCharBuffer() const
586{
587 return charBuffer_;
588}
589
590template <typename Ordinal, typename T, typename Serializer>
592getBytes() const
593{
594 return bytes_;
595}
596
597
598template <typename Ordinal, typename T, typename Serializer>
599const ArrayView<char>
601getCharBufferView() const
602{
603 return arrayView(charBuffer_, bytes_);
604}
605
606
607//
608// ConstValueTypeSerializationBufferImp
609//
610
611template <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
622template <typename Ordinal, typename T, typename Serializer>
625{
626 // There is nothing to do since the type uses direct serialization!
627}
628
629template <typename Ordinal, typename T, typename Serializer>
631getCharBuffer() const
632{
633 return charBuffer_;
634}
635
636template <typename Ordinal, typename T, typename Serializer>
638getBytes() const
639{
640 return bytes_;
641}
642
643template <typename Ordinal, typename T, typename Serializer>
646getCharBufferView() const
647{
648 return arrayView(charBuffer_, bytes_);
649}
650
651//
652// ValueTypeDeserializationBufferImp
653//
654
655template <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
666template <typename Ordinal, typename T, typename Serializer>
669{
670 // There is nothing to do since the type uses direct serialization!
671}
672
673template <typename Ordinal, typename T, typename Serializer>
675getBuffer() const
676{
677 return buffer_;
678}
679
680template <typename Ordinal, typename T, typename Serializer>
682getCount() const
683{
684 return count_;
685}
686
687//
688// ConstValueTypeDeserializationBufferImp
689//
690
691template <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
702template <typename Ordinal, typename T, typename Serializer>
705{
706 // There is nothing to do since the type uses direct serialization!
707}
708
709template <typename Ordinal, typename T, typename Serializer>
711getBuffer() const
712{
713 return buffer_;
714}
715
716template <typename Ordinal, typename T, typename Serializer>
718getCount() const
719{
720 return count_;
721}
722
723
724// /////////////////////////////////////
725// Template implementations for indirect specializations
726
727//
728// ValueTypeSerializationBufferImp
729//
730
731template <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
743template <typename Ordinal, typename T, typename Serializer>
746{
747 serializer_->deserialize(bytes_, &charBuffer_[0], count_, buffer_);
748}
749
750template <typename Ordinal, typename T, typename Serializer>
752getCharBuffer() const
753{
754 return &charBuffer_[0];
755}
756
757template <typename Ordinal, typename T, typename Serializer>
759getBytes() const
760{
761 return bytes_;
762}
763
764template <typename Ordinal, typename T, typename Serializer>
765const ArrayView<char>
767getCharBufferView() const
768{
769 return charBuffer_.view(0, bytes_);
770}
771
772
773//
774// ConstValueTypeSerializationBufferImp
775//
776
777template <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
789template <typename Ordinal, typename T, typename Serializer>
792{
793}
794
795template <typename Ordinal, typename T, typename Serializer>
797getCharBuffer() const
798{
799 return &charBuffer_[0];
800}
801
802template <typename Ordinal, typename T, typename Serializer>
804getBytes() const
805{
806 return bytes_;
807}
808
809template <typename Ordinal, typename T, typename Serializer>
812getCharBufferView() const
813{
814 return charBuffer_.view(0, bytes_);
815}
816
817//
818// ValueTypeDeserializationBufferImp
819//
820
821template <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
833template <typename Ordinal, typename T, typename Serializer>
836{
837 serializer_->serialize(count_, &buffer_[0], bytes_, charBuffer_);
838}
839
840template <typename Ordinal, typename T, typename Serializer>
842getBuffer() const
843{
844 return &buffer_[0];
845}
846
847template <typename Ordinal, typename T, typename Serializer>
849getCount() const
850{
851 return count_;
852}
853
854//
855// ConstValueTypeDeserializationBufferImp
856//
857
858template <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
870template <typename Ordinal, typename T, typename Serializer>
873{
874}
875
876template <typename Ordinal, typename T, typename Serializer>
878getBuffer() const
879{
880 return &buffer_[0];
881}
882
883template <typename Ordinal, typename T, typename Serializer>
885getCount() const
886{
887 return count_;
888}
889
890} // namespace Teuchos
891
892#endif // TEUCHOS_SERIALIZATION_TRAITS_HELPERS_HPP
Templated array class derived from the STL std::vector.
Teuchos::SerializationTraits and Teuchos::DirectSerializationTraits definitions.
Nonowning array view.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes.
Encapsulate how an array of non-const serialized objects with value sematics stored in a char[] array...
ConstValueTypeDeserializationBuffer(const Ordinal bytes, const 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...
ConstValueTypeDeserializationBuffer(const Ordinal bytes, const char charBuffer[], const RCP< const Serializer > &serializer)
Serialize to an internally stored char[] buffer.
Encapsulate how an array of const objects with value sematics is serialized into a const char[] array...
ConstValueTypeSerializationBuffer(const Ordinal count, const T buffer[], const RCP< const Serializer > &serializer=DS::getDefaultSerializerRCP())
Serialize to an internally stored char[] buffer.
Encapsulate how an array of const objects with value sematics is serialized into a const char[] array...
ConstValueTypeSerializationBuffer(const Ordinal count, const T buffer[], const RCP< const Serializer > &serializer)
Serialize to an internally stored char[] buffer.
A class for instantiating a default serialization object.
static DefaultSerializerType getDefaultSerializer()
Return an instance of the default serializer.
SerializationTraits< Ordinal, T > DefaultSerializerType
Typename of default serializer.
static Teuchos::RCP< DefaultSerializerType > getDefaultSerializerRCP()
Return an RCP of an instance of the default serializer.
Smart reference counting pointer class for automatic garbage collection.
Serialization traits class for types T that use value semantics.
Strategy interface for the indirect serializing and deserializing objects of a given type handled usi...
Encapsulate how an array of non-const serialized objects with value sematics stored in a char[] array...
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...
ValueTypeDeserializationBuffer(const Ordinal bytes, char charBuffer[], const RCP< const Serializer > &serializer)
Serialize to an internally stored char[] buffer.
Encapsulate how an array of non-const objects with value sematics is serialized into a char[] array.
ValueTypeSerializationBuffer(const Ordinal count, T buffer[], const RCP< const Serializer > &serializer=DS::getDefaultSerializerRCP())
Serialize to an internally stored char[] buffer.
Encapsulate how an array of non-const objects with value sematics is serialized into a char[] array.
ValueTypeSerializationBuffer(const Ordinal count, T buffer[], const RCP< const Serializer > &serializer)
Serialize to an internally stored char[] buffer.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.