Skip to content

Commit af5774b

Browse files
committed
Add new type traits && Rename existing
Add bew type traits for std::array - isStdArray and numeric std::array - isNumericStdArray. Add arraySize trait to get array size. Rename is_flags to isFlags, is_enum to isEnum, is_typemask to isTypeMask.
1 parent 1bf824e commit af5774b

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

libraries/libim/content/text/text_resource_reader.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace libim::content::text {
3030
void assertKeyValue(std::string_view key, T v)
3131
{
3232
using DT = typename std::decay_t<T>;
33-
if constexpr (utils::is_enum<DT>){
33+
if constexpr (utils::isEnum<DT>){
3434
return assertKeyValue(key, utils::to_underlying(v));
3535
}
3636

@@ -56,7 +56,7 @@ namespace libim::content::text {
5656
template<typename T, typename DT = typename std::decay_t<T>>
5757
DT readFlags()
5858
{
59-
static_assert(utils::is_enum<DT>,
59+
static_assert(utils::isEnum<DT>,
6060
"T must be either enum, Flags or TypeMask type"
6161
);
6262
return getFlags<DT>();
@@ -160,7 +160,7 @@ namespace libim::content::text {
160160

161161
if constexpr(!hasListSize && utils::has_mf_capacity<Container>)
162162
{
163-
if(result.capacity() < 10) {
163+
if (result.capacity() < 10) {
164164
reserve(result, 256);
165165
}
166166
}

libraries/libim/content/text/text_resource_writer.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace libim::content::text {
3232
indent = indent - (std::signbit(n) ? 1 : 0);
3333

3434
std::size_t digits = 0;
35-
if constexpr(std::is_unsigned_v<T>) {
35+
if constexpr (std::is_unsigned_v<T>) {
3636
digits = utils::numdigits(static_cast<uint64_t>(n));
3737
}
3838
else {
@@ -80,14 +80,14 @@ namespace libim::content::text {
8080
template<std::size_t precision = 0,
8181
typename T,
8282
bool isArithmetic = std::is_arithmetic_v<T>,
83-
typename = std::enable_if_t<isArithmetic || utils::is_enum<T>>
83+
typename = std::enable_if_t<isArithmetic || utils::isEnum<T>>
8484
>
8585
TextResourceWriter& writeKeyValue(std::string_view key, T value, std::size_t indent = 1)
8686
{
8787
constexpr std::size_t p = [&](){
8888
// Set default precision for float and enum type
8989
if constexpr(precision == 0 &&
90-
(std::is_floating_point_v<T> || utils::is_enum<T>))
90+
(std::is_floating_point_v<T> || utils::isEnum<T>))
9191
{
9292
if constexpr (std::is_floating_point_v<T>) {
9393
return std::size_t(6);
@@ -168,7 +168,7 @@ namespace libim::content::text {
168168
}
169169

170170

171-
for(auto[i, v] : utils::enumerate(list))
171+
for (auto[i, v] : utils::enumerate(list))
172172
{
173173
auto pos = tell();
174174
writeRow(*this, i, v); // TODO: detect via function trait the return type of row writer function.

libraries/libim/text/tokenizer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace libim::text {
3434
template <typename T, typename DT = std::decay_t<T>>
3535
DT getFlags()
3636
{
37-
static_assert(utils::is_enum<DT>,
37+
static_assert(utils::isEnum<DT>,
3838
"T must be either enum, Flags or TypeMask type"
3939
);
4040
auto tkn = getNextToken();

libraries/libim/utils/traits.h

+44-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#define LIBIM_TRAITS_H
33
#include <libim/types/flags.h>
44
#include <libim/types/typemask.h>
5+
#include <libim/math/abstract_vector.h>
6+
7+
#include <array>
58
#include <type_traits>
69

710
namespace libim::utils {
@@ -48,11 +51,36 @@ namespace libim::utils {
4851
template<typename T>
4952
struct is_flags<Flags<T>> : std::true_type {};
5053

54+
template<typename>
55+
struct is_std_array : std::false_type {};
56+
57+
template<typename T, std::size_t N>
58+
struct is_std_array<std::array<T, N>> : std::true_type {};
59+
60+
template<typename>
61+
struct is_numeric_std_array : std::false_type {};
62+
63+
template<typename T, std::size_t N>
64+
struct is_numeric_std_array<std::array<T, N>> : std::is_arithmetic<T> {};
65+
5166
template<typename>
5267
struct is_typemask : std::false_type {};
5368

5469
template<typename T>
5570
struct is_typemask<TypeMask<T>> : std::true_type {};
71+
72+
template<typename>
73+
struct array_size;
74+
75+
template<typename T, size_t N>
76+
struct array_size<std::array<T,N>> {
77+
static constexpr size_t size = N;
78+
};
79+
80+
template<typename T, size_t N>
81+
struct array_size<T[N]> {
82+
static size_t const size = N;
83+
};
5684
}
5785

5886

@@ -67,7 +95,7 @@ namespace libim::utils {
6795
typename Container::value_type
6896
>;
6997

70-
// Triats
98+
/* Triats */
7199

72100
// Does C have member function 'capacity'
73101
template<typename C>
@@ -85,16 +113,28 @@ namespace libim::utils {
85113
template<typename C>
86114
constexpr bool has_mf_reserve = detail::has_reserve<C>::value;
87115

116+
// Is T of type std::array<T,N>
117+
template<typename T>
118+
constexpr bool isStdArray = detail::is_std_array<T>::value;
119+
120+
// Is T numeric std::array<T, N> type
121+
template<typename T>
122+
constexpr bool isNumericStdArray = detail::is_numeric_std_array<T>::value;
123+
88124
// Is T of type Flags
89125
template<typename T>
90-
constexpr bool is_flags = detail::is_flags<T>::value;
126+
constexpr bool isFlags = detail::is_flags<T>::value;
91127

92128
// Is T of type TypeMask
93129
template<typename T>
94-
constexpr bool is_typemask = detail::is_typemask<T>::value;
130+
constexpr bool isTypeMask = detail::is_typemask<T>::value;
95131

96132
// is T of type enum or Flags or TypeMask
97133
template<typename T>
98-
constexpr bool is_enum = std::is_enum_v<T> || is_flags<T> || is_typemask<T>;
134+
constexpr bool isEnum = std::is_enum_v<T> || isFlags<T> || isTypeMask<T>;
135+
136+
/* Utility type triats */
137+
template<typename T>
138+
constexpr std::size_t arraySize = detail::array_size<T>::size;
99139
}
100140
#endif // LIBIM_TRAITS_H

0 commit comments

Comments
 (0)