WPILibC++  2020.3.2
SendableHelper.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 <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 
51  WPI_DEPRECATED("use SendableRegistry::GetName()")
52  std::string GetName() const {
54  static_cast<const Derived*>(this));
55  }
56 
62  WPI_DEPRECATED("use SendableRegistry::SetName()")
63  void SetName(const wpi::Twine& name) {
64  SendableRegistry::GetInstance().SetName(static_cast<Derived*>(this), name);
65  }
66 
73  WPI_DEPRECATED("use SendableRegistry::SetName()")
74  void SetName(const wpi::Twine& subsystem, const wpi::Twine& name) {
75  SendableRegistry::GetInstance().SetName(static_cast<Derived*>(this),
76  subsystem, name);
77  }
78 
84  WPI_DEPRECATED("use SendableRegistry::GetSubsystem()")
85  std::string GetSubsystem() const {
87  static_cast<const Derived*>(this));
88  }
89 
95  WPI_DEPRECATED("use SendableRegistry::SetSubsystem()")
96  void SetSubsystem(const wpi::Twine& subsystem) {
97  SendableRegistry::GetInstance().SetSubsystem(static_cast<Derived*>(this),
98  subsystem);
99  }
100 
101  protected:
107  WPI_DEPRECATED("use SendableRegistry::AddChild()")
108  void AddChild(std::shared_ptr<Sendable> child) {
109  SendableRegistry::GetInstance().AddChild(static_cast<Derived*>(this),
110  child.get());
111  }
112 
118  WPI_DEPRECATED("use SendableRegistry::AddChild()")
119  void AddChild(void* child) {
120  SendableRegistry::GetInstance().AddChild(static_cast<Derived*>(this),
121  child);
122  }
123 
131  WPI_DEPRECATED("use SendableRegistry::SetName()")
132  void SetName(const wpi::Twine& moduleType, int channel) {
133  SendableRegistry::GetInstance().SetName(static_cast<Derived*>(this),
134  moduleType, channel);
135  }
136 
146  WPI_DEPRECATED("use SendableRegistry::SetName()")
147  void SetName(const wpi::Twine& moduleType, int moduleNumber, int channel) {
148  SendableRegistry::GetInstance().SetName(static_cast<Derived*>(this),
149  moduleType, moduleNumber, channel);
150  }
151 
152  protected:
153  SendableHelper() = default;
154 
155  ~SendableHelper() {
156  // it is safe to call Remove() multiple times with the same object
157  SendableRegistry::GetInstance().Remove(static_cast<Derived*>(this));
158  }
159 };
160 
161 } // namespace frc
frc::SendableHelper::GetName
std::string GetName() const
Gets the name of this Sendable object.
Definition: SendableHelper.h:52
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:85
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: EventLoopRunner.h:17
frc::SendableHelper::AddChild
void AddChild(std::shared_ptr< Sendable > child)
Add a child component.
Definition: SendableHelper.h:108
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:63
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.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:96
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.