Replace SFINAE with concepts (#5361)

Concepts are cleaner to use and result in much better error messages for incorrect template use.
This commit is contained in:
Tyler Veness
2023-06-07 09:50:09 -07:00
committed by GitHub
parent d57d1a4598
commit 91cbcea841
42 changed files with 397 additions and 361 deletions

View File

@@ -9,6 +9,7 @@
#include <span>
#include <utility>
#include <wpi/concepts.h>
#include <wpi/deprecated.h>
#include "Trigger.h"
@@ -65,8 +66,7 @@ class Button : public Trigger {
* @return The trigger, for chained calls.
* @deprecated Replace with Trigger::OnTrue()
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
template <std::derived_from<Command> T>
WPI_DEPRECATED("Replace with Trigger#OnTrue()")
Button WhenPressed(T&& command) {
WhenActive(std::forward<T>(command));
@@ -117,8 +117,7 @@ class Button : public Trigger {
* @return The button, for chained calls.
* @deprecated Replace with Trigger::WhileTrue(command.Repeatedly())
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
template <std::derived_from<Command> T>
WPI_DEPRECATED("Replace with Trigger#WhileTrue(command.Repeatedly())")
Button WhileHeld(T&& command) {
WhileActiveContinous(std::forward<T>(command));
@@ -169,8 +168,7 @@ class Button : public Trigger {
* @return The button, for chained calls.
* @deprecated Replace with Trigger::WhileTrue()
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
template <std::derived_from<Command> T>
WPI_DEPRECATED("Replace with Trigger#WhileTrue()")
Button WhenHeld(T&& command) {
WhileActiveOnce(std::forward<T>(command));
@@ -199,8 +197,7 @@ class Button : public Trigger {
* @return The button, for chained calls.
* @deprecated Replace with Trigger::OnFalse()
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
template <std::derived_from<Command> T>
WPI_DEPRECATED("Replace with Trigger#OnFalse()")
Button WhenReleased(T&& command) {
WhenInactive(std::forward<T>(command));
@@ -251,8 +248,7 @@ class Button : public Trigger {
* @return The button, for chained calls.
* @deprecated Replace with Trigger::ToggleOnTrue()
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
template <std::derived_from<Command> T>
WPI_DEPRECATED("Replace with Trigger#ToggleOnTrue()")
Button ToggleWhenPressed(T&& command) {
ToggleWhenActive(std::forward<T>(command));

View File

@@ -14,6 +14,7 @@
#include <frc/event/EventLoop.h>
#include <frc/filter/Debouncer.h>
#include <units/time.h>
#include <wpi/concepts.h>
#include <wpi/deprecated.h>
#include "frc2/command/Command.h"
@@ -226,12 +227,11 @@ class Trigger {
* @return The trigger, for chained calls.
* @deprecated Use OnTrue(Command) instead
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
template <std::derived_from<Command> T>
WPI_DEPRECATED("Use OnTrue(Command) instead")
Trigger WhenActive(T&& command) {
m_loop->Bind([condition = m_condition, previous = m_condition(),
command = std::make_unique<std::remove_reference_t<T>>(
command = std::make_unique<std::decay_t<T>>(
std::forward<T>(command))]() mutable {
bool current = condition();
@@ -296,14 +296,13 @@ class Trigger {
* @deprecated Use WhileTrue(Command) with RepeatCommand, or bind
command::Schedule with IfHigh(std::function<void()>).
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
template <std::derived_from<Command> T>
WPI_DEPRECATED(
"Use WhileTrue(Command) with RepeatCommand, or bind command::Schedule "
"with IfHigh(std::function<void()>).")
Trigger WhileActiveContinous(T&& command) {
m_loop->Bind([condition = m_condition, previous = m_condition(),
command = std::make_unique<std::remove_reference_t<T>>(
command = std::make_unique<std::decay_t<T>>(
std::forward<T>(command))]() mutable {
bool current = condition();
@@ -363,12 +362,11 @@ class Trigger {
* @return The trigger, for chained calls.
* @deprecated Use WhileTrue(Command) instead.
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
template <std::derived_from<Command> T>
WPI_DEPRECATED("Use WhileTrue(Command) instead.")
Trigger WhileActiveOnce(T&& command) {
m_loop->Bind([condition = m_condition, previous = m_condition(),
command = std::make_unique<std::remove_reference_t<T>>(
command = std::make_unique<std::decay_t<T>>(
std::forward<T>(command))]() mutable {
bool current = condition();
@@ -405,12 +403,11 @@ class Trigger {
* @return The trigger, for chained calls.
* @deprecated Use OnFalse(Command) instead.
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
template <std::derived_from<Command> T>
WPI_DEPRECATED("Use OnFalse(Command) instead.")
Trigger WhenInactive(T&& command) {
m_loop->Bind([condition = m_condition, previous = m_condition(),
command = std::make_unique<std::remove_reference_t<T>>(
command = std::make_unique<std::decay_t<T>>(
std::forward<T>(command))]() mutable {
bool current = condition();
@@ -471,12 +468,11 @@ class Trigger {
* @return The trigger, for chained calls.
* @deprecated Use ToggleOnTrue(Command) instead.
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
template <std::derived_from<Command> T>
WPI_DEPRECATED("Use ToggleOnTrue(Command) instead.")
Trigger ToggleWhenActive(T&& command) {
m_loop->Bind([condition = m_condition, previous = m_condition(),
command = std::make_unique<std::remove_reference_t<T>>(
command = std::make_unique<std::decay_t<T>>(
std::forward<T>(command))]() mutable {
bool current = condition();