source_location 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // <source_location> -*- C++ -*-
  2. // Copyright (C) 2020-2021 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. public:
  37. // [support.srcloc.cons], creation
  38. static consteval source_location
  39. current(const void* __p = __builtin_source_location()) noexcept
  40. {
  41. source_location __ret;
  42. __ret._M_impl = static_cast <const __impl*>(__p);
  43. return __ret;
  44. }
  45. constexpr source_location() noexcept { }
  46. // [support.srcloc.obs], observers
  47. constexpr uint_least32_t
  48. line() const noexcept
  49. { return _M_impl ? _M_impl->_M_line : 0u; }
  50. constexpr uint_least32_t
  51. column() const noexcept
  52. { return _M_impl ? _M_impl->_M_column : 0u; }
  53. constexpr const char*
  54. file_name() const noexcept
  55. { return _M_impl ? _M_impl->_M_file_name : ""; }
  56. constexpr const char*
  57. function_name() const noexcept
  58. { return _M_impl ? _M_impl->_M_function_name : ""; }
  59. private:
  60. struct __impl
  61. {
  62. const char* _M_file_name;
  63. const char* _M_function_name;
  64. unsigned _M_line;
  65. unsigned _M_column;
  66. };
  67. const __impl* _M_impl = nullptr;
  68. };
  69. _GLIBCXX_END_NAMESPACE_VERSION
  70. } // namespace std
  71. #endif // C++20 && __builtin_source_location
  72. #endif // _GLIBCXX_SRCLOC