Goby3  3.1.4
2024.02.22
group.h
Go to the documentation of this file.
1 // Copyright 2017-2022:
2 // GobySoft, LLC (2013-)
3 // Community contributors (see AUTHORS file)
4 // File authors:
5 // Toby Schneider <toby@gobysoft.org>
6 // Ryan Govostes <rgovostes+git@gmail.com>
7 //
8 //
9 // This file is part of the Goby Underwater Autonomy Project Libraries
10 // ("The Goby Libraries").
11 //
12 // The Goby Libraries are free software: you can redistribute them and/or modify
13 // them under the terms of the GNU Lesser General Public License as published by
14 // the Free Software Foundation, either version 2.1 of the License, or
15 // (at your option) any later version.
16 //
17 // The Goby Libraries are distributed in the hope that they will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public License
23 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef GOBY_MIDDLEWARE_GROUP_H
26 #define GOBY_MIDDLEWARE_GROUP_H
27 
28 #include <limits>
29 #include <memory>
30 #include <string>
31 
32 #ifndef __clang__
33 #ifdef __GNUC__
34 #if __GNUC__ < 7 || (__GNUC__ == 7 && __GNUC_MINOR__ < 2)
35 // bug in gcc < 7.2 requires extern
36 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52036
37 #error Must use Clang or GCC > 7.2 to compile goby3 middleware
38 #endif
39 #endif
40 #endif
41 
42 namespace goby
43 {
45 namespace middleware
46 {
58 class Group
59 {
60  public:
62  static constexpr std::uint32_t broadcast_group{0};
64  static constexpr std::uint32_t invalid_numeric_group{std::numeric_limits<std::uint32_t>::max()};
65 
66  static constexpr std::uint32_t maximum_valid_group{std::numeric_limits<std::uint32_t>::max() -
67  1};
68 
70  constexpr Group(const char* c, std::uint32_t i = invalid_numeric_group) : c_(c), i_(i) {}
71 
73  constexpr Group(std::uint32_t i = invalid_numeric_group) : i_(i) {}
74 
76  constexpr std::uint32_t numeric() const { return i_; }
77 
79  constexpr const char* c_str() const { return c_; }
80 
82  operator std::string() const
83  {
84  if (c_ != nullptr)
85  {
86  if (i_ == invalid_numeric_group)
87  return std::string(c_);
88  else
89  return std::string(c_) + ";" + std::to_string(i_);
90  }
91  else
92  {
93  return std::to_string(i_);
94  }
95  }
96 
97  protected:
98  void set_c_str(const char* c) { c_ = c; }
99 
100  private:
101  const char* c_{nullptr};
102  std::uint32_t i_{invalid_numeric_group};
103 };
104 
105 inline bool operator==(const Group& a, const Group& b)
106 {
107  if (a.c_str() != nullptr && b.c_str() != nullptr)
108  return (std::string(a.c_str()) == std::string(b.c_str())) && (a.numeric() == b.numeric());
109  else
110  return a.numeric() == b.numeric();
111 }
112 
113 inline bool operator!=(const Group& a, const Group& b) { return !(a == b); }
114 
115 inline std::ostream& operator<<(std::ostream& os, const Group& g) { return (os << std::string(g)); }
116 
118 class DynamicGroup : public Group
119 {
120  public:
122  DynamicGroup(const std::string& s, std::uint32_t i = Group::invalid_numeric_group)
123  : Group(i), s_(new std::string(s))
124  {
125  Group::set_c_str(s_->c_str());
126  }
127 
129  DynamicGroup(std::uint32_t i) : Group(i) {}
130 
131  private:
132  std::unique_ptr<const std::string> s_;
133 };
134 
135 } // namespace middleware
136 } // namespace goby
137 
138 namespace std
139 {
140 template <> struct hash<goby::middleware::Group>
141 {
142  size_t operator()(const goby::middleware::Group& group) const noexcept
143  {
144  return std::hash<std::string>{}(std::string(group));
145  }
146 };
147 } // namespace std
148 
149 #endif
goby::middleware::Group::Group
constexpr Group(std::uint32_t i=invalid_numeric_group)
Construct a group with only a numeric value.
Definition: group.h:73
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:66
goby::middleware::Group::Group
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:70
group
goby::util::logger::GroupSetter group(std::string n)
Definition: logger_manipulators.h:134
goby::middleware::DynamicGroup::DynamicGroup
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:122
goby::middleware::operator==
bool operator==(const Group &a, const Group &b)
Definition: group.h:105
goby::middleware::operator<<
std::ostream & operator<<(std::ostream &os, const Group &g)
Definition: group.h:115
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::DynamicGroup::DynamicGroup
DynamicGroup(std::uint32_t i)
Construct a group with a numeric value only.
Definition: group.h:129
goby::middleware::Group::numeric
constexpr std::uint32_t numeric() const
Access the group's numeric value.
Definition: group.h:76
goby::middleware::operator!=
bool operator!=(const Group &a, const Group &b)
Definition: group.h:113
goby::middleware::Group
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition: group.h:58
goby::middleware::Group::set_c_str
void set_c_str(const char *c)
Definition: group.h:98
detail::hash
std::size_t hash(const BasicJsonType &j)
hash a JSON value
Definition: json.hpp:5951
goby::middleware::Group::broadcast_group
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:62
goby::middleware::Group::invalid_numeric_group
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:64
std::hash< goby::middleware::Group >::operator()
size_t operator()(const goby::middleware::Group &group) const noexcept
Definition: group.h:142
goby::middleware::Group::c_str
constexpr const char * c_str() const
Access the group's string value as a C string.
Definition: group.h:79
goby::middleware::DynamicGroup
Implementation of Group for dynamic (run-time) instantiations. Use Group directly for static (compile...
Definition: group.h:118