Skip to content

Commit 1b44f9f

Browse files
committed
Refactor commands implementation to be easier to wrap
- Remove usage of CommandHelper - Use std::shared_ptr instead of raw pointers or std::unique_ptr - Modify protection levels
1 parent 93edf83 commit 1b44f9f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+517
-426
lines changed

commands2/src/cpp/frc2/command/Command.cpp

+47-46
Original file line numberDiff line numberDiff line change
@@ -18,90 +18,90 @@
1818
using namespace frc2;
1919

2020
Command::~Command() {
21-
CommandScheduler::GetInstance().Cancel(this);
21+
// CommandScheduler::GetInstance().Cancel(shared_from_this());
2222
}
2323

24-
Command::Command(const Command& rhs) : ErrorBase(rhs) {}
24+
// Command::Command(const Command& rhs) : ErrorBase(rhs) {}
2525

26-
Command& Command::operator=(const Command& rhs) {
27-
ErrorBase::operator=(rhs);
28-
m_isGrouped = false;
29-
return *this;
30-
}
26+
// Command& Command::operator=(const Command& rhs) {
27+
// ErrorBase::operator=(rhs);
28+
// m_isGrouped = false;
29+
// return *this;
30+
// }
3131

3232
void Command::Initialize() {}
3333
void Command::Execute() {}
3434
void Command::End(bool interrupted) {}
3535

