Goby3 3.5.1
2026.06.04
Loading...
Searching...
No Matches
group.h
Go to the documentation of this file.
1// Copyright 2017-2026:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6// Copilot <198982749+Copilot@users.noreply.github.com>
7// Ryan Govostes <rgovostes+git@gmail.com>
8//
9//
10// This file is part of the Goby Underwater Autonomy Project Libraries
11// ("The Goby Libraries").
12//
13// The Goby Libraries are free software: you can redistribute them and/or modify
14// them under the terms of the GNU Lesser General Public License as published by
15// the Free Software Foundation, either version 2.1 of the License, or
16// (at your option) any later version.
17//
18// The Goby Libraries are distributed in the hope that they will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU Lesser General Public License for more details.
22//
23// You should have received a copy of the GNU Lesser General Public License
24// along with Goby. If not, see <http://www.gnu.org/licenses/>.
25
26#ifndef GOBY_MIDDLEWARE_GROUP_H
27#define GOBY_MIDDLEWARE_GROUP_H
28
29#include <cstdint>
30#include <limits>
31#include <memory>
32#include <string>
33
34#ifndef __clang__
35#ifdef __GNUC__
36#if __GNUC__ < 7 || (__GNUC__ == 7 && __GNUC_MINOR__ < 2)
37// bug in gcc < 7.2 requires extern
38// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52036
39#error Must use Clang or GCC > 7.2 to compile goby3 middleware
40#endif
41#endif
42#endif
43
44namespace goby
45{
47namespace middleware
48{
60class Group
61{
62 public:
64 static constexpr std::uint32_t broadcast_group{0};
66 static constexpr std::uint32_t invalid_numeric_group{std::numeric_limits<std::uint32_t>::max()};
67
68 static constexpr std::uint32_t maximum_valid_group{std::numeric_limits<std::uint32_t>::max() -
69 1};
70
72 constexpr Group(const char* c, std::uint32_t i = invalid_numeric_group) : c_(c), i_(i) {}
73
75 constexpr Group(std::uint32_t i = invalid_numeric_group) : i_(i) {}
76
78 constexpr std::uint32_t numeric() const { return i_; }
79
81 constexpr const char* c_str() const { return c_; }
82
84 operator std::string() const
85 {
86 if (c_ != nullptr)
87 {
88 if (i_ == invalid_numeric_group)
89 return std::string(c_);
90 else
91 return std::string(c_) + ";" + std::to_string(i_);
92 }
93 else
94 {
95 return std::to_string(i_);
96 }
97 }
98
99 protected:
100 void set_c_str(const char* c) { c_ = c; }
101
102 private:
103 const char* c_{nullptr};
104 std::uint32_t i_{invalid_numeric_group};
105};
106
107inline bool operator==(const Group& a, const Group& b)
108{
109 if (a.c_str() != nullptr && b.c_str() != nullptr)
110 return (std::string(a.c_str()) == std::string(b.c_str())) && (a.numeric() == b.numeric());
111 else
112 return a.numeric() == b.numeric();
113}
114
115inline bool operator!=(const Group& a, const Group& b) { return !(a == b); }
116
117inline std::ostream& operator<<(std::ostream& os, const Group& g) { return (os << std::string(g)); }
118
120class DynamicGroup : public Group
121{
122 public:
124 DynamicGroup(const std::string& s, std::uint32_t i = Group::invalid_numeric_group)
125 : Group(i), s_(new std::string(s))
126 {
127 Group::set_c_str(s_->c_str());
128 }
129
131 DynamicGroup(std::uint32_t i) : Group(i) {}
132
133 private:
134 std::unique_ptr<const std::string> s_;
135};
136
137} // namespace middleware
138} // namespace goby
139
140namespace std
141{
142template <> struct hash<goby::middleware::Group>
143{
144 size_t operator()(const goby::middleware::Group& group) const noexcept
145 {
146 return std::hash<std::string>{}(std::string(group));
147 }
148};
149} // namespace std
150
166#define GOBY_DEFINE_GROUP(QNAME, NAME) \
167 namespace QNAME \
168 { \
169 constexpr goby::middleware::Group NAME{#QNAME "::" #NAME}; \
170 }
171
188#define GOBY_DEFINE_INTERVEHICLE_GROUP(QNAME, NAME, NUMERIC) \
189 namespace QNAME \
190 { \
191 constexpr goby::middleware::Group NAME{#QNAME "::" #NAME, static_cast<std::uint32_t>(NUMERIC)}; \
192 }
193
194#endif
Implementation of Group for dynamic (run-time) instantiations. Use Group directly for static (compile...
Definition group.h:121
DynamicGroup(std::uint32_t i)
Construct a group with a numeric value only.
Definition group.h:131
DynamicGroup(const std::string &s, std::uint32_t i=Group::invalid_numeric_group)
Construct a group with a string and possibly a numeric value (when this Group will be used on interve...
Definition group.h:124
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition group.h:61
static constexpr std::uint32_t broadcast_group
Special group number representing the broadcast group (used when no grouping is required for a given ...
Definition group.h:64
constexpr Group(const char *c, std::uint32_t i=invalid_numeric_group)
Construct a group with a (C-style) string and possibly a numeric value (when this Group will be used ...
Definition group.h:72
constexpr Group(std::uint32_t i=invalid_numeric_group)
Construct a group with only a numeric value.
Definition group.h:75
constexpr std::uint32_t numeric() const
Access the group's numeric value.
Definition group.h:78
void set_c_str(const char *c)
Definition group.h:100
constexpr const char * c_str() const
Access the group's string value as a C string.
Definition group.h:81
static constexpr std::uint32_t maximum_valid_group
Definition group.h:68
operator std::string() const
Access the group's string value as a C++ string.
Definition group.h:84
static constexpr std::uint32_t invalid_numeric_group
Special group number representing an invalid numeric group (unsuitable for intervehicle and outer lay...
Definition group.h:66
goby::util::logger::GroupSetter group(std::string n)
bool operator!=(const Group &a, const Group &b)
Definition group.h:115
bool operator==(const Group &a, const Group &b)
Definition group.h:107
The global namespace for the Goby project.
STL namespace.