Simplify Sendable interface (#1864)

This removes the name and subsystem from individual objects, and instead
puts this data into a new singleton class, SendableRegistry.  Much of
LiveWindow has been refactored into SendableRegistry.

In C++, a new CRTP helper class, SendableHelper, has been added to provide
move and destruction functionality.

Shims for GetName, SetName, GetSubsystem, and SetSubsystem have been added
to Command and Subsystem (both old and new), and also to SendableHelper to
prevent code breakage.

This deprecates SendableBase in preparation for future removal.
This commit is contained in:
Peter Johnson
2019-09-14 15:22:54 -05:00
committed by GitHub
parent 1d8c4d016f
commit 471f375a38
216 changed files with 2448 additions and 1433 deletions

View File

@@ -52,9 +52,13 @@ class ProxyScheduleCommand;
class Command : public frc::ErrorBase {
public:
Command() = default;
Command(Command&& other) = default;
virtual ~Command();
Command(const Command&);
Command& operator=(const Command&);
Command(Command&&) = default;
Command& operator=(Command&&) = default;
/**
* The initial subroutine of a command. Called once when the command is
* initially scheduled.

View File

@@ -8,11 +8,12 @@
#pragma once
#include <frc/smartdashboard/Sendable.h>
#include <frc/smartdashboard/SendableHelper.h>
#include <string>
#include <wpi/SmallSet.h>
#include <wpi/SmallVector.h>
#include <wpi/Twine.h>
#include "Command.h"
@@ -20,12 +21,10 @@ namespace frc2 {
/**
* A Sendable base class for Commands.
*/
class CommandBase : public frc::Sendable, public Command {
class CommandBase : public Command,
public frc::Sendable,
public frc::SendableHelper<CommandBase> {
public:
CommandBase(CommandBase&& other) = default;
CommandBase(const CommandBase& other);
/**
* Adds the specified requirements to the command.
*
@@ -37,20 +36,38 @@ class CommandBase : public frc::Sendable, public Command {
wpi::SmallSet<Subsystem*, 4> GetRequirements() const override;
void SetName(const wpi::Twine& name) override;
/**
* Sets the name of this Command.
*
* @param name name
*/
void SetName(const wpi::Twine& name);
/**
* Gets the name of this Command.
*
* @return Name
*/
std::string GetName() const override;
std::string GetSubsystem() const override;
/**
* Gets the subsystem name of this Command.
*
* @return Subsystem name
*/
std::string GetSubsystem() const;
void SetSubsystem(const wpi::Twine& subsystem) override;
/**
* Sets the subsystem name of this Command.
*
* @param subsystem subsystem name
*/
void SetSubsystem(const wpi::Twine& subsystem);
void InitSendable(frc::SendableBuilder& builder) override;
protected:
CommandBase();
std::string m_name;
std::string m_subsystem;
wpi::SmallSet<Subsystem*, 4> m_requirements;
};
} // namespace frc2

View File

@@ -10,7 +10,8 @@
#include <frc/ErrorBase.h>
#include <frc/RobotState.h>
#include <frc/WPIErrors.h>
#include <frc/smartdashboard/SendableBase.h>
#include <frc/smartdashboard/Sendable.h>
#include <frc/smartdashboard/SendableHelper.h>
#include <memory>
#include <unordered_map>
@@ -34,7 +35,9 @@ class Subsystem;
* with the scheduler using RegisterSubsystem() in order for their Periodic()
* methods to be called and for their default commands to be scheduled.
*/
class CommandScheduler final : public frc::SendableBase, frc::ErrorBase {
class CommandScheduler final : public frc::Sendable,
public frc::ErrorBase,
public frc::SendableHelper<CommandScheduler> {
public:
/**
* Returns the Scheduler instance.

View File

@@ -8,6 +8,7 @@
#pragma once
#include <frc/smartdashboard/Sendable.h>
#include <frc/smartdashboard/SendableHelper.h>
#include <string>
@@ -18,17 +19,50 @@ namespace frc2 {
* A base for subsystems that handles registration in the constructor, and
* provides a more intuitive method for setting the default command.
*/
class SubsystemBase : public Subsystem, public frc::Sendable {
class SubsystemBase : public Subsystem,
public frc::Sendable,
public frc::SendableHelper<SubsystemBase> {
public:
void InitSendable(frc::SendableBuilder& builder) override;
std::string GetName() const override;
void SetName(const wpi::Twine& name) override;
std::string GetSubsystem() const override;
void SetSubsystem(const wpi::Twine& name) override;
/**
* Gets the name of this Subsystem.
*
* @return Name
*/
std::string GetName() const;
/**
* Sets the name of this Subsystem.
*
* @param name name
*/
void SetName(const wpi::Twine& name);
/**
* Gets the subsystem name of this Subsystem.
*
* @return Subsystem name
*/
std::string GetSubsystem() const;
/**
* Sets the subsystem name of this Subsystem.
*
* @param subsystem subsystem name
*/
void SetSubsystem(const wpi::Twine& name);
/**
* Associate a Sendable with this Subsystem.
* Also update the child's name.
*
* @param name name to give child
* @param child sendable
*/
void AddChild(std::string name, frc::Sendable* child);
protected:
SubsystemBase();
std::string m_name;
};
} // namespace frc2