forked from albertz/playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_template_selection.cpp
More file actions
96 lines (79 loc) · 2.93 KB
/
test_template_selection.cpp
File metadata and controls
96 lines (79 loc) · 2.93 KB
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
#include <boost/type_traits.hpp>
enum {
SVT_INT,
SVT_FLOAT,
SVT_BASEOBJ,
SVT_CUSTOMVAR
};
struct BaseObject {};
struct CustomVar {};
template<typename T> struct GetType;
template<> struct GetType<int> { static const int value = SVT_INT; };
template<> struct GetType<float> { static const int value = SVT_FLOAT; };
template<> struct GetType<BaseObject> { static const int value = SVT_BASEOBJ; };
template<bool> struct GetType_BaseCustomVar;
template<> struct GetType_BaseCustomVar<true> {
struct Type { static const int value = SVT_CUSTOMVAR; };
};
template<typename T> struct GetType : GetType_BaseCustomVar<boost::is_base_of<CustomVar,T>::value>::Type {};
struct ScriptVar_t;
template<typename T> T CastScriptVarConst(const ScriptVar_t& s);
struct ScriptVar_t {
operator int() const { return 0; }
operator float() const { return 0.0f; }
operator BaseObject() const { return BaseObject(); }
template<typename T> T* as() const { return NULL; }
template <typename T> T castConst() const { return CastScriptVarConst<T>(*this); }
};
template<typename T>
T _CastScriptVarConst(const ScriptVar_t& s, T*, typename boost::enable_if_c<(GetType<T>::value < SVT_BASEOBJ), T>::type*) {
return (T) s;
}
template<typename T>
T _CastScriptVarConst(const ScriptVar_t& s, T*, typename boost::enable_if_c<boost::is_base_of<CustomVar,T>::value, T>::type*) {
return *s.as<T>();
}
template<typename T> T CastScriptVarConst(const ScriptVar_t& s) { return _CastScriptVarConst(s, (T*)NULL, (T*)NULL); }
/*
template<typename T>
typename boost::enable_if_c<(GetType<T>::value < SVT_BASEOBJ), T>::type
CastScriptVarConst(const ScriptVar_t& s) {
return (T) s;
}
template<typename T>
typename boost::enable_if_c<!(GetType<T>::value < SVT_BASEOBJ)
&& boost::is_base_of<CustomVar,T>::value,T>::type
CastScriptVarConst(const ScriptVar_t& s) {
return *s.as<T>();
}
*/
/*
template<typename T> T CastScriptVarConst(const ScriptVar_t& s);
template<bool> struct CastScriptVar1;
template<typename T> struct CastScriptVar1_IsSimpleType {
static const bool value = GetType<T>::value < SVT_BASEOBJ;
};
template<> struct CastScriptVar1<true> {
template<typename T> static T castConst(const ScriptVar_t& s, const T&) { return (T) s; }
};
template<bool> struct CastScriptVar2;
template<typename T> struct CastScriptVar2_IsCustomVar {
static const bool value = boost::is_base_of<CustomVar,T>::value;
};
template<> struct CastScriptVar2<true> {
template<typename T> static T castConst(const ScriptVar_t& s, const T&) { return *s.as<T>(); }
};
template<> struct CastScriptVar1<false> {
template<typename T> static T castConst(const ScriptVar_t& s, const T&) {
return CastScriptVar2<CastScriptVar2_IsCustomVar<T>::value>::castConst(s, T());
}
};
template<typename T> T CastScriptVarConst(const ScriptVar_t& s) {
return CastScriptVar1<CastScriptVar1_IsSimpleType<T>::value>::castConst(s, T());
}
*/
int main() {
ScriptVar_t v;
v.castConst<int>();
v.castConst<CustomVar>();
}