WPILibC++  2020.3.2-60-g3011ebe
SendableRegistry.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <memory>
11 #include <string>
12 
13 #include <networktables/NetworkTable.h>
14 #include <wpi/STLExtras.h>
15 #include <wpi/Twine.h>
16 
17 namespace frc {
18 
19 class Sendable;
20 class SendableBuilderImpl;
21 
27  public:
28  SendableRegistry(const SendableRegistry&) = delete;
29  SendableRegistry& operator=(const SendableRegistry&) = delete;
30 
31  using UID = size_t;
32 
39  static SendableRegistry& GetInstance();
40 
47  void Add(Sendable* sendable, const wpi::Twine& name);
48 
57  void Add(Sendable* sendable, const wpi::Twine& moduleType, int channel);
58 
68  void Add(Sendable* sendable, const wpi::Twine& moduleType, int moduleNumber,
69  int channel);
70 
78  void Add(Sendable* sendable, const wpi::Twine& subsystem,
79  const wpi::Twine& name);
80 
87  void AddLW(Sendable* sendable, const wpi::Twine& name);
88 
97  void AddLW(Sendable* sendable, const wpi::Twine& moduleType, int channel);
98 
108  void AddLW(Sendable* sendable, const wpi::Twine& moduleType, int moduleNumber,
109  int channel);
110 
118  void AddLW(Sendable* sendable, const wpi::Twine& subsystem,
119  const wpi::Twine& name);
120 
128  void AddChild(Sendable* parent, Sendable* child);
129 
137  void AddChild(Sendable* parent, void* child);
138 
145  bool Remove(Sendable* sendable);
146 
153  void Move(Sendable* to, Sendable* from);
154 
161  bool Contains(const Sendable* sendable) const;
162 
169  std::string GetName(const Sendable* sendable) const;
170 
177  void SetName(Sendable* sendable, const wpi::Twine& name);
178 
187  void SetName(Sendable* sendable, const wpi::Twine& moduleType, int channel);
188 
198  void SetName(Sendable* sendable, const wpi::Twine& moduleType,
199  int moduleNumber, int channel);
200 
208  void SetName(Sendable* sendable, const wpi::Twine& subsystem,
209  const wpi::Twine& name);
210 
217  std::string GetSubsystem(const Sendable* sendable) const;
218 
225  void SetSubsystem(Sendable* sendable, const wpi::Twine& subsystem);
226 
232  int GetDataHandle();
233 
242  std::shared_ptr<void> SetData(Sendable* sendable, int handle,
243  std::shared_ptr<void> data);
244 
252  std::shared_ptr<void> GetData(Sendable* sendable, int handle);
253 
259  void EnableLiveWindow(Sendable* sendable);
260 
266  void DisableLiveWindow(Sendable* sendable);
267 
275  UID GetUniqueId(Sendable* sendable);
276 
283  Sendable* GetSendable(UID uid);
284 
291  void Publish(UID sendableUid, std::shared_ptr<NetworkTable> table);
292 
298  void Update(UID sendableUid);
299 
303  struct CallbackData {
304  CallbackData(Sendable* sendable_, wpi::StringRef name_,
305  wpi::StringRef subsystem_, Sendable* parent_,
306  std::shared_ptr<void>& data_, SendableBuilderImpl& builder_)
307  : sendable(sendable_),
308  name(name_),
309  subsystem(subsystem_),
310  parent(parent_),
311  data(data_),
312  builder(builder_) {}
313 
314  Sendable* sendable;
315  wpi::StringRef name;
316  wpi::StringRef subsystem;
317  Sendable* parent;
318  std::shared_ptr<void>& data;
319  SendableBuilderImpl& builder;
320  };
321 
330  void ForeachLiveWindow(
331  int dataHandle,
332  wpi::function_ref<void(CallbackData& cbdata)> callback) const;
333 
334  private:
336 
337  struct Impl;
338  std::unique_ptr<Impl> m_impl;
339 };
340 
341 } // namespace frc
frc::SendableRegistry::SetData
std::shared_ptr< void > SetData(Sendable *sendable, int handle, std::shared_ptr< void > data)
Associates arbitrary data with an object in the registry.
frc::SendableRegistry
The SendableRegistry class is the public interface for registering sensors and actuators for use on d...
Definition: SendableRegistry.h:26
frc::SendableBuilderImpl
Definition: SendableBuilderImpl.h:27
frc::SendableRegistry::GetSubsystem
std::string GetSubsystem(const Sendable *sendable) const
Gets the subsystem name of an object.
frc::SendableRegistry::CallbackData
Data passed to ForeachLiveWindow() callback function.
Definition: SendableRegistry.h:303
frc::SendableRegistry::Update
void Update(UID sendableUid)
Updates network table information from an object.
frc::SendableRegistry::GetName
std::string GetName(const Sendable *sendable) const
Gets the name of an object.
frc::SendableRegistry::GetData
std::shared_ptr< void > GetData(Sendable *sendable, int handle)
Gets arbitrary data associated with an object in the registry.
frc::SendableRegistry::Move
void Move(Sendable *to, Sendable *from)
Moves an object in the registry (for use in move constructors/assignments).
frc::SendableRegistry::ForeachLiveWindow
void ForeachLiveWindow(int dataHandle, wpi::function_ref< void(CallbackData &cbdata)> callback) const
Iterates over LiveWindow-enabled objects in the registry.
frc::SendableRegistry::GetInstance
static SendableRegistry & GetInstance()
Gets an instance of the SendableRegistry class.
frc::SendableRegistry::GetUniqueId
UID GetUniqueId(Sendable *sendable)
Get unique id for an object.
frc::SendableRegistry::EnableLiveWindow
void EnableLiveWindow(Sendable *sendable)
Enables LiveWindow for an object.
frc::SendableRegistry::SetSubsystem
void SetSubsystem(Sendable *sendable, const wpi::Twine &subsystem)
Sets the subsystem name of an object.
frc::SendableRegistry::Add
void Add(Sendable *sendable, const wpi::Twine &name)
Adds an object to the registry.
frc::SendableRegistry::GetDataHandle
int GetDataHandle()
Gets a unique handle for setting/getting data with SetData() and GetData().
frc::SendableRegistry::DisableLiveWindow
void DisableLiveWindow(Sendable *sendable)
Disables LiveWindow for an object.
wpi::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
frc::SendableRegistry::GetSendable
Sendable * GetSendable(UID uid)
Get sendable object for a given unique id.
frc::SendableRegistry::Publish
void Publish(UID sendableUid, std::shared_ptr< NetworkTable > table)
Publishes an object in the registry to a network table.
wpi::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:103
frc::Sendable
Interface for Sendable objects.
Definition: Sendable.h:17
frc::SendableRegistry::Contains
bool Contains(const Sendable *sendable) const
Determines if an object is in the registry.
frc
A class that enforces constraints on the differential drive kinematics.
Definition: PDPSim.h:16
frc::SendableRegistry::SetName
void SetName(Sendable *sendable, const wpi::Twine &name)
Sets the name of an object.
frc::SendableRegistry::Remove
bool Remove(Sendable *sendable)
Removes an object from the registry.
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85
frc::SendableRegistry::AddLW
void AddLW(Sendable *sendable, const wpi::Twine &name)
Adds an object to the registry and LiveWindow.
frc::SendableRegistry::AddChild
void AddChild(Sendable *parent, Sendable *child)
Adds a child object to an object.