[wpilibc] Revert "Return reference from GetInstance" (#3342)

This reverts commit a79faace1b.

This change will be superseded in a non-breaking way by changing to static functions and deprecating GetInstance() entirely.
This commit is contained in:
Peter Johnson
2021-05-09 18:16:07 -07:00
committed by GitHub
parent 3cc2da3328
commit 3fe8fc75aa
18 changed files with 63 additions and 67 deletions

View File

@@ -158,7 +158,7 @@ bool ReadConfig() {
void StartCamera(const CameraConfig& config) {
wpi::outs() << "Starting camera '" << config.name << "' on " << config.path
<< '\n';
auto camera = frc::CameraServer::GetInstance().StartAutomaticCapture(
auto camera = frc::CameraServer::GetInstance()->StartAutomaticCapture(
config.name, config.path);
camera.SetConfigJson(config.config);

View File

@@ -45,7 +45,7 @@ struct CameraServer::Impl {
std::vector<std::string> m_addresses;
};
CameraServer& CameraServer::GetInstance() {
CameraServer* CameraServer::GetInstance() {
struct Creator {
static void* call() { return new CameraServer{}; }
};
@@ -53,7 +53,7 @@ CameraServer& CameraServer::GetInstance() {
static void call(void* ptr) { delete static_cast<CameraServer*>(ptr); }
};
static wpi::ManagedStatic<CameraServer, Creator, Deleter> instance;
return *instance;
return &(*instance);
}
static wpi::StringRef MakeSourceValue(CS_Source source,

View File

@@ -32,7 +32,7 @@ class CameraServer {
/**
* Get the CameraServer instance.
*/
static CameraServer& GetInstance();
static CameraServer* GetInstance();
/**
* Start automatically capturing images to send to the dashboard.

View File

@@ -70,19 +70,19 @@ CommandScheduler::CommandScheduler()
HAL_Report(HALUsageReporting::kResourceType_Command,
HALUsageReporting::kCommand2_Scheduler);
frc::SendableRegistry::GetInstance().AddLW(this, "Scheduler");
auto& scheduler = frc::LiveWindow::GetInstance();
scheduler.enabled = [this] {
Disable();
CancelAll();
auto scheduler = frc::LiveWindow::GetInstance();
scheduler->enabled = [this] {
this->Disable();
this->CancelAll();
};
scheduler.disabled = [this] { Enable(); };
scheduler->disabled = [this] { this->Enable(); };
}
CommandScheduler::~CommandScheduler() {
frc::SendableRegistry::GetInstance().Remove(this);
auto& scheduler = frc::LiveWindow::GetInstance();
scheduler.enabled = nullptr;
scheduler.disabled = nullptr;
auto scheduler = frc::LiveWindow::GetInstance();
scheduler->enabled = nullptr;
scheduler->disabled = nullptr;
std::unique_ptr<Impl>().swap(m_impl);
}

View File

@@ -201,19 +201,19 @@ Scheduler::Scheduler() : m_impl(new Impl) {
HAL_Report(HALUsageReporting::kResourceType_Command,
HALUsageReporting::kCommand_Scheduler);
SendableRegistry::GetInstance().AddLW(this, "Scheduler");
auto& scheduler = frc::LiveWindow::GetInstance();
scheduler.enabled = [this] {
SetEnabled(false);
RemoveAll();
auto scheduler = frc::LiveWindow::GetInstance();
scheduler->enabled = [this] {
this->SetEnabled(false);
this->RemoveAll();
};
scheduler.disabled = [this] { SetEnabled(true); };
scheduler->disabled = [this] { this->SetEnabled(true); };
}
Scheduler::~Scheduler() {
SendableRegistry::GetInstance().Remove(this);
auto& scheduler = frc::LiveWindow::GetInstance();
scheduler.enabled = nullptr;
scheduler.disabled = nullptr;
auto scheduler = frc::LiveWindow::GetInstance();
scheduler->enabled = nullptr;
scheduler->disabled = nullptr;
}
void Scheduler::Impl::Remove(Command* command) {

View File

@@ -112,7 +112,7 @@ void IterativeRobotBase::LoopFunc() {
// Call DisabledInit() if we are now just entering disabled mode from
// either a different mode or from power-on.
if (m_lastMode != Mode::kDisabled) {
LiveWindow::GetInstance().SetEnabled(false);
LiveWindow::GetInstance()->SetEnabled(false);
Shuffleboard::DisableActuatorWidgets();
DisabledInit();
m_watchdog.AddEpoch("DisabledInit()");
@@ -126,7 +126,7 @@ void IterativeRobotBase::LoopFunc() {
// Call AutonomousInit() if we are now just entering autonomous mode from
// either a different mode or from power-on.
if (m_lastMode != Mode::kAutonomous) {
LiveWindow::GetInstance().SetEnabled(false);
LiveWindow::GetInstance()->SetEnabled(false);
Shuffleboard::DisableActuatorWidgets();
AutonomousInit();
m_watchdog.AddEpoch("AutonomousInit()");
@@ -140,7 +140,7 @@ void IterativeRobotBase::LoopFunc() {
// Call TeleopInit() if we are now just entering teleop mode from
// either a different mode or from power-on.
if (m_lastMode != Mode::kTeleop) {
LiveWindow::GetInstance().SetEnabled(false);
LiveWindow::GetInstance()->SetEnabled(false);
Shuffleboard::DisableActuatorWidgets();
TeleopInit();
m_watchdog.AddEpoch("TeleopInit()");
@@ -154,7 +154,7 @@ void IterativeRobotBase::LoopFunc() {
// Call TestInit() if we are now just entering test mode from
// either a different mode or from power-on.
if (m_lastMode != Mode::kTest) {
LiveWindow::GetInstance().SetEnabled(true);
LiveWindow::GetInstance()->SetEnabled(true);
Shuffleboard::EnableActuatorWidgets();
TestInit();
m_watchdog.AddEpoch("TestInit()");
@@ -171,7 +171,7 @@ void IterativeRobotBase::LoopFunc() {
SmartDashboard::UpdateValues();
m_watchdog.AddEpoch("SmartDashboard::UpdateValues()");
LiveWindow::GetInstance().UpdateValues();
LiveWindow::GetInstance()->UpdateValues();
m_watchdog.AddEpoch("LiveWindow::UpdateValues()");
Shuffleboard::Update();
m_watchdog.AddEpoch("Shuffleboard::Update()");

View File

@@ -15,9 +15,9 @@ using namespace frc;
// The Preferences table name
static wpi::StringRef kTableName{"Preferences"};
Preferences& Preferences::GetInstance() {
Preferences* Preferences::GetInstance() {
static Preferences instance;
return instance;
return &instance;
}
std::vector<std::string> Preferences::GetKeys() {
@@ -165,5 +165,3 @@ Preferences::Preferences()
NT_NOTIFY_NEW | NT_NOTIFY_IMMEDIATE);
HAL_Report(HALUsageReporting::kResourceType_Preferences, 0);
}
Preferences::~Preferences() = default;

View File

@@ -61,9 +61,9 @@ std::shared_ptr<LiveWindow::Impl::Component> LiveWindow::Impl::GetOrAdd(
return data;
}
LiveWindow& LiveWindow::GetInstance() {
LiveWindow* LiveWindow::GetInstance() {
static LiveWindow instance;
return instance;
return &instance;
}
void LiveWindow::EnableTelemetry(Sendable* sendable) {

View File

@@ -214,7 +214,7 @@ RobotBase::RobotBase() : m_ds(DriverStation::GetInstance()) {
->GetEntry("LW Enabled")
.SetBoolean(false);
LiveWindow::GetInstance().SetEnabled(false);
LiveWindow::GetInstance()->SetEnabled(false);
}
RobotBase::RobotBase(RobotBase&&) noexcept

View File

@@ -34,9 +34,9 @@ class Preferences {
/**
* Get the one and only {@link Preferences} object.
*
* @return reference to the {@link Preferences}
* @return pointer to the {@link Preferences}
*/
static Preferences& GetInstance();
static Preferences* GetInstance();
/**
* Returns a vector of all the keys.
@@ -292,7 +292,6 @@ class Preferences {
protected:
Preferences();
~Preferences();
Preferences(Preferences&&) = default;
Preferences& operator=(Preferences&&) = default;

View File

@@ -29,7 +29,7 @@ class LiveWindow {
* This is a singleton to guarantee that there is only a single instance
* regardless of how many times GetInstance is called.
*/
static LiveWindow& GetInstance();
static LiveWindow* GetInstance();
/**
* Enable telemetry for a single component.
@@ -69,7 +69,6 @@ class LiveWindow {
private:
LiveWindow();
~LiveWindow() = default;
struct Impl;
std::unique_ptr<Impl> m_impl;

View File

@@ -19,15 +19,15 @@ class Robot : public frc::TimedRobot {
static void VisionThread() {
// Get the Axis camera from CameraServer
cs::AxisCamera camera =
frc::CameraServer::GetInstance().AddAxisCamera("axis-camera.local");
frc::CameraServer::GetInstance()->AddAxisCamera("axis-camera.local");
// Set the resolution
camera.SetResolution(640, 480);
// Get a CvSink. This will capture Mats from the Camera
cs::CvSink cvSink = frc::CameraServer::GetInstance().GetVideo();
cs::CvSink cvSink = frc::CameraServer::GetInstance()->GetVideo();
// Setup a CvSource. This will send images back to the Dashboard
cs::CvSource outputStream =
frc::CameraServer::GetInstance().PutVideo("Rectangle", 640, 480);
frc::CameraServer::GetInstance()->PutVideo("Rectangle", 640, 480);
// Mats are very memory expensive. Lets reuse this Mat.
cv::Mat mat;

View File

@@ -50,7 +50,7 @@ class Robot : public frc::TimedRobot {
frc::DifferentialDrive m_robotDrive{m_left, m_right};
frc::Joystick m_stick{0};
frc::LiveWindow& m_lw = frc::LiveWindow::GetInstance();
frc::LiveWindow& m_lw = *frc::LiveWindow::GetInstance();
frc::Timer m_timer;
};

View File

@@ -24,15 +24,15 @@ class Robot : public frc::TimedRobot {
static void VisionThread() {
// Get the USB camera from CameraServer
cs::UsbCamera camera =
frc::CameraServer::GetInstance().StartAutomaticCapture();
frc::CameraServer::GetInstance()->StartAutomaticCapture();
// Set the resolution
camera.SetResolution(640, 480);
// Get a CvSink. This will capture Mats from the Camera
cs::CvSink cvSink = frc::CameraServer::GetInstance().GetVideo();
cs::CvSink cvSink = frc::CameraServer::GetInstance()->GetVideo();
// Setup a CvSource. This will send images back to the Dashboard
cs::CvSource outputStream =
frc::CameraServer::GetInstance().PutVideo("Rectangle", 640, 480);
frc::CameraServer::GetInstance()->PutVideo("Rectangle", 640, 480);
// Mats are very memory expensive. Lets reuse this Mat.
cv::Mat mat;

View File

@@ -16,7 +16,7 @@ class Robot : public frc::TimedRobot {
public:
void RobotInit() override {
#if defined(__linux__)
frc::CameraServer::GetInstance().StartAutomaticCapture();
frc::CameraServer::GetInstance()->StartAutomaticCapture();
#else
wpi::errs() << "Vision only available on Linux.\n";
wpi::errs().flush();

View File

@@ -21,7 +21,7 @@ void Robot::Teleop() {}
void Robot::Test() {}
void Robot::StartCompetition() {
auto& lw = frc::LiveWindow::GetInstance();
auto& lw = *frc::LiveWindow::GetInstance();
RobotInit();

View File

@@ -44,14 +44,14 @@ TEST(PreferencesTest, ReadPreferencesFromFile) {
preferencesFile.close();
inst.StartServer();
Preferences& preferences = Preferences::GetInstance();
Preferences* preferences = Preferences::GetInstance();
EXPECT_EQ("Hello, preferences file",
preferences.GetString("testFileGetString"));
EXPECT_EQ(1, preferences.GetInt("testFileGetInt"));
EXPECT_FLOAT_EQ(0.5, preferences.GetDouble("testFileGetDouble"));
EXPECT_FLOAT_EQ(0.25f, preferences.GetFloat("testFileGetFloat"));
EXPECT_TRUE(preferences.GetBoolean("testFileGetBoolean"));
EXPECT_EQ(1000000000000000000ll, preferences.GetLong("testFileGetLong"));
preferences->GetString("testFileGetString"));
EXPECT_EQ(1, preferences->GetInt("testFileGetInt"));
EXPECT_FLOAT_EQ(0.5, preferences->GetDouble("testFileGetDouble"));
EXPECT_FLOAT_EQ(0.25f, preferences->GetFloat("testFileGetFloat"));
EXPECT_TRUE(preferences->GetBoolean("testFileGetBoolean"));
EXPECT_EQ(1000000000000000000ll, preferences->GetLong("testFileGetLong"));
}
/**
@@ -61,22 +61,22 @@ TEST(PreferencesTest, ReadPreferencesFromFile) {
TEST(PreferencesTest, WritePreferencesToFile) {
auto inst = nt::NetworkTableInstance::GetDefault();
inst.StartServer();
Preferences& preferences = Preferences::GetInstance();
preferences.Remove("testFileGetString");
preferences.Remove("testFileGetInt");
preferences.Remove("testFileGetDouble");
preferences.Remove("testFileGetFloat");
preferences.Remove("testFileGetBoolean");
preferences.Remove("testFileGetLong");
Preferences* preferences = Preferences::GetInstance();
preferences->Remove("testFileGetString");
preferences->Remove("testFileGetInt");
preferences->Remove("testFileGetDouble");
preferences->Remove("testFileGetFloat");
preferences->Remove("testFileGetBoolean");
preferences->Remove("testFileGetLong");
Wait(kSaveTime);
preferences.PutString("testFilePutString", "Hello, preferences file");
preferences.PutInt("testFilePutInt", 1);
preferences.PutDouble("testFilePutDouble", 0.5);
preferences.PutFloat("testFilePutFloat", 0.25f);
preferences.PutBoolean("testFilePutBoolean", true);
preferences.PutLong("testFilePutLong", 1000000000000000000ll);
preferences->PutString("testFilePutString", "Hello, preferences file");
preferences->PutInt("testFilePutInt", 1);
preferences->PutDouble("testFilePutDouble", 0.5);
preferences->PutFloat("testFilePutFloat", 0.25f);
preferences->PutBoolean("testFilePutBoolean", true);
preferences->PutLong("testFilePutLong", 1000000000000000000ll);
Wait(kSaveTime);

View File

@@ -40,7 +40,7 @@ class TestEnvironment : public testing::Environment {
station returns that the robot is enabled, to ensure that tests
will be able to run on the hardware. */
HAL_ObserveUserProgramStarting();
LiveWindow::GetInstance().SetEnabled(false);
LiveWindow::GetInstance()->SetEnabled(false);
wpi::outs() << "Started coms\n";