From 98f2d08103c685786e8faed37e7f5afeba43ec9e Mon Sep 17 00:00:00 2001 From: James Kuszmaul Date: Tue, 30 Jun 2015 15:01:20 -0400 Subject: [PATCH] Changed const char* -> string in most of wpilibc. The HAL will remain untouched in order to maintain C-style compatibility. A few places in wpilibc were left as C-style strings, especially if special formatting (eg, elaborate uses of snprintf or sscanf) was being used. In general, const char* was changed to std::string. character buffers used for formatting were either untouched, changed to std::stringstream, or changed to std::string, depending on what was done with the buffer. Change-Id: I5e431ddf1cc4d9a6d534e1f21b16ea23be26e7f1 --- .../wpilibC++/include/Buttons/NetworkButton.h | 4 +- wpilibc/wpilibC++/include/Commands/Command.h | 6 +- .../wpilibC++/include/Commands/CommandGroup.h | 2 +- .../wpilibC++/include/Commands/PIDCommand.h | 6 +- .../wpilibC++/include/Commands/PIDSubsystem.h | 6 +- .../wpilibC++/include/Commands/PrintCommand.h | 2 +- .../wpilibC++/include/Commands/Subsystem.h | 2 +- .../wpilibC++/include/Commands/WaitCommand.h | 2 +- .../include/Commands/WaitForChildren.h | 2 +- .../include/Commands/WaitUntilCommand.h | 2 +- .../wpilibC++/include/LiveWindow/LiveWindow.h | 8 +- wpilibc/wpilibC++/include/Resource.h | 4 +- .../include/SmartDashboard/SendableChooser.h | 4 +- wpilibc/wpilibC++/include/Task.h | 2 +- wpilibc/wpilibC++/include/Task.inc | 2 +- wpilibc/wpilibC++/include/Utility.h | 28 ++--- wpilibc/wpilibC++/include/WPIErrors.h | 4 +- .../wpilibC++/src/Buttons/NetworkButton.cpp | 4 +- wpilibc/wpilibC++/src/Commands/Command.cpp | 21 ++-- .../wpilibC++/src/Commands/CommandGroup.cpp | 2 +- wpilibc/wpilibC++/src/Commands/PIDCommand.cpp | 6 +- .../wpilibC++/src/Commands/PIDSubsystem.cpp | 6 +- .../wpilibC++/src/Commands/PrintCommand.cpp | 2 +- wpilibc/wpilibC++/src/Commands/Subsystem.cpp | 2 +- .../wpilibC++/src/Commands/WaitCommand.cpp | 2 +- .../src/Commands/WaitForChildren.cpp | 2 +- .../src/Commands/WaitUntilCommand.cpp | 2 +- .../wpilibC++/src/LiveWindow/LiveWindow.cpp | 14 ++- wpilibc/wpilibC++/src/Resource.cpp | 4 +- .../src/SmartDashboard/SendableChooser.cpp | 10 +- wpilibc/wpilibC++Devices/include/SerialPort.h | 2 +- .../src/AnalogAccelerometer.cpp | 5 + wpilibc/wpilibC++Devices/src/AnalogInput.cpp | 11 +- wpilibc/wpilibC++Devices/src/AnalogOutput.cpp | 10 +- .../src/AnalogPotentiometer.cpp | 5 + wpilibc/wpilibC++Devices/src/CANJaguar.cpp | 32 +++--- wpilibc/wpilibC++Devices/src/Counter.cpp | 18 ++++ wpilibc/wpilibC++Devices/src/DigitalInput.cpp | 8 +- .../wpilibC++Devices/src/DigitalOutput.cpp | 8 +- .../wpilibC++Devices/src/DoubleSolenoid.cpp | 30 +++--- wpilibc/wpilibC++Devices/src/Encoder.cpp | 9 ++ wpilibc/wpilibC++Devices/src/Gyro.cpp | 3 + .../wpilibC++Devices/src/PIDController.cpp | 12 +-- wpilibc/wpilibC++Devices/src/PWM.cpp | 8 +- .../src/PowerDistributionPanel.cpp | 8 +- wpilibc/wpilibC++Devices/src/Relay.cpp | 16 +-- wpilibc/wpilibC++Devices/src/RobotDrive.cpp | 4 + wpilibc/wpilibC++Devices/src/SerialPort.cpp | 4 +- wpilibc/wpilibC++Devices/src/Solenoid.cpp | 16 +-- wpilibc/wpilibC++Devices/src/Task.cpp | 2 +- wpilibc/wpilibC++Devices/src/Ultrasonic.cpp | 6 ++ wpilibc/wpilibC++Devices/src/Utility.cpp | 38 +++---- .../src/command/CommandTest.cpp | 2 +- wpilibc/wpilibC++Sim/src/PIDController.cpp | 12 +-- wpilibc/wpilibC++Sim/src/Utility.cpp | 102 +++++++++--------- 55 files changed, 300 insertions(+), 234 deletions(-) diff --git a/wpilibc/wpilibC++/include/Buttons/NetworkButton.h b/wpilibc/wpilibC++/include/Buttons/NetworkButton.h index be003d3a17..4a37e9d5b6 100644 --- a/wpilibc/wpilibC++/include/Buttons/NetworkButton.h +++ b/wpilibc/wpilibC++/include/Buttons/NetworkButton.h @@ -14,8 +14,8 @@ class NetworkButton : public Button { public: - NetworkButton(const char *tableName, const char *field); - NetworkButton(::std::shared_ptr table, const char *field); + NetworkButton(const std::string &tableName, const std::string &field); + NetworkButton(::std::shared_ptr table, const std::string &field); virtual ~NetworkButton() = default; virtual bool Get(); diff --git a/wpilibc/wpilibC++/include/Commands/Command.h b/wpilibc/wpilibC++/include/Commands/Command.h index 6f2747bc3d..9c6463ca9c 100644 --- a/wpilibc/wpilibC++/include/Commands/Command.h +++ b/wpilibc/wpilibC++/include/Commands/Command.h @@ -56,9 +56,9 @@ class Command : public ErrorBase, public NamedSendable, public ITableListener { public: Command(); - Command(const char *name); + Command(const std::string &name); Command(double timeout); - Command(const char *name, double timeout); + Command(const std::string &name, double timeout); virtual ~Command(); double TimeSinceInitialized() const; void Requires(Subsystem *s); @@ -80,7 +80,7 @@ class Command : public ErrorBase, public NamedSendable, public ITableListener { protected: void SetTimeout(double timeout); bool IsTimedOut() const; - bool AssertUnlocked(const char *message); + bool AssertUnlocked(const std::string &message); void SetParent(CommandGroup *parent); /** * The initialize method is called the first time this Command is run after diff --git a/wpilibc/wpilibC++/include/Commands/CommandGroup.h b/wpilibc/wpilibC++/include/Commands/CommandGroup.h index c7a87a060a..7f289cac8e 100644 --- a/wpilibc/wpilibC++/include/Commands/CommandGroup.h +++ b/wpilibc/wpilibC++/include/Commands/CommandGroup.h @@ -37,7 +37,7 @@ class CommandGroup : public Command { public: CommandGroup() = default; - CommandGroup(const char *name); + CommandGroup(const std::string &name); virtual ~CommandGroup() = default; void AddSequential(Command *command); diff --git a/wpilibc/wpilibC++/include/Commands/PIDCommand.h b/wpilibc/wpilibC++/include/Commands/PIDCommand.h index a4038cb135..216818febd 100644 --- a/wpilibc/wpilibC++/include/Commands/PIDCommand.h +++ b/wpilibc/wpilibC++/include/Commands/PIDCommand.h @@ -18,9 +18,9 @@ class PIDController; class PIDCommand : public Command, public PIDOutput, public PIDSource { public: - PIDCommand(const char *name, double p, double i, double d); - PIDCommand(const char *name, double p, double i, double d, double period); - PIDCommand(const char *name, double p, double i, double d, double f, + PIDCommand(const std::string &name, double p, double i, double d); + PIDCommand(const std::string &name, double p, double i, double d, double period); + PIDCommand(const std::string &name, double p, double i, double d, double f, double perioid); PIDCommand(double p, double i, double d); PIDCommand(double p, double i, double d, double period); diff --git a/wpilibc/wpilibC++/include/Commands/PIDSubsystem.h b/wpilibc/wpilibC++/include/Commands/PIDSubsystem.h index f5e4e14280..4c32c489b5 100644 --- a/wpilibc/wpilibC++/include/Commands/PIDSubsystem.h +++ b/wpilibc/wpilibC++/include/Commands/PIDSubsystem.h @@ -29,9 +29,9 @@ */ class PIDSubsystem : public Subsystem, public PIDOutput, public PIDSource { public: - PIDSubsystem(const char *name, double p, double i, double d); - PIDSubsystem(const char *name, double p, double i, double d, double f); - PIDSubsystem(const char *name, double p, double i, double d, double f, + PIDSubsystem(const std::string &name, double p, double i, double d); + PIDSubsystem(const std::string &name, double p, double i, double d, double f); + PIDSubsystem(const std::string &name, double p, double i, double d, double f, double period); PIDSubsystem(double p, double i, double d); PIDSubsystem(double p, double i, double d, double f); diff --git a/wpilibc/wpilibC++/include/Commands/PrintCommand.h b/wpilibc/wpilibC++/include/Commands/PrintCommand.h index 14ab971386..cbb3e5eea4 100644 --- a/wpilibc/wpilibC++/include/Commands/PrintCommand.h +++ b/wpilibc/wpilibC++/include/Commands/PrintCommand.h @@ -13,7 +13,7 @@ class PrintCommand : public Command { public: - PrintCommand(const char *message); + PrintCommand(const std::string &message); virtual ~PrintCommand() = default; protected: diff --git a/wpilibc/wpilibC++/include/Commands/Subsystem.h b/wpilibc/wpilibC++/include/Commands/Subsystem.h index 9192c5155a..c9581ffd6c 100644 --- a/wpilibc/wpilibC++/include/Commands/Subsystem.h +++ b/wpilibc/wpilibC++/include/Commands/Subsystem.h @@ -19,7 +19,7 @@ class Subsystem : public ErrorBase, public NamedSendable { friend class Scheduler; public: - Subsystem(const char *name); + Subsystem(const std::string &name); virtual ~Subsystem() = default; void SetDefaultCommand(Command *command); diff --git a/wpilibc/wpilibC++/include/Commands/WaitCommand.h b/wpilibc/wpilibC++/include/Commands/WaitCommand.h index 33e45e9183..6db6dac3e4 100644 --- a/wpilibc/wpilibC++/include/Commands/WaitCommand.h +++ b/wpilibc/wpilibC++/include/Commands/WaitCommand.h @@ -13,7 +13,7 @@ class WaitCommand : public Command { public: WaitCommand(double timeout); - WaitCommand(const char *name, double timeout); + WaitCommand(const std::string &name, double timeout); virtual ~WaitCommand() = default; protected: diff --git a/wpilibc/wpilibC++/include/Commands/WaitForChildren.h b/wpilibc/wpilibC++/include/Commands/WaitForChildren.h index 60db4aff06..858d243f16 100644 --- a/wpilibc/wpilibC++/include/Commands/WaitForChildren.h +++ b/wpilibc/wpilibC++/include/Commands/WaitForChildren.h @@ -13,7 +13,7 @@ class WaitForChildren : public Command { public: WaitForChildren(double timeout); - WaitForChildren(const char *name, double timeout); + WaitForChildren(const std::string &name, double timeout); virtual ~WaitForChildren() = default; protected: diff --git a/wpilibc/wpilibC++/include/Commands/WaitUntilCommand.h b/wpilibc/wpilibC++/include/Commands/WaitUntilCommand.h index d445bfae1b..fd77f8e30d 100644 --- a/wpilibc/wpilibC++/include/Commands/WaitUntilCommand.h +++ b/wpilibc/wpilibC++/include/Commands/WaitUntilCommand.h @@ -13,7 +13,7 @@ class WaitUntilCommand : public Command { public: WaitUntilCommand(double time); - WaitUntilCommand(const char *name, double time); + WaitUntilCommand(const std::string &name, double time); virtual ~WaitUntilCommand() = default; protected: diff --git a/wpilibc/wpilibC++/include/LiveWindow/LiveWindow.h b/wpilibc/wpilibC++/include/LiveWindow/LiveWindow.h index 1a50f2e2b2..037a6a242e 100644 --- a/wpilibc/wpilibC++/include/LiveWindow/LiveWindow.h +++ b/wpilibc/wpilibC++/include/LiveWindow/LiveWindow.h @@ -35,16 +35,16 @@ class LiveWindow { [[deprecated( "Raw pointers are deprecated; pass the component using shared_ptr " "instead.")]] - void AddSensor(const char *subsystem, const char *name, + void AddSensor(const std::string &subsystem, const std::string &name, LiveWindowSendable *component); - void AddSensor(const char *subsystem, const char *name, + void AddSensor(const std::string &subsystem, const std::string &name, ::std::shared_ptr component); [[deprecated( "Raw pointers are deprecated; pass the component using shared_ptr " "instead.")]] - void AddActuator(const char *subsystem, const char *name, + void AddActuator(const std::string &subsystem, const std::string &name, LiveWindowSendable *component); - void AddActuator(const char *subsystem, const char *name, + void AddActuator(const std::string &subsystem, const std::string &name, ::std::shared_ptr component); void AddSensor(std::string type, int channel, LiveWindowSendable *component); void AddActuator(std::string type, int channel, diff --git a/wpilibc/wpilibC++/include/Resource.h b/wpilibc/wpilibC++/include/Resource.h index 599c73f16e..7b8ea709bf 100644 --- a/wpilibc/wpilibC++/include/Resource.h +++ b/wpilibc/wpilibC++/include/Resource.h @@ -27,8 +27,8 @@ class Resource : public ErrorBase { virtual ~Resource() = default; static void CreateResourceObject(::std::unique_ptr& r, uint32_t elements); explicit Resource(uint32_t size); - uint32_t Allocate(const char *resourceDesc); - uint32_t Allocate(uint32_t index, const char *resourceDesc); + uint32_t Allocate(const std::string &resourceDesc); + uint32_t Allocate(uint32_t index, const std::string &resourceDesc); void Free(uint32_t index); private: diff --git a/wpilibc/wpilibC++/include/SmartDashboard/SendableChooser.h b/wpilibc/wpilibC++/include/SmartDashboard/SendableChooser.h index 652244fd70..b3f92e5372 100644 --- a/wpilibc/wpilibC++/include/SmartDashboard/SendableChooser.h +++ b/wpilibc/wpilibC++/include/SmartDashboard/SendableChooser.h @@ -35,8 +35,8 @@ class SendableChooser : public Sendable { public: virtual ~SendableChooser() = default; - void AddObject(const char *name, void *object); - void AddDefault(const char *name, void *object); + void AddObject(const std::string &name, void *object); + void AddDefault(const std::string &name, void *object); void *GetSelected(); virtual void InitTable(::std::shared_ptr subtable); diff --git a/wpilibc/wpilibC++/include/Task.h b/wpilibc/wpilibC++/include/Task.h index 1501a214eb..326989f43f 100644 --- a/wpilibc/wpilibC++/include/Task.h +++ b/wpilibc/wpilibC++/include/Task.h @@ -28,7 +28,7 @@ class Task : public ErrorBase { Task& operator=(Task&& task); template - Task(const char* name, Function&& function, Args&&... args); + Task(const std::string& name, Function&& function, Args&&... args); virtual ~Task(); diff --git a/wpilibc/wpilibC++/include/Task.inc b/wpilibc/wpilibC++/include/Task.inc index 26277ab359..2a76938275 100644 --- a/wpilibc/wpilibC++/include/Task.inc +++ b/wpilibc/wpilibC++/include/Task.inc @@ -10,7 +10,7 @@ * @param stackSize The size of the stack for the task */ template -Task::Task(const char* name, Function&& function, Args&&... args) { +Task::Task(const std::string& name, Function&& function, Args&&... args) { m_taskName = "FRC_"; m_taskName += name; diff --git a/wpilibc/wpilibC++/include/Utility.h b/wpilibc/wpilibC++/include/Utility.h index 345b694c66..3d453f0e29 100644 --- a/wpilibc/wpilibC++/include/Utility.h +++ b/wpilibc/wpilibC++/include/Utility.h @@ -14,33 +14,33 @@ #include #define wpi_assert(condition) \ - wpi_assert_impl(condition, #condition, nullptr, __FILE__, __LINE__, __FUNCTION__) + wpi_assert_impl(condition, #condition, "", __FILE__, __LINE__, __FUNCTION__) #define wpi_assertWithMessage(condition, message) \ wpi_assert_impl(condition, #condition, message, __FILE__, __LINE__, \ __FUNCTION__) #define wpi_assertEqual(a, b) \ - wpi_assertEqual_impl(a, b, #a, #b, nullptr, __FILE__, __LINE__, __FUNCTION__) + wpi_assertEqual_impl(a, b, #a, #b, "", __FILE__, __LINE__, __FUNCTION__) #define wpi_assertEqualWithMessage(a, b, message) \ wpi_assertEqual_impl(a, b, #a, #b, message, __FILE__, __LINE__, __FUNCTION__) #define wpi_assertNotEqual(a, b) \ - wpi_assertNotEqual_impl(a, b, #a, #b, nullptr, __FILE__, __LINE__, __FUNCTION__) + wpi_assertNotEqual_impl(a, b, #a, #b, "", __FILE__, __LINE__, __FUNCTION__) #define wpi_assertNotEqualWithMessage(a, b, message) \ wpi_assertNotEqual_impl(a, b, #a, #b, message, __FILE__, __LINE__, \ __FUNCTION__) -bool wpi_assert_impl(bool conditionValue, const char *conditionText, - const char *message, const char *fileName, - uint32_t lineNumber, const char *funcName); -bool wpi_assertEqual_impl(int valueA, int valueB, const char *valueAString, - const char *valueBString, const char *message, - const char *fileName, uint32_t lineNumber, - const char *funcName); -bool wpi_assertNotEqual_impl(int valueA, int valueB, const char *valueAString, - const char *valueBString, const char *message, - const char *fileName, uint32_t lineNumber, - const char *funcName); +bool wpi_assert_impl(bool conditionValue, const std::string &conditionText, + const std::string &message, const std::string &fileName, + uint32_t lineNumber, const std::string &funcName); +bool wpi_assertEqual_impl(int valueA, int valueB, const std::string &valueAString, + const std::string &valueBString, const std::string &message, + const std::string &fileName, uint32_t lineNumber, + const std::string &funcName); +bool wpi_assertNotEqual_impl(int valueA, int valueB, const std::string &valueAString, + const std::string &valueBString, const std::string &message, + const std::string &fileName, uint32_t lineNumber, + const std::string &funcName); void wpi_suspendOnAssertEnabled(bool enabled); diff --git a/wpilibc/wpilibC++/include/WPIErrors.h b/wpilibc/wpilibC++/include/WPIErrors.h index 25cb5fe7be..8346fdf5e6 100644 --- a/wpilibc/wpilibC++/include/WPIErrors.h +++ b/wpilibc/wpilibC++/include/WPIErrors.h @@ -10,11 +10,11 @@ #ifdef WPI_ERRORS_DEFINE_STRINGS #define S(label, offset, message) \ - const char *wpi_error_s_##label = message; \ + const char * wpi_error_s_##label = message; \ const int32_t wpi_error_value_##label = offset #else #define S(label, offset, message) \ - extern const char *wpi_error_s_##label; \ + extern const char * wpi_error_s_##label; \ const int32_t wpi_error_value_##label = offset #endif diff --git a/wpilibc/wpilibC++/src/Buttons/NetworkButton.cpp b/wpilibc/wpilibC++/src/Buttons/NetworkButton.cpp index e8020e1af8..dd54269344 100644 --- a/wpilibc/wpilibC++/src/Buttons/NetworkButton.cpp +++ b/wpilibc/wpilibC++/src/Buttons/NetworkButton.cpp @@ -8,12 +8,12 @@ #include "Buttons/NetworkButton.h" #include "networktables/NetworkTable.h" -NetworkButton::NetworkButton(const char *tableName, const char *field) +NetworkButton::NetworkButton(const std::string &tableName, const std::string &field) : // TODO how is this supposed to work??? m_netTable(NetworkTable::GetTable(tableName)), m_field(field) {} -NetworkButton::NetworkButton(::std::shared_ptr table, const char *field) +NetworkButton::NetworkButton(::std::shared_ptr table, const std::string &field) : m_netTable(table), m_field(field) {} bool NetworkButton::Get() { diff --git a/wpilibc/wpilibC++/src/Commands/Command.cpp b/wpilibc/wpilibC++/src/Commands/Command.cpp index 395e184fb5..4317bc4b0d 100644 --- a/wpilibc/wpilibC++/src/Commands/Command.cpp +++ b/wpilibc/wpilibC++/src/Commands/Command.cpp @@ -13,9 +13,9 @@ #include "WPIErrors.h" #include -static const char *kName = "name"; -static const char *kRunning = "running"; -static const char *kIsParented = "isParented"; +static const std::string kName = "name"; +static const std::string kRunning = "running"; +static const std::string kIsParented = "isParented"; int Command::m_commandCounter = 0; @@ -29,7 +29,7 @@ Command::Command() : Command(nullptr, -1.0) {} * Creates a new command with the given name and no timeout. * @param name the name for this command */ -Command::Command(const char *name) : Command(name, -1.0) {} +Command::Command(const std::string &name) : Command(name, -1.0) {} /** * Creates a new command with the given timeout and a default name. @@ -44,13 +44,13 @@ Command::Command(double timeout) : Command(nullptr, timeout) {} * @param timeout the time (in seconds) before this command "times out" * @see Command#isTimedOut() isTimedOut() */ -Command::Command(const char *name, double timeout) { - if (name == nullptr) wpi_setWPIErrorWithContext(NullParameter, "name"); +Command::Command(const std::string &name, double timeout) { + if (name.size() == 0) wpi_setWPIErrorWithContext(NullParameter, "name"); if (timeout < 0.0) wpi_setWPIErrorWithContext(ParameterOutOfRange, "timeout < 0.0"); m_timeout = timeout; - m_name = name == nullptr ? std::string() : name; + m_name = name; } Command::~Command() { // TODO deal with cleaning up all listeners @@ -218,12 +218,9 @@ void Command::LockChanges() { m_locked = true; } * message) * @return true if assert passed, false if assert failed */ -bool Command::AssertUnlocked(const char *message) { +bool Command::AssertUnlocked(const std::string &message) { if (m_locked) { - char buf[128]; - snprintf(buf, 128, - "%s after being started or being added to a command group", - message); + std::string buf = message + " after being started or being added to a command group"; wpi_setWPIErrorWithContext(CommandIllegalUse, buf); return false; } diff --git a/wpilibc/wpilibC++/src/Commands/CommandGroup.cpp b/wpilibc/wpilibC++/src/Commands/CommandGroup.cpp index 43be3b87ca..edb5ec9299 100644 --- a/wpilibc/wpilibC++/src/Commands/CommandGroup.cpp +++ b/wpilibc/wpilibC++/src/Commands/CommandGroup.cpp @@ -12,7 +12,7 @@ * Creates a new {@link CommandGroup CommandGroup} with the given name. * @param name the name for this command group */ -CommandGroup::CommandGroup(const char *name) : Command(name) {} +CommandGroup::CommandGroup(const std::string &name) : Command(name) {} /** * Adds a new {@link Command Command} to the group. The {@link Command Command} diff --git a/wpilibc/wpilibC++/src/Commands/PIDCommand.cpp b/wpilibc/wpilibC++/src/Commands/PIDCommand.cpp index b3a4c9829b..e508e366b4 100644 --- a/wpilibc/wpilibC++/src/Commands/PIDCommand.cpp +++ b/wpilibc/wpilibC++/src/Commands/PIDCommand.cpp @@ -10,7 +10,7 @@ #include "PIDController.h" #include "float.h" -PIDCommand::PIDCommand(const char *name, double p, double i, double d, double f, +PIDCommand::PIDCommand(const std::string &name, double p, double i, double d, double f, double period) : Command(name) { m_controller = std::make_unique(p, i, d, this, this, period); @@ -20,12 +20,12 @@ PIDCommand::PIDCommand(double p, double i, double d, double f, double period) { m_controller = std::make_unique(p, i, d, f, this, this, period); } -PIDCommand::PIDCommand(const char *name, double p, double i, double d) +PIDCommand::PIDCommand(const std::string &name, double p, double i, double d) : Command(name) { m_controller = std::make_unique(p, i, d, this, this); } -PIDCommand::PIDCommand(const char *name, double p, double i, double d, +PIDCommand::PIDCommand(const std::string &name, double p, double i, double d, double period) : Command(name) { m_controller = std::make_unique(p, i, d, this, this, period); diff --git a/wpilibc/wpilibC++/src/Commands/PIDSubsystem.cpp b/wpilibc/wpilibC++/src/Commands/PIDSubsystem.cpp index 1dcdc6b240..94fa7962e0 100644 --- a/wpilibc/wpilibC++/src/Commands/PIDSubsystem.cpp +++ b/wpilibc/wpilibC++/src/Commands/PIDSubsystem.cpp @@ -17,7 +17,7 @@ * @param i the integral value * @param d the derivative value */ -PIDSubsystem::PIDSubsystem(const char *name, double p, double i, double d) +PIDSubsystem::PIDSubsystem(const std::string &name, double p, double i, double d) : Subsystem(name) { m_controller = std::make_unique(p, i, d, this, this); } @@ -31,7 +31,7 @@ PIDSubsystem::PIDSubsystem(const char *name, double p, double i, double d) * @param d the derivative value * @param f the feedforward value */ -PIDSubsystem::PIDSubsystem(const char *name, double p, double i, double d, +PIDSubsystem::PIDSubsystem(const std::string &name, double p, double i, double d, double f) : Subsystem(name) { m_controller = std::make_unique(p, i, d, f, this, this); @@ -48,7 +48,7 @@ PIDSubsystem::PIDSubsystem(const char *name, double p, double i, double d, * @param f the feedfoward value * @param period the time (in seconds) between calculations */ -PIDSubsystem::PIDSubsystem(const char *name, double p, double i, double d, +PIDSubsystem::PIDSubsystem(const std::string &name, double p, double i, double d, double f, double period) : Subsystem(name) { m_controller = std::make_unique(p, i, d, f, this, this, period); diff --git a/wpilibc/wpilibC++/src/Commands/PrintCommand.cpp b/wpilibc/wpilibC++/src/Commands/PrintCommand.cpp index 51145de850..b5e8dc7f8a 100644 --- a/wpilibc/wpilibC++/src/Commands/PrintCommand.cpp +++ b/wpilibc/wpilibC++/src/Commands/PrintCommand.cpp @@ -9,7 +9,7 @@ #include "stdio.h" #include -PrintCommand::PrintCommand(const char *message) +PrintCommand::PrintCommand(const std::string &message) : Command(((std::stringstream &)(std::stringstream("Print \"") << message << "\"")) .str() diff --git a/wpilibc/wpilibC++/src/Commands/Subsystem.cpp b/wpilibc/wpilibC++/src/Commands/Subsystem.cpp index cda458994d..0fb1ce7b15 100644 --- a/wpilibc/wpilibC++/src/Commands/Subsystem.cpp +++ b/wpilibc/wpilibC++/src/Commands/Subsystem.cpp @@ -15,7 +15,7 @@ * Creates a subsystem with the given name * @param name the name of the subsystem */ -Subsystem::Subsystem(const char *name) { +Subsystem::Subsystem(const std::string &name) { m_name = name; Scheduler::GetInstance().RegisterSubsystem(this); } diff --git a/wpilibc/wpilibC++/src/Commands/WaitCommand.cpp b/wpilibc/wpilibC++/src/Commands/WaitCommand.cpp index d11c915605..f4e4c00ef8 100644 --- a/wpilibc/wpilibC++/src/Commands/WaitCommand.cpp +++ b/wpilibc/wpilibC++/src/Commands/WaitCommand.cpp @@ -15,7 +15,7 @@ WaitCommand::WaitCommand(double timeout) .c_str(), timeout) {} -WaitCommand::WaitCommand(const char *name, double timeout) +WaitCommand::WaitCommand(const std::string &name, double timeout) : Command(name, timeout) {} void WaitCommand::Initialize() {} diff --git a/wpilibc/wpilibC++/src/Commands/WaitForChildren.cpp b/wpilibc/wpilibC++/src/Commands/WaitForChildren.cpp index 2ec602fa01..a3a129da27 100644 --- a/wpilibc/wpilibC++/src/Commands/WaitForChildren.cpp +++ b/wpilibc/wpilibC++/src/Commands/WaitForChildren.cpp @@ -11,7 +11,7 @@ WaitForChildren::WaitForChildren(double timeout) : Command("WaitForChildren", timeout) {} -WaitForChildren::WaitForChildren(const char *name, double timeout) +WaitForChildren::WaitForChildren(const std::string &name, double timeout) : Command(name, timeout) {} void WaitForChildren::Initialize() {} diff --git a/wpilibc/wpilibC++/src/Commands/WaitUntilCommand.cpp b/wpilibc/wpilibC++/src/Commands/WaitUntilCommand.cpp index 989047966a..ea0e6645fe 100644 --- a/wpilibc/wpilibC++/src/Commands/WaitUntilCommand.cpp +++ b/wpilibc/wpilibC++/src/Commands/WaitUntilCommand.cpp @@ -19,7 +19,7 @@ WaitUntilCommand::WaitUntilCommand(double time) m_time = time; } -WaitUntilCommand::WaitUntilCommand(const char *name, double time) +WaitUntilCommand::WaitUntilCommand(const std::string &name, double time) : Command(name, time) { m_time = time; } diff --git a/wpilibc/wpilibC++/src/LiveWindow/LiveWindow.cpp b/wpilibc/wpilibC++/src/LiveWindow/LiveWindow.cpp index a7ed789853..5839ae24d8 100644 --- a/wpilibc/wpilibC++/src/LiveWindow/LiveWindow.cpp +++ b/wpilibc/wpilibC++/src/LiveWindow/LiveWindow.cpp @@ -56,14 +56,17 @@ void LiveWindow::SetEnabled(bool enabled) { * @param name The name of this component. * @param component A LiveWindowSendable component that represents a sensor. */ -void LiveWindow::AddSensor(const char *subsystem, const char *name, +void LiveWindow::AddSensor(const std::string &subsystem, const std::string &name, ::std::shared_ptr component) { m_components[component].subsystem = subsystem; m_components[component].name = name; m_components[component].isSensor = true; } -void LiveWindow::AddSensor(const char *subsystem, const char *name, LiveWindowSendable *component) { +[[deprecated( + "Raw pointers are deprecated; pass the component using shared_ptr " + "instead.")]] +void LiveWindow::AddSensor(const std::string &subsystem, const std::string &name, LiveWindowSendable *component) { AddSensor(subsystem, name, ::std::shared_ptr( component, NullDeleter())); } @@ -75,14 +78,17 @@ void LiveWindow::AddSensor(const char *subsystem, const char *name, LiveWindowSe * @param name The name of this component. * @param component A LiveWindowSendable component that represents a actuator. */ -void LiveWindow::AddActuator(const char *subsystem, const char *name, +void LiveWindow::AddActuator(const std::string &subsystem, const std::string &name, ::std::shared_ptr component) { m_components[component].subsystem = subsystem; m_components[component].name = name; m_components[component].isSensor = false; } -void LiveWindow::AddActuator(const char *subsystem, const char *name, +[[deprecated( + "Raw pointers are deprecated; pass the component using shared_ptr " + "instead.")]] +void LiveWindow::AddActuator(const std::string &subsystem, const std::string &name, LiveWindowSendable *component) { AddActuator(subsystem, name, ::std::shared_ptr( diff --git a/wpilibc/wpilibC++/src/Resource.cpp b/wpilibc/wpilibC++/src/Resource.cpp index 37cd093dc6..a259bd39b4 100644 --- a/wpilibc/wpilibC++/src/Resource.cpp +++ b/wpilibc/wpilibC++/src/Resource.cpp @@ -47,7 +47,7 @@ Resource::Resource(uint32_t elements) { * resource value * within the range is located and returned after it is marked allocated. */ -uint32_t Resource::Allocate(const char *resourceDesc) { +uint32_t Resource::Allocate(const std::string &resourceDesc) { std::unique_lock sync(m_allocateLock); for (uint32_t i = 0; i < m_isAllocated.size(); i++) { if (!m_isAllocated[i]) { @@ -65,7 +65,7 @@ uint32_t Resource::Allocate(const char *resourceDesc) { * verified * unallocated, then returned. */ -uint32_t Resource::Allocate(uint32_t index, const char *resourceDesc) { +uint32_t Resource::Allocate(uint32_t index, const std::string &resourceDesc) { std::unique_lock sync(m_allocateLock); if (index >= m_isAllocated.size()) { wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, resourceDesc); diff --git a/wpilibc/wpilibC++/src/SmartDashboard/SendableChooser.cpp b/wpilibc/wpilibC++/src/SmartDashboard/SendableChooser.cpp index 24885de7d1..1cda6b2c39 100644 --- a/wpilibc/wpilibC++/src/SmartDashboard/SendableChooser.cpp +++ b/wpilibc/wpilibC++/src/SmartDashboard/SendableChooser.cpp @@ -10,9 +10,9 @@ #include -static const char *kDefault = "default"; -static const char *kOptions = "options"; -static const char *kSelected = "selected"; +static const std::string kDefault = "default"; +static const std::string kOptions = "options"; +static const std::string kSelected = "selected"; /** * Adds the given object to the list of options. On the {@link SmartDashboard} @@ -21,7 +21,7 @@ static const char *kSelected = "selected"; * @param name the name of the option * @param object the option */ -void SendableChooser::AddObject(const char *name, void *object) { +void SendableChooser::AddObject(const std::string &name, void *object) { m_choices[name] = object; } @@ -34,7 +34,7 @@ void SendableChooser::AddObject(const char *name, void *object) { * @param name the name of the option * @param object the option */ -void SendableChooser::AddDefault(const char *name, void *object) { +void SendableChooser::AddDefault(const std::string &name, void *object) { m_defaultChoice = name; AddObject(name, object); } diff --git a/wpilibc/wpilibC++Devices/include/SerialPort.h b/wpilibc/wpilibC++Devices/include/SerialPort.h index 04a61cc55d..168746c21f 100644 --- a/wpilibc/wpilibC++Devices/include/SerialPort.h +++ b/wpilibc/wpilibC++Devices/include/SerialPort.h @@ -53,7 +53,7 @@ class SerialPort : public ErrorBase { void DisableTermination(); int32_t GetBytesReceived(); uint32_t Read(char *buffer, int32_t count); - uint32_t Write(const char *buffer, int32_t count); + uint32_t Write(const std::string &buffer, int32_t count); void SetTimeout(float timeout); void SetReadBufferSize(uint32_t size); void SetWriteBufferSize(uint32_t size); diff --git a/wpilibc/wpilibC++Devices/src/AnalogAccelerometer.cpp b/wpilibc/wpilibC++Devices/src/AnalogAccelerometer.cpp index e33645c6c2..2ebec024d3 100644 --- a/wpilibc/wpilibC++Devices/src/AnalogAccelerometer.cpp +++ b/wpilibc/wpilibC++Devices/src/AnalogAccelerometer.cpp @@ -39,6 +39,11 @@ AnalogAccelerometer::AnalogAccelerometer(int32_t channel) { * @param channel The existing AnalogInput object for the analog input the * accelerometer is connected to */ +[[deprecated( + "Raw pointers are deprecated; if you just want to construct an " + "AnalogAccelerometer with its own AnalogInput, then call the " + "AnalogAccelerometer(int channel). If you want to keep your own copy of " + "the AnalogInput, use std::shared_ptr.")]] AnalogAccelerometer::AnalogAccelerometer(AnalogInput *channel) : m_analogInput(channel, NullDeleter()) { if (channel == nullptr) { diff --git a/wpilibc/wpilibC++Devices/src/AnalogInput.cpp b/wpilibc/wpilibC++Devices/src/AnalogInput.cpp index e063909ba3..31dec18340 100644 --- a/wpilibc/wpilibC++Devices/src/AnalogInput.cpp +++ b/wpilibc/wpilibC++Devices/src/AnalogInput.cpp @@ -11,6 +11,8 @@ #include "WPIErrors.h" #include "LiveWindow/LiveWindow.h" +#include + static ::std::unique_ptr inputs; const uint8_t AnalogInput::kAccumulatorModuleNumber; @@ -24,17 +26,16 @@ const uint32_t AnalogInput::kAccumulatorChannels[] = {0, 1}; * on-board 4-7 are on the MXP port. */ AnalogInput::AnalogInput(uint32_t channel) { - char buf[64]; + std::stringstream buf; + buf << "Analog Input " << channel; Resource::CreateResourceObject(inputs, kAnalogInputs); if (!checkAnalogInputChannel(channel)) { - snprintf(buf, 64, "analog input %d", channel); - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf); + wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); return; } - snprintf(buf, 64, "Analog Input %d", channel); - if (inputs->Allocate(channel, buf) == ~0ul) { + if (inputs->Allocate(channel, buf.str()) == ~0ul) { CloneError(*inputs); return; } diff --git a/wpilibc/wpilibC++Devices/src/AnalogOutput.cpp b/wpilibc/wpilibC++Devices/src/AnalogOutput.cpp index e449b4a177..4c3246e93c 100644 --- a/wpilibc/wpilibC++Devices/src/AnalogOutput.cpp +++ b/wpilibc/wpilibC++Devices/src/AnalogOutput.cpp @@ -10,6 +10,8 @@ #include "WPIErrors.h" #include "LiveWindow/LiveWindow.h" +#include + static ::std::unique_ptr outputs; /** @@ -20,15 +22,15 @@ static ::std::unique_ptr outputs; AnalogOutput::AnalogOutput(uint32_t channel) { Resource::CreateResourceObject(outputs, kAnalogOutputs); - char buf[64]; + std::stringstream buf; + buf << "analog input " << channel; if (!checkAnalogOutputChannel(channel)) { - snprintf(buf, 64, "analog input %d", channel); - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf); + wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); return; } - if (outputs->Allocate(channel, buf) == ~0ul) { + if (outputs->Allocate(channel, buf.str()) == ~0ul) { CloneError(*outputs); return; } diff --git a/wpilibc/wpilibC++Devices/src/AnalogPotentiometer.cpp b/wpilibc/wpilibC++Devices/src/AnalogPotentiometer.cpp index 74152a2096..74ac6bd8f4 100644 --- a/wpilibc/wpilibC++Devices/src/AnalogPotentiometer.cpp +++ b/wpilibc/wpilibC++Devices/src/AnalogPotentiometer.cpp @@ -25,6 +25,11 @@ AnalogPotentiometer::AnalogPotentiometer(int channel, double fullRange, * @param offset The angular value (in desired units) representing the angular * output at 0V. */ +[[deprecated( + "Raw pointers are deprecated; if you just want to construct an " + "AnalogPotentiometer with its own AnalogInput, then call the " + "AnalogPotentiometer(int channel). If you want to keep your own copy of " + "the AnalogInput, use std::shared_ptr.")]] AnalogPotentiometer::AnalogPotentiometer(AnalogInput *input, double fullRange, double offset) : m_analog_input(input, NullDeleter()), diff --git a/wpilibc/wpilibC++Devices/src/CANJaguar.cpp b/wpilibc/wpilibC++Devices/src/CANJaguar.cpp index da229dcbd5..5d3a2ca18d 100644 --- a/wpilibc/wpilibC++Devices/src/CANJaguar.cpp +++ b/wpilibc/wpilibC++Devices/src/CANJaguar.cpp @@ -111,10 +111,10 @@ void CANJaguar::InitCANJaguar() { m_hardwareVersion = dataBuffer[0]; if (m_deviceNumber < 1 || m_deviceNumber > 63) { - char buf[256]; - snprintf(buf, 256, "device number \"%d\" must be between 1 and 63", - m_deviceNumber); - wpi_setWPIErrorWithContext(ParameterOutOfRange, buf); + std::stringstream buf; + buf << "device number \"" << m_deviceNumber + << "\" must be between 1 and 63"; + wpi_setWPIErrorWithContext(ParameterOutOfRange, buf.str()); return; } @@ -122,19 +122,17 @@ void CANJaguar::InitCANJaguar() { // 3330 was the first shipping RDK firmware version for the Jaguar if (m_firmwareVersion >= 3330 || m_firmwareVersion < 108) { - char buf[256]; + std::stringstream buf; if (m_firmwareVersion < 3330) { - snprintf(buf, 256, - "Jag #%d firmware (%d) is too old (must be at least version 108 " - "of the FIRST approved firmware)", - m_deviceNumber, m_firmwareVersion); + buf << "Jag #" << m_deviceNumber << " firmware (" << m_firmwareVersion + << ") is too old (must be at least version 108 " + "of the FIRST approved firmware)"; } else { - snprintf(buf, 256, - "Jag #%d firmware (%d) is not FIRST approved (must be at least " - "version 108 of the FIRST approved firmware)", - m_deviceNumber, m_firmwareVersion); + buf << "Jag #" << m_deviceNumber << " firmware (" << m_firmwareVersion + << ") is not FIRST approved (must be at least " + "version 108 of the FIRST approved firmware)"; } - wpi_setWPIErrorWithContext(JaguarVersionError, buf); + wpi_setWPIErrorWithContext(JaguarVersionError, buf.str()); return; } @@ -178,11 +176,11 @@ void CANJaguar::InitCANJaguar() { */ CANJaguar::CANJaguar(uint8_t deviceNumber) : m_deviceNumber(deviceNumber) { - char buf[64]; - snprintf(buf, 64, "CANJaguar device number %d", m_deviceNumber); + std::stringstream buf; + buf << "CANJaguar device number " << m_deviceNumber; Resource::CreateResourceObject(allocated, 63); - if (allocated->Allocate(m_deviceNumber - 1, buf) == ~0ul) { + if (allocated->Allocate(m_deviceNumber - 1, buf.str()) == ~0ul) { CloneError(*allocated); return; } diff --git a/wpilibc/wpilibC++Devices/src/Counter.cpp b/wpilibc/wpilibC++Devices/src/Counter.cpp index c6edddb9b5..a2e306d8b8 100644 --- a/wpilibc/wpilibC++Devices/src/Counter.cpp +++ b/wpilibc/wpilibC++Devices/src/Counter.cpp @@ -46,6 +46,10 @@ Counter::Counter(Mode mode) { * @param source A pointer to the existing DigitalSource object. It will be set * as the Up Source. */ +[[deprecated( + "Raw pointers are deprecated; if you just want to construct a Counter with " + "its own DigitalSource, then call the Counter(int channel). If you want to " + "keep your own copy of the DigitalSource, use std::shared_ptr.")]] Counter::Counter(DigitalSource *source) : Counter() { SetUpSource(source); ClearDownSource(); @@ -89,6 +93,8 @@ Counter::Counter(int32_t channel) : Counter() { * The counter will start counting immediately. * @param trigger The pointer to the existing AnalogTrigger object. */ +[[deprecated( + "Raw pointers are deprecated. Use pass-by-reference instead.")]] Counter::Counter(AnalogTrigger *trigger) : Counter() { SetUpSource(trigger->CreateOutput(kState)); ClearDownSource(); @@ -115,6 +121,8 @@ Counter::Counter(const AnalogTrigger &trigger) : Counter() { * @param downSource The pointer to the DigitalSource to set as the down source * @param inverted True to invert the output (reverse the direction) */ +[[deprecated( + "Raw pointers are deprecated; prefer to use shared_ptr instead.")]] Counter::Counter(EncodingType encodingType, DigitalSource *upSource, DigitalSource *downSource, bool inverted) : Counter(encodingType, @@ -185,6 +193,9 @@ void Counter::SetUpSource(int32_t channel) { * @param analogTrigger The analog trigger object that is used for the Up Source * @param triggerType The analog trigger output that will trigger the counter. */ +[[deprecated( + "Raw pointers are deprecated; prefer to call either SetUpSource(int) or " + "SetUpSource(shared_ptr).")]] void Counter::SetUpSource(AnalogTrigger *analogTrigger, AnalogTriggerType triggerType) { SetUpSource(::std::shared_ptr(analogTrigger, @@ -221,6 +232,7 @@ void Counter::SetUpSource(::std::shared_ptr source) { } } +[[deprecated("Raw pointers are deprecated. Use std::shared_ptr instead.")]] void Counter::SetUpSource(DigitalSource *source) { SetUpSource( ::std::shared_ptr(source, NullDeleter())); @@ -231,6 +243,7 @@ void Counter::SetUpSource(DigitalSource *source) { * Set the up counting DigitalSource. * @param source Reference to the DigitalSource object to set as the up source */ +[[deprecated("References are deprecated. Use std::shared_ptr instead.")]] void Counter::SetUpSource(DigitalSource &source) { SetUpSource( ::std::shared_ptr(&source, NullDeleter())); @@ -281,6 +294,9 @@ void Counter::SetDownSource(int32_t channel) { * Source * @param triggerType The analog trigger output that will trigger the counter. */ +[[deprecated( + "Raw pointers are deprecated; prefer to call either SetUpSource(int) or " + "SetUpSource(shared_ptr).")]] void Counter::SetDownSource(AnalogTrigger *analogTrigger, AnalogTriggerType triggerType) { SetDownSource(::std::shared_ptr(analogTrigger, NullDeleter()), triggerType); @@ -316,6 +332,7 @@ void Counter::SetDownSource(::std::shared_ptr source) { } } +[[deprecated("Raw pointers are deprecated. Use std::shared_ptr instead.")]] void Counter::SetDownSource(DigitalSource *source) { SetDownSource(::std::shared_ptr(source, NullDeleter())); } @@ -325,6 +342,7 @@ void Counter::SetDownSource(DigitalSource *source) { * Set the down counting DigitalSource. * @param source Reference to the DigitalSource object to set as the down source */ +[[deprecated("References are deprecated. Use std::shared_ptr instead.")]] void Counter::SetDownSource(DigitalSource &source) { SetDownSource(::std::shared_ptr(&source, NullDeleter())); } diff --git a/wpilibc/wpilibC++Devices/src/DigitalInput.cpp b/wpilibc/wpilibC++Devices/src/DigitalInput.cpp index def300e5f2..b5db0939f5 100644 --- a/wpilibc/wpilibC++Devices/src/DigitalInput.cpp +++ b/wpilibc/wpilibC++Devices/src/DigitalInput.cpp @@ -11,6 +11,8 @@ #include "WPIErrors.h" #include "LiveWindow/LiveWindow.h" +#include + /** * Create an instance of a Digital Input class. * Creates a digital input given a channel. @@ -18,11 +20,11 @@ * @param channel The DIO channel 0-9 are on-board, 10-25 are on the MXP port */ DigitalInput::DigitalInput(uint32_t channel) { - char buf[64]; + std::stringstream buf; if (!CheckDigitalChannel(channel)) { - snprintf(buf, 64, "Digital Channel %d", channel); - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf); + buf << "Digital Channel " << channel; + wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); return; } m_channel = channel; diff --git a/wpilibc/wpilibC++Devices/src/DigitalOutput.cpp b/wpilibc/wpilibC++Devices/src/DigitalOutput.cpp index c5619f1108..c0b4b799f3 100644 --- a/wpilibc/wpilibC++Devices/src/DigitalOutput.cpp +++ b/wpilibc/wpilibC++Devices/src/DigitalOutput.cpp @@ -9,6 +9,8 @@ #include "Resource.h" #include "WPIErrors.h" +#include + /** * Create an instance of a digital output. * Create a digital output given a channel. @@ -17,11 +19,11 @@ * port */ DigitalOutput::DigitalOutput(uint32_t channel) { - char buf[64]; + std::stringstream buf; if (!CheckDigitalChannel(channel)) { - snprintf(buf, 64, "Digital Channel %d", channel); - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf); + buf << "Digital Channel " << channel; + wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); return; } m_channel = channel; diff --git a/wpilibc/wpilibC++Devices/src/DoubleSolenoid.cpp b/wpilibc/wpilibC++Devices/src/DoubleSolenoid.cpp index 9a5e72a485..1cb6aa70cb 100644 --- a/wpilibc/wpilibC++Devices/src/DoubleSolenoid.cpp +++ b/wpilibc/wpilibC++Devices/src/DoubleSolenoid.cpp @@ -9,6 +9,8 @@ #include "WPIErrors.h" #include "LiveWindow/LiveWindow.h" +#include + ::std::unique_ptr SolenoidBase::m_allocated = ::std::make_unique(solenoid_kNumDO7_0Elements * kSolenoidChannels); @@ -34,37 +36,39 @@ DoubleSolenoid::DoubleSolenoid(uint8_t moduleNumber, uint32_t forwardChannel, : SolenoidBase(moduleNumber), m_forwardChannel(forwardChannel), m_reverseChannel(reverseChannel) { - char buf[64]; + std::stringstream buf; if (!CheckSolenoidModule(m_moduleNumber)) { - snprintf(buf, 64, "Solenoid Module %d", m_moduleNumber); - wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf); + buf << "Solenoid Module " << m_moduleNumber; + wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf.str()); return; } if (!CheckSolenoidChannel(m_forwardChannel)) { - snprintf(buf, 64, "Solenoid Channel %d", m_forwardChannel); - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf); + buf << "Solenoid Module " << m_forwardChannel; + wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); return; } if (!CheckSolenoidChannel(m_reverseChannel)) { - snprintf(buf, 64, "Solenoid Channel %d", m_reverseChannel); - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf); + buf << "Solenoid Module " << m_reverseChannel; + wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); return; } Resource::CreateResourceObject( m_allocated, solenoid_kNumDO7_0Elements * kSolenoidChannels); - snprintf(buf, 64, "Solenoid %d (Module: %d)", m_forwardChannel, - m_moduleNumber); + buf << "Solenoid " << m_forwardChannel << " (Module: " << m_moduleNumber + << ")"; if (m_allocated->Allocate( - m_moduleNumber * kSolenoidChannels + m_forwardChannel, buf) == ~0ul) { + m_moduleNumber * kSolenoidChannels + m_forwardChannel, buf.str()) == + ~0ul) { CloneError(*m_allocated); return; } - snprintf(buf, 64, "Solenoid %d (Module: %d)", m_reverseChannel, - m_moduleNumber); + buf << "Solenoid " << m_reverseChannel << " (Module: " << m_moduleNumber + << ")"; if (m_allocated->Allocate( - m_moduleNumber * kSolenoidChannels + m_reverseChannel, buf) == ~0ul) { + m_moduleNumber * kSolenoidChannels + m_reverseChannel, buf.str()) == + ~0ul) { CloneError(*m_allocated); return; } diff --git a/wpilibc/wpilibC++Devices/src/Encoder.cpp b/wpilibc/wpilibC++Devices/src/Encoder.cpp index 5924aa3329..9df94f2600 100644 --- a/wpilibc/wpilibC++Devices/src/Encoder.cpp +++ b/wpilibc/wpilibC++Devices/src/Encoder.cpp @@ -125,6 +125,10 @@ Encoder::Encoder(uint32_t aChannel, uint32_t bChannel, bool reverseDirection, * match the spec'd count * or be double (2x) the spec'd count. */ +[[deprecated( + "Raw pointers are deprecated; if you wish to construct your own copy of " + "the DigitalSource and pass it to the constructor, use an std::shared_ptr " + "instead.")]] Encoder::Encoder(DigitalSource *aSource, DigitalSource *bSource, bool reverseDirection, EncodingType encodingType) : m_aSource(aSource, NullDeleter()), @@ -170,6 +174,10 @@ Encoder::Encoder(::std::shared_ptr aSource, * match the spec'd count * or be double (2x) the spec'd count. */ +[[deprecated( + "References are deprecated; if you wish to construct your own copy of " + "the DigitalSource and pass it to the constructor, use an std::shared_ptr " + "instead.")]] Encoder::Encoder(DigitalSource &aSource, DigitalSource &bSource, bool reverseDirection, EncodingType encodingType) : m_aSource(&aSource, NullDeleter()), @@ -520,6 +528,7 @@ void Encoder::SetIndexSource(uint32_t channel, Encoder::IndexingType type) { * @param channel A digital source to set as the encoder index * @param type The state that will cause the encoder to reset */ +[[deprecated("Raw pointers are dperecated; use references instead.")]] void Encoder::SetIndexSource(DigitalSource *source, Encoder::IndexingType type) { SetIndexSource(*source, type); diff --git a/wpilibc/wpilibC++Devices/src/Gyro.cpp b/wpilibc/wpilibC++Devices/src/Gyro.cpp index 2d6ed18a9d..ee87ce3871 100644 --- a/wpilibc/wpilibC++Devices/src/Gyro.cpp +++ b/wpilibc/wpilibC++Devices/src/Gyro.cpp @@ -88,6 +88,9 @@ Gyro::Gyro(int32_t channel) { * @param channel A pointer to the AnalogInput object that the gyro is connected * to. */ +[[deprecated( + "Raw pointers are deprecated; consider calling the Gyro constructor with " + "a channel number or passing a shared_ptr instead.")]] Gyro::Gyro(AnalogInput *channel) : Gyro(::std::shared_ptr(channel, NullDeleter())) {} diff --git a/wpilibc/wpilibC++Devices/src/PIDController.cpp b/wpilibc/wpilibC++Devices/src/PIDController.cpp index 415e18b627..c25ad4a401 100644 --- a/wpilibc/wpilibC++Devices/src/PIDController.cpp +++ b/wpilibc/wpilibC++Devices/src/PIDController.cpp @@ -13,12 +13,12 @@ #include #include "HAL/HAL.hpp" -static const char *kP = "p"; -static const char *kI = "i"; -static const char *kD = "d"; -static const char *kF = "f"; -static const char *kSetpoint = "setpoint"; -static const char *kEnabled = "enabled"; +static const std::string kP = "p"; +static const std::string kI = "i"; +static const std::string kD = "d"; +static const std::string kF = "f"; +static const std::string kSetpoint = "setpoint"; +static const std::string kEnabled = "enabled"; /** * Allocate a PID object with the given constants for P, I, D diff --git a/wpilibc/wpilibC++Devices/src/PWM.cpp b/wpilibc/wpilibC++Devices/src/PWM.cpp index 996105438e..1dabd1c7a8 100644 --- a/wpilibc/wpilibC++Devices/src/PWM.cpp +++ b/wpilibc/wpilibC++Devices/src/PWM.cpp @@ -13,6 +13,8 @@ #include "WPIErrors.h" #include "HAL/HAL.hpp" +#include + constexpr float PWM::kDefaultPwmPeriod; constexpr float PWM::kDefaultPwmCenter; const int32_t PWM::kDefaultPwmStepsDown; @@ -28,11 +30,11 @@ const int32_t PWM::kPwmDisabled; * port */ PWM::PWM(uint32_t channel) { - char buf[64]; + std::stringstream buf; if (!CheckPWMChannel(channel)) { - snprintf(buf, 64, "PWM Channel %d", channel); - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf); + buf << "PWM Channel " << channel; + wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); return; } diff --git a/wpilibc/wpilibC++Devices/src/PowerDistributionPanel.cpp b/wpilibc/wpilibC++Devices/src/PowerDistributionPanel.cpp index 24ac8a800b..cab445d60a 100644 --- a/wpilibc/wpilibC++Devices/src/PowerDistributionPanel.cpp +++ b/wpilibc/wpilibC++Devices/src/PowerDistributionPanel.cpp @@ -10,6 +10,8 @@ #include "HAL/PDP.hpp" #include "LiveWindow/LiveWindow.h" +#include + PowerDistributionPanel::PowerDistributionPanel() : PowerDistributionPanel(0) {} /** @@ -60,9 +62,9 @@ double PowerDistributionPanel::GetCurrent(uint8_t channel) const { int32_t status = 0; if (!CheckPDPChannel(channel)) { - char buf[64]; - snprintf(buf, 64, "PDP Channel %d", channel); - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf); + std::stringstream buf; + buf << "PDP Channel " << channel; + wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); } double current = getPDPChannelCurrent(channel, &status, m_module); diff --git a/wpilibc/wpilibC++Devices/src/Relay.cpp b/wpilibc/wpilibC++Devices/src/Relay.cpp index 3d0fcca907..c4fa81bd11 100644 --- a/wpilibc/wpilibC++Devices/src/Relay.cpp +++ b/wpilibc/wpilibC++Devices/src/Relay.cpp @@ -14,6 +14,8 @@ #include "LiveWindow/LiveWindow.h" #include "HAL/HAL.hpp" +#include + // Allocate each direction separately. static ::std::unique_ptr relayChannels; @@ -27,18 +29,18 @@ static ::std::unique_ptr relayChannels; */ Relay::Relay(uint32_t channel, Relay::Direction direction) : m_channel(channel), m_direction(direction) { - char buf[64]; + std::stringstream buf; Resource::CreateResourceObject(relayChannels, dio_kNumSystems * kRelayChannels * 2); if (!SensorBase::CheckRelayChannel(m_channel)) { - snprintf(buf, 64, "Relay Channel %d", m_channel); - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf); + buf << "Relay Channel " << m_channel; + wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); return; } if (m_direction == kBothDirections || m_direction == kForwardOnly) { - snprintf(buf, 64, "Forward Relay %d", m_channel); - if (relayChannels->Allocate(m_channel * 2, buf) == ~0ul) { + buf << "Forward Relay " << m_channel; + if (relayChannels->Allocate(m_channel * 2, buf.str()) == ~0ul) { CloneError(*relayChannels); return; } @@ -46,8 +48,8 @@ Relay::Relay(uint32_t channel, Relay::Direction direction) HALReport(HALUsageReporting::kResourceType_Relay, m_channel); } if (m_direction == kBothDirections || m_direction == kReverseOnly) { - snprintf(buf, 64, "Reverse Relay %d", m_channel); - if (relayChannels->Allocate(m_channel * 2 + 1, buf) == ~0ul) { + buf << "Reverse Relay " << m_channel; + if (relayChannels->Allocate(m_channel * 2 + 1, buf.str()) == ~0ul) { CloneError(*relayChannels); return; } diff --git a/wpilibc/wpilibC++Devices/src/RobotDrive.cpp b/wpilibc/wpilibC++Devices/src/RobotDrive.cpp index 428d726c2d..8061b046fe 100644 --- a/wpilibc/wpilibC++Devices/src/RobotDrive.cpp +++ b/wpilibc/wpilibC++Devices/src/RobotDrive.cpp @@ -98,6 +98,7 @@ RobotDrive::RobotDrive(uint32_t frontLeftMotor, uint32_t rearLeftMotor, * @param leftMotor The left SpeedController object used to drive the robot. * @param rightMotor the right SpeedController object used to drive the robot. */ +[[deprecated("Raw pointers are deprecated; use shared_ptr instead.")]] RobotDrive::RobotDrive(SpeedController *leftMotor, SpeedController *rightMotor) { InitRobotDrive(); @@ -111,6 +112,7 @@ RobotDrive::RobotDrive(SpeedController *leftMotor, } //TODO: Change to rvalue references & move syntax. +[[deprecated("References are deprecated; use shared_ptr instead.")]] RobotDrive::RobotDrive(SpeedController &leftMotor, SpeedController &rightMotor) { InitRobotDrive(); @@ -143,6 +145,7 @@ RobotDrive::RobotDrive(::std::shared_ptr leftMotor, * @param frontRightMotor The front right SpeedController object used to drive * the robot. */ +[[deprecated("Raw pointers are deprecated; use shared_ptr instead.")]] RobotDrive::RobotDrive(SpeedController *frontLeftMotor, SpeedController *rearLeftMotor, SpeedController *frontRightMotor, @@ -159,6 +162,7 @@ RobotDrive::RobotDrive(SpeedController *frontLeftMotor, m_rearRightMotor = make_shared_nodelete(rearRightMotor); } +[[deprecated("References are deprecated; use shared_ptr instead.")]] RobotDrive::RobotDrive(SpeedController &frontLeftMotor, SpeedController &rearLeftMotor, SpeedController &frontRightMotor, diff --git a/wpilibc/wpilibC++Devices/src/SerialPort.cpp b/wpilibc/wpilibC++Devices/src/SerialPort.cpp index d1a6c28b5e..9edc21d96c 100644 --- a/wpilibc/wpilibC++Devices/src/SerialPort.cpp +++ b/wpilibc/wpilibC++Devices/src/SerialPort.cpp @@ -135,9 +135,9 @@ uint32_t SerialPort::Read(char *buffer, int32_t count) { * @param count The maximum number of bytes to write. * @return The number of bytes actually written into the port. */ -uint32_t SerialPort::Write(const char *buffer, int32_t count) { +uint32_t SerialPort::Write(const std::string &buffer, int32_t count) { int32_t status = 0; - int32_t retVal = serialWrite(m_port, buffer, count, &status); + int32_t retVal = serialWrite(m_port, buffer.c_str(), count, &status); wpi_setErrorWithContext(status, getHALErrorMessage(status)); return retVal; } diff --git a/wpilibc/wpilibC++Devices/src/Solenoid.cpp b/wpilibc/wpilibC++Devices/src/Solenoid.cpp index 97078cdaa7..f5e2304c79 100644 --- a/wpilibc/wpilibC++Devices/src/Solenoid.cpp +++ b/wpilibc/wpilibC++Devices/src/Solenoid.cpp @@ -10,6 +10,8 @@ #include "WPIErrors.h" #include "LiveWindow/LiveWindow.h" +#include + /** * Constructor using the default PCM ID (0). * @@ -26,22 +28,22 @@ Solenoid::Solenoid(uint32_t channel) */ Solenoid::Solenoid(uint8_t moduleNumber, uint32_t channel) : SolenoidBase(moduleNumber), m_channel(channel) { - char buf[64]; + std::stringstream buf; if (!CheckSolenoidModule(m_moduleNumber)) { - snprintf(buf, 64, "Solenoid Module %d", m_moduleNumber); - wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf); + buf << "Solenoid Module " << m_moduleNumber; + wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf.str()); return; } if (!CheckSolenoidChannel(m_channel)) { - snprintf(buf, 64, "Solenoid Channel %d", m_channel); - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf); + buf << "Solenoid Module " << m_channel; + wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); return; } Resource::CreateResourceObject(m_allocated, kSolenoidChannels * 63); - snprintf(buf, 64, "Solenoid %d (Module: %d)", m_channel, m_moduleNumber); + buf << "Solenoid " << m_channel << " (Module: " << m_moduleNumber << ")"; if (m_allocated->Allocate(m_moduleNumber * kSolenoidChannels + m_channel, - buf) == ~0ul) { + buf.str()) == ~0ul) { CloneError(*m_allocated); return; } diff --git a/wpilibc/wpilibC++Devices/src/Task.cpp b/wpilibc/wpilibC++Devices/src/Task.cpp index 2a802dfc5a..e6f57f742a 100644 --- a/wpilibc/wpilibC++Devices/src/Task.cpp +++ b/wpilibc/wpilibC++Devices/src/Task.cpp @@ -91,7 +91,7 @@ bool Task::SetPriority(int32_t priority) { /** * Returns the name of the task. - * @returns Pointer to the name of the task or nullptr if not allocated + * @returns The name of the task (0 length if not allocated). */ std::string Task::GetName() const { return m_taskName; } diff --git a/wpilibc/wpilibC++Devices/src/Ultrasonic.cpp b/wpilibc/wpilibC++Devices/src/Ultrasonic.cpp index 551bf1c467..08cdd0b9a7 100644 --- a/wpilibc/wpilibC++Devices/src/Ultrasonic.cpp +++ b/wpilibc/wpilibC++Devices/src/Ultrasonic.cpp @@ -116,6 +116,9 @@ Ultrasonic::Ultrasonic(uint32_t pingChannel, uint32_t echoChannel, * determine the range. * @param units The units returned in either kInches or kMilliMeters */ +[[deprecated( + "Raw pointers are deprecated; prefer either specifying the channel numbers " + "as integers or passing shared_ptrs.")]] Ultrasonic::Ultrasonic(DigitalOutput *pingChannel, DigitalInput *echoChannel, DistanceUnit units) : m_pingChannel(pingChannel, NullDeleter()), @@ -139,6 +142,9 @@ Ultrasonic::Ultrasonic(DigitalOutput *pingChannel, DigitalInput *echoChannel, * determine the range. * @param units The units returned in either kInches or kMilliMeters */ +[[deprecated( + "References are deprecated; prefer either specifying the channel numbers " + "as integers or passing shared_ptrs.")]] Ultrasonic::Ultrasonic(DigitalOutput &pingChannel, DigitalInput &echoChannel, DistanceUnit units) : m_pingChannel(&pingChannel, NullDeleter()), diff --git a/wpilibc/wpilibC++Devices/src/Utility.cpp b/wpilibc/wpilibC++Devices/src/Utility.cpp index 1933dbcd01..a8f076a0f0 100644 --- a/wpilibc/wpilibC++Devices/src/Utility.cpp +++ b/wpilibc/wpilibC++Devices/src/Utility.cpp @@ -25,17 +25,17 @@ * The users don't call this, but instead use the wpi_assert macros in * Utility.h. */ -bool wpi_assert_impl(bool conditionValue, const char *conditionText, - const char *message, const char *fileName, - uint32_t lineNumber, const char *funcName) { +bool wpi_assert_impl(bool conditionValue, const std::string &conditionText, + const std::string &message, const std::string &fileName, + uint32_t lineNumber, const std::string &funcName) { if (!conditionValue) { std::stringstream errorStream; errorStream << "Assertion \"" << conditionText << "\" "; errorStream << "on line " << lineNumber << " "; - errorStream << "of " << basename(fileName) << " "; + errorStream << "of " << basename(fileName.c_str()) << " "; - if (message) { + if (message.size() > 0) { errorStream << "failed: " << message << std::endl; } else { errorStream << "failed." << std::endl; @@ -59,18 +59,18 @@ bool wpi_assert_impl(bool conditionValue, const char *conditionText, * wpi_assertEqual_impl * and wpi_assertNotEqual_impl. */ -void wpi_assertEqual_common_impl(const char *valueA, const char *valueB, - const char *equalityType, const char *message, - const char *fileName, uint32_t lineNumber, - const char *funcName) { +void wpi_assertEqual_common_impl(const std::string &valueA, const std::string &valueB, + const std::string &equalityType, const std::string &message, + const std::string &fileName, uint32_t lineNumber, + const std::string &funcName) { std::stringstream errorStream; errorStream << "Assertion \"" << valueA << " " << equalityType << " " << valueB << "\" "; errorStream << "on line " << lineNumber << " "; - errorStream << "of " << basename(fileName) << " "; + errorStream << "of " << basename(fileName.c_str()) << " "; - if (message) { + if (message.size() > 0) { errorStream << "failed: " << message << std::endl; } else { errorStream << "failed." << std::endl; @@ -92,10 +92,10 @@ void wpi_assertEqual_common_impl(const char *valueA, const char *valueB, * The users don't call this, but instead use the wpi_assertEqual macros in * Utility.h. */ -bool wpi_assertEqual_impl(int valueA, int valueB, const char *valueAString, - const char *valueBString, const char *message, - const char *fileName, uint32_t lineNumber, - const char *funcName) { +bool wpi_assertEqual_impl(int valueA, int valueB, const std::string &valueAString, + const std::string &valueBString, const std::string &message, + const std::string &fileName, uint32_t lineNumber, + const std::string &funcName) { if (!(valueA == valueB)) { wpi_assertEqual_common_impl(valueAString, valueBString, "==", message, fileName, lineNumber, funcName); @@ -110,10 +110,10 @@ bool wpi_assertEqual_impl(int valueA, int valueB, const char *valueAString, * The users don't call this, but instead use the wpi_assertNotEqual macros in * Utility.h. */ -bool wpi_assertNotEqual_impl(int valueA, int valueB, const char *valueAString, - const char *valueBString, const char *message, - const char *fileName, uint32_t lineNumber, - const char *funcName) { +bool wpi_assertNotEqual_impl(int valueA, int valueB, const std::string &valueAString, + const std::string &valueBString, const std::string &message, + const std::string &fileName, uint32_t lineNumber, + const std::string &funcName) { if (!(valueA != valueB)) { wpi_assertEqual_common_impl(valueAString, valueBString, "!=", message, fileName, lineNumber, funcName); diff --git a/wpilibc/wpilibC++IntegrationTests/src/command/CommandTest.cpp b/wpilibc/wpilibC++IntegrationTests/src/command/CommandTest.cpp index 019e67a87a..920b97ac71 100644 --- a/wpilibc/wpilibC++IntegrationTests/src/command/CommandTest.cpp +++ b/wpilibc/wpilibC++IntegrationTests/src/command/CommandTest.cpp @@ -44,7 +44,7 @@ class ASubsystem : public Subsystem { Command *m_command = nullptr; public: - ASubsystem(const char *name) : Subsystem(name) {} + ASubsystem(const std::string &name) : Subsystem(name) {} virtual void InitDefaultCommand() override { if (m_command != nullptr) { diff --git a/wpilibc/wpilibC++Sim/src/PIDController.cpp b/wpilibc/wpilibC++Sim/src/PIDController.cpp index fae795f431..7f636b900e 100644 --- a/wpilibc/wpilibC++Sim/src/PIDController.cpp +++ b/wpilibc/wpilibC++Sim/src/PIDController.cpp @@ -10,12 +10,12 @@ #include "PIDOutput.h" #include -static const char *kP = "p"; -static const char *kI = "i"; -static const char *kD = "d"; -static const char *kF = "f"; -static const char *kSetpoint = "setpoint"; -static const char *kEnabled = "enabled"; +static const std::string kP = "p"; +static const std::string kI = "i"; +static const std::string kD = "d"; +static const std::string kF = "f"; +static const std::string kSetpoint = "setpoint"; +static const std::string kEnabled = "enabled"; /** diff --git a/wpilibc/wpilibC++Sim/src/Utility.cpp b/wpilibc/wpilibC++Sim/src/Utility.cpp index 1046cd60bf..d71aea5680 100644 --- a/wpilibc/wpilibC++Sim/src/Utility.cpp +++ b/wpilibc/wpilibC++Sim/src/Utility.cpp @@ -56,34 +56,30 @@ static void wpi_handleTracing() * This allows breakpoints to be set on an assert. * The users don't call this, but instead use the wpi_assert macros in Utility.h. */ -bool wpi_assert_impl(bool conditionValue, - const char *conditionText, - const char *message, - const char *fileName, - uint32_t lineNumber, - const char *funcName) -{ - if (!conditionValue) - { - // Error string buffer - char error[256]; +bool wpi_assert_impl(bool conditionValue, const std::string &conditionText, + const std::string &message, const std::string &fileName, + uint32_t lineNumber, const std::string &funcName) { + if (!conditionValue) { + // Error string buffer + std::stringstream error; - // If an error message was specified, include it - // Build error string - if(message != nullptr) { - sprintf(error, "Assertion failed: \"%s\", \"%s\" failed in %s() in %s at line %dd\n", - message, conditionText, funcName, fileName, lineNumber); - } else { - sprintf(error, "Assertion failed: \"%s\" in %s() in %s at line %dd\n", - conditionText, funcName, fileName, lineNumber); - } + // If an error message was specified, include it + // Build error string + if (message.size()) { + error << "Assertion failed: \"" << message << "\", \"" << conditionText + << "\" failed in " << funcName + "() in " << fileName << " at line " + << lineNumber; + } else { + error << "Assertion failed: \"" << conditionText << "\" in " << funcName + << "() in " << fileName << " at line " << lineNumber; + } - // Print to console and send to remote dashboard - printf("\n\n>>>>%s", error); + // Print to console and send to remote dashboard + ::std::cout << "\n\n>>>>" << error; - wpi_handleTracing(); - } - return conditionValue; + wpi_handleTracing(); + } + return conditionValue; } /** @@ -91,31 +87,31 @@ bool wpi_assert_impl(bool conditionValue, * This should not be called directly; it should only be used by wpi_assertEqual_impl * and wpi_assertNotEqual_impl. */ -void wpi_assertEqual_common_impl(int valueA, - int valueB, - const char *equalityType, - const char *message, - const char *fileName, - uint32_t lineNumber, - const char *funcName) -{ - // Error string buffer - char error[256]; +void wpi_assertEqual_common_impl(int valueA, int valueB, + const std::string &equalityType, + const std::string &message, + const std::string &fileName, + uint32_t lineNumber, + const std::string &funcName) { + // Error string buffer + std::stringstream error; - // If an error message was specified, include it - // Build error string - if(message != nullptr) { - sprintf(error, "Assertion failed: \"%s\", \"%d\" %s \"%d\" in %s() in %s at line %d\n", - message, valueA, equalityType, valueB, funcName, fileName, lineNumber); - } else { - sprintf(error, "Assertion failed: \"%d\" %s \"%d\" in %s() in %s at line %d\n", - valueA, equalityType, valueB, funcName, fileName, lineNumber); - } + // If an error message was specified, include it + // Build error string + if (message.size() > 0) { + error << "Assertion failed: \"" << message << "\", \"" << valueA << "\" " + << equalityType << " \"" << valueB << "\" in " << funcName << "() in " + << fileName << " at line " << lineNumber << "\n"; + } else { + error << "Assertion failed: \"" << valueA << "\" " << equalityType << " \"" + << valueB << "\" in " << funcName << "() in " << fileName + << " at line " << lineNumber << "\n"; + } - // Print to console and send to remote dashboard - printf("\n\n>>>>%s", error); + // Print to console and send to remote dashboard + std::cout << "\n\n>>>>" << error; - wpi_handleTracing(); + wpi_handleTracing(); } /** @@ -126,10 +122,10 @@ void wpi_assertEqual_common_impl(int valueA, */ bool wpi_assertEqual_impl(int valueA, int valueB, - const char *message, - const char *fileName, + const std::string &message, + const std::string &fileName, uint32_t lineNumber, - const char *funcName) + const std::string &funcName) { if(!(valueA == valueB)) { @@ -146,10 +142,10 @@ bool wpi_assertEqual_impl(int valueA, */ bool wpi_assertNotEqual_impl(int valueA, int valueB, - const char *message, - const char *fileName, + const std::string &message, + const std::string &fileName, uint32_t lineNumber, - const char *funcName) + const std::string &funcName) { if(!(valueA != valueB)) {