summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Dominik Laskowski <domlaskowski@google.com> 2021-12-15 13:14:54 -0800
committer Dominik Laskowski <domlaskowski@google.com> 2022-01-14 17:00:07 -0800
commit04667b7f2d792ee498bde06af63cc3771849a3c3 (patch)
tree710997e7e70d3da392cab36bdba7249868c3755c
parent4e0d20d43aa955587e468a92a4b0a491b4733f8e (diff)
FTL: Tidy up API
Move array_traits.h to details, and rename enum_cast to its proposed C++23 counterpart. Bug: 185536303 Test: ftl_test Change-Id: I432448d388d388491066ef37db05c5b9d67550bc
-rw-r--r--include/ftl/Flags.h4
-rw-r--r--include/ftl/details/array_traits.h (renamed from include/ftl/array_traits.h)6
-rw-r--r--include/ftl/enum.h30
-rw-r--r--include/ftl/small_vector.h12
-rw-r--r--include/ftl/static_vector.h12
-rw-r--r--libs/ftl/enum_test.cpp2
6 files changed, 34 insertions, 32 deletions
diff --git a/include/ftl/Flags.h b/include/ftl/Flags.h
index ae708313fd..932af2d9f9 100644
--- a/include/ftl/Flags.h
+++ b/include/ftl/Flags.h
@@ -209,12 +209,12 @@ namespace flag_operators {
template <typename F, typename = std::enable_if_t<ftl::is_scoped_enum_v<F>>>
inline Flags<F> operator~(F f) {
- return static_cast<F>(~ftl::enum_cast(f));
+ return static_cast<F>(~ftl::to_underlying(f));
}
template <typename F, typename = std::enable_if_t<ftl::is_scoped_enum_v<F>>>
Flags<F> operator|(F lhs, F rhs) {
- return static_cast<F>(ftl::enum_cast(lhs) | ftl::enum_cast(rhs));
+ return static_cast<F>(ftl::to_underlying(lhs) | ftl::to_underlying(rhs));
}
} // namespace flag_operators
diff --git a/include/ftl/array_traits.h b/include/ftl/details/array_traits.h
index 1265fa15fe..16e63eca74 100644
--- a/include/ftl/array_traits.h
+++ b/include/ftl/details/array_traits.h
@@ -21,9 +21,9 @@
#include <new>
#include <type_traits>
-#define FTL_ARRAY_TRAIT(T, U) using U = typename ArrayTraits<T>::U
+#define FTL_ARRAY_TRAIT(T, U) using U = typename details::ArrayTraits<T>::U
-namespace android::ftl {
+namespace android::ftl::details {
template <typename T>
struct ArrayTraits {
@@ -132,4 +132,4 @@ struct ArrayComparators {
}
};
-} // namespace android::ftl
+} // namespace android::ftl::details
diff --git a/include/ftl/enum.h b/include/ftl/enum.h
index dfe3a0976b..5234c051b1 100644
--- a/include/ftl/enum.h
+++ b/include/ftl/enum.h
@@ -87,11 +87,13 @@ inline constexpr bool is_scoped_enum_v = is_scoped_enum<T>::value;
// Shorthand for casting an enumerator to its integral value.
//
+// TODO: Replace with std::to_underlying in C++23.
+//
// enum class E { A, B, C };
-// static_assert(ftl::enum_cast(E::B) == 1);
+// static_assert(ftl::to_underlying(E::B) == 1);
//
template <typename E>
-constexpr auto enum_cast(E v) {
+constexpr auto to_underlying(E v) {
return static_cast<std::underlying_type_t<E>>(v);
}
@@ -137,19 +139,19 @@ struct enum_end {
template <typename E>
struct enum_end<E, std::void_t<decltype(E::ftl_last)>> {
- static constexpr E value = E{enum_cast(E::ftl_last) + 1};
+ static constexpr E value = E{to_underlying(E::ftl_last) + 1};
};
template <typename E>
inline constexpr E enum_end_v = enum_end<E>::value;
template <typename E>
-inline constexpr E enum_last_v = E{enum_cast(enum_end_v<E>) - 1};
+inline constexpr E enum_last_v = E{to_underlying(enum_end_v<E>) - 1};
template <typename E>
struct enum_size {
- static constexpr auto kBegin = enum_cast(enum_begin_v<E>);
- static constexpr auto kEnd = enum_cast(enum_end_v<E>);
+ static constexpr auto kBegin = to_underlying(enum_begin_v<E>);
+ static constexpr auto kEnd = to_underlying(enum_end_v<E>);
static_assert(kBegin < kEnd, "Invalid range");
static constexpr std::size_t value = kEnd - kBegin;
@@ -174,7 +176,7 @@ struct EnumRange;
template <typename E, template <E> class F, typename T, T... Vs>
struct EnumRange<E, F, std::integer_sequence<T, Vs...>> {
- static constexpr auto kBegin = enum_cast(enum_begin_v<E>);
+ static constexpr auto kBegin = to_underlying(enum_begin_v<E>);
static constexpr auto kSize = enum_size_v<E>;
using R = decltype(F<E{}>::value);
@@ -194,7 +196,7 @@ struct FlagName {
using E = decltype(I);
using U = std::underlying_type_t<E>;
- static constexpr E V{U{1} << enum_cast(I)};
+ static constexpr E V{U{1} << to_underlying(I)};
static constexpr auto value = ftl_enum<E, V>();
};
@@ -237,10 +239,10 @@ constexpr std::string_view enum_name() {
//
template <typename E>
constexpr std::optional<std::string_view> enum_name(E v) {
- const auto value = enum_cast(v);
+ const auto value = to_underlying(v);
- constexpr auto kBegin = enum_cast(enum_begin_v<E>);
- constexpr auto kLast = enum_cast(enum_last_v<E>);
+ constexpr auto kBegin = to_underlying(enum_begin_v<E>);
+ constexpr auto kLast = to_underlying(enum_last_v<E>);
if (value < kBegin || value > kLast) return {};
constexpr auto kRange = details::EnumRange<E, details::EnumName>{};
@@ -256,7 +258,7 @@ constexpr std::optional<std::string_view> enum_name(E v) {
//
template <typename E>
constexpr std::optional<std::string_view> flag_name(E v) {
- const auto value = enum_cast(v);
+ const auto value = to_underlying(v);
// TODO: Replace with std::popcount and std::countr_zero in C++20.
if (__builtin_popcountl(value) != 1) return {};
@@ -277,7 +279,7 @@ inline std::string enum_string(E v) {
if (const auto name = enum_name(v)) {
return std::string(*name);
}
- return to_string(enum_cast(v));
+ return to_string(to_underlying(v));
}
// Returns a stringified flag enumerator, or its integral value if not named.
@@ -293,7 +295,7 @@ inline std::string flag_string(E v) {
return std::string(*name);
}
constexpr auto radix = sizeof(E) == 1 ? Radix::kBin : Radix::kHex;
- return to_string(enum_cast(v), radix);
+ return to_string(to_underlying(v), radix);
}
} // namespace android::ftl
diff --git a/include/ftl/small_vector.h b/include/ftl/small_vector.h
index 65a953670d..03587e35ac 100644
--- a/include/ftl/small_vector.h
+++ b/include/ftl/small_vector.h
@@ -16,7 +16,7 @@
#pragma once
-#include <ftl/array_traits.h>
+#include <ftl/details/array_traits.h>
#include <ftl/static_vector.h>
#include <algorithm>
@@ -73,7 +73,7 @@ struct is_small_vector;
// assert(strings[2] == "???");
//
template <typename T, std::size_t N>
-class SmallVector final : ArrayTraits<T>, ArrayComparators<SmallVector> {
+class SmallVector final : details::ArrayTraits<T>, details::ArrayComparators<SmallVector> {
using Static = StaticVector<T, N>;
using Dynamic = SmallVector<T, 0>;
@@ -266,12 +266,12 @@ class SmallVector final : ArrayTraits<T>, ArrayComparators<SmallVector> {
// Partial specialization without static storage.
template <typename T>
-class SmallVector<T, 0> final : ArrayTraits<T>,
- ArrayIterators<SmallVector<T, 0>, T>,
+class SmallVector<T, 0> final : details::ArrayTraits<T>,
+ details::ArrayIterators<SmallVector<T, 0>, T>,
std::vector<T> {
- using ArrayTraits<T>::construct_at;
+ using details::ArrayTraits<T>::construct_at;
- using Iter = ArrayIterators<SmallVector, T>;
+ using Iter = details::ArrayIterators<SmallVector, T>;
using Impl = std::vector<T>;
friend Iter;
diff --git a/include/ftl/static_vector.h b/include/ftl/static_vector.h
index cd7b92a758..b7f8c29dec 100644
--- a/include/ftl/static_vector.h
+++ b/include/ftl/static_vector.h
@@ -16,7 +16,7 @@
#pragma once
-#include <ftl/array_traits.h>
+#include <ftl/details/array_traits.h>
#include <ftl/initializer_list.h>
#include <algorithm>
@@ -73,14 +73,14 @@ constexpr struct IteratorRangeTag {
// assert(strings[2] == "???");
//
template <typename T, std::size_t N>
-class StaticVector final : ArrayTraits<T>,
- ArrayIterators<StaticVector<T, N>, T>,
- ArrayComparators<StaticVector> {
+class StaticVector final : details::ArrayTraits<T>,
+ details::ArrayIterators<StaticVector<T, N>, T>,
+ details::ArrayComparators<StaticVector> {
static_assert(N > 0);
- using ArrayTraits<T>::construct_at;
+ using details::ArrayTraits<T>::construct_at;
- using Iter = ArrayIterators<StaticVector, T>;
+ using Iter = details::ArrayIterators<StaticVector, T>;
friend Iter;
// There is ambiguity when constructing from two iterator-like elements like pointers:
diff --git a/libs/ftl/enum_test.cpp b/libs/ftl/enum_test.cpp
index 1fd43abbc6..d8ce7a5e7b 100644
--- a/libs/ftl/enum_test.cpp
+++ b/libs/ftl/enum_test.cpp
@@ -72,7 +72,7 @@ enum class Planet : std::uint8_t {
kNeptune
};
-constexpr Planet kPluto{ftl::enum_cast(Planet::kNeptune) + 1}; // Honorable mention.
+constexpr Planet kPluto{ftl::to_underlying(Planet::kNeptune) + 1}; // Honorable mention.
static_assert(ftl::enum_begin_v<Planet> == Planet::kMercury);
static_assert(ftl::enum_last_v<Planet> == Planet::kNeptune);