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