source_location 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // <source_location> -*- C++ -*-
  2. // Copyright (C) 2020-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/source_location
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_SRCLOC
  24. #define _GLIBCXX_SRCLOC 1
  25. #if __cplusplus > 201703L && __has_builtin(__builtin_source_location)
  26. #include <bits/c++config.h>
  27. namespace std
  28. {
  29. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  30. #define __cpp_lib_source_location 201907L
  31. /// A class that describes a location in source code.
  32. struct source_location
  33. {
  34. private:
  35. using uint_least32_t = __UINT_LEAST32_TYPE__;
  36. struct __impl
  37. {
  38. const char* _M_file_name;
  39. const char* _M_function_name;
  40. unsigned _M_line;
  41. unsigned _M_column;
  42. };
  43. using __builtin_ret_type = decltype(__builtin_source_location());
  44. public:
  45. // [support.srcloc.cons], creation
  46. static consteval source_location
  47. current(__builtin_ret_type __p = __builtin_source_location()) noexcept
  48. {
  49. source_location __ret;
  50. __ret._M_impl = static_cast <const __impl*>(__p);
  51. return __ret;
  52. }
  53. constexpr source_location() noexcept { }
  54. // [support.srcloc.obs], observers
  55. constexpr uint_least32_t
  56. line() const noexcept
  57. { return _M_impl ? _M_impl->_M_line : 0u; }
  58. constexpr uint_least32_t
  59. column() const noexcept
  60. { return _M_impl ? _M_impl->_M_column : 0u; }
  61. constexpr const char*
  62. file_name() const noexcept
  63. { return _M_impl ? _M_impl->_M_file_name : ""; }
  64. constexpr const char*
  65. function_name() const noexcept
  66. { return _M_impl ? _M_impl->_M_function_name : ""; }
  67. private:
  68. const __impl* _M_impl = nullptr;
  69. };
  70. _GLIBCXX_END_NAMESPACE_VERSION
  71. } // namespace std
  72. #endif // C++20 && __builtin_source_location
  73. #endif // _GLIBCXX_SRCLOC