WPILibC++ 2024.3.2
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 * Sets the factory for LiveWindow builders.
31 *
32 * @param factory factory function
33 */
35 std::function<std::unique_ptr<SendableBuilder>()> factory);
36
37 /**
38 * Adds an object to the registry.
39 *
40 * @param sendable object to add
41 * @param name component name
42 */
43 static void Add(Sendable* sendable, std::string_view name);
44
45 /**
46 * Adds an object to the registry.
47 *
48 * @param sendable object to add
49 * @param moduleType A string that defines the module name in the label for
50 * the value
51 * @param channel The channel number the device is plugged into
52 */
53 static void Add(Sendable* sendable, std::string_view moduleType, int channel);
54
55 /**
56 * Adds an object to the registry.
57 *
58 * @param sendable object to add
59 * @param moduleType A string that defines the module name in the label for
60 * the value
61 * @param moduleNumber The number of the particular module type
62 * @param channel The channel number the device is plugged into
63 */
64 static void Add(Sendable* sendable, std::string_view moduleType,
65 int moduleNumber, int channel);
66
67 /**
68 * Adds an object to the registry.
69 *
70 * @param sendable object to add
71 * @param subsystem subsystem name
72 * @param name component name
73 */
74 static void Add(Sendable* sendable, std::string_view subsystem,
75 std::string_view name);
76
77 /**
78 * Adds an object to the registry and LiveWindow.
79 *
80 * @param sendable object to add
81 * @param name component name
82 */
83 static void AddLW(Sendable* sendable, std::string_view name);
84
85 /**
86 * Adds an object to the registry and LiveWindow.
87 *
88 * @param sendable object to add
89 * @param moduleType A string that defines the module name in the label for
90 * the value
91 * @param channel The channel number the device is plugged into
92 */
93 static void AddLW(Sendable* sendable, std::string_view moduleType,
94 int channel);
95
96 /**
97 * Adds an object to the registry and LiveWindow.
98 *
99 * @param sendable object to add
100 * @param moduleType A string that defines the module name in the label for
101 * the value
102 * @param moduleNumber The number of the particular module type
103 * @param channel The channel number the device is plugged into
104 */
105 static void AddLW(Sendable* sendable, std::string_view moduleType,
106 int moduleNumber, int channel);
107
108 /**
109 * Adds an object to the registry and LiveWindow.
110 *
111 * @param sendable object to add
112 * @param subsystem subsystem name
113 * @param name component name
114 */
115 static void AddLW(Sendable* sendable, std::string_view subsystem,
116 std::string_view name);
117
118 /**
119 * Adds a child object to an object. Adds the child object to the registry
120 * if it's not already present.
121 *
122 * @param parent parent object
123 * @param child child object
124 */
125 static void AddChild(Sendable* parent, Sendable* child);
126
127 /**
128 * Adds a child object to an object. Adds the child object to the registry
129 * if it's not already present.
130 *
131 * @param parent parent object
132 * @param child child object
133 */
134 static void AddChild(Sendable* parent, void* child);
135
136 /**
137 * Removes an object from the registry.
138 *
139 * @param sendable object to remove
140 * @return true if the object was removed; false if it was not present
141 */
142 static bool Remove(Sendable* sendable);
143
144 /**
145 * Moves an object in the registry (for use in move constructors/assignments).
146 *
147 * @param to new object
148 * @param from old object
149 */
150 static void Move(Sendable* to, Sendable* from);
151
152 /**
153 * Determines if an object is in the registry.
154 *
155 * @param sendable object to check
156 * @return True if in registry, false if not.
157 */
158 static bool Contains(const Sendable* sendable);
159
160 /**
161 * Gets the name of an object.
162 *
163 * @param sendable object
164 * @return Name (empty if object is not in registry)
165 */
166 static std::string GetName(const Sendable* sendable);
167
168 /**
169 * Sets the name of an object.
170 *
171 * @param sendable object
172 * @param name name
173 */
174 static void SetName(Sendable* sendable, std::string_view name);
175
176 /**
177 * Sets the name of an object with a channel number.
178 *
179 * @param sendable object
180 * @param moduleType A string that defines the module name in the label for
181 * the value
182 * @param channel The channel number the device is plugged into
183 */
184 static void SetName(Sendable* sendable, std::string_view moduleType,
185 int channel);
186
187 /**
188 * Sets the name of an object with a module and channel number.
189 *
190 * @param sendable object
191 * @param moduleType A string that defines the module name in the label for
192 * the value
193 * @param moduleNumber The number of the particular module type
194 * @param channel The channel number the device is plugged into
195 */
196 static void SetName(Sendable* sendable, std::string_view moduleType,
197 int moduleNumber, int channel);
198
199 /**
200 * Sets both the subsystem name and device name of an object.
201 *
202 * @param sendable object
203 * @param subsystem subsystem name
204 * @param name device name
205 */
206 static void SetName(Sendable* sendable, std::string_view subsystem,
207 std::string_view name);
208
209 /**
210 * Gets the subsystem name of an object.
211 *
212 * @param sendable object
213 * @return Subsystem name (empty if object is not in registry)
214 */
215 static std::string GetSubsystem(const Sendable* sendable);
216
217 /**
218 * Sets the subsystem name of an object.
219 *
220 * @param sendable object
221 * @param subsystem subsystem name
222 */
223 static void SetSubsystem(Sendable* sendable, std::string_view subsystem);
224
225 /**
226 * Gets a unique handle for setting/getting data with SetData() and GetData().
227 *
228 * @return Handle
229 */
230 static int GetDataHandle();
231
232 /**
233 * Associates arbitrary data with an object in the registry.
234 *
235 * @param sendable object
236 * @param handle data handle returned by GetDataHandle()
237 * @param data data to set
238 * @return Previous data (may be null)
239 */
240 static std::shared_ptr<void> SetData(Sendable* sendable, int handle,
241 std::shared_ptr<void> data);
242
243 /**
244 * Gets arbitrary data associated with an object in the registry.
245 *
246 * @param sendable object
247 * @param handle data handle returned by GetDataHandle()
248 * @return data (may be null if none associated)
249 */
250 static std::shared_ptr<void> GetData(Sendable* sendable, int handle);
251
252 /**
253 * Enables LiveWindow for an object.
254 *
255 * @param sendable object
256 */
257 static void EnableLiveWindow(Sendable* sendable);
258
259 /**
260 * Disables LiveWindow for an object.
261 *
262 * @param sendable object
263 */
264 static void DisableLiveWindow(Sendable* sendable);
265
266 /**
267 * Get unique id for an object. Since objects can move, use this instead
268 * of storing Sendable* directly if ownership is in question.
269 *
270 * @param sendable object
271 * @return unique id
272 */
273 static UID GetUniqueId(Sendable* sendable);
274
275 /**
276 * Get sendable object for a given unique id.
277 *
278 * @param uid unique id
279 * @return sendable object (may be null)
280 */
282
283 /**
284 * Publishes an object in the registry.
285 *
286 * @param sendableUid sendable unique id
287 * @param builder publisher backend
288 */
289 static void Publish(UID sendableUid,
290 std::unique_ptr<SendableBuilder> builder);
291
292 /**
293 * Updates published information from an object.
294 *
295 * @param sendableUid sendable unique id
296 */
297 static void Update(UID sendableUid);
298
299 /**
300 * Data passed to ForeachLiveWindow() callback function
301 */
304 std::string_view subsystem_, wpi::Sendable* parent_,
305 std::shared_ptr<void>& data_, SendableBuilder& builder_)
306 : sendable(sendable_),
307 name(name_),
308 subsystem(subsystem_),
309 parent(parent_),
310 data(data_),
311 builder(builder_) {}
312
317 std::shared_ptr<void>& data;
319 };
320
321 /**
322 * Iterates over LiveWindow-enabled objects in the registry.
323 * It is *not* safe to call other SendableRegistry functions from the
324 * callback (this will likely deadlock).
325 *
326 * @param dataHandle data handle to get data pointer passed to callback
327 * @param callback function to call for each object
328 */
329 static void ForeachLiveWindow(
330 int dataHandle, wpi::function_ref<void(CallbackData& cbdata)> callback);
331};
332
333} // 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 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:128
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 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
basic_string_view< char > string_view
Definition: core.h:501
Definition: ntcore_cpp.h:26
Data passed to ForeachLiveWindow() callback function.
Definition: SendableRegistry.h:302
CallbackData(Sendable *sendable_, std::string_view name_, std::string_view subsystem_, wpi::Sendable *parent_, std::shared_ptr< void > &data_, SendableBuilder &builder_)
Definition: SendableRegistry.h:303
std::string_view name
Definition: SendableRegistry.h:314
SendableBuilder & builder
Definition: SendableRegistry.h:318
Sendable * parent
Definition: SendableRegistry.h:316
std::shared_ptr< void > & data
Definition: SendableRegistry.h:317
Sendable * sendable
Definition: SendableRegistry.h:313
std::string_view subsystem
Definition: SendableRegistry.h:315