WebCFace 2.5.2
Web-based Communication Framework & Dashboard-like UI
Loading...
Searching...
No Matches
array_like.h
Go to the documentation of this file.
1#pragma once
2#include <stdexcept>
3#include <vector>
4#include <array>
5#include <type_traits>
6#include <string>
7#include "trait.h"
8#ifdef WEBCFACE_MESON
9#include "webcface-config.h"
10#else
11#include "webcface/common/webcface-config.h"
12#endif
13
15namespace traits {
16
17template <bool>
19template <>
20struct ArrayLikeTraitCheck<true> {
22};
23template <bool>
25template <>
26struct ArraySizeTraitCheck<true> {
28};
29
30constexpr std::false_type isArrayLike(...) { return {}; }
31template <typename T>
32constexpr auto isArrayLike(T)
33 -> std::bool_constant<
34 std::is_convertible_v<decltype(*std::begin(std::declval<T>())),
35 double> &&
36 std::is_convertible_v<decltype(*std::end(std::declval<T>())), double>> {
37 return {};
38}
39template <typename T>
40using IsArrayLike = decltype(isArrayLike(std::declval<T>()));
41
42constexpr std::true_type arraySizeMatch(...) { return {}; }
43template <typename T, std::size_t Num>
44constexpr auto arraySizeMatch(T, std::integral_constant<std::size_t, Num>)
45 -> std::bool_constant<std::tuple_size<T>::value == Num> {
46 return {};
47}
48template <typename T, std::size_t Num>
50 std::declval<T>(), std::integral_constant<std::size_t, Num>()));
51
57template <typename T>
58struct ArrayLikeTrait : ArrayLikeTraitCheck<IsArrayLike<T>::value> {};
63template <typename T, std::size_t N>
64struct ArrayLikeTrait<T[N]>
65 : ArrayLikeTraitCheck<std::is_convertible_v<T, double>> {};
66template <typename T, std::size_t N>
67struct ArrayLikeTrait<T (&)[N]> : ArrayLikeTrait<T[N]> {};
68
74template <typename T, std::size_t Num>
75struct ArraySizeTrait : ArraySizeTraitCheck<ArraySizeMatch<T, Num>::value> {};
80template <typename T, std::size_t N, std::size_t Num>
81struct ArraySizeTrait<T[N], Num> : ArraySizeTraitCheck<N == Num> {};
82template <typename T, std::size_t N, std::size_t Num>
83struct ArraySizeTrait<T (&)[N], Num> : ArraySizeTrait<T[N], Num> {};
84
85template <typename T>
86std::vector<double> arrayLikeToVector(const T &array) {
87 return std::vector<double>(std::begin(array), std::end(array));
88}
89
90template <std::size_t Num, typename T>
91std::array<double, Num> arrayLikeToArray(const T &array) {
92 std::array<double, Num> ret{};
93 std::size_t n = 0;
94 auto it = std::begin(array);
95 for (; n < Num && it != std::end(array); ++it, ++n) {
96 ret[n] = *it;
97 }
98 if (n == Num && it == std::end(array)) {
99 return ret;
100 } else {
101 throw std::invalid_argument(
102 "array size mismatch, expected: " + std::to_string(Num) +
103 ", got: " + std::to_string(n));
104 }
105}
106
107} // namespace traits
std::nullptr_t TraitOkType
Definition trait.h:11
constexpr std::false_type isArrayLike(...)
Definition array_like.h:30
decltype(isArrayLike(std::declval< T >())) IsArrayLike
Definition array_like.h:40
decltype(arraySizeMatch(std::declval< T >(), std::integral_constant< std::size_t, Num >())) ArraySizeMatch
Definition array_like.h:50
std::vector< double > arrayLikeToVector(const T &array)
Definition array_like.h:86
std::array< double, Num > arrayLikeToArray(const T &array)
Definition array_like.h:91
constexpr std::true_type arraySizeMatch(...)
Definition array_like.h:42
TraitOkType ArrayLike
Definition array_like.h:21
Definition array_like.h:18
Definition array_like.h:58
TraitOkType SizeMatchOrDynamic
Definition array_like.h:27
Definition array_like.h:24
Definition array_like.h:75
#define WEBCFACE_NS_END
Definition webcface-config.h:104
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:103