UdonLibrary 1.0.0
機械システム研究部 C++ ライブラリ
読み取り中…
検索中…
一致する文字列を見つけられません
SerializerTraits.hpp
[詳解]
1//
2// シリアライズ可能か判定するメタ関数
3//
4// Copyright (c) 2022 udonrobo
5//
6
7#pragma once
8
11#include <utility>
12
13namespace Udon
14{
15
16 namespace Impl
17 {
18 using namespace Udon::Traits;
19
21 {
22 using ResultType = bool;
23
24 template <typename... Args>
25 constexpr ResultType operator()(Args&&... args) const noexcept
26 {
27 return argsUnpack(std::forward<Args>(args)...);
28 }
29
30 private:
31 // 可変長引数展開
32 template <typename Head, typename... Tail>
33 constexpr ResultType argsUnpack(Head&& head, Tail&&... tail) const noexcept
34 {
35 // 全メンバが出力可能か判定する
36 // 1つでも出力できないものがあれば false を返す
37 return Test<RemoveReferenceT<Head>>::test(*this, std::forward<Head>(head)) and
38 argsUnpack(std::forward<Tail>(tail)...);
39 }
40
41 // 可変長引数展開 (終端)
42 constexpr ResultType argsUnpack() const noexcept { return true; }
43
44 public:
45 // T のビット数を取得できるか判定するテスト
46 // 部分特殊化を用いる
47 template <typename T, typename = void>
48 struct Test
49 {
50 static constexpr bool test(...) { return false; }
51 };
52
53 // 算術型は可能
54 template <typename Arithmetic>
55 struct Test<Arithmetic, EnableIfVoidT<IsArithmetic<Arithmetic>::value>>
56 {
57 static constexpr bool test(...) { return true; }
58 };
59
60 // 列挙型は可能
61 template <typename Enum>
62 struct Test<Enum, EnableIfVoidT<IsEnum<Enum>::value>>
63 {
64 static constexpr bool test(...) { return true; }
65 };
66
67 // 配列型は要素がシリアライズ可能であれば可能
68 template <typename Array>
69 struct Test<Array, EnableIfVoidT<IsArray<Array>::value>>
70 {
71 template <typename T>
72 static constexpr bool test(const IsSerializableImpl& self, T&& array)
73 {
74 return Test<typename std::remove_extent<Array>::type>::test(self, *array); // 要素のサイズが取得可能である場合
75 }
76 };
77
78 // enumerate 関数を持つ型は enumerate 関数が true を返した場合可能
79 template <typename Enumerable>
80 struct Test<Enumerable, EnableIfVoidT<HasMemberFunctionEnumerate<Enumerable>::value>>
81 {
82 template <typename T>
83 static constexpr bool test(const IsSerializableImpl& tester, T&& e)
84 {
85 return e.enumerate(tester);
86 }
87 };
88 };
89
90 } // namespace Impl
91
92 namespace Traits
93 {
95 template <typename T>
97 : std::integral_constant<bool, Impl::IsSerializableImpl{}(RemoveReferenceT<T>{})>
98 {
99 };
100
101 } // namespace Traits
102} // namespace Udon
Definition CanUtility.hpp:11
Definition SerializerTraits.hpp:93
std::is_enum< T > IsEnum
Definition Typedef.hpp:49
std::is_arithmetic< T > IsArithmetic
Definition Typedef.hpp:43
EnableIfT< Test, void > EnableIfVoidT
Definition Typedef.hpp:69
std::is_array< T > IsArray
Definition Typedef.hpp:52
Definition Bit.hpp:12
CanBusTeensy< Bus > * CanBusTeensy< Bus >::self
Definition CanBusTeensy.hpp:275
static constexpr bool test(const IsSerializableImpl &self, T &&array)
Definition SerializerTraits.hpp:72
static constexpr bool test(...)
Definition SerializerTraits.hpp:64
static constexpr bool test(const IsSerializableImpl &tester, T &&e)
Definition SerializerTraits.hpp:83
Definition SerializerTraits.hpp:49
static constexpr bool test(...)
Definition SerializerTraits.hpp:50
Definition SerializerTraits.hpp:21
constexpr ResultType operator()(Args &&... args) const noexcept
Definition SerializerTraits.hpp:25
bool ResultType
Definition SerializerTraits.hpp:22
T に enumerate(f) が存在するかどうかを判定する
Definition HasMemberFunction.hpp:135
T が シリアライズ可能か判定する
Definition SerializerTraits.hpp:98