PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/simulator/libsimulator/lib/protobuf-lite/google/protobuf/stubs/type_traits.h

https://github.com/dumganhar/cocos2d-x
C Header | 336 lines | 175 code | 39 blank | 122 comment | 12 complexity | f67f37d7756e3185d5b3728c6fe0e77e MD5 | raw file
  1. // Copyright (c) 2006, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // ----
  30. // Author: Matt Austern
  31. //
  32. // This code is compiled directly on many platforms, including client
  33. // platforms like Windows, Mac, and embedded systems. Before making
  34. // any changes here, make sure that you're not breaking any platforms.
  35. //
  36. // Define a small subset of tr1 type traits. The traits we define are:
  37. // is_integral
  38. // is_floating_point
  39. // is_pointer
  40. // is_enum
  41. // is_reference
  42. // is_pod
  43. // has_trivial_constructor
  44. // has_trivial_copy
  45. // has_trivial_assign
  46. // has_trivial_destructor
  47. // remove_const
  48. // remove_volatile
  49. // remove_cv
  50. // remove_reference
  51. // add_reference
  52. // remove_pointer
  53. // is_same
  54. // is_convertible
  55. // We can add more type traits as required.
  56. #ifndef GOOGLE_PROTOBUF_TYPE_TRAITS_H_
  57. #define GOOGLE_PROTOBUF_TYPE_TRAITS_H_
  58. #include <utility> // For pair
  59. #include <google/protobuf/stubs/template_util.h> // For true_type and false_type
  60. namespace google {
  61. namespace protobuf {
  62. namespace internal {
  63. template <class T> struct is_integral;
  64. template <class T> struct is_floating_point;
  65. template <class T> struct is_pointer;
  66. // MSVC can't compile this correctly, and neither can gcc 3.3.5 (at least)
  67. #if !defined(_MSC_VER) && !(defined(__GNUC__) && __GNUC__ <= 3)
  68. // is_enum uses is_convertible, which is not available on MSVC.
  69. template <class T> struct is_enum;
  70. #endif
  71. template <class T> struct is_reference;
  72. template <class T> struct is_pod;
  73. template <class T> struct has_trivial_constructor;
  74. template <class T> struct has_trivial_copy;
  75. template <class T> struct has_trivial_assign;
  76. template <class T> struct has_trivial_destructor;
  77. template <class T> struct remove_const;
  78. template <class T> struct remove_volatile;
  79. template <class T> struct remove_cv;
  80. template <class T> struct remove_reference;
  81. template <class T> struct add_reference;
  82. template <class T> struct remove_pointer;
  83. template <class T, class U> struct is_same;
  84. #if !defined(_MSC_VER) && !(defined(__GNUC__) && __GNUC__ <= 3)
  85. template <class From, class To> struct is_convertible;
  86. #endif
  87. // is_integral is false except for the built-in integer types. A
  88. // cv-qualified type is integral if and only if the underlying type is.
  89. template <class T> struct is_integral : false_type { };
  90. template<> struct is_integral<bool> : true_type { };
  91. template<> struct is_integral<char> : true_type { };
  92. template<> struct is_integral<unsigned char> : true_type { };
  93. template<> struct is_integral<signed char> : true_type { };
  94. #if defined(_MSC_VER)
  95. // wchar_t is not by default a distinct type from unsigned short in
  96. // Microsoft C.
  97. // See http://msdn2.microsoft.com/en-us/library/dh8che7s(VS.80).aspx
  98. template<> struct is_integral<__wchar_t> : true_type { };
  99. #else
  100. template<> struct is_integral<wchar_t> : true_type { };
  101. #endif
  102. template<> struct is_integral<short> : true_type { };
  103. template<> struct is_integral<unsigned short> : true_type { };
  104. template<> struct is_integral<int> : true_type { };
  105. template<> struct is_integral<unsigned int> : true_type { };
  106. template<> struct is_integral<long> : true_type { };
  107. template<> struct is_integral<unsigned long> : true_type { };
  108. #ifdef HAVE_LONG_LONG
  109. template<> struct is_integral<long long> : true_type { };
  110. template<> struct is_integral<unsigned long long> : true_type { };
  111. #endif
  112. template <class T> struct is_integral<const T> : is_integral<T> { };
  113. template <class T> struct is_integral<volatile T> : is_integral<T> { };
  114. template <class T> struct is_integral<const volatile T> : is_integral<T> { };
  115. // is_floating_point is false except for the built-in floating-point types.
  116. // A cv-qualified type is integral if and only if the underlying type is.
  117. template <class T> struct is_floating_point : false_type { };
  118. template<> struct is_floating_point<float> : true_type { };
  119. template<> struct is_floating_point<double> : true_type { };
  120. template<> struct is_floating_point<long double> : true_type { };
  121. template <class T> struct is_floating_point<const T>
  122. : is_floating_point<T> { };
  123. template <class T> struct is_floating_point<volatile T>
  124. : is_floating_point<T> { };
  125. template <class T> struct is_floating_point<const volatile T>
  126. : is_floating_point<T> { };
  127. // is_pointer is false except for pointer types. A cv-qualified type (e.g.
  128. // "int* const", as opposed to "int const*") is cv-qualified if and only if
  129. // the underlying type is.
  130. template <class T> struct is_pointer : false_type { };
  131. template <class T> struct is_pointer<T*> : true_type { };
  132. template <class T> struct is_pointer<const T> : is_pointer<T> { };
  133. template <class T> struct is_pointer<volatile T> : is_pointer<T> { };
  134. template <class T> struct is_pointer<const volatile T> : is_pointer<T> { };
  135. #if !defined(_MSC_VER) && !(defined(__GNUC__) && __GNUC__ <= 3)
  136. namespace internal {
  137. template <class T> struct is_class_or_union {
  138. template <class U> static small_ tester(void (U::*)());
  139. template <class U> static big_ tester(...);
  140. static const bool value = sizeof(tester<T>(0)) == sizeof(small_);
  141. };
  142. // is_convertible chokes if the first argument is an array. That's why
  143. // we use add_reference here.
  144. template <bool NotUnum, class T> struct is_enum_impl
  145. : is_convertible<typename add_reference<T>::type, int> { };
  146. template <class T> struct is_enum_impl<true, T> : false_type { };
  147. } // namespace internal
  148. // Specified by TR1 [4.5.1] primary type categories.
  149. // Implementation note:
  150. //
  151. // Each type is either void, integral, floating point, array, pointer,
  152. // reference, member object pointer, member function pointer, enum,
  153. // union or class. Out of these, only integral, floating point, reference,
  154. // class and enum types are potentially convertible to int. Therefore,
  155. // if a type is not a reference, integral, floating point or class and
  156. // is convertible to int, it's a enum. Adding cv-qualification to a type
  157. // does not change whether it's an enum.
  158. //
  159. // Is-convertible-to-int check is done only if all other checks pass,
  160. // because it can't be used with some types (e.g. void or classes with
  161. // inaccessible conversion operators).
  162. template <class T> struct is_enum
  163. : internal::is_enum_impl<
  164. is_same<T, void>::value ||
  165. is_integral<T>::value ||
  166. is_floating_point<T>::value ||
  167. is_reference<T>::value ||
  168. internal::is_class_or_union<T>::value,
  169. T> { };
  170. template <class T> struct is_enum<const T> : is_enum<T> { };
  171. template <class T> struct is_enum<volatile T> : is_enum<T> { };
  172. template <class T> struct is_enum<const volatile T> : is_enum<T> { };
  173. #endif
  174. // is_reference is false except for reference types.
  175. template<typename T> struct is_reference : false_type {};
  176. template<typename T> struct is_reference<T&> : true_type {};
  177. // We can't get is_pod right without compiler help, so fail conservatively.
  178. // We will assume it's false except for arithmetic types, enumerations,
  179. // pointers and cv-qualified versions thereof. Note that std::pair<T,U>
  180. // is not a POD even if T and U are PODs.
  181. template <class T> struct is_pod
  182. : integral_constant<bool, (is_integral<T>::value ||
  183. is_floating_point<T>::value ||
  184. #if !defined(_MSC_VER) && !(defined(__GNUC__) && __GNUC__ <= 3)
  185. // is_enum is not available on MSVC.
  186. is_enum<T>::value ||
  187. #endif
  188. is_pointer<T>::value)> { };
  189. template <class T> struct is_pod<const T> : is_pod<T> { };
  190. template <class T> struct is_pod<volatile T> : is_pod<T> { };
  191. template <class T> struct is_pod<const volatile T> : is_pod<T> { };
  192. // We can't get has_trivial_constructor right without compiler help, so
  193. // fail conservatively. We will assume it's false except for: (1) types
  194. // for which is_pod is true. (2) std::pair of types with trivial
  195. // constructors. (3) array of a type with a trivial constructor.
  196. // (4) const versions thereof.
  197. template <class T> struct has_trivial_constructor : is_pod<T> { };
  198. template <class T, class U> struct has_trivial_constructor<std::pair<T, U> >
  199. : integral_constant<bool,
  200. (has_trivial_constructor<T>::value &&
  201. has_trivial_constructor<U>::value)> { };
  202. template <class A, int N> struct has_trivial_constructor<A[N]>
  203. : has_trivial_constructor<A> { };
  204. template <class T> struct has_trivial_constructor<const T>
  205. : has_trivial_constructor<T> { };
  206. // We can't get has_trivial_copy right without compiler help, so fail
  207. // conservatively. We will assume it's false except for: (1) types
  208. // for which is_pod is true. (2) std::pair of types with trivial copy
  209. // constructors. (3) array of a type with a trivial copy constructor.
  210. // (4) const versions thereof.
  211. template <class T> struct has_trivial_copy : is_pod<T> { };
  212. template <class T, class U> struct has_trivial_copy<std::pair<T, U> >
  213. : integral_constant<bool,
  214. (has_trivial_copy<T>::value &&
  215. has_trivial_copy<U>::value)> { };
  216. template <class A, int N> struct has_trivial_copy<A[N]>
  217. : has_trivial_copy<A> { };
  218. template <class T> struct has_trivial_copy<const T> : has_trivial_copy<T> { };
  219. // We can't get has_trivial_assign right without compiler help, so fail
  220. // conservatively. We will assume it's false except for: (1) types
  221. // for which is_pod is true. (2) std::pair of types with trivial copy
  222. // constructors. (3) array of a type with a trivial assign constructor.
  223. template <class T> struct has_trivial_assign : is_pod<T> { };
  224. template <class T, class U> struct has_trivial_assign<std::pair<T, U> >
  225. : integral_constant<bool,
  226. (has_trivial_assign<T>::value &&
  227. has_trivial_assign<U>::value)> { };
  228. template <class A, int N> struct has_trivial_assign<A[N]>
  229. : has_trivial_assign<A> { };
  230. // We can't get has_trivial_destructor right without compiler help, so
  231. // fail conservatively. We will assume it's false except for: (1) types
  232. // for which is_pod is true. (2) std::pair of types with trivial
  233. // destructors. (3) array of a type with a trivial destructor.
  234. // (4) const versions thereof.
  235. template <class T> struct has_trivial_destructor : is_pod<T> { };
  236. template <class T, class U> struct has_trivial_destructor<std::pair<T, U> >
  237. : integral_constant<bool,
  238. (has_trivial_destructor<T>::value &&
  239. has_trivial_destructor<U>::value)> { };
  240. template <class A, int N> struct has_trivial_destructor<A[N]>
  241. : has_trivial_destructor<A> { };
  242. template <class T> struct has_trivial_destructor<const T>
  243. : has_trivial_destructor<T> { };
  244. // Specified by TR1 [4.7.1]
  245. template<typename T> struct remove_const { typedef T type; };
  246. template<typename T> struct remove_const<T const> { typedef T type; };
  247. template<typename T> struct remove_volatile { typedef T type; };
  248. template<typename T> struct remove_volatile<T volatile> { typedef T type; };
  249. template<typename T> struct remove_cv {
  250. typedef typename remove_const<typename remove_volatile<T>::type>::type type;
  251. };
  252. // Specified by TR1 [4.7.2] Reference modifications.
  253. template<typename T> struct remove_reference { typedef T type; };
  254. template<typename T> struct remove_reference<T&> { typedef T type; };
  255. template <typename T> struct add_reference { typedef T& type; };
  256. template <typename T> struct add_reference<T&> { typedef T& type; };
  257. // Specified by TR1 [4.7.4] Pointer modifications.
  258. template<typename T> struct remove_pointer { typedef T type; };
  259. template<typename T> struct remove_pointer<T*> { typedef T type; };
  260. template<typename T> struct remove_pointer<T* const> { typedef T type; };
  261. template<typename T> struct remove_pointer<T* volatile> { typedef T type; };
  262. template<typename T> struct remove_pointer<T* const volatile> {
  263. typedef T type; };
  264. // Specified by TR1 [4.6] Relationships between types
  265. template<typename T, typename U> struct is_same : public false_type { };
  266. template<typename T> struct is_same<T, T> : public true_type { };
  267. // Specified by TR1 [4.6] Relationships between types
  268. #if !defined(_MSC_VER) && !(defined(__GNUC__) && __GNUC__ <= 3)
  269. namespace internal {
  270. // This class is an implementation detail for is_convertible, and you
  271. // don't need to know how it works to use is_convertible. For those
  272. // who care: we declare two different functions, one whose argument is
  273. // of type To and one with a variadic argument list. We give them
  274. // return types of different size, so we can use sizeof to trick the
  275. // compiler into telling us which function it would have chosen if we
  276. // had called it with an argument of type From. See Alexandrescu's
  277. // _Modern C++ Design_ for more details on this sort of trick.
  278. template <typename From, typename To>
  279. struct ConvertHelper {
  280. static small_ Test(To);
  281. static big_ Test(...);
  282. static From Create();
  283. };
  284. } // namespace internal
  285. // Inherits from true_type if From is convertible to To, false_type otherwise.
  286. template <typename From, typename To>
  287. struct is_convertible
  288. : integral_constant<bool,
  289. sizeof(internal::ConvertHelper<From, To>::Test(
  290. internal::ConvertHelper<From, To>::Create()))
  291. == sizeof(small_)> {
  292. };
  293. #endif
  294. } // namespace internal
  295. } // namespace protobuf
  296. } // namespace google
  297. #endif // GOOGLE_PROTOBUF_TYPE_TRAITS_H_