Goby3  3.1.5
2024.05.14
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
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:119
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition: group.h:59
static constexpr std::uint32_t maximum_valid_group
Definition: group.h:66
Implements the forwarder concept for the interprocess layer.
Definition: interprocess.h:299
A transporter for the interthread layer.
Definition: interthread.h:59
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
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:192
The global namespace for the Goby project.