roo_transceivers
API Documentation for roo_transceivers
Loading...
Searching...
No Matches
id.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4
5#include "roo_collections/hash.h"
6#include "roo_collections/small_string.h"
7#include "roo_logging.h"
8
9namespace roo_transceivers {
10
11/// Device schema identifier (short string).
12using DeviceSchema = roo_collections::SmallString<16>;
13/// Device identifier (short string).
14using DeviceId = roo_collections::SmallString<24>;
15/// Sensor identifier (short string).
16using SensorId = roo_collections::SmallString<24>;
17/// Actuator identifier (short string).
18using ActuatorId = roo_collections::SmallString<24>;
19
20/// Identifies a transceiver device by schema and device id.
22 public:
24
25 DeviceLocator(roo::string_view schema, roo::string_view device_id);
26
27 /// Returns the device schema.
28 const DeviceSchema& schema() const { return schema_; }
29 /// Returns the device id.
30 const DeviceId& device_id() const { return device_id_; }
31
32 /// Returns true if schema or device id is set.
33 bool isDefined() const { return !schema_.empty() || !device_id().empty(); }
34
35 /// Writes a null-terminated string representation into `buf`.
36 void write_cstr(char* buf) const;
37
38 /// Returns a string representation.
39 std::string toString() const;
40
41 private:
42 DeviceSchema schema_;
43 DeviceId device_id_;
44};
45
46inline bool operator==(const DeviceLocator& a, const DeviceLocator& b) {
47 return a.schema() == b.schema() && a.device_id() == b.device_id();
48}
49
50inline bool operator!=(const DeviceLocator& a, const DeviceLocator& b) {
51 return !(a == b);
52}
53
54/// Identifies sensor within a transceiver device.
55///
56/// Consists of device locator and sensor id.
58 public:
60
61 SensorLocator(roo::string_view schema, roo::string_view device_id,
62 roo::string_view sensor_id);
63
64 SensorLocator(const DeviceLocator& device_loc, roo::string_view sensor_id);
65
66 /// Returns the device locator.
67 const DeviceLocator& device_locator() const { return device_locator_; }
68
69 /// Returns the device schema.
70 const DeviceSchema& schema() const { return device_locator_.schema(); }
71
72 /// Returns the device id.
73 const DeviceId& device_id() const { return device_locator_.device_id(); }
74
75 /// Returns the sensor id.
76 const SensorId& sensor_id() const { return sensor_id_; }
77
78 /// Returns true if the locator is defined.
79 bool isDefined() const { return device_locator().isDefined(); }
80
81 /// Writes a null-terminated string representation into `buf`.
82 void write_cstr(char* buf) const;
83
84 /// Returns a string representation.
85 std::string toString() const;
86
87 private:
88 DeviceLocator device_locator_;
89 SensorId sensor_id_;
90};
91
92inline bool operator==(const SensorLocator& a, const SensorLocator& b) {
93 return a.device_locator() == b.device_locator() &&
94 a.sensor_id() == b.sensor_id();
95}
96
97inline bool operator!=(const SensorLocator& a, const SensorLocator& b) {
98 return !(a == b);
99}
100
101/// Identifies actuator within a transceiver device.
102///
103/// Consists of device locator and actuator id.
105 public:
107
108 ActuatorLocator(const DeviceLocator& device_loc,
109 roo::string_view actuator_id);
110
111 ActuatorLocator(roo::string_view schema, roo::string_view device_id,
112 roo::string_view actuator_id);
113
114 /// Returns the device locator.
115 const DeviceLocator& device_locator() const { return device_locator_; }
116
117 /// Returns the device schema.
118 const DeviceSchema& schema() const { return device_locator_.schema(); }
119
120 /// Returns the device id.
121 const DeviceId& device_id() const { return device_locator_.device_id(); }
122
123 /// Returns the actuator id.
124 const ActuatorId& actuator_id() const { return actuator_id_; }
125
126 /// Returns true if the locator is defined.
127 bool isDefined() const { return device_locator().isDefined(); }
128
129 /// Writes a null-terminated string representation into `buf`.
130 void write_cstr(char* buf) const;
131
132 /// Returns a string representation.
133 std::string toString() const;
134
135 private:
136 DeviceLocator device_locator_;
137 ActuatorId actuator_id_;
138};
139
140inline bool operator==(const ActuatorLocator& a, const ActuatorLocator& b) {
141 return a.device_locator() == b.device_locator() &&
142 a.actuator_id() == b.actuator_id();
143}
144
145inline bool operator!=(const ActuatorLocator& a, const ActuatorLocator& b) {
146 return !(a == b);
147}
148
149} // namespace roo_transceivers
150
151namespace std {
152
153template <>
154struct hash<roo_transceivers::DeviceSchema> {
155 size_t operator()(const roo_transceivers::DeviceSchema& schema) const {
156 return roo_collections::murmur3_32(schema.c_str(), strlen(schema.c_str()),
157 0);
158 }
159};
160
161template <>
162struct hash<roo_transceivers::DeviceLocator> {
163 size_t operator()(const roo_transceivers::DeviceLocator& loc) const {
164 return roo_collections::murmur3_32(
165 loc.device_id().c_str(), strlen(loc.device_id().c_str()),
166 std::hash<roo_transceivers::DeviceSchema>()(loc.schema()));
167 }
168};
169
170template <>
171struct hash<roo_transceivers::SensorLocator> {
172 size_t operator()(const roo_transceivers::SensorLocator& loc) const {
173 return roo_collections::murmur3_32(
174 loc.sensor_id().c_str(), strlen(loc.sensor_id().c_str()),
175 std::hash<roo_transceivers::DeviceLocator>()(loc.device_locator()));
176 }
177};
178
179template <>
180struct hash<roo_transceivers::ActuatorLocator> {
181 size_t operator()(const roo_transceivers::ActuatorLocator& loc) const {
182 return roo_collections::murmur3_32(
183 loc.actuator_id().c_str(), strlen(loc.actuator_id().c_str()),
184 std::hash<roo_transceivers::DeviceLocator>()(loc.device_locator()));
185 }
186};
187
188} // namespace std
189
190roo_logging::Stream& operator<<(roo_logging::Stream& s,
191 const roo_transceivers::DeviceSchema& schema);
192
193/// Streams a device locator in a human-readable form.
194roo_logging::Stream& operator<<(roo_logging::Stream& s,
196
197/// Streams a sensor locator in a human-readable form.
198roo_logging::Stream& operator<<(roo_logging::Stream& s,
200
201/// Streams an actuator locator in a human-readable form.
202roo_logging::Stream& operator<<(roo_logging::Stream& s,
Identifies actuator within a transceiver device.
Definition id.h:104
const DeviceLocator & device_locator() const
Returns the device locator.
Definition id.h:115
const DeviceSchema & schema() const
Returns the device schema.
Definition id.h:118
const DeviceId & device_id() const
Returns the device id.
Definition id.h:121
void write_cstr(char *buf) const
Writes a null-terminated string representation into buf.
Definition id.cpp:64
std::string toString() const
Returns a string representation.
Definition id.cpp:73
bool isDefined() const
Returns true if the locator is defined.
Definition id.h:127
const ActuatorId & actuator_id() const
Returns the actuator id.
Definition id.h:124
Identifies a transceiver device by schema and device id.
Definition id.h:21
bool isDefined() const
Returns true if schema or device id is set.
Definition id.h:33
const DeviceId & device_id() const
Returns the device id.
Definition id.h:30
const DeviceSchema & schema() const
Returns the device schema.
Definition id.h:28
std::string toString() const
Returns a string representation.
Definition id.cpp:20
void write_cstr(char *buf) const
Writes a null-terminated string representation into buf.
Definition id.cpp:11
Identifies sensor within a transceiver device.
Definition id.h:57
const SensorId & sensor_id() const
Returns the sensor id.
Definition id.h:76
const DeviceLocator & device_locator() const
Returns the device locator.
Definition id.h:67
const DeviceSchema & schema() const
Returns the device schema.
Definition id.h:70
const DeviceId & device_id() const
Returns the device id.
Definition id.h:73
std::string toString() const
Returns a string representation.
Definition id.cpp:46
bool isDefined() const
Returns true if the locator is defined.
Definition id.h:79
void write_cstr(char *buf) const
Writes a null-terminated string representation into buf.
Definition id.cpp:37
roo_logging::Stream & operator<<(roo_logging::Stream &s, const roo_transceivers::DeviceSchema &schema)
Definition id.cpp:82
roo_collections::SmallString< 24 > SensorId
Sensor identifier (short string).
Definition id.h:16
bool operator!=(const DeviceLocator &a, const DeviceLocator &b)
Definition id.h:50
bool operator==(const DeviceLocator &a, const DeviceLocator &b)
Definition id.h:46
roo_collections::SmallString< 16 > DeviceSchema
Device schema identifier (short string).
Definition id.h:12
roo_collections::SmallString< 24 > ActuatorId
Actuator identifier (short string).
Definition id.h:18
roo_collections::SmallString< 24 > DeviceId
Device identifier (short string).
Definition id.h:14
Definition id.h:151