21 #ifndef __SSRC_WISP_SERIALIZATION_H
22 #define __SSRC_WISP_SERIALIZATION_H
25 #include <ssrc/spread/detail/ByteBuffer.h>
29 #include <boost/iostreams/stream.hpp>
30 #include <boost/iostreams/device/array.hpp>
32 #include <boost/archive/binary_iarchive.hpp>
33 #include <boost/archive/binary_oarchive.hpp>
35 #include <boost/serialization/split_free.hpp>
36 #include <boost/serialization/binary_object.hpp>
43 #define __BEGIN_BOOST_SERIALIZATION \
45 namespace serialization {
51 #define __END_BOOST_SERIALIZATION \
57 using NS_SSRC_SPREAD::detail::ByteBuffer;
67 template<
typename Ch,
typename mode_type = boost::iostreams::seekable>
71 typedef std::pair<char_type*, char_type*>
pair_type;
80 char_type *_begin, *_end;
87 _begin(begin), _end(end)
91 _begin(begin), _end(begin + size)
94 void init(char_type* begin, char_type* end) {
99 void init(char_type* begin, std::size_t size) {
105 _end = _begin + size;
123 template<
typename DeviceAdapter>
129 boost::iostreams::detail::direct_streambuf<DeviceAdapter,
130 typename super::traits_type>
133 void open(
const DeviceAdapter & device_adapter) {
134 direct_streambuf::open(device_adapter, 0, 0);
149 template<
typename Archive>
181 unsigned int pack(
const T & obj, ByteBuffer & buffer,
182 const unsigned int archive_flags =
183 boost::archive::no_header | boost::archive::no_codecvt |
184 boost::archive::no_tracking)
185 SSRC_DECL_THROW(
boost::archive::archive_exception,
std::ios_base::failure)
187 unsigned int offset, bytes_written;
189 _sink.
init(&buffer[0], buffer.capacity());
190 _out_buf.
open(_sink);
191 _out_buf.pubseekoff(buffer.offset(), std::basic_ios<sink_buffer>::beg);
193 output_archive out_ar(_out_buf, archive_flags);
196 offset = _out_buf.pubseekoff(0, std::basic_ios<sink_buffer>::cur);
197 bytes_written = offset - buffer.offset();
198 buffer.resize(offset);
201 return bytes_written;
213 template<
typename Archive>
242 unsigned int unpack(T & obj, ByteBuffer & buffer,
243 const unsigned int archive_flags =
244 boost::archive::no_header | boost::archive::no_codecvt |
245 boost::archive::no_tracking)
246 SSRC_DECL_THROW(
boost::archive::archive_exception,
std::ios_base::failure)
248 unsigned int bytes_read(0);
250 if(buffer.size() > 0) {
253 _source.
init(&buffer[0], buffer.size());
254 _in_buf.
open(_source);
255 _in_buf.pubseekoff(buffer.offset(), std::basic_ios<source_buffer>::beg);
257 input_archive in_ar(_in_buf, archive_flags);
260 offset = _in_buf.pubseekoff(0, std::basic_ios<source_buffer>::cur);
261 bytes_read = offset - buffer.offset();
284 unsigned int unpack(T & obj,
const void *buffer,
unsigned int size,
285 const unsigned int archive_flags =
286 boost::archive::no_header | boost::archive::no_codecvt |
287 boost::archive::no_tracking)
288 SSRC_DECL_THROW(
boost::archive::archive_exception,
std::ios_base::failure)
290 unsigned int bytes_read(0);
293 _source.
init(static_cast<typename detail::char_array_source::char_type *>(const_cast<void *>(buffer)), size);
294 _in_buf.
open(_source);
295 _in_buf.pubseekoff(0, std::basic_ios<source_buffer>::beg);
297 input_archive in_ar(_in_buf, archive_flags);
300 bytes_read = _in_buf.pubseekoff(0, std::basic_ios<source_buffer>::cur);
307 template<
typename P,
typename U>
321 template<
class Archive>
324 const NS_SSRC_SPREAD::detail::ByteBuffer *buffer,
328 ar << buffer->size();
331 template<
class Archive>
334 NS_SSRC_SPREAD::detail::ByteBuffer *buffer,
337 unsigned int capacity;
340 ::new(buffer)NS_SSRC_SPREAD::detail::ByteBuffer(capacity);
343 template<
class Archive>
345 const NS_SSRC_SPREAD::detail::ByteBuffer & buffer,
348 unsigned int size = buffer.size();
353 const binary_object obj(&buffer[0], size);
354 unsigned int offset = buffer.offset();
359 template<
class Archive>
361 NS_SSRC_SPREAD::detail::ByteBuffer & buffer,
371 binary_object obj(&buffer[0], size);
381 BOOST_SERIALIZATION_SPLIT_FREE(NS_SSRC_SPREAD::detail::ByteBuffer)