WPILibC++  2020.3.2-60-g3011ebe
SendableHelper.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2019-2020 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 <wpi/Twine.h>
14 #include <wpi/deprecated.h>
15 
16 #include "frc/smartdashboard/SendableRegistry.h"
17 
18 namespace frc {
19 
27 template <typename Derived>
29  public:
30  SendableHelper(const SendableHelper& rhs) = default;
31  SendableHelper& operator=(const SendableHelper& rhs) = default;
32 
34  // it is safe to call Move() multiple times with the same rhs
35  SendableRegistry::GetInstance().Move(static_cast<Derived*>(this),
36  static_cast<Derived*>(&rhs));
37  }
38 
39  SendableHelper& operator=(SendableHelper&& rhs) {
40  // it is safe to call Move() multiple times with the same rhs
41  SendableRegistry::GetInstance().Move(static_cast<Derived*>(this),
42  static_cast<Derived*>(&rhs));
43  return *this;
44  }
45 
53  WPI_DEPRECATED("use SendableRegistry::GetName()")
54  std::string GetName() const {
56  static_cast<const Derived*>(this));
57  }
58 
66  WPI_DEPRECATED("use SendableRegistry::SetName()")
67  void SetName(const wpi::Twine& name) {
68  SendableRegistry::GetInstance().SetName(static_cast<Derived*>(this), name);
69  }
70 
79  WPI_DEPRECATED("use SendableRegistry::SetName()")
80  void SetName(const wpi::Twine& subsystem, const wpi::Twine& name) {
81  SendableRegistry::GetInstance().SetName(static_cast<Derived*>(this),
82  subsystem, name);
83  }
84 
92  WPI_DEPRECATED("use SendableRegistry::GetSubsystem()")
93  std::string GetSubsystem() const {
95  static_cast<const Derived*>(this));
96  }
97 
105  WPI_DEPRECATED("use SendableRegistry::SetSubsystem()")
106  void SetSubsystem(const wpi::Twine& subsystem) {
107  SendableRegistry::GetInstance().SetSubsystem(static_cast<Derived*>(this),
108  subsystem);
109  }
110 
111  protected:
119  WPI_DEPRECATED("use SendableRegistry::AddChild()")
120  void AddChild(std::shared_ptr<Sendable> child) {
121  SendableRegistry::GetInstance().AddChild(static_cast<Derived*>(this),
122  child.get());
123  }
124 
132  WPI_DEPRECATED("use SendableRegistry::AddChild()")
133  void AddChild(void* child) {
134  SendableRegistry::GetInstance().AddChild(static_cast<Derived*>(this),
135  child);
136  }
137 
147  WPI_DEPRECATED("use SendableRegistry::SetName()")
148  void SetName(const wpi::Twine& moduleType, int channel) {
149  SendableRegistry::GetInstance().SetName(static_cast<Derived*>(this),
150  moduleType, channel);
151  }
152 
164  WPI_DEPRECATED("use SendableRegistry::SetName()")
165  void SetName(const wpi::Twine& moduleType, int moduleNumber, int channel) {
166  SendableRegistry::GetInstance().SetName(static_cast<Derived*>(this),
167  moduleType, moduleNumber, channel);
168  }
169 
170  protected:
171  SendableHelper() = default;
172 
173  ~SendableHelper() {
174  // it is safe to call Remove() multiple times with the same object
175  SendableRegistry::GetInstance().Remove(static_cast<Derived*>(this));
176  }
177 };
178 
179 } // namespace frc
frc::SendableHelper::GetName
std::string GetName() const
Gets the name of this Sendable object.
Definition: SendableHelper.h:54
frc::SendableRegistry::GetSubsystem
std::string GetSubsystem(const Sendable *sendable) const
Gets the subsystem name of an object.
frc::SendableHelper::GetSubsystem
std::string GetSubsystem() const
Gets the subsystem name of this Sendable object.
Definition: SendableHelper.h:93
frc::SendableRegistry::GetName
std::string GetName(const Sendable *sendable) const
Gets the name of an object.
frc::SendableRegistry::Move
void Move(Sendable *to, Sendable *from)
Moves an object in the registry (for use in move constructors/assignments).
frc::SendableRegistry::GetInstance
static SendableRegistry & GetInstance()
Gets an instance of the SendableRegistry class.
frc::SendableRegistry::SetSubsystem
void SetSubsystem(Sendable *sendable, const wpi::Twine &subsystem)
Sets the subsystem name of an object.
wpi
WPILib C++ utilities (wpiutil) namespace.
Definition: Endian.h:31
frc::SendableHelper::AddChild
void AddChild(std::shared_ptr< Sendable > child)
Add a child component.
Definition: SendableHelper.h:120
frc::Sendable
Interface for Sendable objects.
Definition: Sendable.h:17
frc::SendableHelper::SetName
void SetName(const wpi::Twine &name)
Sets the name of this Sendable object.
Definition: SendableHelper.h:67
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::SendableHelper::SetSubsystem
void SetSubsystem(const wpi::Twine &subsystem)
Sets the subsystem name of this Sendable object.
Definition: SendableHelper.h:106
frc::SendableRegistry::Remove
bool Remove(Sendable *sendable)
Removes an object from the registry.
frc::SendableHelper
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:28
frc::SendableRegistry::AddChild
void AddChild(Sendable *parent, Sendable *child)
Adds a child object to an object.