Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Teuchos_SerializerHelpers.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_SERIALIZER_HELPERS_HPP
43#define TEUCHOS_SERIALIZER_HELPERS_HPP
44
46#include "Teuchos_Array.hpp"
47
48namespace Teuchos {
49
53template <typename Ordinal, typename T>
55public:
58 const Serializer<Ordinal,T> &serializer
59 ,const Ordinal count, T*const buffer[]
60 );
66 char* getCharBuffer() const;
68 Ordinal getBytes() const;
69private:
71 Ordinal count_;
72 T*const *buffer_;
74 // Not defined and not to be called
78};
79
83template <typename Ordinal, typename T>
85public:
88 const Serializer<Ordinal,T> &serializer
89 ,const Ordinal count, const T*const buffer[]
90 );
96 const char* getCharBuffer() const;
98 Ordinal getBytes() const;
99private:
101 Ordinal count_;
102 const T*const *buffer_;
103 Ordinal bytes_;
105 // Not defined and not to be called
109};
110
115template <typename Ordinal, typename T>
117public:
120 const Serializer<Ordinal,T> &serializer
121 ,const Ordinal bytes, char charBuffer[]
122 );
128 T*const* getBuffer() const;
130 Ordinal getCount() const;
131private:
135 Ordinal bytes_;
139 // Not defined and not to be called
143};
144
149template <typename Ordinal, typename T>
151public:
154 const Serializer<Ordinal,T> &serializer
155 ,const Ordinal bytes, const char charBuffer[]
156 );
162 const T*const* getBuffer() const;
164 Ordinal getCount() const;
165private:
169 Ordinal bytes_;
170 const char *charBuffer_;
173 // Not defined and not to be called
177};
178
179// /////////////////////////////////////
180// Template implementations
181
182//
183// ReferenceTypeSerializationBuffer
184//
185
186template <typename Ordinal, typename T>
188 const Serializer<Ordinal,T> &serializer
189 ,const Ordinal count, T*const buffer[]
190 )
191 :serializer_(serializer), count_(count), buffer_(buffer)
192{
193 const Ordinal bytes = serializer_.getBufferSize(count_);
194 charBuffer_.resize(bytes);
195 serializer_.serialize(count_,buffer_,bytes,&charBuffer_[0]);
196}
197
198template <typename Ordinal, typename T>
200{
201 serializer_.deserialize(charBuffer_.size(),&charBuffer_[0],count_,buffer_);
202}
203
204template <typename Ordinal, typename T>
206{
208 return &(const_cast<this_ptr_t>(this)->charBuffer_)[0];
209 // The above const_cast is a better alternative to declaring charBuffer_ to
210 // be mutable, in my opinion.
211}
212
213template <typename Ordinal, typename T>
215{
216 return charBuffer_.size();
217}
218
219//
220// ConstReferenceTypeSerializationBuffer
221//
222
223template <typename Ordinal, typename T>
225 const Serializer<Ordinal,T> &serializer
226 ,const Ordinal count, const T*const buffer[]
227 ):
228 serializer_(serializer),
229 count_(count),
230 buffer_(buffer),
231 bytes_(0)
232{
233 const Ordinal bytes = serializer_.getBufferSize(count_);
234 charBuffer_.resize(bytes);
235 serializer_.serialize(count_,buffer_,bytes,&charBuffer_[0]);
236}
237
238template <typename Ordinal, typename T>
240{
241 // No need to copy back from the char[] buffer!
242}
243
244template <typename Ordinal, typename T>
246{
247 return &charBuffer_[0];
248}
249
250template <typename Ordinal, typename T>
252{
253 return charBuffer_.size();
254}
255
256//
257// ReferenceTypeDeserializationBuffer
258//
259
260template <typename Ordinal, typename T>
262 const Serializer<Ordinal,T> &serializer
263 ,const Ordinal bytes, char charBuffer[]
264 )
265 :serializer_(serializer),bytes_(bytes),charBuffer_(charBuffer)
266{
267 const Ordinal extent = serializer_.getBufferSize(1);
268 const Ordinal count = bytes_ / extent;
269#ifdef TEUCHOS_DEBUG
270 TEUCHOS_TEST_FOR_EXCEPT( !( bytes_ % extent == 0 ) );
271#endif
272 buffer_ptr_.resize(count);
273 buffer_.resize(count);
274 for( int i = 0; i < count; ++i ) {
275 buffer_ptr_[i] = serializer_.createObj();
276 buffer_[i] = &*buffer_ptr_[i];
277 }
278 serializer_.deserialize(
279 bytes_,charBuffer_,count,&buffer_[0]
280 );
281}
282
283template <typename Ordinal, typename T>
285{
286 serializer_.serialize(
287 buffer_.size(),&buffer_[0],bytes_,charBuffer_
288 );
289}
290
291template <typename Ordinal, typename T>
293{
295 return &(const_cast<this_ptr_t>(this)->buffer_)[0];
296 // The above const_cast is a better alternative to declaring buffer_ to be
297 // mutable, in my opinion.
298}
299
300template <typename Ordinal, typename T>
302{
303 return buffer_.size();
304}
305
306//
307// ConstReferenceTypeDeserializationBuffer
308//
309
310template <typename Ordinal, typename T>
312 const Serializer<Ordinal,T> &serializer
313 ,const Ordinal bytes, const char charBuffer[]
314 )
315 :serializer_(serializer),bytes_(bytes),charBuffer_(charBuffer)
316{
317 const Ordinal extent = serializer_.getBufferSize(1);
318 const Ordinal count = bytes_ / extent;
319#ifdef TEUCHOS_DEBUG
320 TEUCHOS_TEST_FOR_EXCEPT( !( bytes_ % extent == 0 ) );
321#endif
322 buffer_ptr_.resize(count);
323 buffer_.resize(count);
324 for( int i = 0; i < count; ++i ) {
325 buffer_ptr_[i] = serializer_.createObj();
326 buffer_[i] = &*buffer_ptr_[i];
327 }
328 serializer_.deserialize(
329 bytes_,charBuffer_,count,&buffer_[0]
330 );
331}
332
333template <typename Ordinal, typename T>
335{
336 // We don't need to serialized back into charBuffer_[] since it is constant!
337}
338
339template <typename Ordinal, typename T>
341{
342 return &buffer_[0];
343}
344
345template <typename Ordinal, typename T>
347{
348 return buffer_.size();
349}
350
351} // namespace Teuchos
352
353#endif // TEUCHOS_SERIALIZER_HELPERS_HPP
Templated array class derived from the STL std::vector.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes.
void resize(size_type new_size, const value_type &x=value_type())
Encapsulate how an array of onst objects with reference sematics is deserialized from a char[] array ...
ConstReferenceTypeDeserializationBuffer & operator=(const ConstReferenceTypeDeserializationBuffer &)
~ConstReferenceTypeDeserializationBuffer()
Reserialize back to the char[] buffer from the internal T*[] buffer.
ConstReferenceTypeDeserializationBuffer(const ConstReferenceTypeDeserializationBuffer &)
Encapsulate how an array of const objects with reference sematics is serialized into a char[] array.
ConstReferenceTypeSerializationBuffer & operator=(const ConstReferenceTypeSerializationBuffer &)
~ConstReferenceTypeSerializationBuffer()
Free the internal char[] buffer (no data to be written back).
ConstReferenceTypeSerializationBuffer(const ConstReferenceTypeSerializationBuffer &)
Encapsulate how an array of non-const objects with reference sematics is deserialized from a char[] a...
ReferenceTypeDeserializationBuffer(const ReferenceTypeDeserializationBuffer &)
~ReferenceTypeDeserializationBuffer()
Reserialize back to the char[] buffer from the internal T*[] buffer.
ReferenceTypeDeserializationBuffer & operator=(const ReferenceTypeDeserializationBuffer &)
Encapsulate how an array of non-const objects with reference sematics is serialized into a char[] arr...
ReferenceTypeSerializationBuffer(const ReferenceTypeSerializationBuffer &)
~ReferenceTypeSerializationBuffer()
Deserialize from the interal char[] buffer back to the original T*[] buffer.
ReferenceTypeSerializationBuffer & operator=(const ReferenceTypeSerializationBuffer &)
Strategy interface for the indirect serializing and deserializing objects of a given type handled usi...
#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.