WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
SendableRegistry.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <functional>
8#include <memory>
9#include <string>
10#include <string_view>
11
12#include "wpi/function_ref.h"
13
14namespace wpi {
15
16class Sendable;
17class SendableBuilder;
18
19/**
20 * The SendableRegistry class is the public interface for registering sensors
21 * and actuators for use on dashboards and LiveWindow.
22 */
23class SendableRegistry final {
24 public:
25 SendableRegistry() = delete;
26
27 using UID = size_t;
28
29 /**
30 * Initializes the SendableRegistry. This is used to ensure initialization and
31 * destruction order relative to Sendables.
32 */
33 static void EnsureInitialized();
34
35 /**
36 * Sets the factory for LiveWindow builders.
37 *
38 * @param factory factory function
39 */
41 std::function<std::unique_ptr<SendableBuilder>()> factory);
42
43 /**
44 * Adds an object to the registry.
45 *
46 * @param sendable object to add
47 * @param name component name
48 */
49 static void Add(Sendable* sendable, std::string_view name);
50
51 /**
52 * Adds an object to the registry.
53 *
54 * @param sendable object to add
55 * @param moduleType A string that defines the module name in the label for
56 * the value
57 * @param channel The channel number the device is plugged into
58 */
59 static void Add(Sendable* sendable, std::string_view moduleType, int channel);
60
61 /**
62 * Adds an object to the registry.
63 *
64 * @param sendable object to add
65 * @param moduleType A string that defines the module name in the label for
66 * the value
67 * @param moduleNumber The number of the particular module type
68 * @param channel The channel number the device is plugged into
69 */
70 static void Add(Sendable* sendable, std::string_view moduleType,
71 int moduleNumber, int channel);
72
73 /**
74 * Adds an object to the registry.
75 *
76 * @param sendable object to add
77 * @param subsystem subsystem name
78 * @param name component name
79 */
80 static void Add(Sendable* sendable, std::string_view subsystem,
81 std::string_view name);
82
83 /**
84 * Adds an object to the registry and LiveWindow.
85 *
86 * @param sendable object to add
87 * @param name component name
88 */
89 static void AddLW(Sendable* sendable, std::string_view name);
90
91 /**
92 * Adds an object to the registry and LiveWindow.
93 *
94 * @param sendable object to add
95 * @param moduleType A string that defines the module name in the label for
96 * the value
97 * @param channel The channel number the device is plugged into
98 */
99 static void AddLW(Sendable* sendable, std::string_view moduleType,
100 int channel);
101
102 /**
103 * Adds an object to the registry and LiveWindow.
104 *
105 * @param sendable object to add
106 * @param moduleType A string that defines the module name in the label for
107 * the value
108 * @param moduleNumber The number of the particular module type
109 * @param channel The channel number the device is plugged into
110 */
111 static void AddLW(Sendable* sendable, std::string_view moduleType,
112 int moduleNumber, int channel);
113
114 /**
115 * Adds an object to the registry and LiveWindow.
116 *
117 * @param sendable object to add
118 * @param subsystem subsystem name
119 * @param name component name
120 */
121 static void AddLW(Sendable* sendable, std::string_view subsystem,
122 std::string_view name);
123
124 /**
125 * Adds a child object to an object. Adds the child object to the registry
126 * if it's not already present.
127 *
128 * @param parent parent object
129 * @param child child object
130 */
131 static void AddChild(Sendable* parent, Sendable* child);
132
133 /**
134 * Adds a child object to an object. Adds the child object to the registry
135 * if it's not already present.
136 *
137 * @param parent parent object
138 * @param child child object
139 */
140 static void AddChild(Sendable* parent, void* child);
141
142 /**
143 * Removes an object from the registry.
144 *
145 * @param sendable object to remove
146 * @return true if the object was removed; false if it was not present
147 */
148 static bool Remove(Sendable* sendable);
149
150 /**
151 * Moves an object in the registry (for use in move constructors/assignments).
152 *
153 * @param to new object
154 * @param from old object
155 */
156 static void Move(Sendable* to, Sendable* from);
157
158 /**
159 * Determines if an object is in the registry.
160 *
161 * @param sendable object to check
162 * @return True if in registry, false if not.
163 */
164 static bool Contains(const Sendable* sendable);
165
166 /**
167 * Gets the name of an object.
168 *
169 * @param sendable object
170 * @return Name (empty if object is not in registry)
171 */
172 static std::string GetName(const Sendable* sendable);
173
174 /**
175 * Sets the name of an object.
176 *
177 * @param sendable object
178 * @param name name
179 */
180 static void SetName(Sendable* sendable, std::string_view name);
181
182 /**
183 * Sets the name of an object with a channel number.
184 *
185 * @param sendable object
186 * @param moduleType A string that defines the module name in the label for
187 * the value
188 * @param channel The channel number the device is plugged into
189 */
190 static void SetName(Sendable* sendable, std::string_view moduleType,
191 int channel);
192
193 /**
194 * Sets the name of an object with a module and channel number.
195 *
196 * @param sendable object
197 * @param moduleType A string that defines the module name in the label for
198 * the value
199 * @param moduleNumber The number of the particular module type
200 * @param channel The channel number the device is plugged into
201 */
202 static void SetName(Sendable* sendable, std::string_view moduleType,
203 int moduleNumber, int channel);
204
205 /**
206 * Sets both the subsystem name and device name of an object.
207 *
208 * @param sendable object
209 * @param subsystem subsystem name
210 * @param name device name
211 */
212 static void SetName(Sendable* sendable, std::string_view subsystem,
213 std::string_view name);
214
215 /**
216 * Gets the subsystem name of an object.
217 *
218 * @param sendable object
219 * @return Subsystem name (empty if object is not in registry)
220 */
221 static std::string GetSubsystem(const Sendable* sendable);
222
223 /**
224 * Sets the subsystem name of an object.
225 *
226 * @param sendable object
227 * @param subsystem subsystem name
228 */
229 static void SetSubsystem(Sendable* sendable, std::string_view subsystem);
230
231 /**
232 * Gets a unique handle for setting/getting data with SetData() and GetData().
233 *
234 * @return Handle
235 */
236 static int GetDataHandle();
237
238 /**
239 * Associates arbitrary data with an object in the registry.
240 *
241 * @param sendable object
242 * @param handle data handle returned by GetDataHandle()
243 * @param data data to set
244 * @return Previous data (may be null)
245 */
246 static std::shared_ptr<void> SetData(Sendable* sendable, int handle,
247 std::shared_ptr<void> data);
248
249 /**
250 * Gets arbitrary data associated with an object in the registry.
251 *
252 * @param sendable object
253 * @param handle data handle returned by GetDataHandle()
254 * @return data (may be null if none associated)
255 */
256 static std::shared_ptr<void> GetData(Sendable* sendable, int handle);
257
258 /**
259 * Enables LiveWindow for an object.
260 *
261 * @param sendable object
262 */
263 static void EnableLiveWindow(Sendable* sendable);
264
265 /**
266 * Disables LiveWindow for an object.
267 *
268 * @param sendable object
269 */
270 static void DisableLiveWindow(Sendable* sendable);
271
272 /**
273 * Get unique id for an object. Since objects can move, use this instead
274 * of storing Sendable* directly if ownership is in question.
275 *
276 * @param sendable object
277 * @return unique id
278 */
279 static UID GetUniqueId(Sendable* sendable);
280
281 /**
282 * Get sendable object for a given unique id.
283 *
284 * @param uid unique id
285 * @return sendable object (may be null)
286 */
288
289 /**
290 * Publishes an object in the registry.
291 *
292 * @param sendableUid sendable unique id
293 * @param builder publisher backend
294 */
295 static void Publish(UID sendableUid,
296 std::unique_ptr<SendableBuilder> builder);
297
298 /**
299 * Updates published information from an object.
300 *
301 * @param sendableUid sendable unique id
302 */
303 static void Update(UID sendableUid);
304
305 /**
306 * Data passed to ForeachLiveWindow() callback function
307 */
309 CallbackData(Sendable* sendable_, std::string_view name_,
310 std::string_view subsystem_, wpi::Sendable* parent_,
311 std::shared_ptr<void>& data_, SendableBuilder& builder_)
312 : sendable(sendable_),
313 name(name_),
314 subsystem(subsystem_),
315 parent(parent_),
316 data(data_),
317 builder(builder_) {}
318
320 std::string_view name;
321 std::string_view subsystem;
323 std::shared_ptr<void>& data;
325 };
326
327 /**
328 * Iterates over LiveWindow-enabled objects in the registry.
329 * It is *not* safe to call other SendableRegistry functions from the
330 * callback (this will likely deadlock).
331 *
332 * @param dataHandle data handle to get data pointer passed to callback
333 * @param callback function to call for each object
334 */
335 static void ForeachLiveWindow(
336 int dataHandle, wpi::function_ref<void(CallbackData& cbdata)> callback);
337};
338
339} // namespace wpi
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or and nanopb were all modified for use in Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable from
Definition ThirdPartyNotices.txt:141
Helper class for building Sendable dashboard representations.
Definition SendableBuilder.h:21
Interface for Sendable objects.
Definition Sendable.h:16
The SendableRegistry class is the public interface for registering sensors and actuators for use on d...
Definition SendableRegistry.h:23
static std::string GetName(const Sendable *sendable)
Gets the name of an object.
static Sendable * GetSendable(UID uid)
Get sendable object for a given unique id.
size_t UID
Definition SendableRegistry.h:27
static std::shared_ptr< void > GetData(Sendable *sendable, int handle)
Gets arbitrary data associated with an object in the registry.
static void EnableLiveWindow(Sendable *sendable)
Enables LiveWindow for an object.
static void SetName(Sendable *sendable, std::string_view name)
Sets the name of an object.
static std::string GetSubsystem(const Sendable *sendable)
Gets the subsystem name of an object.
static void SetSubsystem(Sendable *sendable, std::string_view subsystem)
Sets the subsystem name of an object.
static void Add(Sendable *sendable, std::string_view moduleType, int channel)
Adds an object to the registry.
static UID GetUniqueId(Sendable *sendable)
Get unique id for an object.
static void EnsureInitialized()
Initializes the SendableRegistry.
static void Add(Sendable *sendable, std::string_view subsystem, std::string_view name)
Adds an object to the registry.
static void SetName(Sendable *sendable, std::string_view subsystem, std::string_view name)
Sets both the subsystem name and device name of an object.
static std::shared_ptr< void > SetData(Sendable *sendable, int handle, std::shared_ptr< void > data)
Associates arbitrary data with an object in the registry.
static void SetName(Sendable *sendable, std::string_view moduleType, int moduleNumber, int channel)
Sets the name of an object with a module and channel number.
static void Publish(UID sendableUid, std::unique_ptr< SendableBuilder > builder)
Publishes an object in the registry.
static void Update(UID sendableUid)
Updates published information from an object.
static void Move(Sendable *to, Sendable *from)
Moves an object in the registry (for use in move constructors/assignments).
static void AddLW(Sendable *sendable, std::string_view name)
Adds an object to the registry and LiveWindow.
static void Add(Sendable *sendable, std::string_view name)
Adds an object to the registry.
static void AddLW(Sendable *sendable, std::string_view subsystem, std::string_view name)
Adds an object to the registry and LiveWindow.
static int GetDataHandle()
Gets a unique handle for setting/getting data with SetData() and GetData().
static void AddChild(Sendable *parent, void *child)
Adds a child object to an object.
static void SetName(Sendable *sendable, std::string_view moduleType, int channel)
Sets the name of an object with a channel number.
static void AddLW(Sendable *sendable, std::string_view moduleType, int channel)
Adds an object to the registry and LiveWindow.
static void AddChild(Sendable *parent, Sendable *child)
Adds a child object to an object.
static void ForeachLiveWindow(int dataHandle, wpi::function_ref< void(CallbackData &cbdata)> callback)
Iterates over LiveWindow-enabled objects in the registry.
static bool Remove(Sendable *sendable)
Removes an object from the registry.
static bool Contains(const Sendable *sendable)
Determines if an object is in the registry.
static void AddLW(Sendable *sendable, std::string_view moduleType, int moduleNumber, int channel)
Adds an object to the registry and LiveWindow.
static void Add(Sendable *sendable, std::string_view moduleType, int moduleNumber, int channel)
Adds an object to the registry.
static void DisableLiveWindow(Sendable *sendable)
Disables LiveWindow for an object.
static void SetLiveWindowBuilderFactory(std::function< std::unique_ptr< SendableBuilder >()> factory)
Sets the factory for LiveWindow builders.
An efficient, type-erasing, non-owning reference to a callable.
Definition function_ref.h:31
Foonathan namespace.
Definition ntcore_cpp.h:26
Data passed to ForeachLiveWindow() callback function.
Definition SendableRegistry.h:308
CallbackData(Sendable *sendable_, std::string_view name_, std::string_view subsystem_, wpi::Sendable *parent_, std::shared_ptr< void > &data_, SendableBuilder &builder_)
Definition SendableRegistry.h:309
std::string_view name
Definition SendableRegistry.h:320
SendableBuilder & builder
Definition SendableRegistry.h:324
Sendable * parent
Definition SendableRegistry.h:322
std::shared_ptr< void > & data
Definition SendableRegistry.h:323
Sendable * sendable
Definition SendableRegistry.h:319
std::string_view subsystem
Definition SendableRegistry.h:321