-
Notifications
You must be signed in to change notification settings - Fork 613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[cpp cmd] Add Requirements struct #5504
[cpp cmd] Add Requirements struct #5504
Conversation
wpilibNewCommands/src/main/native/include/frc2/command/Requirements.h
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add tests confirming that all the semantics and resolved overloads we intend are indeed what happens? With implicit conversions and overloads it's always better to be sure.
Additionally, an idea (open for discussion) -- maybe we can deprecate/remove the AddRequirements(Subsystem*)
overload? Adding the braces isn't significantly verbose, and if it complicates overload resolution than maybe we don't need it. How much benefit does it provide?
wpilibNewCommands/src/main/native/include/frc2/command/Requirements.h
Outdated
Show resolved
Hide resolved
The only benefit I can think of is that conceptually it's nice when teams define their own command classes (instead of using the factory methods), since those command classes usually only require one subsystem. I haven't seen too many teams' robot code, so I might be missing other benefits, but if that is the only benefit, I'd feel fine removing it since I don't think anything else provides Another thought I just had is that we could rename |
I wasn't sure if I should add a test for the |
Let's deprecate the Also, is there a reason to keep the |
I wasn't completely sure about what to recommend teams use instead of the deprecated overload, so any thoughts would be appreciated.
We use the |
Oh, right -- @PeterJohnson -- your thoughts? |
I'm in favor of just keeping the |
So let's just do that. |
@KangarooKoala This needs to be rebased on main- merging with main caused a bunch of files to be touched |
e191d21
to
9b3829c
Compare
wpilibNewCommands/src/main/native/include/frc2/command/Requirements.h
Outdated
Show resolved
Hide resolved
Seems like clang 14 doesn't recognize |
I think it's because the member variable is const-qualified. Making member variables const-qualified is generally a bad idea because it breaks things like copy construction and copy assignment. Enforce immutability through the interface instead. The iterator functions are already const-qualified, so you should be able to just remove the const from the member variable. |
wpilibNewCommands/src/test/native/cpp/frc2/command/AddRequirementsTest.cpp
Outdated
Show resolved
Hide resolved
wpilibNewCommands/src/test/native/cpp/frc2/command/AddRequirementsTest.cpp
Outdated
Show resolved
Hide resolved
wpilibNewCommands/src/test/native/cpp/frc2/command/AddRequirementsTest.cpp
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you think MockAddRequirementsCommand
shouldn't inherit from Command
, go for it.
If not, I think this is good to go.
ac0818e
to
2be8fe4
Compare
2be8fe4
to
215b5c6
Compare
Fixes #4793
Potential changes to user code:
AddRequirements({subsystemPointer})
now selects theSubsystem*
overload instead of the initializer list (now Requirements struct) overload, it causes warnings about the redundant braces. Adding an overload takingstd::initializer_list
(which could defer to the Requirements overload) would fix this issue, but I don't know how important that use case is.std::initializer_list
orstd::span
would also need to be written so at least one of those conversions is explicit. (For example,AddRequirements(array)
no longer works, butAddRequirements(Requirements{array})
orAddRequirements({array})
do.)Notes:
std::vector
. However, Mac 12 doesn't seem to support the constexprstd::vector
constructors (see https://discord.com/channels/176186766946992128/1137101704127578236), so it's left non-constexpr for now. If there's a fix, though, we could always implement that.Requirements
struct also copies the vector, which I'm pretty sure copies the pointers to subsystems. This could lead to a lot of copies in nested calls, such ascmd::RunOnce -> InstantCommand -> FunctionalCommand -> AddRequirements
. This isn't necessarily expensive, but we *could* avoid them by making the backing structure a span, at the cost of leading to bugs if the struct is initialized from an initializer list and we use the struct outside of the function call (e.g., store it in an instance variable which we access later).CommandScheduler::RegisterSubsystems
,CommandScheduler::Cancel
, and potentially other parts of wpilib- but I think changing those usages would be out of the scope of this PR.