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