You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. // Borrowed from Chromium's src/base/template_util.h.
  11. #ifndef RTC_BASE_TEMPLATE_UTIL_H_
  12. #define RTC_BASE_TEMPLATE_UTIL_H_
  13. #include <stddef.h> // For size_t.
  14. namespace rtc {
  15. // Template definitions from tr1.
  16. template <class T, T v>
  17. struct integral_constant {
  18. static const T value = v;
  19. typedef T value_type;
  20. typedef integral_constant<T, v> type;
  21. };
  22. template <class T, T v>
  23. const T integral_constant<T, v>::value;
  24. typedef integral_constant<bool, true> true_type;
  25. typedef integral_constant<bool, false> false_type;
  26. template <class T>
  27. struct is_pointer : false_type {};
  28. template <class T>
  29. struct is_pointer<T*> : true_type {};
  30. template <class T, class U>
  31. struct is_same : public false_type {};
  32. template <class T>
  33. struct is_same<T, T> : true_type {};
  34. template <class>
  35. struct is_array : public false_type {};
  36. template <class T, size_t n>
  37. struct is_array<T[n]> : public true_type {};
  38. template <class T>
  39. struct is_array<T[]> : public true_type {};
  40. template <class T>
  41. struct is_non_const_reference : false_type {};
  42. template <class T>
  43. struct is_non_const_reference<T&> : true_type {};
  44. template <class T>
  45. struct is_non_const_reference<const T&> : false_type {};
  46. template <class T>
  47. struct is_void : false_type {};
  48. template <>
  49. struct is_void<void> : true_type {};
  50. // Helper useful for converting a tuple to variadic template function
  51. // arguments.
  52. //
  53. // sequence_generator<3>::type will be sequence<0, 1, 2>.
  54. template <int...>
  55. struct sequence {};
  56. template <int N, int... S>
  57. struct sequence_generator : sequence_generator<N - 1, N - 1, S...> {};
  58. template <int... S>
  59. struct sequence_generator<0, S...> {
  60. typedef sequence<S...> type;
  61. };
  62. namespace internal {
  63. // Types YesType and NoType are guaranteed such that sizeof(YesType) <
  64. // sizeof(NoType).
  65. typedef char YesType;
  66. struct NoType {
  67. YesType dummy[2];
  68. };
  69. // This class is an implementation detail for is_convertible, and you
  70. // don't need to know how it works to use is_convertible. For those
  71. // who care: we declare two different functions, one whose argument is
  72. // of type To and one with a variadic argument list. We give them
  73. // return types of different size, so we can use sizeof to trick the
  74. // compiler into telling us which function it would have chosen if we
  75. // had called it with an argument of type From. See Alexandrescu's
  76. // _Modern C++ Design_ for more details on this sort of trick.
  77. struct ConvertHelper {
  78. template <typename To>
  79. static YesType Test(To);
  80. template <typename To>
  81. static NoType Test(...);
  82. template <typename From>
  83. static From& Create();
  84. };
  85. // Used to determine if a type is a struct/union/class. Inspired by Boost's
  86. // is_class type_trait implementation.
  87. struct IsClassHelper {
  88. template <typename C>
  89. static YesType Test(void (C::*)(void));
  90. template <typename C>
  91. static NoType Test(...);
  92. };
  93. } // namespace internal
  94. // Inherits from true_type if From is convertible to To, false_type otherwise.
  95. //
  96. // Note that if the type is convertible, this will be a true_type REGARDLESS
  97. // of whether or not the conversion would emit a warning.
  98. template <typename From, typename To>
  99. struct is_convertible
  100. : integral_constant<bool,
  101. sizeof(internal::ConvertHelper::Test<To>(
  102. internal::ConvertHelper::Create<From>())) ==
  103. sizeof(internal::YesType)> {};
  104. template <typename T>
  105. struct is_class
  106. : integral_constant<bool,
  107. sizeof(internal::IsClassHelper::Test<T>(0)) ==
  108. sizeof(internal::YesType)> {};
  109. } // namespace rtc
  110. #endif // RTC_BASE_TEMPLATE_UTIL_H_