RInside Version 0.2.12
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
typeid.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Christian Authmann
3  */
4 
5 #pragma once
6 
7 #include <cstdint>
8 #include <type_traits>
9 #include <string>
10 #include <vector>
11 
12 /*
13  * We need a value for each type so we can communicate which type to send or receive over the socket.
14  *
15  * std::type_info won't help, since its values may change on each program start, making them unsuitable for client/server-communication.
16  *
17  * Our typeid is an int32_t. Negative values are reserved for native types (int, float, std::string, ...) while positive values
18  * can be used in custom classes. See datatypes/foo.h for the syntax.
19  */
20 
21 namespace typeid_helpers {
22  /*
23  * For void_t, see the CppCon2014 talk by Walter E. Brown: "Modern Template Metaprogramming: A Compendium", Part II
24  */
25  template<typename...>
26  struct void_t_struct { using type = void; };
27  template<typename... C>
28  using void_t = typename void_t_struct<C...>::type;
29 
30 
31 
32  template<typename T, typename = void>
33  struct has_typeid_member : std::false_type { };
34 
35  template<typename T>
36  struct has_typeid_member<T, void_t<decltype(T::TYPEID)> > : std::true_type { };
37 
38 
39  /*
40  * Note: Calling TYPEID() on an unsupported type yields some cryptic compiler errors. If you have seen errors in one of the lines below,
41  * make sure that the type you're calling TYPEID() on is either
42  * - a supported native type and has a specialization below
43  * or
44  * - a custom class with a public static const int32_t TYPEID
45  */
46  template <typename T, typename V = void>
47  struct id {
48  };
49 
50  template <typename T>
51  struct id<T, typename std::enable_if< has_typeid_member<T>::value >::type> {
52  static const int32_t value = T::TYPEID;
53  };
54 
55  template <>
56  struct id<void, void> {
57  static const int32_t value = 0;
58  };
59 
60  template <>
61  struct id<int8_t, void> {
62  static const int32_t value = -1;
63  };
64 
65  template <>
66  struct id<uint8_t, void> {
67  static const int32_t value = -2;
68  };
69 
70  template <>
71  struct id<int16_t, void> {
72  static const int32_t value = -3;
73  };
74 
75  template <>
76  struct id<uint16_t, void> {
77  static const int32_t value = -4;
78  };
79 
80  template <>
81  struct id<int32_t, void> {
82  static const int32_t value = -5;
83  };
84 
85  template <>
86  struct id<uint32_t, void> {
87  static const int32_t value = -6;
88  };
89 
90  template <>
91  struct id<int64_t, void> {
92  static const int32_t value = -7;
93  };
94 
95  template <>
96  struct id<uint64_t, void> {
97  static const int32_t value = -8;
98  };
99 
100  template <>
101  struct id<float, void> {
102  static const int32_t value = -9;
103  };
104 
105  template <>
106  struct id<double, void> {
107  static const int32_t value = -10;
108  };
109 
110  template <>
111  struct id<std::string, void> {
112  static const int32_t value = -11;
113  };
114 
115 
116 
117  template <>
118  struct id<std::vector<int8_t>, void> {
119  static const int32_t value = -21;
120  };
121 
122  template <>
123  struct id<std::vector<uint8_t>, void> {
124  static const int32_t value = -22;
125  };
126 
127  template <>
128  struct id<std::vector<int16_t>, void> {
129  static const int32_t value = -23;
130  };
131 
132  template <>
133  struct id<std::vector<uint16_t>, void> {
134  static const int32_t value = -24;
135  };
136 
137  template <>
138  struct id<std::vector<int32_t>, void> {
139  static const int32_t value = -25;
140  };
141 
142  template <>
143  struct id<std::vector<uint32_t>, void> {
144  static const int32_t value = -26;
145  };
146 
147  template <>
148  struct id<std::vector<int64_t>, void> {
149  static const int32_t value = -27;
150  };
151 
152  template <>
153  struct id<std::vector<uint64_t>, void> {
154  static const int32_t value = -28;
155  };
156 
157  template <>
158  struct id<std::vector<float>, void> {
159  static const int32_t value = -29;
160  };
161 
162  template <>
163  struct id<std::vector<double>, void> {
164  static const int32_t value = -30;
165  };
166 
167  template <>
168  struct id<std::vector<std::string>, void> {
169  static const int32_t value = -31;
170  };
171 }
172 
173 template <typename T>
174 constexpr int32_t TYPEID() {
176 }
177 
178 
179 template<typename T, typename = void>
180 struct has_typeid : std::false_type { };
181 
182 template<typename T>
183 struct has_typeid<T, typeid_helpers::void_t<decltype(typeid_helpers::id<typename std::decay<T>::type, void>::value)> > : std::true_type { };
184 
constexpr int32_t TYPEID()
Definition: typeid.h:174
typename void_t_struct< C...>::type void_t
Definition: typeid.h:28