Goby3 3.4.0
2026.04.13
Loading...
Searching...
No Matches
io_transporters.h
Go to the documentation of this file.
1// Copyright 2021-2026:
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_IO_DETAIL_IO_TRANSPORTERS_H
25#define GOBY_MIDDLEWARE_IO_DETAIL_IO_TRANSPORTERS_H
26
27#include "goby/exception.h"
29
30namespace goby
31{
32namespace middleware
33{
34class Group;
35class InterThreadTransporter;
36template <typename InnerTransporter, typename ImplementationTag> class InterProcessForwarder;
37namespace io
38{
39enum class PubSubLayer
40{
43};
44
45namespace detail
46{
47enum class Direction
48{
49 PUBLISH,
51};
52
53// Direction template parameter required to avoid diamond inheritance problem with IOThread
54template <class Derived, Direction direction, typename ImplementationTag, PubSubLayer layer>
56{
57};
58
59template <class Derived, Direction direction, typename ImplementationTag>
60struct IOTransporterByLayer<Derived, direction, ImplementationTag, PubSubLayer::INTERTHREAD>
61{
63 Transporter& io_transporter() { return static_cast<Derived*>(this)->interthread(); }
64};
65
66template <class Derived, Direction direction, typename ImplementationTag>
67struct IOTransporterByLayer<Derived, direction, ImplementationTag, PubSubLayer::INTERPROCESS>
68{
70 Transporter& io_transporter() { return static_cast<Derived*>(this)->interprocess(); }
71};
72
73template <class Derived, const goby::middleware::Group& line_in_group, PubSubLayer layer,
74 typename ImplementationTag, bool use_indexed_group>
76{
77};
78
79template <class Derived, const goby::middleware::Group& line_in_group, PubSubLayer layer,
80 typename ImplementationTag>
81struct IOPublishTransporter<Derived, line_in_group, layer, ImplementationTag, false>
82 : IOTransporterByLayer<Derived, Direction::PUBLISH, ImplementationTag, layer>
83{
85 template <typename Data,
87 Data, typename IOTransporterByLayer<Derived, Direction::PUBLISH,
88 ImplementationTag, layer>::Transporter>()>
89 void publish_in(std::shared_ptr<Data> data)
90 {
91 this->io_transporter().template publish<line_in_group, Data, scheme>(data);
92 }
93};
94
95template <class Derived, const goby::middleware::Group& line_in_group, PubSubLayer layer,
96 typename ImplementationTag>
97struct IOPublishTransporter<Derived, line_in_group, layer, ImplementationTag, true>
98 : IOTransporterByLayer<Derived, Direction::PUBLISH, ImplementationTag, layer>
99
100{
102 : in_group_(std::string(line_in_group), index == -1 ? Group::invalid_numeric_group : index)
103 {
104 if (index > Group::maximum_valid_group)
105 throw(goby::Exception("Index must be less than or equal to: " +
106 std::to_string(Group::maximum_valid_group)));
107 }
108 template <typename Data,
110 Data, typename IOTransporterByLayer<Derived, Direction::PUBLISH,
111 ImplementationTag, layer>::Transporter>()>
112 void publish_in(std::shared_ptr<Data> data)
113 {
114 this->io_transporter().template publish_dynamic<Data, scheme>(data, in_group_);
115 }
116
117 private:
118 DynamicGroup in_group_;
119};
120
121template <class Derived, const goby::middleware::Group& line_out_group, PubSubLayer layer,
122 typename ImplementationTag, bool use_indexed_group>
124{
125};
126
127template <class Derived, const goby::middleware::Group& line_out_group, PubSubLayer layer,
128 typename ImplementationTag>
129struct IOSubscribeTransporter<Derived, line_out_group, layer, ImplementationTag, false>
130 : IOTransporterByLayer<Derived, Direction::SUBSCRIBE, ImplementationTag, layer>
131{
133
134 template <typename Data,
136 Data, typename IOTransporterByLayer<Derived, Direction::SUBSCRIBE,
137 ImplementationTag, layer>::Transporter>(),
138 Necessity necessity = Necessity::OPTIONAL>
139 void subscribe_out(std::function<void(std::shared_ptr<const Data>)> f)
140 {
141 this->io_transporter().template subscribe<line_out_group, Data, scheme, necessity>(f);
142 }
143
144 template <typename Data,
146 Data, typename IOTransporterByLayer<Derived, Direction::SUBSCRIBE,
147 ImplementationTag, layer>::Transporter>()>
149 {
150 this->io_transporter().template unsubscribe<line_out_group, Data, scheme>();
151 }
152};
153
154template <class Derived, const goby::middleware::Group& line_out_group, PubSubLayer layer,
155 typename ImplementationTag>
156struct IOSubscribeTransporter<Derived, line_out_group, layer, ImplementationTag, true>
157 : IOTransporterByLayer<Derived, Direction::SUBSCRIBE, ImplementationTag, layer>
158{
160 : out_group_(std::string(line_out_group),
161 index == -1 ? Group::invalid_numeric_group : index)
162 {
163 if (index > Group::maximum_valid_group)
164 throw(goby::Exception("Index must be less than or equal to: " +
165 std::to_string(Group::maximum_valid_group)));
166 }
167
168 template <typename Data,
170 Data, typename IOTransporterByLayer<Derived, Direction::SUBSCRIBE,
171 ImplementationTag, layer>::Transporter>(),
172 Necessity necessity = Necessity::OPTIONAL>
173 void subscribe_out(std::function<void(std::shared_ptr<const Data>)> f)
174 {
175 this->io_transporter().template subscribe_dynamic<Data, scheme>(f, out_group_);
176 }
177
178 template <typename Data,
180 Data, typename IOTransporterByLayer<Derived, Direction::SUBSCRIBE,
181 ImplementationTag, layer>::Transporter>()>
183 {
184 this->io_transporter().template unsubscribe_dynamic<Data, scheme>(out_group_);
185 }
186
187 private:
188 DynamicGroup out_group_;
189};
190
191} // namespace detail
192} // namespace io
193} // namespace middleware
194} // namespace goby
195
196#endif
simple exception class for goby applications
Definition exception.h:35
Implementation of Group for dynamic (run-time) instantiations. Use Group directly for static (compile...
Definition group.h:120
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition group.h:60
static constexpr std::uint32_t maximum_valid_group
Definition group.h:67
A transporter for the interthread layer.
Definition interthread.h:59
detail namespace with internal helper functions
Definition json.hpp:247
constexpr int transporter_scheme()
Helper function for calling a particular transporter's scheme method.
Definition interface.h:147
constexpr int scheme()
Placeholder to provide an interface for the scheme() function family.
Definition cstr.h:65
Necessity
Used to tag subscriptions based on their necessity (e.g. required for correct functioning,...
Definition interface.h:222
middleware::InterProcessForwarder< InnerTransporter, detail::InterProcessTag > InterProcessForwarder
The global namespace for the Goby project.
STL namespace.