mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
[commands] C++ CommandPtr: Prevent null initialization (#5991)
This commit is contained in:
@@ -22,6 +22,11 @@
|
||||
|
||||
using namespace frc2;
|
||||
|
||||
CommandPtr::CommandPtr(std::unique_ptr<Command>&& command)
|
||||
: m_ptr(std::move(command)) {
|
||||
AssertValid();
|
||||
}
|
||||
|
||||
void CommandPtr::AssertValid() const {
|
||||
if (!m_ptr) {
|
||||
throw FRC_MakeError(frc::err::CommandIllegalUse,
|
||||
|
||||
@@ -27,8 +27,7 @@ namespace frc2 {
|
||||
*/
|
||||
class CommandPtr final {
|
||||
public:
|
||||
explicit CommandPtr(std::unique_ptr<Command>&& command)
|
||||
: m_ptr(std::move(command)) {}
|
||||
explicit CommandPtr(std::unique_ptr<Command>&& command);
|
||||
|
||||
template <std::derived_from<Command> T>
|
||||
// NOLINTNEXTLINE(bugprone-forwarding-reference-overload)
|
||||
@@ -39,6 +38,8 @@ class CommandPtr final {
|
||||
CommandPtr(CommandPtr&&) = default;
|
||||
CommandPtr& operator=(CommandPtr&&) = default;
|
||||
|
||||
explicit CommandPtr(std::nullptr_t) = delete;
|
||||
|
||||
/**
|
||||
* Decorates this command to run repeatedly, restarting it when it ends, until
|
||||
* this command is interrupted. The decorated command can still be canceled.
|
||||
|
||||
@@ -34,3 +34,7 @@ TEST_F(CommandPtrTest, MovedFrom) {
|
||||
|
||||
EXPECT_EQ(1, counter);
|
||||
}
|
||||
|
||||
TEST_F(CommandPtrTest, NullInitialization) {
|
||||
EXPECT_THROW(CommandPtr{std::unique_ptr<Command>{}}, frc::RuntimeError);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user