-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscratchpad.cpp
191 lines (147 loc) · 4.37 KB
/
scratchpad.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <string_view>
template<typename T, typename Tensor>
constexpr auto getTensorContents(Tensor tensor)
{
if constexpr (std::is_same_v<T, bool>)
return true;
if constexpr (std::is_same_v<T, float>)
return 1.2345;
if constexpr (std::is_same_v<T, const char*>)
return "asdfghj";
}
template<class T, typename Tensor>
constexpr void call_a(int i, const char* c, Tensor tensor)
{
auto t = getTensorContents<T>(tensor);
if constexpr (std::is_same_v<T, bool>)
std::cout << "BOOL" << std::endl;
if constexpr (std::is_same_v<T, float>)
std::cout << "FLOAT" << std::endl;
if constexpr (std::is_same_v<T, const char*>)
std::cout << "CONST CHAR" << std::endl;
std::cout << t << std::endl;
}
template<class T, typename Tensor>
constexpr void call_b(const std::string& s, bool b, Tensor tensor)
{
auto t = getTensorContents<T>(tensor);
if constexpr (std::is_same_v<T, bool>)
std::cout << "BOOL" << std::endl;
if constexpr (std::is_same_v<T, float>)
std::cout << "FLOAT" << std::endl;
if constexpr (std::is_same_v<T, const char*>)
std::cout << "CONST CHAR" << std::endl;
std::cout << t << std::endl;
}
/*
struct callable_t
{
template<class T, typename Tensor>
constexpr void operator()(int i, const char* c, Tensor tensor)
{
auto t = getTensorContents<T>(tensor);
if constexpr (std::is_same_v<T, bool>)
std::cout << "BOOL" << std::endl;
if constexpr (std::is_same_v<T, float>)
std::cout << "FLOAT" << std::endl;
if constexpr (std::is_same_v<T, const char*>)
std::cout << "CONST CHAR" << std::endl;
std::cout << t << std::endl;
}
};
*/
enum value : uint8_t
{
BOOL,
FLOAT,
CONST_CHAR,
};
template<value v>
struct tv;
template<>
struct tv<value::BOOL>
{
using type = bool;
};
template<>
struct tv<value::FLOAT>
{
using type = float;
};
template<>
struct tv<value::CONST_CHAR>
{
using type = const char*;
};
template<typename Tensor>
constexpr void simple_dispatch(value v, Tensor tensor)
{
switch(v)
{
case BOOL:
std::invoke(call_a<bool, decltype(tensor)>, 1, "some string", tensor);
break;
case FLOAT:
std::invoke(call_a<float, decltype(tensor)>, 1, "some string", tensor);
break;
case CONST_CHAR:
std::invoke(call_a<const char*, decltype(tensor)>, 1, "some string", tensor);
break;
}
}
template<typename T, typename Tensor>
struct f1
{
constexpr void operator()(Tensor tensor, int i, const char* c)
{
auto t = getTensorContents<T>(tensor);
if constexpr (std::is_same_v<T, bool>)
std::cout << "BOOL" << std::endl;
if constexpr (std::is_same_v<T, float>)
std::cout << "FLOAT" << std::endl;
if constexpr (std::is_same_v<T, const char*>)
std::cout << "CONST CHAR" << std::endl;
std::cout << t << std::endl;
}
};
template<typename T, typename Tensor>
struct f2
{
constexpr void operator()(Tensor tensor, const std::string& s, bool b)
{
auto t = getTensorContents<T>(tensor);
if constexpr (std::is_same_v<T, bool>)
std::cout << "BOOL" << std::endl;
if constexpr (std::is_same_v<T, float>)
std::cout << "FLOAT" << std::endl;
if constexpr (std::is_same_v<T, const char*>)
std::cout << "CONST CHAR" << std::endl;
std::cout << t << std::endl;
}
};
template<template<typename, typename> class func_t, typename Tensor, typename... Args>
constexpr void callable_wrapper1(value v, Tensor tensor, Args... args)
{
switch(v)
{
case BOOL: return std::invoke(func_t<bool, Tensor>(), tensor, args...);
case FLOAT: return std::invoke(func_t<float, Tensor>(), tensor, args...);
case CONST_CHAR: return std::invoke(func_t<const char*, Tensor>(), tensor, args...);
}
}
class MyTensorA{};
class MyTensorB{};
int main()
{
MyTensorA tensor_a;
value v_a = value::FLOAT;
callable_wrapper1<f1>(v_a, tensor_a, 5, "");
MyTensorB tensor_b;
value v_b = value::CONST_CHAR;
callable_wrapper1<f2>(v_b, tensor_b, "", false);
return 0;
}