typeinfo 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // -*- C++ -*-
  2. //===-------------------------- typeinfo ----------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef __LIBCPP_TYPEINFO
  11. #define __LIBCPP_TYPEINFO
  12. /*
  13. typeinfo synopsis
  14. namespace std {
  15. class type_info
  16. {
  17. public:
  18. virtual ~type_info();
  19. bool operator==(const type_info& rhs) const noexcept;
  20. bool operator!=(const type_info& rhs) const noexcept;
  21. bool before(const type_info& rhs) const noexcept;
  22. size_t hash_code() const noexcept;
  23. const char* name() const noexcept;
  24. type_info(const type_info& rhs) = delete;
  25. type_info& operator=(const type_info& rhs) = delete;
  26. };
  27. class bad_cast
  28. : public exception
  29. {
  30. public:
  31. bad_cast() noexcept;
  32. bad_cast(const bad_cast&) noexcept;
  33. bad_cast& operator=(const bad_cast&) noexcept;
  34. virtual const char* what() const noexcept;
  35. };
  36. class bad_typeid
  37. : public exception
  38. {
  39. public:
  40. bad_typeid() noexcept;
  41. bad_typeid(const bad_typeid&) noexcept;
  42. bad_typeid& operator=(const bad_typeid&) noexcept;
  43. virtual const char* what() const noexcept;
  44. };
  45. } // std
  46. */
  47. #include <__config>
  48. #if defined(_LIBCPP_SGX_CONFIG)
  49. #include <../stdc++/typeinfo>
  50. #else // !defined(_LIBCPP_SGX_CONFIG)
  51. #include <exception>
  52. #include <cstddef>
  53. #include <cstdint>
  54. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  55. #pragma GCC system_header
  56. #endif
  57. namespace std // purposefully not using versioning namespace
  58. {
  59. class _LIBCPP_EXCEPTION_ABI type_info
  60. {
  61. type_info& operator=(const type_info&);
  62. type_info(const type_info&);
  63. protected:
  64. #ifndef _LIBCPP_NONUNIQUE_RTTI_BIT
  65. const char* __type_name;
  66. #else
  67. // A const char* with the non-unique RTTI bit possibly set.
  68. uintptr_t __type_name;
  69. #endif
  70. _LIBCPP_INLINE_VISIBILITY
  71. explicit type_info(const char* __n)
  72. #ifndef _LIBCPP_NONUNIQUE_RTTI_BIT
  73. : __type_name(__n) {}
  74. #else
  75. : __type_name(reinterpret_cast<uintptr_t>(__n)) {}
  76. #endif
  77. public:
  78. virtual ~type_info();
  79. _LIBCPP_INLINE_VISIBILITY
  80. const char* name() const _NOEXCEPT
  81. #ifndef _LIBCPP_NONUNIQUE_RTTI_BIT
  82. {return __type_name;}
  83. #else
  84. {return reinterpret_cast<const char*>(__type_name & ~_LIBCPP_NONUNIQUE_RTTI_BIT);}
  85. #endif
  86. _LIBCPP_INLINE_VISIBILITY
  87. bool before(const type_info& __arg) const _NOEXCEPT
  88. #ifndef _LIBCPP_NONUNIQUE_RTTI_BIT
  89. {return __type_name < __arg.__type_name;}
  90. #else
  91. {if (!((__type_name & __arg.__type_name) & _LIBCPP_NONUNIQUE_RTTI_BIT))
  92. return __type_name < __arg.__type_name;
  93. return __compare_nonunique_names(__arg) < 0;}
  94. #endif
  95. _LIBCPP_INLINE_VISIBILITY
  96. size_t hash_code() const _NOEXCEPT
  97. #ifndef _LIBCPP_NONUNIQUE_RTTI_BIT
  98. {return *reinterpret_cast<const size_t*>(&__type_name);}
  99. #else
  100. {if (!(__type_name & _LIBCPP_NONUNIQUE_RTTI_BIT)) return __type_name;
  101. const char *__ptr = name();
  102. size_t __hash = 5381;
  103. while (unsigned char __c = static_cast<unsigned char>(*__ptr++))
  104. __hash = (__hash * 33) ^ __c;
  105. return __hash;}
  106. #endif
  107. _LIBCPP_INLINE_VISIBILITY
  108. bool operator==(const type_info& __arg) const _NOEXCEPT
  109. #ifndef _LIBCPP_NONUNIQUE_RTTI_BIT
  110. {return __type_name == __arg.__type_name;}
  111. #else
  112. {if (__type_name == __arg.__type_name) return true;
  113. if (!((__type_name & __arg.__type_name) & _LIBCPP_NONUNIQUE_RTTI_BIT))
  114. return false;
  115. return __compare_nonunique_names(__arg) == 0;}
  116. #endif
  117. _LIBCPP_INLINE_VISIBILITY
  118. bool operator!=(const type_info& __arg) const _NOEXCEPT
  119. {return !operator==(__arg);}
  120. #ifdef _LIBCPP_NONUNIQUE_RTTI_BIT
  121. private:
  122. _LIBCPP_INLINE_VISIBILITY
  123. int __compare_nonunique_names(const type_info &__arg) const _NOEXCEPT
  124. {return __builtin_strcmp(name(), __arg.name());}
  125. #endif
  126. };
  127. class _LIBCPP_EXCEPTION_ABI bad_cast
  128. : public exception
  129. {
  130. public:
  131. bad_cast() _NOEXCEPT;
  132. virtual ~bad_cast() _NOEXCEPT;
  133. virtual const char* what() const _NOEXCEPT;
  134. };
  135. class _LIBCPP_EXCEPTION_ABI bad_typeid
  136. : public exception
  137. {
  138. public:
  139. bad_typeid() _NOEXCEPT;
  140. virtual ~bad_typeid() _NOEXCEPT;
  141. virtual const char* what() const _NOEXCEPT;
  142. };
  143. } // std
  144. #endif // !defined(_LIBCPP_SGX_CONFIG)
  145. #endif // __LIBCPP_TYPEINFO