Goby3  3.0.2
2021.08.17
group.h
Go to the documentation of this file.
1 // Copyright 2017-2021:
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 <memory>
29 #include <string>
30 
31 #ifndef __clang__
32 #ifdef __GNUC__
33 #if __GNUC__ < 7 || (__GNUC__ == 7 && __GNUC_MINOR__ < 2)
34 // bug in gcc < 7.2 requires extern
35 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52036
36 #error Must use Clang or GCC > 7.2 to compile goby3 middleware
37 #endif
38 #endif
39 #endif
40 
41 namespace goby
42 {
44 namespace middleware
45 {
57 class Group
58 {
59  public:
61  static constexpr std::uint8_t broadcast_group{0};
63  static constexpr std::uint8_t invalid_numeric_group{255};
64 
65  static constexpr std::uint8_t maximum_valid_group{254};
66 
68  constexpr Group(const char* c, std::uint8_t i = invalid_numeric_group) : c_(c), i_(i) {}
69 
71  constexpr Group(std::uint8_t i = invalid_numeric_group) : i_(i) {}
72 
74  constexpr std::uint8_t numeric() const { return i_; }
75 
77  constexpr const char* c_str() const { return c_; }
78 
80  operator std::string() const
81  {
82  if (c_ != nullptr)
83  {
84  if (i_ == invalid_numeric_group)
85  return std::string(c_);
86  else
87  return std::string(c_) + ";" + std::to_string(i_);
88  }
89  else
90  {
91  return std::to_string(i_);
92  }
93  }
94 
95  protected:
96  void set_c_str(const char* c) { c_ = c; }
97 
98  private:
99  const char* c_{nullptr};
100  std::uint8_t i_{invalid_numeric_group};
101 };
102 
103 inline bool operator==(const Group& a, const Group& b)
104 {
105  if (a.c_str() != nullptr && b.c_str() != nullptr)
106  return (std::string(a.c_str()) == std::string(b.c_str())) && (a.numeric() == b.numeric());
107  else
108  return a.numeric() == b.numeric();
109 }
110 
111 inline bool operator!=(const Group& a, const Group& b) { return !(a == b); }
112 
113 inline std::ostream& operator<<(std::ostream& os, const Group& g) { return (os << std::string(g)); }
114 
116 class DynamicGroup : public Group
117 {
118  public:
120  DynamicGroup(const std::string& s, std::uint8_t i = Group::invalid_numeric_group)
121  : Group(i), s_(new std::string(s))
122  {
123  Group::set_c_str(s_->c_str());
124  }
125 
127  DynamicGroup(std::uint8_t i) : Group(i) {}
128 
129  private:
130  std::unique_ptr<const std::string> s_;
131 };
132 
133 } // namespace middleware
134 } // namespace goby
135 
136 namespace std
137 {
138 template <> struct hash<goby::middleware::Group>
139 {
140  size_t operator()(const goby::middleware::Group& group) const noexcept
141  {
142  return std::hash<std::string>{}(std::string(group));
143  }
144 };
145 } // namespace std
146 
147 #endif
nlohmann::detail::hash
std::size_t hash(const BasicJsonType &j)
hash a JSON value
Definition: json.hpp:4680
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::middleware::DynamicGroup::DynamicGroup
DynamicGroup(std::uint8_t i)
Construct a group with a numeric value only.
Definition: group.h:127
goby::middleware::Group::numeric
constexpr std::uint8_t numeric() const
Access the group's numeric value.
Definition: group.h:74
goby::middleware::Group::maximum_valid_group
static constexpr std::uint8_t maximum_valid_group
Definition: group.h:65
group
goby::util::logger::GroupSetter group(std::string n)
Definition: logger_manipulators.h:134
goby::middleware::operator==
bool operator==(const Group &a, const Group &b)
Definition: group.h:103
goby::middleware::Group::invalid_numeric_group
static constexpr std::uint8_t invalid_numeric_group
Special group number representing an invalid numeric group (unsuitable for intervehicle and outer lay...
Definition: group.h:63
goby::middleware::operator<<
std::ostream & operator<<(std::ostream &os, const Group &g)
Definition: group.h:113
goby::middleware::Group::broadcast_group
static constexpr std::uint8_t broadcast_group
Special group number representing the broadcast group (used when no grouping is required for a given ...
Definition: group.h:61
goby::middleware::Group::Group
constexpr Group(const char *c, std::uint8_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:68
goby::middleware::operator!=
bool operator!=(const Group &a, const Group &b)
Definition: group.h:111
goby::middleware::Group
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition: group.h:57
goby::middleware::Group::set_c_str
void set_c_str(const char *c)
Definition: group.h:96
goby::middleware::Group::Group
constexpr Group(std::uint8_t i=invalid_numeric_group)
Construct a group with only a numeric value.
Definition: group.h:71
goby::middleware::DynamicGroup::DynamicGroup
DynamicGroup(const std::string &s, std::uint8_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:120
std::hash< goby::middleware::Group >::operator()
size_t operator()(const goby::middleware::Group &group) const noexcept
Definition: group.h:140
goby::middleware::Group::c_str
constexpr const char * c_str() const
Access the group's string value as a C string.
Definition: group.h:77
goby::middleware::to_string
std::string to_string(goby::middleware::protobuf::Layer layer)
Definition: common.h:41
goby::middleware::DynamicGroup
Implementation of Group for dynamic (run-time) instantiations. Use Group directly for static (compile...
Definition: group.h:116