Goby3  3.1.5a
2024.05.23
group.h
Go to the documentation of this file.
1 // Copyright 2017-2024:
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 <cstdint>
29 #include <limits>
30 #include <memory>
31 #include <string>
32 
33 #ifndef __clang__
34 #ifdef __GNUC__
35 #if __GNUC__ < 7 || (__GNUC__ == 7 && __GNUC_MINOR__ < 2)
36 // bug in gcc < 7.2 requires extern
37 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52036
38 #error Must use Clang or GCC > 7.2 to compile goby3 middleware
39 #endif
40 #endif
41 #endif
42 
43 namespace goby
44 {
46 namespace middleware
47 {
59 class Group
60 {
61  public:
63  static constexpr std::uint32_t broadcast_group{0};
65  static constexpr std::uint32_t invalid_numeric_group{std::numeric_limits<std::uint32_t>::max()};
66 
67  static constexpr std::uint32_t maximum_valid_group{std::numeric_limits<std::uint32_t>::max() -
68  1};
69 
71  constexpr Group(const char* c, std::uint32_t i = invalid_numeric_group) : c_(c), i_(i) {}
72 
74  constexpr Group(std::uint32_t i = invalid_numeric_group) : i_(i) {}
75 
77  constexpr std::uint32_t numeric() const { return i_; }
78 
80  constexpr const char* c_str() const { return c_; }
81 
83  operator std::string() const
84  {
85  if (c_ != nullptr)
86  {
87  if (i_ == invalid_numeric_group)
88  return std::string(c_);
89  else
90  return std::string(c_) + ";" + std::to_string(i_);
91  }
92  else
93  {
94  return std::to_string(i_);
95  }
96  }
97 
98  protected:
99  void set_c_str(const char* c) { c_ = c; }
100 
101  private:
102  const char* c_{nullptr};
103  std::uint32_t i_{invalid_numeric_group};
104 };
105 
106 inline bool operator==(const Group& a, const Group& b)
107 {
108  if (a.c_str() != nullptr && b.c_str() != nullptr)
109  return (std::string(a.c_str()) == std::string(b.c_str())) && (a.numeric() == b.numeric());
110  else
111  return a.numeric() == b.numeric();
112 }
113 
114 inline bool operator!=(const Group& a, const Group& b) { return !(a == b); }
115 
116 inline std::ostream& operator<<(std::ostream& os, const Group& g) { return (os << std::string(g)); }
117 
119 class DynamicGroup : public Group
120 {
121  public:
123  DynamicGroup(const std::string& s, std::uint32_t i = Group::invalid_numeric_group)
124  : Group(i), s_(new std::string(s))
125  {
126  Group::set_c_str(s_->c_str());
127  }
128 
130  DynamicGroup(std::uint32_t i) : Group(i) {}
131 
132  private:
133  std::unique_ptr<const std::string> s_;
134 };
135 
136 } // namespace middleware
137 } // namespace goby
138 
139 namespace std
140 {
141 template <> struct hash<goby::middleware::Group>
142 {
143  size_t operator()(const goby::middleware::Group& group) const noexcept
144  {
145  return std::hash<std::string>{}(std::string(group));
146  }
147 };
148 } // namespace std
149 
150 #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:74
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::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:71
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:123
goby::middleware::operator==
bool operator==(const Group &a, const Group &b)
Definition: group.h:106
goby::middleware::operator<<
std::ostream & operator<<(std::ostream &os, const Group &g)
Definition: group.h:116
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:130
goby::middleware::Group::numeric
constexpr std::uint32_t numeric() const
Access the group's numeric value.
Definition: group.h:77
goby::middleware::operator!=
bool operator!=(const Group &a, const Group &b)
Definition: group.h:114
goby::middleware::Group
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition: group.h:59
goby::middleware::Group::set_c_str
void set_c_str(const char *c)
Definition: group.h:99
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:63
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:65
std::hash< goby::middleware::Group >::operator()
size_t operator()(const goby::middleware::Group &group) const noexcept
Definition: group.h:143
goby::middleware::Group::c_str
constexpr const char * c_str() const
Access the group's string value as a C string.
Definition: group.h:80
goby::middleware::DynamicGroup
Implementation of Group for dynamic (run-time) instantiations. Use Group directly for static (compile...
Definition: group.h:119