36-
ParallelRaceGroup Command::WithTimeout(units::second_t duration) && {
37-
std::vector<std::unique_ptr<Command>> temp;
38-
temp.emplace_back(std::make_unique<WaitCommand>(duration));
39-
temp.emplace_back(std::move(*this).TransferOwnership());
40-
return ParallelRaceGroup(std::move(temp));
36+
std::shared_ptr<ParallelRaceGroup> Command::WithTimeout(units::second_t duration) {
37+
std::vector<std::shared_ptr<Command>> temp;
38+
temp.emplace_back(std::make_shared<WaitCommand>(duration));
39+
temp.emplace_back(shared_from_this());
40+
return std::make_shared<ParallelRaceGroup>(std::move(temp));
4141
}
4242

43-
ParallelRaceGroup Command::WithInterrupt(std::function<bool()> condition) && {
44-
std::vector<std::unique_ptr<Command>> temp;
45-
temp.emplace_back(std::make_unique<WaitUntilCommand>(std::move(condition)));
46-
temp.emplace_back(std::move(*this).TransferOwnership());
47-
return ParallelRaceGroup(std::move(temp));
43+
std::shared_ptr<ParallelRaceGroup> Command::WithInterrupt(std::function<bool()> condition) {
44+
std::vector<std::shared_ptr<Command>> temp;
45+
temp.emplace_back(std::make_shared<WaitUntilCommand>(std::move(condition)));
46+
temp.emplace_back(shared_from_this());
47+
return std::make_shared<ParallelRaceGroup>(std::move(temp));
4848
}
4949

50-
SequentialCommandGroup Command::BeforeStarting(
50+
std::shared_ptr<SequentialCommandGroup> Command::BeforeStarting(
5151
std::function<void()> toRun,
52-
std::initializer_list<Subsystem*> requirements) && {
53-
return std::move(*this).BeforeStarting(
52+
std::initializer_list<std::shared_ptr<Subsystem>> requirements) {
53+
return BeforeStarting(
5454
std::move(toRun),
5555
wpi::makeArrayRef(requirements.begin(), requirements.end()));
5656
}
5757

58-
SequentialCommandGroup Command::BeforeStarting(
59-
std::function<void()> toRun, wpi::ArrayRef<Subsystem*> requirements) && {
60-
std::vector<std::unique_ptr<Command>> temp;
58+
std::shared_ptr<SequentialCommandGroup> Command::BeforeStarting(
59+
std::function<void()> toRun, wpi::ArrayRef<std::shared_ptr<Subsystem>> requirements) {
60+
std::vector<std::shared_ptr<Command>> temp;
6161
temp.emplace_back(
62-
std::make_unique<InstantCommand>(std::move(toRun), requirements));
63-
temp.emplace_back(std::move(*this).TransferOwnership());
64-
return SequentialCommandGroup(std::move(temp));
62+
std::make_shared<InstantCommand>(std::move(toRun), requirements));
63+
temp.emplace_back(shared_from_this());
64+
return std::make_shared<SequentialCommandGroup>(std::move(temp));
6565
}
6666

67-
SequentialCommandGroup Command::AndThen(
67+
std::shared_ptr<SequentialCommandGroup> Command::AndThen(
6868
std::function<void()> toRun,
69-
std::initializer_list<Subsystem*> requirements) && {
70-
return std::move(*this).AndThen(
69+
std::initializer_list<std::shared_ptr<Subsystem>> requirements) {
70+
return AndThen(
7171
std::move(toRun),
7272
wpi::makeArrayRef(requirements.begin(), requirements.end()));
7373
}
7474

75-
SequentialCommandGroup Command::AndThen(
76-
std::function<void()> toRun, wpi::ArrayRef<Subsystem*> requirements) && {
77-
std::vector<std::unique_ptr<Command>> temp;
78-
temp.emplace_back(std::move(*this).TransferOwnership());
75+
std::shared_ptr<SequentialCommandGroup> Command::AndThen(
76+
std::function<void()> toRun, wpi::ArrayRef<std::shared_ptr<Subsystem>> requirements) {
77+
std::vector<std::shared_ptr<Command>> temp;
78+
temp.emplace_back(shared_from_this());
7979
temp.emplace_back(
80-
std::make_unique<InstantCommand>(std::move(toRun), requirements));
81-
return SequentialCommandGroup(std::move(temp));
80+
std::make_shared<InstantCommand>(std::move(toRun), requirements));
81+
return std::make_shared<SequentialCommandGroup>(std::move(temp));
8282
}
8383

84-
PerpetualCommand Command::Perpetually() && {
85-
return PerpetualCommand(std::move(*this).TransferOwnership());
84+
std::shared_ptr<PerpetualCommand> Command::Perpetually() {
85+
return std::make_shared<PerpetualCommand>(shared_from_this());
8686
}
8787

88-
ProxyScheduleCommand Command::AsProxy() {
89-
return ProxyScheduleCommand(this);
88+
std::shared_ptr<ProxyScheduleCommand> Command::AsProxy() {
89+
return std::make_shared<ProxyScheduleCommand>(shared_from_this());
9090
}
9191

9292
void Command::Schedule(bool interruptible) {
93-
CommandScheduler::GetInstance().Schedule(interruptible, this);
93+
CommandScheduler::GetInstance().Schedule(interruptible, shared_from_this());
9494
}
9595

9696
void Command::Cancel() {
97-
CommandScheduler::GetInstance().Cancel(this);
97+
CommandScheduler::GetInstance().Cancel(shared_from_this());
9898
}
9999

100-
bool Command::IsScheduled() const {
101-
return CommandScheduler::GetInstance().IsScheduled(this);
100+
bool Command::IsScheduled() {
101+
return CommandScheduler::GetInstance().IsScheduled(shared_from_this());
102102
}
103103

104-
bool Command::HasRequirement(Subsystem* requirement) const {
104+
bool Command::HasRequirement(std::shared_ptr<Subsystem> requirement) const {
105105
bool hasRequirement = false;
106106
for (auto&& subsystem : GetRequirements()) {
107107
hasRequirement |= requirement == subsystem;
@@ -110,7 +110,7 @@ bool Command::HasRequirement(Subsystem* requirement) const {
110110
}
111111

112112
std::string Command::GetName() const {
113-
return GetTypeName(*this);
113+
return GetTypeName(shared_from_this());
114114
}
115115

116116
bool Command::IsGrouped() const {
@@ -126,7 +126,8 @@ bool RequirementsDisjoint(Command* first, Command* second) {
126126
bool disjoint = true;
127127
auto&& requirements = second->GetRequirements();
128128
for (auto&& requirement : first->GetRequirements()) {
129-
disjoint &= requirements.find(requirement) == requirements.end();
129+
// disjoint &= requirements.count(requirement) == requirements.end();
130+
disjoint &= requirements.count(requirement) == 0;
130131
}
131132
return disjoint;
132133
}

commands2/src/cpp/frc2/command/CommandBase.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ CommandBase::CommandBase() {
1414
}
1515

1616
void CommandBase::AddRequirements(
17-
std::initializer_list<Subsystem*> requirements) {
17+
std::initializer_list<std::shared_ptr<Subsystem>> requirements) {
1818
m_requirements.insert(requirements.begin(), requirements.end());
1919
}
2020

21-
void CommandBase::AddRequirements(wpi::ArrayRef<Subsystem*> requirements) {
21+
void CommandBase::AddRequirements(wpi::ArrayRef<std::shared_ptr<Subsystem>> requirements) {
2222
m_requirements.insert(requirements.begin(), requirements.end());
2323
}
2424

25-
void CommandBase::AddRequirements(wpi::SmallSet<Subsystem*, 4> requirements) {
25+
void CommandBase::AddRequirements(wpi::SmallSet<std::shared_ptr<Subsystem>, 4> requirements) {
2626
m_requirements.insert(requirements.begin(), requirements.end());
2727
}
2828

29-
wpi::SmallSet<Subsystem*, 4> CommandBase::GetRequirements() const {
29+
wpi::SmallSet<std::shared_ptr<Subsystem>, 4> CommandBase::GetRequirements() const {
3030
return m_requirements;
3131
}
3232

commands2/src/cpp/frc2/command/CommandGroupBase.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,39 @@ using namespace frc2;
1010

1111
bool CommandGroupBase::RequireUngrouped(Command& command) {
1212
if (command.IsGrouped()) {
13-
wpi_setGlobalWPIErrorWithContext(
14-
CommandIllegalUse,
15-
"Commands cannot be added to more than one CommandGroup");
16-
return false;
13+
// wpi_setGlobalWPIErrorWithContext(
14+
// CommandIllegalUse,
15+
throw std::runtime_error("Commands cannot be added to more than one CommandGroup");
16+
// return false;
1717
} else {
1818
return true;
1919
}
2020
}
2121

2222
bool CommandGroupBase::RequireUngrouped(
23-
wpi::ArrayRef<std::unique_ptr<Command>> commands) {
23+
wpi::ArrayRef<std::shared_ptr<Command>> commands) {
2424
bool allUngrouped = true;
2525
for (auto&& command : commands) {
2626
allUngrouped &= !command.get()->IsGrouped();
2727
}
2828
if (!allUngrouped) {
29-
wpi_setGlobalWPIErrorWithContext(
30-
CommandIllegalUse,
31-
"Commands cannot be added to more than one CommandGroup");
29+
// wpi_setGlobalWPIErrorWithContext(
30+
// CommandIllegalUse,
31+
throw std::runtime_error("Commands cannot be added to more than one CommandGroup");
3232
}
3333
return allUngrouped;
3434
}
3535

3636
bool CommandGroupBase::RequireUngrouped(
37-
std::initializer_list<Command*> commands) {
37+
std::initializer_list<std::shared_ptr<Command>> commands) {
3838
bool allUngrouped = true;
3939
for (auto&& command : commands) {
4040
allUngrouped &= !command->IsGrouped();
4141
}
4242
if (!allUngrouped) {
43-
wpi_setGlobalWPIErrorWithContext(
44-
CommandIllegalUse,
45-
"Commands cannot be added to more than one CommandGroup");
43+
// wpi_setGlobalWPIErrorWithContext(
44+
// CommandIllegalUse,
45+
throw std::runtime_error("Commands cannot be added to more than one CommandGroup");
4646
}
4747
return allUngrouped;
4848
}

0 commit comments

Comments
 (0)