[commands] Clean up make_vector.h (#5917)

This commit is contained in:
Tyler Veness
2023-11-12 20:23:34 -08:00
committed by GitHub
parent 78ebc6e9ec
commit c1a57e422a
5 changed files with 31 additions and 63 deletions

View File

@@ -20,7 +20,7 @@ TEST_F(ParallelCommandGroupTest, ParallelGroupSchedule) {
MockCommand* command1 = command1Holder.get();
MockCommand* command2 = command2Holder.get();
ParallelCommandGroup group(tcb::make_vector<std::unique_ptr<Command>>(
ParallelCommandGroup group(make_vector<std::unique_ptr<Command>>(
std::move(command1Holder), std::move(command2Holder)));
EXPECT_CALL(*command1, Initialize());
@@ -50,7 +50,7 @@ TEST_F(ParallelCommandGroupTest, ParallelGroupInterrupt) {
MockCommand* command1 = command1Holder.get();
MockCommand* command2 = command2Holder.get();
ParallelCommandGroup group(tcb::make_vector<std::unique_ptr<Command>>(
ParallelCommandGroup group(make_vector<std::unique_ptr<Command>>(
std::move(command1Holder), std::move(command2Holder)));
EXPECT_CALL(*command1, Initialize());

View File

@@ -24,8 +24,8 @@ TEST_F(ParallelDeadlineGroupTest, DeadlineGroupSchedule) {
ParallelDeadlineGroup group(
std::move(command1Holder),
tcb::make_vector<std::unique_ptr<Command>>(std::move(command2Holder),
std::move(command3Holder)));
make_vector<std::unique_ptr<Command>>(std::move(command2Holder),
std::move(command3Holder)));
EXPECT_CALL(*command1, Initialize());
EXPECT_CALL(*command1, Execute()).Times(2);
@@ -64,8 +64,8 @@ TEST_F(ParallelDeadlineGroupTest, SequentialGroupInterrupt) {
ParallelDeadlineGroup group(
std::move(command1Holder),
tcb::make_vector<std::unique_ptr<Command>>(std::move(command2Holder),
std::move(command3Holder)));
make_vector<std::unique_ptr<Command>>(std::move(command2Holder),
std::move(command3Holder)));
EXPECT_CALL(*command1, Initialize());
EXPECT_CALL(*command1, Execute()).Times(1);

View File

@@ -23,7 +23,7 @@ TEST_F(ParallelRaceGroupTest, ParallelRaceSchedule) {
MockCommand* command2 = command2Holder.get();
MockCommand* command3 = command3Holder.get();
ParallelRaceGroup group{tcb::make_vector<std::unique_ptr<Command>>(
ParallelRaceGroup group{make_vector<std::unique_ptr<Command>>(
std::move(command1Holder), std::move(command2Holder),
std::move(command3Holder))};
@@ -59,7 +59,7 @@ TEST_F(ParallelRaceGroupTest, ParallelRaceInterrupt) {
MockCommand* command2 = command2Holder.get();
MockCommand* command3 = command3Holder.get();
ParallelRaceGroup group{tcb::make_vector<std::unique_ptr<Command>>(
ParallelRaceGroup group{make_vector<std::unique_ptr<Command>>(
std::move(command1Holder), std::move(command2Holder),
std::move(command3Holder))};
@@ -164,7 +164,7 @@ TEST_F(ParallelRaceGroupTest, ParallelRaceScheduleTwice) {
MockCommand* command2 = command2Holder.get();
MockCommand* command3 = command3Holder.get();
ParallelRaceGroup group{tcb::make_vector<std::unique_ptr<Command>>(
ParallelRaceGroup group{make_vector<std::unique_ptr<Command>>(
std::move(command1Holder), std::move(command2Holder),
std::move(command3Holder))};

View File

@@ -22,7 +22,7 @@ TEST_F(SequentialCommandGroupTest, SequentialGroupSchedule) {
MockCommand* command2 = command2Holder.get();
MockCommand* command3 = command3Holder.get();
SequentialCommandGroup group{tcb::make_vector<std::unique_ptr<Command>>(
SequentialCommandGroup group{make_vector<std::unique_ptr<Command>>(
std::move(command1Holder), std::move(command2Holder),
std::move(command3Holder))};
@@ -61,7 +61,7 @@ TEST_F(SequentialCommandGroupTest, SequentialGroupInterrupt) {
MockCommand* command2 = command2Holder.get();
MockCommand* command3 = command3Holder.get();
SequentialCommandGroup group{tcb::make_vector<std::unique_ptr<Command>>(
SequentialCommandGroup group{make_vector<std::unique_ptr<Command>>(
std::move(command1Holder), std::move(command2Holder),
std::move(command3Holder))};

View File

@@ -8,59 +8,27 @@
#include <utility>
#include <vector>
namespace tcb {
namespace detail {
template <typename T, typename...>
struct vec_type_helper {
using type = T;
};
template <typename... Args>
struct vec_type_helper<void, Args...> {
using type = typename std::common_type_t<Args...>;
};
template <typename T, typename... Args>
using vec_type_helper_t = typename vec_type_helper<T, Args...>::type;
template <typename, typename...>
struct all_constructible_and_convertible : std::true_type {};
template <typename T, typename First, typename... Rest>
struct all_constructible_and_convertible<T, First, Rest...>
: std::conditional_t<
std::is_constructible_v<T, First> && std::is_convertible_v<First, T>,
all_constructible_and_convertible<T, Rest...>, std::false_type> {};
template <typename T, typename First, typename... Rest>
inline constexpr bool all_constructible_and_convertible_v =
all_constructible_and_convertible<T, First, Rest...>::value;
template <typename T, typename... Args>
requires(!std::is_trivially_copyable_v<T>)
std::vector<T> make_vector_impl(Args&&... args) {
std::vector<T> vec;
vec.reserve(sizeof...(Args));
using arr_t = int[];
(void)arr_t{0, (vec.emplace_back(std::forward<Args>(args)), 0)...};
return vec;
}
template <typename T, typename... Args>
requires std::is_trivially_copyable_v<T>
std::vector<T> make_vector_impl(Args&&... args) {
return std::vector<T>{std::forward<Args>(args)...};
}
} // namespace detail
namespace frc2 {
template <typename T = void, typename... Args,
typename V = detail::vec_type_helper_t<T, Args...>>
requires detail::all_constructible_and_convertible_v<V, Args...>
std::vector<V> make_vector(Args&&... args) {
return detail::make_vector_impl<V>(std::forward<Args>(args)...);
typename CommonType = std::conditional_t<
std::same_as<T, void>, std::common_type_t<Args...>, T>>
requires((std::is_constructible_v<T, Args> &&
std::is_convertible_v<Args, T>) &&
...)
std::vector<CommonType> make_vector(Args&&... args) {
if constexpr (std::is_trivially_copyable_v<CommonType>) {
return std::vector<CommonType>{std::forward<Args>(args)...};
} else {
std::vector<CommonType> vec;
vec.reserve(sizeof...(Args));
using arr_t = int[];
[[maybe_unused]] arr_t arr{
0, (vec.emplace_back(std::forward<Args>(args)), 0)...};
return vec;
}
}
} // namespace tcb
} // namespace frc2