Goby3 3.7.0
2026.11.12
Loading...
Searching...
No Matches
publisher.h
Go to the documentation of this file.
1// Copyright 2019-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_TRANSPORT_PUBLISHER_H
25#define GOBY_MIDDLEWARE_TRANSPORT_PUBLISHER_H
26
27#include <functional>
28
33
34namespace goby
35{
36namespace middleware
37{
39template <typename Data> class Publisher
40{
41 public:
42 using set_group_func_type = std::function<void(Data&, const Group&)>;
44 std::function<void(const Data&, const intervehicle::protobuf::AckData&)>;
46 std::function<void(const Data&, const intervehicle::protobuf::ExpireData&)>;
47
59 : cfg_(cfg),
60 set_group_func_(set_group_func),
61 acked_func_(acked_func),
62 expired_func_(expired_func)
63 {
64 // if an ack function is set, we assume we require an ack
65 if (acked_func_ && !cfg_.intervehicle().buffer().has_ack_required())
66 {
68 }
69 }
70
77
97
99 explicit Publisher(Options options)
100 : Publisher(options.cfg, options.set_group_func, options.acked_func, options.expired_func)
101 {
102 group_in_message_ = options.group_in_message;
103 }
104
106
109
111 void set_group(Data& data, const Group& group) const
112 {
113 if (set_group_func_)
114 set_group_func_(data, group);
115 };
116
118 acked_func_type acked_func() const { return acked_func_; }
120 expired_func_type expired_func() const { return expired_func_; }
121
122 bool has_set_group_func() const { return bool(set_group_func_); }
123
125 bool group_in_message() const { return group_in_message_; }
126
127 private:
129 set_group_func_type set_group_func_;
130 acked_func_type acked_func_;
131 expired_func_type expired_func_;
132 bool group_in_message_{true};
133};
134
135} // namespace middleware
136} // namespace goby
137
138#endif
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition group.h:60
Class that holds additional metadata and callback functions related to a publication (and is optional...
Definition publisher.h:40
const goby::middleware::protobuf::TransporterConfig & cfg() const
Returns the metadata configuration.
Definition publisher.h:108
Publisher(Options options)
Construct a Publisher from Options.
Definition publisher.h:99
std::function< void(const Data &, const intervehicle::protobuf::AckData &)> acked_func_type
Definition publisher.h:44
acked_func_type acked_func() const
Returns the acked data callback (or an empty function if none is set)
Definition publisher.h:118
bool has_set_group_func() const
Definition publisher.h:122
bool group_in_message() const
False if the group isn't stored in the message (see Options::group_in_message)
Definition publisher.h:125
void set_group(Data &data, const Group &group) const
Sets the group using the set_group_func. Only intended to be called by the various transporters.
Definition publisher.h:111
std::function< void(Data &, const Group &)> set_group_func_type
Definition publisher.h:42
Publisher(const goby::middleware::protobuf::TransporterConfig &cfg, acked_func_type acked_func, expired_func_type expired_func=expired_func_type())
Construct a Publisher but without the set_group_func callback.
Definition publisher.h:72
std::function< void(const Data &, const intervehicle::protobuf::ExpireData &)> expired_func_type
Definition publisher.h:46
Publisher(const goby::middleware::protobuf::TransporterConfig &cfg=goby::middleware::protobuf::TransporterConfig(), set_group_func_type set_group_func=set_group_func_type(), acked_func_type acked_func=acked_func_type(), expired_func_type expired_func=expired_func_type())
Construct a Publisher with all available metadata and callbacks.
Definition publisher.h:54
expired_func_type expired_func() const
Returns the expired data callback (or an empty function if none is set)
Definition publisher.h:120
const ::goby::acomms::protobuf::DynamicBufferConfig & buffer() const
const ::goby::middleware::intervehicle::protobuf::TransporterConfig & intervehicle() const
::goby::middleware::intervehicle::protobuf::TransporterConfig * mutable_intervehicle()
goby::util::logger::GroupSetter group(std::string n)
The global namespace for the Goby project.
Everything a Publisher can be given, named, so that it can be initialized with C++20 designated initi...
Definition publisher.h:84
expired_func_type expired_func
Called when data expire without reaching any subscribers.
Definition publisher.h:92
acked_func_type acked_func
Called when data are acknowledged by subscribers (sets ack_required)
Definition publisher.h:90
goby::middleware::protobuf::TransporterConfig cfg
Additional metadata for all publish calls for which this Publisher is provided.
Definition publisher.h:86
set_group_func_type set_group_func
Sets the group in the message (see the full constructor)
Definition publisher.h:88