Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
45 #include "Teuchos_Serializer.hpp"
46 #include "Teuchos_Array.hpp"
47 
48 namespace Teuchos {
49 
53 template <typename Ordinal, typename T>
55 public:
58  const Serializer<Ordinal,T> &serializer
59  ,const Ordinal count, T*const buffer[]
60  );
66  char* getCharBuffer() const;
68  Ordinal getBytes() const;
69 private:
71  Ordinal count_;
72  T*const *buffer_;
74  // Not defined and not to be called
78 };
79 
83 template <typename Ordinal, typename T>
85 public:
88  const Serializer<Ordinal,T> &serializer
89  ,const Ordinal count, const T*const buffer[]
90  );
96  const char* getCharBuffer() const;
98  Ordinal getBytes() const;
99 private:
101  Ordinal count_;
102  const T*const *buffer_;
103  Ordinal bytes_;
105  // Not defined and not to be called
109 };
110 
115 template <typename Ordinal, typename T>
117 public:
120  const Serializer<Ordinal,T> &serializer
121  ,const Ordinal bytes, char charBuffer[]
122  );
128  T*const* getBuffer() const;
130  Ordinal getCount() const;
131 private:
135  Ordinal bytes_;
136  char *charBuffer_;
139  // Not defined and not to be called
143 };
144 
149 template <typename Ordinal, typename T>
151 public:
154  const Serializer<Ordinal,T> &serializer
155  ,const Ordinal bytes, const char charBuffer[]
156  );
162  const T*const* getBuffer() const;
164  Ordinal getCount() const;
165 private:
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 
186 template <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 
198 template <typename Ordinal, typename T>
200 {
201  serializer_.deserialize(charBuffer_.size(),&charBuffer_[0],count_,buffer_);
202 }
203 
204 template <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 
213 template <typename Ordinal, typename T>
215 {
216  return charBuffer_.size();
217 }
218 
219 //
220 // ConstReferenceTypeSerializationBuffer
221 //
222 
223 template <typename Ordinal, typename T>
225  const Serializer<Ordinal,T> &serializer
226  ,const Ordinal count, const T*const buffer[]
227  )
228  :serializer_(serializer), count_(count), buffer_(buffer)
229 {
230  const Ordinal bytes = serializer_.getBufferSize(count_);
231  charBuffer_.resize(bytes);
232  serializer_.serialize(count_,buffer_,bytes,&charBuffer_[0]);
233 }
234 
235 template <typename Ordinal, typename T>
237 {
238  // No need to copy back from the char[] buffer!
239 }
240 
241 template <typename Ordinal, typename T>
243 {
244  return &charBuffer_[0];
245 }
246 
247 template <typename Ordinal, typename T>
249 {
250  return charBuffer_.size();
251 }
252 
253 //
254 // ReferenceTypeDeserializationBuffer
255 //
256 
257 template <typename Ordinal, typename T>
259  const Serializer<Ordinal,T> &serializer
260  ,const Ordinal bytes, char charBuffer[]
261  )
262  :serializer_(serializer),bytes_(bytes),charBuffer_(charBuffer)
263 {
264  const Ordinal extent = serializer_.getBufferSize(1);
265  const Ordinal count = bytes_ / extent;
266 #ifdef TEUCHOS_DEBUG
267  TEUCHOS_TEST_FOR_EXCEPT( !( bytes_ % extent == 0 ) );
268 #endif
269  buffer_ptr_.resize(count);
270  buffer_.resize(count);
271  for( int i = 0; i < count; ++i ) {
272  buffer_ptr_[i] = serializer_.createObj();
273  buffer_[i] = &*buffer_ptr_[i];
274  }
275  serializer_.deserialize(
276  bytes_,charBuffer_,count,&buffer_[0]
277  );
278 }
279 
280 template <typename Ordinal, typename T>
282 {
283  serializer_.serialize(
284  buffer_.size(),&buffer_[0],bytes_,charBuffer_
285  );
286 }
287 
288 template <typename Ordinal, typename T>
290 {
292  return &(const_cast<this_ptr_t>(this)->buffer_)[0];
293  // The above const_cast is a better alternative to declaring buffer_ to be
294  // mutable, in my opinion.
295 }
296 
297 template <typename Ordinal, typename T>
299 {
300  return buffer_.size();
301 }
302 
303 //
304 // ConstReferenceTypeDeserializationBuffer
305 //
306 
307 template <typename Ordinal, typename T>
309  const Serializer<Ordinal,T> &serializer
310  ,const Ordinal bytes, const char charBuffer[]
311  )
312  :serializer_(serializer),bytes_(bytes),charBuffer_(charBuffer)
313 {
314  const Ordinal extent = serializer_.getBufferSize(1);
315  const Ordinal count = bytes_ / extent;
316 #ifdef TEUCHOS_DEBUG
317  TEUCHOS_TEST_FOR_EXCEPT( !( bytes_ % extent == 0 ) );
318 #endif
319  buffer_ptr_.resize(count);
320  buffer_.resize(count);
321  for( int i = 0; i < count; ++i ) {
322  buffer_ptr_[i] = serializer_.createObj();
323  buffer_[i] = &*buffer_ptr_[i];
324  }
325  serializer_.deserialize(
326  bytes_,charBuffer_,count,&buffer_[0]
327  );
328 }
329 
330 template <typename Ordinal, typename T>
332 {
333  // We don't need to serialized back into charBuffer_[] since it is constant!
334 }
335 
336 template <typename Ordinal, typename T>
338 {
339  return &buffer_[0];
340 }
341 
342 template <typename Ordinal, typename T>
344 {
345  return buffer_.size();
346 }
347 
348 } // namespace Teuchos
349 
350 #endif // TEUCHOS_SERIALIZER_HELPERS_HPP
Strategy interface for the indirect serializing and deserializing objects of a given type handled usi...
Encapsulate how an array of non-const objects with reference sematics is deserialized from a char[] a...
Encapsulate how an array of non-const objects with reference sematics is serialized into a char[] arr...
Encapsulate how an array of onst objects with reference sematics is deserialized from a char[] array ...
ReferenceTypeSerializationBuffer & operator=(const ReferenceTypeSerializationBuffer &)
~ReferenceTypeDeserializationBuffer()
Reserialize back to the char[] buffer from the internal T*[] buffer.
ConstReferenceTypeDeserializationBuffer & operator=(const ConstReferenceTypeDeserializationBuffer &)
Encapsulate how an array of const objects with reference sematics is serialized into a char[] array...
~ConstReferenceTypeDeserializationBuffer()
Reserialize back to the char[] buffer from the internal T*[] buffer.
~ConstReferenceTypeSerializationBuffer()
Free the internal char[] buffer (no data to be written back).
void resize(size_type new_size, const value_type &x=value_type())
ConstReferenceTypeSerializationBuffer & operator=(const ConstReferenceTypeSerializationBuffer &)
Templated array class derived from the STL std::vector.
ReferenceTypeDeserializationBuffer & operator=(const ReferenceTypeDeserializationBuffer &)
#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...
~ReferenceTypeSerializationBuffer()
Deserialize from the interal char[] buffer back to the original T*[] buffer.