11#pragma once
22
3+ #include < concepts>
34#include < string>
45#include < string_view>
5- #include < type_traits>
66
77#include < units/base.h>
88
@@ -22,25 +22,19 @@ namespace tkit {
2222 * tkit::RecordOutput("/Speed", units::meters_per_second_t{2.5});
2323 */
2424
25- namespace detail {
26-
27- // Detects whether a type is a WPILib units::unit_t
28- template <typename T, typename = void >
29- struct is_unit_type : std::false_type {};
30-
25+ // Concept for WPILib unit types
3126template <typename T>
32- struct is_unit_type <T, std:: void_t <
33- decltype ( std::declval<T>().value()),
34- typename T::unit_type
35- >> : std::true_type { };
27+ concept UnitType = requires (T t) {
28+ { t. value () } -> std::convertible_to< double >;
29+ typename T::unit_type;
30+ };
3631
37- template <typename T>
38- inline constexpr bool is_unit_type_v = is_unit_type<T>::value;
32+ namespace detail {
3933
4034// Returns the abbreviation string for a unit type
41- template <typename UnitType>
35+ template <UnitType T >
4236std::string GetUnitAbbreviation () {
43- return std::string (units::abbreviation (UnitType {}));
37+ return std::string (units::abbreviation (T {}));
4438}
4539
4640} // namespace detail
@@ -54,21 +48,17 @@ std::string GetUnitAbbreviation() {
5448 * tkit::RecordOutput("/Distance", units::meter_t{3.5}); // 3.5, "m"
5549 * tkit::RecordOutput("/Angle", units::degree_t{90.0}); // 90.0, "deg"
5650 */
57- template <typename T>
58- inline auto RecordOutput (std::string_view key, const T& value)
59- -> std::enable_if_t<detail::is_unit_type_v<T>, void>
60- {
51+ template <UnitType T>
52+ inline void RecordOutput (std::string_view key, const T& value) {
6153 std::string unit = detail::GetUnitAbbreviation<T>();
6254 Logger::GetInstance ().RecordOutput (key, value.value (), unit);
6355}
6456
6557/* *
6658 * LogTable::Put overload for WPILib unit types.
6759 */
68- template <typename T>
69- inline auto Put (LogTable& table, std::string_view key, const T& value)
70- -> std::enable_if_t<detail::is_unit_type_v<T>, void>
71- {
60+ template <UnitType T>
61+ inline void Put (LogTable& table, std::string_view key, const T& value) {
7262 std::string unit = detail::GetUnitAbbreviation<T>();
7363 table.Put (key, value.value (), unit);
7464}
0 commit comments