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 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TEUCHOS_SERIALIZER_HELPERS_HPP
11 #define TEUCHOS_SERIALIZER_HELPERS_HPP
12 
13 #include "Teuchos_Serializer.hpp"
14 #include "Teuchos_Array.hpp"
15 
16 namespace Teuchos {
17 
21 template <typename Ordinal, typename T>
23 public:
26  const Serializer<Ordinal,T> &serializer
27  ,const Ordinal count, T*const buffer[]
28  );
34  char* getCharBuffer() const;
36  Ordinal getBytes() const;
37 private:
38  const Serializer<Ordinal,T> &serializer_;
39  Ordinal count_;
40  T*const *buffer_;
41  Array<char> charBuffer_;
42  // Not defined and not to be called
46 };
47 
51 template <typename Ordinal, typename T>
53 public:
56  const Serializer<Ordinal,T> &serializer
57  ,const Ordinal count, const T*const buffer[]
58  );
64  const char* getCharBuffer() const;
66  Ordinal getBytes() const;
67 private:
68  const Serializer<Ordinal,T> &serializer_;
69  Ordinal count_;
70  const T*const *buffer_;
71  Ordinal bytes_;
72  Array<char> charBuffer_;
73  // Not defined and not to be called
77 };
78 
83 template <typename Ordinal, typename T>
85 public:
88  const Serializer<Ordinal,T> &serializer
89  ,const Ordinal bytes, char charBuffer[]
90  );
96  T*const* getBuffer() const;
98  Ordinal getCount() const;
99 private:
100  typedef Array<RCP<T> > buffer_ptr_t;
101  typedef Array<T*> buffer_t;
102  const Serializer<Ordinal,T> &serializer_;
103  Ordinal bytes_;
104  char *charBuffer_;
105  buffer_ptr_t buffer_ptr_;
106  buffer_t buffer_;
107  // Not defined and not to be called
111 };
112 
117 template <typename Ordinal, typename T>
119 public:
122  const Serializer<Ordinal,T> &serializer
123  ,const Ordinal bytes, const char charBuffer[]
124  );
130  const T*const* getBuffer() const;
132  Ordinal getCount() const;
133 private:
134  typedef Array<RCP<T> > buffer_ptr_t;
135  typedef Array<T*> buffer_t;
136  const Serializer<Ordinal,T> &serializer_;
137  Ordinal bytes_;
138  const char *charBuffer_;
139  buffer_ptr_t buffer_ptr_;
140  buffer_t buffer_;
141  // Not defined and not to be called
145 };
146 
147 // /////////////////////////////////////
148 // Template implementations
149 
150 //
151 // ReferenceTypeSerializationBuffer
152 //
153 
154 template <typename Ordinal, typename T>
156  const Serializer<Ordinal,T> &serializer
157  ,const Ordinal count, T*const buffer[]
158  )
159  :serializer_(serializer), count_(count), buffer_(buffer)
160 {
161  const Ordinal bytes = serializer_.getBufferSize(count_);
162  charBuffer_.resize(bytes);
163  serializer_.serialize(count_,buffer_,bytes,&charBuffer_[0]);
164 }
165 
166 template <typename Ordinal, typename T>
168 {
169  serializer_.deserialize(charBuffer_.size(),&charBuffer_[0],count_,buffer_);
170 }
171 
172 template <typename Ordinal, typename T>
174 {
176  return &(const_cast<this_ptr_t>(this)->charBuffer_)[0];
177  // The above const_cast is a better alternative to declaring charBuffer_ to
178  // be mutable, in my opinion.
179 }
180 
181 template <typename Ordinal, typename T>
183 {
184  return charBuffer_.size();
185 }
186 
187 //
188 // ConstReferenceTypeSerializationBuffer
189 //
190 
191 template <typename Ordinal, typename T>
193  const Serializer<Ordinal,T> &serializer
194  ,const Ordinal count, const T*const buffer[]
195  ):
196  serializer_(serializer),
197  count_(count),
198  buffer_(buffer),
199  bytes_(0)
200 {
201  const Ordinal bytes = serializer_.getBufferSize(count_);
202  charBuffer_.resize(bytes);
203  serializer_.serialize(count_,buffer_,bytes,&charBuffer_[0]);
204 }
205 
206 template <typename Ordinal, typename T>
208 {
209  // No need to copy back from the char[] buffer!
210 }
211 
212 template <typename Ordinal, typename T>
214 {
215  return &charBuffer_[0];
216 }
217 
218 template <typename Ordinal, typename T>
220 {
221  return charBuffer_.size();
222 }
223 
224 //
225 // ReferenceTypeDeserializationBuffer
226 //
227 
228 template <typename Ordinal, typename T>
230  const Serializer<Ordinal,T> &serializer
231  ,const Ordinal bytes, char charBuffer[]
232  )
233  :serializer_(serializer),bytes_(bytes),charBuffer_(charBuffer)
234 {
235  const Ordinal extent = serializer_.getBufferSize(1);
236  const Ordinal count = bytes_ / extent;
237 #ifdef TEUCHOS_DEBUG
238  TEUCHOS_TEST_FOR_EXCEPT( !( bytes_ % extent == 0 ) );
239 #endif
240  buffer_ptr_.resize(count);
241  buffer_.resize(count);
242  for( int i = 0; i < count; ++i ) {
243  buffer_ptr_[i] = serializer_.createObj();
244  buffer_[i] = &*buffer_ptr_[i];
245  }
246  serializer_.deserialize(
247  bytes_,charBuffer_,count,&buffer_[0]
248  );
249 }
250 
251 template <typename Ordinal, typename T>
253 {
254  serializer_.serialize(
255  buffer_.size(),&buffer_[0],bytes_,charBuffer_
256  );
257 }
258 
259 template <typename Ordinal, typename T>
261 {
263  return &(const_cast<this_ptr_t>(this)->buffer_)[0];
264  // The above const_cast is a better alternative to declaring buffer_ to be
265  // mutable, in my opinion.
266 }
267 
268 template <typename Ordinal, typename T>
270 {
271  return buffer_.size();
272 }
273 
274 //
275 // ConstReferenceTypeDeserializationBuffer
276 //
277 
278 template <typename Ordinal, typename T>
280  const Serializer<Ordinal,T> &serializer
281  ,const Ordinal bytes, const char charBuffer[]
282  )
283  :serializer_(serializer),bytes_(bytes),charBuffer_(charBuffer)
284 {
285  const Ordinal extent = serializer_.getBufferSize(1);
286  const Ordinal count = bytes_ / extent;
287 #ifdef TEUCHOS_DEBUG
288  TEUCHOS_TEST_FOR_EXCEPT( !( bytes_ % extent == 0 ) );
289 #endif
290  buffer_ptr_.resize(count);
291  buffer_.resize(count);
292  for( int i = 0; i < count; ++i ) {
293  buffer_ptr_[i] = serializer_.createObj();
294  buffer_[i] = &*buffer_ptr_[i];
295  }
296  serializer_.deserialize(
297  bytes_,charBuffer_,count,&buffer_[0]
298  );
299 }
300 
301 template <typename Ordinal, typename T>
303 {
304  // We don't need to serialized back into charBuffer_[] since it is constant!
305 }
306 
307 template <typename Ordinal, typename T>
309 {
310  return &buffer_[0];
311 }
312 
313 template <typename Ordinal, typename T>
315 {
316  return buffer_.size();
317 }
318 
319 } // namespace Teuchos
320 
321 #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.