nameof.hpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. // _ _ __ _____
  2. // | \ | | / _| / ____|_ _
  3. // | \| | __ _ _ __ ___ ___ ___ | |_ | | _| |_ _| |_
  4. // | . ` |/ _` | '_ ` _ \ / _ \/ _ \| _| | | |_ _|_ _|
  5. // | |\ | (_| | | | | | | __/ (_) | | | |____|_| |_|
  6. // |_| \_|\__,_|_| |_| |_|\___|\___/|_| \_____|
  7. // https://github.com/Neargye/nameof
  8. // version 0.10.2
  9. //
  10. // Licensed under the MIT License <http://opensource.org/licenses/MIT>.
  11. // SPDX-License-Identifier: MIT
  12. // Copyright (c) 2016 - 2022 Daniil Goncharov <neargye@gmail.com>.
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining a copy
  15. // of this software and associated documentation files (the "Software"), to deal
  16. // in the Software without restriction, including without limitation the rights
  17. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. // copies of the Software, and to permit persons to whom the Software is
  19. // furnished to do so, subject to the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be included in all
  22. // copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. // SOFTWARE.
  31. #ifndef NEARGYE_NAMEOF_HPP
  32. #define NEARGYE_NAMEOF_HPP
  33. #define NAMEOF_VERSION_MAJOR 0
  34. #define NAMEOF_VERSION_MINOR 10
  35. #define NAMEOF_VERSION_PATCH 2
  36. #include <array>
  37. #include <cassert>
  38. #include <cstdint>
  39. #include <cstddef>
  40. #include <iosfwd>
  41. #include <iterator>
  42. #include <limits>
  43. #include <type_traits>
  44. #include <utility>
  45. #if !defined(NAMEOF_USING_ALIAS_STRING)
  46. #include <string>
  47. #endif
  48. #if !defined(NAMEOF_USING_ALIAS_STRING_VIEW)
  49. #include <string_view>
  50. #endif
  51. #if __has_include(<cxxabi.h>)
  52. #include <cxxabi.h>
  53. #include <cstdlib>
  54. #endif
  55. #if defined(__clang__)
  56. # pragma clang diagnostic push
  57. #elif defined(__GNUC__)
  58. # pragma GCC diagnostic push
  59. # pragma GCC diagnostic ignored "-Wstringop-overflow" // Missing terminating nul 'enum_name_v'.
  60. #elif defined(_MSC_VER)
  61. # pragma warning(push)
  62. # pragma warning(disable : 26495) // Variable 'cstring<N>::chars_' is uninitialized.
  63. # pragma warning(disable : 28020) // Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value.
  64. # pragma warning(disable : 26451) // The expression '0<=_Param_(1)&&_Param_(1)<=1-1' is not true at this call.
  65. # pragma warning(disable : 4514) // Unreferenced inline function has been removed.
  66. #endif
  67. // Checks nameof_type compiler compatibility.
  68. #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 7 || defined(_MSC_VER) && _MSC_VER >= 1910
  69. # undef NAMEOF_TYPE_SUPPORTED
  70. # define NAMEOF_TYPE_SUPPORTED 1
  71. #endif
  72. // Checks nameof_type_rtti compiler compatibility.
  73. #if defined(__clang__)
  74. # if __has_feature(cxx_rtti)
  75. # undef NAMEOF_TYPE_RTTI_SUPPORTED
  76. # define NAMEOF_TYPE_RTTI_SUPPORTED 1
  77. # endif
  78. #elif defined(__GNUC__)
  79. # if defined(__GXX_RTTI)
  80. # undef NAMEOF_TYPE_RTTI_SUPPORTED
  81. # define NAMEOF_TYPE_RTTI_SUPPORTED 1
  82. # endif
  83. #elif defined(_MSC_VER)
  84. # if defined(_CPPRTTI)
  85. # undef NAMEOF_TYPE_RTTI_SUPPORTED
  86. # define NAMEOF_TYPE_RTTI_SUPPORTED 1
  87. # endif
  88. #endif
  89. // Checks nameof_member compiler compatibility.
  90. #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 7 || defined(_MSC_VER) && defined(_MSVC_LANG) && _MSVC_LANG >= 202002L
  91. # undef NAMEOF_MEMBER_SUPPORTED
  92. # define NAMEOF_MEMBER_SUPPORTED 1
  93. #endif
  94. // Checks nameof_enum compiler compatibility.
  95. #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1910
  96. # undef NAMEOF_ENUM_SUPPORTED
  97. # define NAMEOF_ENUM_SUPPORTED 1
  98. #endif
  99. // Checks nameof_enum compiler aliases compatibility.
  100. #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1920
  101. # undef NAMEOF_ENUM_SUPPORTED_ALIASES
  102. # define NAMEOF_ENUM_SUPPORTED_ALIASES 1
  103. #endif
  104. // Enum value must be greater or equals than NAMEOF_ENUM_RANGE_MIN. By default NAMEOF_ENUM_RANGE_MIN = -128.
  105. // If need another min range for all enum types by default, redefine the macro NAMEOF_ENUM_RANGE_MIN.
  106. #if !defined(NAMEOF_ENUM_RANGE_MIN)
  107. # define NAMEOF_ENUM_RANGE_MIN -128
  108. #endif
  109. // Enum value must be less or equals than NAMEOF_ENUM_RANGE_MAX. By default NAMEOF_ENUM_RANGE_MAX = 128.
  110. // If need another max range for all enum types by default, redefine the macro NAMEOF_ENUM_RANGE_MAX.
  111. #if !defined(NAMEOF_ENUM_RANGE_MAX)
  112. # define NAMEOF_ENUM_RANGE_MAX 128
  113. #endif
  114. namespace nameof {
  115. // If need another string_view type, define the macro NAMEOF_USING_ALIAS_STRING_VIEW.
  116. #if defined(NAMEOF_USING_ALIAS_STRING_VIEW)
  117. NAMEOF_USING_ALIAS_STRING_VIEW
  118. #else
  119. using std::string_view;
  120. #endif
  121. // If need another string type, define the macro NAMEOF_USING_ALIAS_STRING.
  122. #if defined(NAMEOF_USING_ALIAS_STRING)
  123. NAMEOF_USING_ALIAS_STRING
  124. #else
  125. using std::string;
  126. #endif
  127. namespace customize {
  128. // Enum value must be in range [NAMEOF_ENUM_RANGE_MIN, NAMEOF_ENUM_RANGE_MAX]. By default NAMEOF_ENUM_RANGE_MIN = -128, NAMEOF_ENUM_RANGE_MAX = 128.
  129. // If need another range for all enum types by default, redefine the macro NAMEOF_ENUM_RANGE_MIN and NAMEOF_ENUM_RANGE_MAX.
  130. // If need another range for specific enum type, add specialization enum_range for necessary enum type.
  131. template <typename E>
  132. struct enum_range {
  133. static_assert(std::is_enum_v<E>, "nameof::customize::enum_range requires enum type.");
  134. inline static constexpr int min = NAMEOF_ENUM_RANGE_MIN;
  135. inline static constexpr int max = NAMEOF_ENUM_RANGE_MAX;
  136. static_assert(max > min, "nameof::customize::enum_range requires max > min.");
  137. };
  138. static_assert(NAMEOF_ENUM_RANGE_MIN <= 0, "NAMEOF_ENUM_RANGE_MIN must be less or equals than 0.");
  139. static_assert(NAMEOF_ENUM_RANGE_MIN > (std::numeric_limits<std::int16_t>::min)(), "NAMEOF_ENUM_RANGE_MIN must be greater than INT16_MIN.");
  140. static_assert(NAMEOF_ENUM_RANGE_MAX > 0, "NAMEOF_ENUM_RANGE_MAX must be greater than 0.");
  141. static_assert(NAMEOF_ENUM_RANGE_MAX < (std::numeric_limits<std::int16_t>::max)(), "NAMEOF_ENUM_RANGE_MAX must be less than INT16_MAX.");
  142. static_assert(NAMEOF_ENUM_RANGE_MAX > NAMEOF_ENUM_RANGE_MIN, "NAMEOF_ENUM_RANGE_MAX must be greater than NAMEOF_ENUM_RANGE_MIN.");
  143. // If need custom names for enum, add specialization enum_name for necessary enum type.
  144. template <typename E>
  145. constexpr string_view enum_name(E) noexcept {
  146. static_assert(std::is_enum_v<E>, "nameof::customize::enum_name requires enum type.");
  147. return {};
  148. }
  149. // If need custom name for type, add specialization type_name for necessary type.
  150. template <typename T>
  151. constexpr string_view type_name() noexcept {
  152. return {};
  153. }
  154. // If need custom name for member, add specialization member_name for necessary type.
  155. template <auto V>
  156. constexpr string_view member_name() noexcept {
  157. return {};
  158. }
  159. } // namespace nameof::customize
  160. template <std::size_t N>
  161. class [[nodiscard]] cstring {
  162. public:
  163. using value_type = const char;
  164. using size_type = std::size_t;
  165. using difference_type = std::ptrdiff_t;
  166. using pointer = const char*;
  167. using const_pointer = const char*;
  168. using reference = const char&;
  169. using const_reference = const char&;
  170. using iterator = const char*;
  171. using const_iterator = const char*;
  172. using reverse_iterator = std::reverse_iterator<iterator>;
  173. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  174. constexpr explicit cstring(string_view str) noexcept : cstring{str, std::make_index_sequence<N>{}} {
  175. assert(str.size() > 0 && str.size() == N);
  176. }
  177. constexpr cstring() = delete;
  178. constexpr cstring(const cstring&) = default;
  179. constexpr cstring(cstring&&) = default;
  180. ~cstring() = default;
  181. cstring& operator=(const cstring&) = default;
  182. cstring& operator=(cstring&&) = default;
  183. [[nodiscard]] constexpr const_pointer data() const noexcept { return chars_; }
  184. [[nodiscard]] constexpr size_type size() const noexcept { return N; }
  185. [[nodiscard]] constexpr const_iterator begin() const noexcept { return data(); }
  186. [[nodiscard]] constexpr const_iterator end() const noexcept { return data() + size(); }
  187. [[nodiscard]] constexpr const_iterator cbegin() const noexcept { return begin(); }
  188. [[nodiscard]] constexpr const_iterator cend() const noexcept { return end(); }
  189. [[nodiscard]] constexpr const_reverse_iterator rbegin() const noexcept { return end(); }
  190. [[nodiscard]] constexpr const_reverse_iterator rend() const noexcept { return begin(); }
  191. [[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept { return rbegin(); }
  192. [[nodiscard]] constexpr const_reverse_iterator crend() const noexcept { return rend(); }
  193. [[nodiscard]] constexpr const_reference operator[](size_type i) const noexcept { return assert(i < size()), chars_[i]; }
  194. [[nodiscard]] constexpr const_reference front() const noexcept { return chars_[0]; }
  195. [[nodiscard]] constexpr const_reference back() const noexcept { return chars_[N]; }
  196. [[nodiscard]] constexpr size_type length() const noexcept { return size(); }
  197. [[nodiscard]] constexpr bool empty() const noexcept { return false; }
  198. [[nodiscard]] constexpr int compare(string_view str) const noexcept { return string_view{data(), size()}.compare(str); }
  199. [[nodiscard]] constexpr const char* c_str() const noexcept { return data(); }
  200. [[nodiscard]] string str() const { return {begin(), end()}; }
  201. [[nodiscard]] constexpr operator string_view() const noexcept { return {data(), size()}; }
  202. [[nodiscard]] constexpr explicit operator const_pointer() const noexcept { return data(); }
  203. [[nodiscard]] explicit operator string() const { return {begin(), end()}; }
  204. private:
  205. template <std::size_t... I>
  206. constexpr cstring(string_view str, std::index_sequence<I...>) noexcept : chars_{str[I]..., '\0'} {}
  207. char chars_[N + 1];
  208. };
  209. template <>
  210. class [[nodiscard]] cstring<0> {
  211. public:
  212. using value_type = const char;
  213. using size_type = std::size_t;
  214. using difference_type = std::ptrdiff_t;
  215. using pointer = const char*;
  216. using const_pointer = const char*;
  217. using reference = const char&;
  218. using const_reference = const char&;
  219. using iterator = const char*;
  220. using const_iterator = const char*;
  221. using reverse_iterator = std::reverse_iterator<iterator>;
  222. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  223. constexpr explicit cstring(string_view) noexcept {}
  224. constexpr cstring() = delete;
  225. constexpr cstring(const cstring&) = default;
  226. constexpr cstring(cstring&&) = default;
  227. ~cstring() = default;
  228. cstring& operator=(const cstring&) = default;
  229. cstring& operator=(cstring&&) = default;
  230. [[nodiscard]] constexpr const_pointer data() const noexcept { return nullptr; }
  231. [[nodiscard]] constexpr size_type size() const noexcept { return 0; }
  232. [[nodiscard]] constexpr const_iterator begin() const noexcept { return nullptr; }
  233. [[nodiscard]] constexpr const_iterator end() const noexcept { return nullptr; }
  234. [[nodiscard]] constexpr const_iterator cbegin() const noexcept { return nullptr; }
  235. [[nodiscard]] constexpr const_iterator cend() const noexcept { return nullptr; }
  236. [[nodiscard]] constexpr const_reverse_iterator rbegin() const noexcept { return {}; }
  237. [[nodiscard]] constexpr const_reverse_iterator rend() const noexcept { return {}; }
  238. [[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept { return {}; }
  239. [[nodiscard]] constexpr const_reverse_iterator crend() const noexcept { return {}; }
  240. [[nodiscard]] constexpr size_type length() const noexcept { return 0; }
  241. [[nodiscard]] constexpr bool empty() const noexcept { return true; }
  242. [[nodiscard]] constexpr int compare(string_view str) const noexcept { return string_view{}.compare(str); }
  243. [[nodiscard]] constexpr const char* c_str() const noexcept { return nullptr; }
  244. [[nodiscard]] string str() const { return {}; }
  245. [[nodiscard]] constexpr operator string_view() const noexcept { return {}; }
  246. [[nodiscard]] constexpr explicit operator const_pointer() const noexcept { return nullptr; }
  247. [[nodiscard]] explicit operator string() const { return {}; }
  248. };
  249. template <std::size_t N>
  250. [[nodiscard]] constexpr bool operator==(const cstring<N>& lhs, string_view rhs) noexcept {
  251. return lhs.compare(rhs) == 0;
  252. }
  253. template <std::size_t N>
  254. [[nodiscard]] constexpr bool operator==(string_view lhs, const cstring<N>& rhs) noexcept {
  255. return lhs.compare(rhs) == 0;
  256. }
  257. template <std::size_t N>
  258. [[nodiscard]] constexpr bool operator!=(const cstring<N>& lhs, string_view rhs) noexcept {
  259. return lhs.compare(rhs) != 0;
  260. }
  261. template <std::size_t N>
  262. [[nodiscard]] constexpr bool operator!=(string_view lhs, const cstring<N>& rhs) noexcept {
  263. return lhs.compare(rhs) != 0;
  264. }
  265. template <std::size_t N>
  266. [[nodiscard]] constexpr bool operator>(const cstring<N>& lhs, string_view rhs) noexcept {
  267. return lhs.compare(rhs) > 0;
  268. }
  269. template <std::size_t N>
  270. [[nodiscard]] constexpr bool operator>(string_view lhs, const cstring<N>& rhs) noexcept {
  271. return lhs.compare(rhs) > 0;
  272. }
  273. template <std::size_t N>
  274. [[nodiscard]] constexpr bool operator>=(const cstring<N>& lhs, string_view rhs) noexcept {
  275. return lhs.compare(rhs) >= 0;
  276. }
  277. template <std::size_t N>
  278. [[nodiscard]] constexpr bool operator>=(string_view lhs, const cstring<N>& rhs) noexcept {
  279. return lhs.compare(rhs) >= 0;
  280. }
  281. template <std::size_t N>
  282. [[nodiscard]] constexpr bool operator<(const cstring<N>& lhs, string_view rhs) noexcept {
  283. return lhs.compare(rhs) < 0;
  284. }
  285. template <std::size_t N>
  286. [[nodiscard]] constexpr bool operator<(string_view lhs, const cstring<N>& rhs) noexcept {
  287. return lhs.compare(rhs) < 0;
  288. }
  289. template <std::size_t N>
  290. [[nodiscard]] constexpr bool operator<=(const cstring<N>& lhs, string_view rhs) noexcept {
  291. return lhs.compare(rhs) <= 0;
  292. }
  293. template <std::size_t N>
  294. [[nodiscard]] constexpr bool operator<=(string_view lhs, const cstring<N>& rhs) noexcept {
  295. return lhs.compare(rhs) <= 0;
  296. }
  297. template <typename Char, typename Traits, std::size_t N>
  298. std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, const cstring<N>& srt) {
  299. for (const auto c : srt) {
  300. os.put(c);
  301. }
  302. return os;
  303. }
  304. namespace detail {
  305. constexpr string_view pretty_name(string_view name, bool remove_suffix = true) noexcept {
  306. if (name.size() >= 1 && (name[0] == '"' || name[0] == '\'')) {
  307. return {}; // Narrow multibyte string literal.
  308. } else if (name.size() >= 2 && name[0] == 'R' && (name[1] == '"' || name[1] == '\'')) {
  309. return {}; // Raw string literal.
  310. } else if (name.size() >= 2 && name[0] == 'L' && (name[1] == '"' || name[1] == '\'')) {
  311. return {}; // Wide string literal.
  312. } else if (name.size() >= 2 && name[0] == 'U' && (name[1] == '"' || name[1] == '\'')) {
  313. return {}; // UTF-32 encoded string literal.
  314. } else if (name.size() >= 2 && name[0] == 'u' && (name[1] == '"' || name[1] == '\'')) {
  315. return {}; // UTF-16 encoded string literal.
  316. } else if (name.size() >= 3 && name[0] == 'u' && name[1] == '8' && (name[2] == '"' || name[2] == '\'')) {
  317. return {}; // UTF-8 encoded string literal.
  318. } else if (name.size() >= 1 && (name[0] >= '0' && name[0] <= '9')) {
  319. return {}; // Invalid name.
  320. }
  321. for (std::size_t i = name.size(), h = 0, s = 0; i > 0; --i) {
  322. if (name[i - 1] == ')') {
  323. ++h;
  324. ++s;
  325. continue;
  326. } else if (name[i - 1] == '(') {
  327. --h;
  328. ++s;
  329. continue;
  330. }
  331. if (h == 0) {
  332. name.remove_suffix(s);
  333. break;
  334. } else {
  335. ++s;
  336. continue;
  337. }
  338. }
  339. std::size_t s = 0;
  340. for (std::size_t i = name.size(), h = 0; i > 0; --i) {
  341. if (name[i - 1] == '>') {
  342. ++h;
  343. ++s;
  344. continue;
  345. } else if (name[i - 1] == '<') {
  346. --h;
  347. ++s;
  348. continue;
  349. }
  350. if (h == 0) {
  351. break;
  352. } else {
  353. ++s;
  354. continue;
  355. }
  356. }
  357. for (std::size_t i = name.size() - s; i > 0; --i) {
  358. if (!((name[i - 1] >= '0' && name[i - 1] <= '9') ||
  359. (name[i - 1] >= 'a' && name[i - 1] <= 'z') ||
  360. (name[i - 1] >= 'A' && name[i - 1] <= 'Z') ||
  361. (name[i - 1] == '_'))) {
  362. name.remove_prefix(i);
  363. break;
  364. }
  365. }
  366. if (remove_suffix) {
  367. name.remove_suffix(s);
  368. }
  369. if (name.size() > 0 && ((name.front() >= 'a' && name.front() <= 'z') ||
  370. (name.front() >= 'A' && name.front() <= 'Z') ||
  371. (name.front() == '_'))) {
  372. return name;
  373. }
  374. return {}; // Invalid name.
  375. }
  376. template <typename T, std::size_t N, std::size_t... I>
  377. constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N], std::index_sequence<I...>) {
  378. return {{a[I]...}};
  379. }
  380. template <typename L, typename R>
  381. constexpr bool cmp_less(L lhs, R rhs) noexcept {
  382. static_assert(std::is_integral_v<L> && std::is_integral_v<R>, "nameof::detail::cmp_less requires integral type.");
  383. if constexpr (std::is_signed_v<L> == std::is_signed_v<R>) {
  384. // If same signedness (both signed or both unsigned).
  385. return lhs < rhs;
  386. } else if constexpr (std::is_same_v<L, bool>) { // bool special case
  387. return static_cast<R>(lhs) < rhs;
  388. } else if constexpr (std::is_same_v<R, bool>) { // bool special case
  389. return lhs < static_cast<L>(rhs);
  390. } else if constexpr (std::is_signed_v<R>) {
  391. // If 'right' is negative, then result is 'false', otherwise cast & compare.
  392. return rhs > 0 && lhs < static_cast<std::make_unsigned_t<R>>(rhs);
  393. } else {
  394. // If 'left' is negative, then result is 'true', otherwise cast & compare.
  395. return lhs < 0 || static_cast<std::make_unsigned_t<L>>(lhs) < rhs;
  396. }
  397. }
  398. template <typename I>
  399. constexpr I log2(I value) noexcept {
  400. static_assert(std::is_integral_v<I>, "nameof::detail::log2 requires integral type.");
  401. if constexpr (std::is_same_v<I, bool>) { // bool special case
  402. return assert(false), value;
  403. } else {
  404. auto ret = I{0};
  405. for (; value > I{1}; value >>= I{1}, ++ret) {}
  406. return ret;
  407. }
  408. }
  409. template <typename T>
  410. struct nameof_enum_supported
  411. #if defined(NAMEOF_ENUM_SUPPORTED) && NAMEOF_ENUM_SUPPORTED || defined(NAMEOF_ENUM_NO_CHECK_SUPPORT)
  412. : std::true_type {};
  413. #else
  414. : std::false_type {};
  415. #endif
  416. template <typename T, typename R>
  417. using enable_if_enum_t = std::enable_if_t<std::is_enum_v<std::decay_t<T>>, R>;
  418. template <typename T>
  419. inline constexpr bool is_enum_v = std::is_enum_v<T> && std::is_same_v<T, std::decay_t<T>>;
  420. template <typename E, E V>
  421. constexpr auto n() noexcept {
  422. static_assert(is_enum_v<E>, "nameof::detail::n requires enum type.");
  423. [[maybe_unused]] constexpr auto custom_name = customize::enum_name<E>(V);
  424. if constexpr (custom_name.empty() && nameof_enum_supported<E>::value) {
  425. #if defined(__clang__) || defined(__GNUC__)
  426. constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2});
  427. #elif defined(_MSC_VER)
  428. constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17});
  429. #else
  430. constexpr auto name = string_view{};
  431. #endif
  432. return cstring<name.size()>{name};
  433. } else {
  434. return cstring<custom_name.size()>{custom_name};
  435. }
  436. }
  437. template <typename E, E V>
  438. inline constexpr auto enum_name_v = n<E, V>();
  439. template <typename E, auto V>
  440. constexpr bool is_valid() noexcept {
  441. static_assert(is_enum_v<E>, "nameof::detail::is_valid requires enum type.");
  442. return n<E, static_cast<E>(V)>().size() != 0;
  443. }
  444. template <typename E, int O, bool IsFlags = false, typename U = std::underlying_type_t<E>>
  445. constexpr E value(std::size_t i) noexcept {
  446. static_assert(is_enum_v<E>, "nameof::detail::value requires enum type.");
  447. if constexpr (std::is_same_v<U, bool>) { // bool special case
  448. static_assert(O == 0, "nameof::detail::value requires valid offset.");
  449. return static_cast<E>(i);
  450. } else if constexpr (IsFlags) {
  451. return static_cast<E>(U{1} << static_cast<U>(static_cast<int>(i) + O));
  452. } else {
  453. return static_cast<E>(static_cast<int>(i) + O);
  454. }
  455. }
  456. template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
  457. constexpr int reflected_min() noexcept {
  458. static_assert(is_enum_v<E>, "nameof::detail::reflected_min requires enum type.");
  459. if constexpr (IsFlags) {
  460. return 0;
  461. } else {
  462. constexpr auto lhs = customize::enum_range<E>::min;
  463. constexpr auto rhs = (std::numeric_limits<U>::min)();
  464. if constexpr (cmp_less(rhs, lhs)) {
  465. return lhs;
  466. } else {
  467. return rhs;
  468. }
  469. }
  470. }
  471. template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
  472. constexpr int reflected_max() noexcept {
  473. static_assert(is_enum_v<E>, "nameof::detail::reflected_max requires enum type.");
  474. if constexpr (IsFlags) {
  475. return std::numeric_limits<U>::digits - 1;
  476. } else {
  477. constexpr auto lhs = customize::enum_range<E>::max;
  478. constexpr auto rhs = (std::numeric_limits<U>::max)();
  479. if constexpr (cmp_less(lhs, rhs)) {
  480. return lhs;
  481. } else {
  482. return rhs;
  483. }
  484. }
  485. }
  486. template <typename E, bool IsFlags = false>
  487. inline constexpr auto reflected_min_v = reflected_min<E, IsFlags>();
  488. template <typename E, bool IsFlags = false>
  489. inline constexpr auto reflected_max_v = reflected_max<E, IsFlags>();
  490. template <std::size_t N>
  491. constexpr std::size_t values_count(const bool (&valid)[N]) noexcept {
  492. auto count = std::size_t{0};
  493. for (std::size_t i = 0; i < N; ++i) {
  494. if (valid[i]) {
  495. ++count;
  496. }
  497. }
  498. return count;
  499. }
  500. template <typename E, bool IsFlags, int Min, std::size_t... I>
  501. constexpr auto values(std::index_sequence<I...>) noexcept {
  502. static_assert(is_enum_v<E>, "nameof::detail::values requires enum type.");
  503. constexpr bool valid[sizeof...(I)] = {is_valid<E, value<E, Min, IsFlags>(I)>()...};
  504. constexpr std::size_t count = values_count(valid);
  505. if constexpr (count > 0) {
  506. E values[count] = {};
  507. for (std::size_t i = 0, v = 0; v < count; ++i) {
  508. if (valid[i]) {
  509. values[v++] = value<E, Min, IsFlags>(i);
  510. }
  511. }
  512. return to_array(values, std::make_index_sequence<count>{});
  513. } else {
  514. return std::array<E, 0>{};
  515. }
  516. }
  517. template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
  518. constexpr auto values() noexcept {
  519. static_assert(is_enum_v<E>, "nameof::detail::values requires enum type.");
  520. constexpr auto min = reflected_min_v<E, IsFlags>;
  521. constexpr auto max = reflected_max_v<E, IsFlags>;
  522. constexpr auto range_size = max - min + 1;
  523. static_assert(range_size > 0, "nameof::enum_range requires valid size.");
  524. static_assert(range_size < (std::numeric_limits<std::uint16_t>::max)(), "nameof::enum_range requires valid size.");
  525. return values<E, IsFlags, reflected_min_v<E, IsFlags>>(std::make_index_sequence<range_size>{});
  526. }
  527. template <typename E, bool IsFlags = false>
  528. inline constexpr auto values_v = values<E, IsFlags>();
  529. template <typename E, bool IsFlags = false, typename D = std::decay_t<E>>
  530. using values_t = decltype((values_v<D, IsFlags>));
  531. template <typename E, bool IsFlags = false>
  532. inline constexpr auto count_v = values_v<E, IsFlags>.size();
  533. template <typename E, bool IsFlags = false, typename U = std::underlying_type_t<E>>
  534. inline constexpr auto min_v = (count_v<E, IsFlags> > 0) ? static_cast<U>(values_v<E, IsFlags>.front()) : U{0};
  535. template <typename E, bool IsFlags = false, typename U = std::underlying_type_t<E>>
  536. inline constexpr auto max_v = (count_v<E, IsFlags> > 0) ? static_cast<U>(values_v<E, IsFlags>.back()) : U{0};
  537. template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
  538. constexpr std::size_t range_size() noexcept {
  539. static_assert(is_enum_v<E>, "nameof::detail::range_size requires enum type.");
  540. constexpr auto max = IsFlags ? log2(max_v<E, IsFlags>) : max_v<E, IsFlags>;
  541. constexpr auto min = IsFlags ? log2(min_v<E, IsFlags>) : min_v<E, IsFlags>;
  542. constexpr auto range_size = max - min + U{1};
  543. static_assert(range_size > 0, "nameof::enum_range requires valid size.");
  544. static_assert(range_size < (std::numeric_limits<std::uint16_t>::max)(), "nameof::enum_range requires valid size.");
  545. return static_cast<std::size_t>(range_size);
  546. }
  547. template <typename E, bool IsFlags = false>
  548. inline constexpr auto range_size_v = range_size<E, IsFlags>();
  549. template <typename E, bool IsFlags = false>
  550. using index_t = std::conditional_t<range_size_v<E, IsFlags> < (std::numeric_limits<std::uint8_t>::max)(), std::uint8_t, std::uint16_t>;
  551. template <typename E, bool IsFlags = false>
  552. inline constexpr auto invalid_index_v = (std::numeric_limits<index_t<E, IsFlags>>::max)();
  553. template <typename E, bool IsFlags, std::size_t... I>
  554. constexpr auto indexes(std::index_sequence<I...>) noexcept {
  555. static_assert(is_enum_v<E>, "nameof::detail::indexes requires enum type.");
  556. constexpr auto min = IsFlags ? log2(min_v<E, IsFlags>) : min_v<E, IsFlags>;
  557. [[maybe_unused]] auto i = index_t<E, IsFlags>{0};
  558. return std::array<decltype(i), sizeof...(I)>{{(is_valid<E, value<E, min, IsFlags>(I)>() ? i++ : invalid_index_v<E, IsFlags>)...}};
  559. }
  560. template <typename E, bool IsFlags = false>
  561. inline constexpr auto indexes_v = indexes<E, IsFlags>(std::make_index_sequence<range_size_v<E, IsFlags>>{});
  562. template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
  563. constexpr bool is_sparse() noexcept {
  564. static_assert(is_enum_v<E>, "nameof::detail::is_sparse requires enum type.");
  565. if constexpr (IsFlags) {
  566. return (sizeof(const char*) * range_size_v<E, IsFlags>) > (sizeof(E) * count_v<E, IsFlags> + sizeof(const char*) * count_v<E, IsFlags>);
  567. } else {
  568. return (sizeof(const char*) * range_size_v<E, IsFlags>) > (sizeof(index_t<E>) * range_size_v<E, IsFlags> + sizeof(const char*) * count_v<E, IsFlags>);
  569. }
  570. }
  571. template <typename E, bool IsFlags = false>
  572. inline constexpr bool is_sparse_v = is_sparse<E, IsFlags>();
  573. template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
  574. [[nodiscard]] constexpr E get_value(std::size_t i) noexcept {
  575. static_assert(is_enum_v<E>, "nameof::detail::strings requires enum type.");
  576. if constexpr (is_sparse_v<E, IsFlags>) {
  577. return values_v<E, IsFlags>[i];
  578. } else {
  579. constexpr auto min = IsFlags ? log2(min_v<E, IsFlags>) : min_v<E, IsFlags>;
  580. return value<E, min, IsFlags>(i);
  581. }
  582. }
  583. template <typename E, bool IsFlags, std::size_t... I>
  584. constexpr auto strings(std::index_sequence<I...>) noexcept {
  585. static_assert(is_enum_v<E>, "nameof::detail::strings requires enum type.");
  586. return std::array<const char*, sizeof...(I)>{{enum_name_v<E, get_value<E, IsFlags>(I)>.data()...}};
  587. }
  588. template <typename E, bool IsFlags>
  589. constexpr auto strings() noexcept {
  590. static_assert(is_enum_v<E>, "nameof::detail::strings requires enum type.");
  591. constexpr auto count = is_sparse_v<E, IsFlags> ? count_v<E, IsFlags> : range_size_v<E, IsFlags>;
  592. return strings<E, IsFlags>(std::make_index_sequence<count>{});
  593. }
  594. template <typename E, bool IsFlags = false>
  595. inline static constexpr auto strings_v = strings<E, IsFlags>();
  596. template <typename... T>
  597. struct nameof_type_supported
  598. #if defined(NAMEOF_TYPE_SUPPORTED) && NAMEOF_TYPE_SUPPORTED || defined(NAMEOF_TYPE_NO_CHECK_SUPPORT)
  599. : std::true_type {};
  600. #else
  601. : std::false_type {};
  602. #endif
  603. template <typename... T>
  604. struct nameof_type_rtti_supported
  605. #if defined(NAMEOF_TYPE_RTTI_SUPPORTED) && NAMEOF_TYPE_RTTI_SUPPORTED || defined(NAMEOF_TYPE_NO_CHECK_SUPPORT)
  606. : std::true_type {};
  607. #else
  608. : std::false_type {};
  609. #endif
  610. template <typename... T>
  611. struct nameof_member_supported
  612. #if defined(NAMEOF_MEMBER_SUPPORTED) && NAMEOF_MEMBER_SUPPORTED || defined(NAMEOF_TYPE_NO_CHECK_SUPPORT)
  613. : std::true_type {};
  614. #else
  615. : std::false_type {};
  616. #endif
  617. #if defined(_MSC_VER) && !defined(__clang__)
  618. template <typename T>
  619. struct identity {
  620. using type = T;
  621. };
  622. #else
  623. template <typename T>
  624. using identity = T;
  625. #endif
  626. template <typename T>
  627. using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
  628. template <typename T, typename R>
  629. using enable_if_has_short_name_t = std::enable_if_t<!std::is_array_v<T> && !std::is_pointer_v<T>, R>;
  630. template <typename... T>
  631. constexpr auto n() noexcept {
  632. # if defined(_MSC_VER) && !defined(__clang__)
  633. [[maybe_unused]] constexpr auto custom_name = customize::type_name<typename T::type...>();
  634. #else
  635. [[maybe_unused]] constexpr auto custom_name = customize::type_name<T...>();
  636. # endif
  637. if constexpr (custom_name.empty() && nameof_type_supported<T...>::value) {
  638. #if defined(__clang__)
  639. constexpr string_view name{__PRETTY_FUNCTION__ + 31, sizeof(__PRETTY_FUNCTION__) - 34};
  640. #elif defined(__GNUC__)
  641. constexpr string_view name{__PRETTY_FUNCTION__ + 46, sizeof(__PRETTY_FUNCTION__) - 49};
  642. #elif defined(_MSC_VER)
  643. constexpr string_view name{__FUNCSIG__ + 63, sizeof(__FUNCSIG__) - 81 - (__FUNCSIG__[sizeof(__FUNCSIG__) - 19] == ' ' ? 1 : 0)};
  644. #else
  645. constexpr auto name = string_view{};
  646. #endif
  647. return cstring<name.size()>{name};
  648. } else {
  649. return cstring<custom_name.size()>{custom_name};
  650. }
  651. }
  652. template <typename... T>
  653. inline constexpr auto type_name_v = n<T...>();
  654. #if __has_include(<cxxabi.h>)
  655. template <typename T>
  656. string nameof_type_rtti(const char* tn) {
  657. static_assert(nameof_type_rtti_supported<T>::value, "nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
  658. const auto dmg = abi::__cxa_demangle(tn, nullptr, nullptr, nullptr);
  659. const auto name = string{dmg};
  660. free(dmg);
  661. assert(name.size() > 0 && "Type does not have a name.");
  662. return name;
  663. }
  664. template <typename T>
  665. string nameof_full_type_rtti(const char* tn) {
  666. static_assert(nameof_type_rtti_supported<T>::value, "nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
  667. const auto dmg = abi::__cxa_demangle(tn, nullptr, nullptr, nullptr);
  668. auto name = string{dmg};
  669. free(dmg);
  670. assert(name.size() > 0 && "Type does not have a name.");
  671. if constexpr (std::is_const_v<std::remove_reference_t<T>>) {
  672. name = "const " + name;
  673. }
  674. if constexpr (std::is_volatile_v<std::remove_reference_t<T>>) {
  675. name = "volatile " + name;
  676. }
  677. if constexpr (std::is_lvalue_reference_v<T>) {
  678. name += '&';
  679. }
  680. if constexpr (std::is_rvalue_reference_v<T>) {
  681. name += "&&";
  682. }
  683. return name;
  684. }
  685. template <typename T, enable_if_has_short_name_t<T, int> = 0>
  686. string nameof_short_type_rtti(const char* tn) {
  687. static_assert(nameof_type_rtti_supported<T>::value, "nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
  688. const auto dmg = abi::__cxa_demangle(tn, nullptr, nullptr, nullptr);
  689. const auto name = string{pretty_name(dmg)};
  690. free(dmg);
  691. assert(name.size() > 0 && "Type does not have a short name.");
  692. return name;
  693. }
  694. #else
  695. template <typename T>
  696. string nameof_type_rtti(const char* tn) noexcept {
  697. static_assert(nameof_type_rtti_supported<T>::value, "nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
  698. const auto name = string_view{tn};
  699. assert(name.size() > 0 && "Type does not have a name.");
  700. return {name.begin(), name.end()};
  701. }
  702. template <typename T>
  703. string nameof_full_type_rtti(const char* tn) noexcept {
  704. static_assert(nameof_type_rtti_supported<T>::value, "nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
  705. auto name = string{tn};
  706. assert(name.size() > 0 && "Type does not have a name.");
  707. if constexpr (std::is_const_v<std::remove_reference_t<T>>) {
  708. name = "const " + name;
  709. }
  710. if constexpr (std::is_volatile_v<std::remove_reference_t<T>>) {
  711. name = "volatile " + name;
  712. }
  713. if constexpr (std::is_lvalue_reference_v<T>) {
  714. name += '&';
  715. }
  716. if constexpr (std::is_rvalue_reference_v<T>) {
  717. name += "&&";
  718. }
  719. return name;
  720. }
  721. template <typename T, enable_if_has_short_name_t<T, int> = 0>
  722. string nameof_short_type_rtti(const char* tn) noexcept {
  723. static_assert(nameof_type_rtti_supported<T>::value, "nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
  724. const auto name = pretty_name(tn);
  725. assert(name.size() > 0 && "Type does not have a short name.");
  726. return {name.begin(), name.end()};
  727. }
  728. #endif
  729. template <auto V, auto U = V>
  730. constexpr auto n() noexcept {
  731. [[maybe_unused]] constexpr auto custom_name = customize::member_name<V>();
  732. if constexpr (custom_name.empty() && nameof_member_supported<decltype(V)>::value) {
  733. #if defined(__clang__) || defined(__GNUC__)
  734. constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2});
  735. #elif defined(_MSC_VER) && defined(_MSVC_LANG) && _MSVC_LANG >= 202002L
  736. constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17});
  737. #else
  738. constexpr auto name = string_view{};
  739. #endif
  740. return cstring<name.size()>{name};
  741. } else {
  742. return cstring<custom_name.size()>{custom_name};
  743. }
  744. }
  745. #if defined(__clang__) || defined(__GNUC__)
  746. template <auto V>
  747. inline constexpr auto member_name_v = n<V>();
  748. #elif defined(_MSC_VER) && defined(_MSVC_LANG) && _MSVC_LANG >= 202002L
  749. template <typename From, typename Type>
  750. From get_base_type(Type From::*);
  751. template <typename T>
  752. union union_type {
  753. char c = {};
  754. T f;
  755. };
  756. template <typename T>
  757. inline constexpr auto static_v = T{};
  758. template <auto V>
  759. constexpr auto get_member_name() noexcept {
  760. if constexpr (std::is_member_function_pointer_v<decltype(V)>) {
  761. return n<V>();
  762. } else {
  763. return n<V, &(static_v<union_type<decltype(get_base_type(V))>>.f.*V)>();
  764. }
  765. }
  766. template <auto V>
  767. inline constexpr auto member_name_v = get_member_name<V>();
  768. #else
  769. template <auto V>
  770. inline constexpr auto member_name_v = cstring<0>{string_view{}};
  771. #endif
  772. } // namespace nameof::detail
  773. // Checks is nameof_type supported compiler.
  774. inline constexpr bool is_nameof_type_supported = detail::nameof_type_supported<void>::value;
  775. // Checks is nameof_type_rtti supported compiler.
  776. inline constexpr bool is_nameof_type_rtti_supported = detail::nameof_type_rtti_supported<void>::value;
  777. // Checks is nameof_member supported compiler.
  778. inline constexpr bool is_nameof_member_supported = detail::nameof_member_supported<void>::value;
  779. // Checks is nameof_enum supported compiler.
  780. inline constexpr bool is_nameof_enum_supported = detail::nameof_enum_supported<void>::value;
  781. // Obtains name of enum variable.
  782. template <typename E>
  783. [[nodiscard]] constexpr auto nameof_enum(E value) noexcept -> detail::enable_if_enum_t<E, string_view> {
  784. using D = std::decay_t<E>;
  785. using U = std::underlying_type_t<D>;
  786. static_assert(detail::nameof_enum_supported<D>::value, "nameof::nameof_enum unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
  787. static_assert(detail::count_v<D> > 0, "nameof::nameof_enum requires enum implementation and valid max and min.");
  788. const bool valid = static_cast<U>(value) >= static_cast<U>(detail::min_v<D>) && static_cast<U>(value) <= static_cast<U>(detail::max_v<D>);
  789. if (const auto i = static_cast<int>(value) - detail::min_v<D>; valid) {
  790. if constexpr (detail::is_sparse_v<D>) {
  791. if (const auto idx = detail::indexes_v<D>[i]; idx != detail::invalid_index_v<D>) {
  792. return detail::strings_v<D>[idx];
  793. }
  794. } else {
  795. return detail::strings_v<D>[static_cast<std::size_t>(i)];
  796. }
  797. }
  798. return {}; // Value out of range.
  799. }
  800. // Obtains name of enum variable or default value if enum variable out of range.
  801. template <typename E>
  802. [[nodiscard]] auto nameof_enum_or(E value, string_view default_value) -> detail::enable_if_enum_t<E, string> {
  803. using D = std::decay_t<E>;
  804. if (auto v = nameof_enum<D>(value); !v.empty()) {
  805. return string{v.data(), v.size()};
  806. }
  807. return string{default_value.data(), default_value.size()};
  808. }
  809. // Obtains name of enum-flags variable.
  810. template <typename E>
  811. [[nodiscard]] auto nameof_enum_flag(E value) -> detail::enable_if_enum_t<E, string> {
  812. using D = std::decay_t<E>;
  813. using U = std::underlying_type_t<D>;
  814. static_assert(detail::nameof_enum_supported<D>::value, "nameof::nameof_enum_flag unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
  815. static_assert(detail::count_v<D, true> > 0, "nameof::nameof_enum_flag requires enum-flags implementation.");
  816. constexpr auto size = detail::is_sparse_v<D, true> ? detail::count_v<D, true> : detail::range_size_v<D, true>;
  817. string name;
  818. auto check_value = U{0};
  819. for (std::size_t i = 0; i < size; ++i) {
  820. if (const auto v = static_cast<U>(detail::get_value<D, true>(i)); (static_cast<U>(value) & v) != 0) {
  821. if (const auto n = detail::strings_v<D, true>[i]; n != nullptr) {
  822. check_value |= v;
  823. if (!name.empty()) {
  824. name.append(1, '|');
  825. }
  826. name.append(n);
  827. } else {
  828. return {}; // Value out of range.
  829. }
  830. }
  831. }
  832. const bool valid = check_value != 0 && check_value == static_cast<U>(value);
  833. if (valid) {
  834. return name;
  835. }
  836. return {}; // Invalid value or out of range.
  837. }
  838. // Obtains name of static storage enum variable.
  839. // This version is much lighter on the compile times and is not restricted to the enum_range limitation.
  840. template <auto V>
  841. [[nodiscard]] constexpr auto nameof_enum() noexcept -> detail::enable_if_enum_t<decltype(V), string_view> {
  842. using D = std::decay_t<decltype(V)>;
  843. static_assert(detail::nameof_enum_supported<D>::value, "nameof::nameof_enum unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
  844. constexpr string_view name = detail::enum_name_v<D, V>;
  845. static_assert(name.size() > 0, "Enum value does not have a name.");
  846. return name;
  847. }
  848. // Obtains name of type, reference and cv-qualifiers are ignored.
  849. template <typename T>
  850. [[nodiscard]] constexpr string_view nameof_type() noexcept {
  851. static_assert(detail::nameof_type_supported<T>::value, "nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
  852. using U = detail::identity<detail::remove_cvref_t<T>>;
  853. constexpr string_view name = detail::type_name_v<U>;
  854. static_assert(name.size() > 0, "Type does not have a name.");
  855. return name;
  856. }
  857. // Obtains full name of type, with reference and cv-qualifiers.
  858. template <typename T>
  859. [[nodiscard]] constexpr string_view nameof_full_type() noexcept {
  860. static_assert(detail::nameof_type_supported<T>::value, "nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
  861. using U = detail::identity<T>;
  862. constexpr string_view name = detail::type_name_v<U>;
  863. static_assert(name.size() > 0, "Type does not have a full name.");
  864. return name;
  865. }
  866. // Obtains short name of type.
  867. template <typename T>
  868. [[nodiscard]] constexpr auto nameof_short_type() noexcept -> detail::enable_if_has_short_name_t<T, string_view> {
  869. static_assert(detail::nameof_type_supported<T>::value, "nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
  870. using U = detail::identity<detail::remove_cvref_t<T>>;
  871. constexpr string_view name = detail::pretty_name(detail::type_name_v<U>);
  872. static_assert(name.size() > 0, "Type does not have a short name.");
  873. return name;
  874. }
  875. // Obtains name of member.
  876. template <auto V>
  877. [[nodiscard]] constexpr auto nameof_member() noexcept -> std::enable_if_t<std::is_member_pointer_v<decltype(V)>, string_view> {
  878. static_assert(detail::nameof_member_supported<decltype(V)>::value, "nameof::nameof_memder unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
  879. constexpr string_view name = detail::member_name_v<V>;
  880. static_assert(name.size() > 0, "Member does not have a name.");
  881. return name;
  882. }
  883. } // namespace nameof
  884. // Obtains name of variable, function, macro.
  885. #define NAMEOF(...) []() constexpr noexcept { \
  886. ::std::void_t<decltype(__VA_ARGS__)>(); \
  887. constexpr auto _name = ::nameof::detail::pretty_name(#__VA_ARGS__); \
  888. static_assert(_name.size() > 0, "Expression does not have a name."); \
  889. constexpr auto _size = _name.size(); \
  890. constexpr auto _nameof = ::nameof::cstring<_size>{_name}; \
  891. return _nameof; }()
  892. // Obtains full name of variable, function, macro.
  893. #define NAMEOF_FULL(...) []() constexpr noexcept { \
  894. ::std::void_t<decltype(__VA_ARGS__)>(); \
  895. constexpr auto _name = ::nameof::detail::pretty_name(#__VA_ARGS__, false); \
  896. static_assert(_name.size() > 0, "Expression does not have a name."); \
  897. constexpr auto _size = _name.size(); \
  898. constexpr auto _nameof_full = ::nameof::cstring<_size>{_name}; \
  899. return _nameof_full; }()
  900. // Obtains raw name of variable, function, macro.
  901. #define NAMEOF_RAW(...) []() constexpr noexcept { \
  902. ::std::void_t<decltype(__VA_ARGS__)>(); \
  903. constexpr auto _name = ::nameof::string_view{#__VA_ARGS__}; \
  904. static_assert(_name.size() > 0, "Expression does not have a name."); \
  905. constexpr auto _size = _name.size(); \
  906. constexpr auto _nameof_raw = ::nameof::cstring<_size>{_name}; \
  907. return _nameof_raw; }()
  908. // Obtains name of enum variable.
  909. #define NAMEOF_ENUM(...) ::nameof::nameof_enum<::std::decay_t<decltype(__VA_ARGS__)>>(__VA_ARGS__)
  910. // Obtains name of enum variable or default value if enum variable out of range.
  911. #define NAMEOF_ENUM_OR(...) ::nameof::nameof_enum_or(__VA_ARGS__)
  912. // Obtains name of static storage enum variable.
  913. // This version is much lighter on the compile times and is not restricted to the enum_range limitation.
  914. #define NAMEOF_ENUM_CONST(...) ::nameof::nameof_enum<__VA_ARGS__>()
  915. // Obtains name of enum-flags variable.
  916. #define NAMEOF_ENUM_FLAG(...) ::nameof::nameof_enum_flag<::std::decay_t<decltype(__VA_ARGS__)>>(__VA_ARGS__)
  917. // Obtains type name, reference and cv-qualifiers are ignored.
  918. #define NAMEOF_TYPE(...) ::nameof::nameof_type<__VA_ARGS__>()
  919. // Obtains full type name, with reference and cv-qualifiers.
  920. #define NAMEOF_FULL_TYPE(...) ::nameof::nameof_full_type<__VA_ARGS__>()
  921. // Obtains short type name.
  922. #define NAMEOF_SHORT_TYPE(...) ::nameof::nameof_short_type<__VA_ARGS__>()
  923. // Obtains type name of expression, reference and cv-qualifiers are ignored.
  924. #define NAMEOF_TYPE_EXPR(...) ::nameof::nameof_type<decltype(__VA_ARGS__)>()
  925. // Obtains full type name of expression, with reference and cv-qualifiers.
  926. #define NAMEOF_FULL_TYPE_EXPR(...) ::nameof::nameof_full_type<decltype(__VA_ARGS__)>()
  927. // Obtains short type name of expression.
  928. #define NAMEOF_SHORT_TYPE_EXPR(...) ::nameof::nameof_short_type<decltype(__VA_ARGS__)>()
  929. // Obtains type name, with reference and cv-qualifiers, using RTTI.
  930. #define NAMEOF_TYPE_RTTI(...) ::nameof::detail::nameof_type_rtti<::std::void_t<decltype(__VA_ARGS__)>>(typeid(__VA_ARGS__).name())
  931. // Obtains full type name, using RTTI.
  932. #define NAMEOF_FULL_TYPE_RTTI(...) ::nameof::detail::nameof_full_type_rtti<decltype(__VA_ARGS__)>(typeid(__VA_ARGS__).name())
  933. // Obtains short type name, using RTTI.
  934. #define NAMEOF_SHORT_TYPE_RTTI(...) ::nameof::detail::nameof_short_type_rtti<decltype(__VA_ARGS__)>(typeid(__VA_ARGS__).name())
  935. // Obtains name of member.
  936. #define NAMEOF_MEMBER(...) ::nameof::nameof_member<__VA_ARGS__>()
  937. #if defined(__clang__)
  938. # pragma clang diagnostic pop
  939. #elif defined(__GNUC__)
  940. # pragma GCC diagnostic pop
  941. #elif defined(_MSC_VER)
  942. # pragma warning(pop)
  943. #endif
  944. #endif // NEARGYE_NAMEOF_HPP