Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_SerializerHelpers.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_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:
70  const Serializer<Ordinal,T> &serializer_;
71  Ordinal count_;
72  T*const *buffer_;
73  Array<char> charBuffer_;
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:
100  const Serializer<Ordinal,T> &serializer_;
101  Ordinal count_;
102  const T*const *buffer_;
103  Ordinal bytes_;
104  Array<char> charBuffer_;
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:
132  typedef Array<RCP<T> > buffer_ptr_t;
133  typedef Array<T*> buffer_t;
134  const Serializer<Ordinal,T> &serializer_;
135  Ordinal bytes_;
136  char *charBuffer_;
137  buffer_ptr_t buffer_ptr_;
138  buffer_t buffer_;
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:
166  typedef Array<RCP<T> > buffer_ptr_t;
167  typedef Array<T*> buffer_t;
168  const Serializer<Ordinal,T> &serializer_;
169  Ordinal bytes_;
170  const char *charBuffer_;
171  buffer_ptr_t buffer_ptr_;
172  buffer_t buffer_;
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),
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 
238 template <typename Ordinal, typename T>
240 {
241  // No need to copy back from the char[] buffer!
242 }
243 
244 template <typename Ordinal, typename T>
246 {
247  return &charBuffer_[0];
248 }
249 
250 template <typename Ordinal, typename T>
252 {
253  return charBuffer_.size();
254 }
255 
256 //
257 // ReferenceTypeDeserializationBuffer
258 //
259 
260 template <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 
283 template <typename Ordinal, typename T>
285 {
286  serializer_.serialize(
287  buffer_.size(),&buffer_[0],bytes_,charBuffer_
288  );
289 }
290 
291 template <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 
300 template <typename Ordinal, typename T>
302 {
303  return buffer_.size();
304 }
305 
306 //
307 // ConstReferenceTypeDeserializationBuffer
308 //
309 
310 template <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 
333 template <typename Ordinal, typename T>
335 {
336  // We don't need to serialized back into charBuffer_[] since it is constant!
337 }
338 
339 template <typename Ordinal, typename T>
341 {
342  return &buffer_[0];
343 }
344 
345 template <typename Ordinal, typename T>
347 {
348  return buffer_.size();
349 }
350 
351 } // namespace Teuchos
352 
353 #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 ...
~ReferenceTypeDeserializationBuffer()
Reserialize back to the char[] buffer from the internal T*[] buffer.
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())
Templated array class derived from the STL std::vector.
#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.