NETSIM  1.0.1
Hardware-in-the-loopacousticnetworksimulator
boost_serialization.h
Go to the documentation of this file.
1 // Copyright 2016-2021:
2 // GobySoft, LLC (2013-)
3 // Community contributors (see AUTHORS file)
4 // File authors:
5 // Toby Schneider <toby@gobysoft.org>
6 //
7 //
8 // This file is part of the Goby Underwater Autonomy Project Libraries
9 // ("The Goby Libraries").
10 //
11 // The Goby Libraries are free software: you can redistribute them and/or modify
12 // them under the terms of the GNU Lesser General Public License as published by
13 // the Free Software Foundation, either version 2.1 of the License, or
14 // (at your option) any later version.
15 //
16 // The Goby Libraries are distributed in the hope that they will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public License
22 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
23 
24 #ifndef GOBY_MIDDLEWARE_MARSHALLING_BOOST_SERIALIZATION_H
25 #define GOBY_MIDDLEWARE_MARSHALLING_BOOST_SERIALIZATION_H
26 
27 #include <boost/archive/binary_iarchive.hpp>
28 #include <boost/archive/binary_oarchive.hpp>
29 #include <boost/iostreams/device/back_inserter.hpp>
30 #include <boost/iostreams/stream.hpp>
31 #include <boost/serialization/shared_ptr.hpp>
32 #include <boost/serialization/string.hpp>
33 #include <boost/serialization/vector.hpp>
34 
35 #include <vector>
36 
37 #include "goby/middleware/marshalling/interface.h"
38 
39 #include "netsim/core/common.h"
40 
41 namespace goby
42 {
43 namespace middleware
44 {
45 
46 // update when moved into Goby
47 constexpr int BOOST_SERIALIZATION_SCHEME = 8;
48 
49 template <typename T, class Enable = void> constexpr const char* boost_serialization_type_name()
50 {
51  return T::goby_boost_serialization_type;
52 }
53 
54 template <typename T> struct SerializerParserHelper<T, BOOST_SERIALIZATION_SCHEME>
55 {
56  static std::vector<char> serialize(const T& msg)
57  {
58  std::vector<char> data;
59  boost::iostreams::back_insert_device<std::vector<char>> inserter(data);
60  boost::iostreams::stream<boost::iostreams::back_insert_device<std::vector<char>>> stream(
61  inserter);
62  boost::archive::binary_oarchive oa(stream);
63 
64  oa << msg;
65  stream.flush();
66  return data;
67  }
68 
69  static std::string type_name(const T& t = T()) { return boost_serialization_type_name<T>(); }
70 
71  template <typename CharIterator>
72  static std::shared_ptr<T> parse(CharIterator bytes_begin, CharIterator bytes_end,
73  CharIterator& actual_end, const std::string& type = type_name())
74  {
75  std::shared_ptr<T> t = std::make_shared<T>();
76 
77  boost::iostreams::basic_array_source<char> device(&(*bytes_begin), &(*bytes_end));
78  boost::iostreams::stream<boost::iostreams::basic_array_source<char>> stream(device);
79  boost::archive::binary_iarchive ia(stream);
80 
81  ia >> *t;
82  actual_end = bytes_end;
83 
84  return t;
85  }
86 };
87 
88 template <typename T,
89  typename std::enable_if<T::goby_boost_serialization_type != nullptr>::type* = nullptr>
90 constexpr int scheme()
91 {
93 }
94 
95 } // namespace middleware
96 } // namespace goby
97 
98 // Serialization for TaggedAudioBuffer
99 
100 namespace boost
101 {
102 namespace serialization
103 {
104 
105 template <class Archive>
106 void serialize(Archive& ar, netsim::AudioBuffer& b, const unsigned int version)
107 {
108  ar& b.buffer_start_time;
109  ar& b.jack_frame_time;
110  ar& b.samples;
111 }
112 
113 template <class Archive>
114 void serialize(Archive& ar, netsim::TaggedAudioBuffer& t, const unsigned int version)
115 {
116  ar& t.buffer;
117  ar& t.marker;
118  ar& t.packet_id;
119 }
120 
121 } // namespace serialization
122 } // namespace boost
123 
124 namespace goby
125 {
126 namespace middleware
127 {
128 
129 template <> constexpr const char* boost_serialization_type_name<netsim::TaggedAudioBuffer>()
130 {
131  return "TaggedAudioBuffer";
132 }
133 } // namespace middleware
134 } // namespace goby
135 
136 #endif
netsim::AudioBuffer::buffer_start_time
double buffer_start_time
Definition: common.h:49
goby
Definition: boost_serialization.h:41
goby::middleware::SerializerParserHelper< T, BOOST_SERIALIZATION_SCHEME >::parse
static std::shared_ptr< T > parse(CharIterator bytes_begin, CharIterator bytes_end, CharIterator &actual_end, const std::string &type=type_name())
Definition: boost_serialization.h:72
boost
Definition: boost_serialization.h:100
netsim::TaggedAudioBuffer::marker
Marker marker
Definition: common.h:66
netsim::TaggedAudioBuffer::packet_id
int packet_id
Definition: common.h:67
goby::middleware::BOOST_SERIALIZATION_SCHEME
constexpr int BOOST_SERIALIZATION_SCHEME
Definition: boost_serialization.h:47
boost::serialization::serialize
void serialize(Archive &ar, netsim::AudioBuffer &b, const unsigned int version)
Definition: boost_serialization.h:106
netsim::TaggedAudioBuffer::buffer
std::shared_ptr< const AudioBuffer > buffer
Definition: common.h:57
goby::middleware::boost_serialization_type_name
constexpr const char * boost_serialization_type_name()
Definition: boost_serialization.h:49
netsim::AudioBuffer::jack_frame_time
jack_nframes_t jack_frame_time
Definition: common.h:50
netsim::TaggedAudioBuffer
Definition: common.h:55
goby::middleware::SerializerParserHelper< T, BOOST_SERIALIZATION_SCHEME >::serialize
static std::vector< char > serialize(const T &msg)
Definition: boost_serialization.h:56
common.h
netsim::AudioBuffer
Definition: common.h:42
goby::middleware::SerializerParserHelper< T, BOOST_SERIALIZATION_SCHEME >::type_name
static std::string type_name(const T &t=T())
Definition: boost_serialization.h:69
goby::middleware::scheme
constexpr int scheme()
Definition: boost_serialization.h:90
netsim::AudioBuffer::samples
std::vector< netsim::sample_t > samples
Definition: common.h:52