source_location.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "source_location.h"
  2. #include <library/cpp/yt/string/format.h>
  3. #include <string.h>
  4. namespace NYT {
  5. ////////////////////////////////////////////////////////////////////////////////
  6. #ifdef __cpp_lib_source_location
  7. void FormatValue(TStringBuilderBase* builder, const std::source_location& location, TStringBuf /*spec*/)
  8. {
  9. if (location.file_name() != nullptr) {
  10. builder->AppendFormat(
  11. "%v:%v:%v",
  12. location.file_name(),
  13. location.line(),
  14. location.column());
  15. } else {
  16. builder->AppendString("<unknown>");
  17. }
  18. }
  19. #endif // __cpp_lib_source_location
  20. ////////////////////////////////////////////////////////////////////////////////
  21. const char* TSourceLocation::GetFileName() const
  22. {
  23. return FileName_;
  24. }
  25. int TSourceLocation::GetLine() const
  26. {
  27. return Line_;
  28. }
  29. bool TSourceLocation::IsValid() const
  30. {
  31. return FileName_ != nullptr;
  32. }
  33. bool TSourceLocation::operator<(const TSourceLocation& other) const
  34. {
  35. const char* fileName = FileName_ ? FileName_ : "";
  36. const char* otherFileName = other.FileName_ ? other.FileName_ : "";
  37. int fileNameResult = strcmp(fileName, otherFileName);
  38. if (fileNameResult != 0) {
  39. return fileNameResult < 0;
  40. }
  41. if (Line_ < other.Line_) {
  42. return true;
  43. }
  44. if (Line_ > other.Line_) {
  45. return false;
  46. }
  47. return false;
  48. }
  49. bool TSourceLocation::operator==(const TSourceLocation& other) const
  50. {
  51. const char* fileName = FileName_ ? FileName_ : "";
  52. const char* otherFileName = other.FileName_ ? other.FileName_ : "";
  53. return
  54. strcmp(fileName, otherFileName) == 0 &&
  55. Line_ == other.Line_;
  56. }
  57. #ifdef __cpp_lib_source_location
  58. TSourceLocation TSourceLocation::FromStd(const std::source_location& location)
  59. {
  60. return TSourceLocation(location.file_name(), location.line());
  61. }
  62. #endif // __cpp_lib_source_location
  63. void FormatValue(TStringBuilderBase* builder, const TSourceLocation& location, TStringBuf /*spec*/)
  64. {
  65. if (location.GetFileName() != nullptr) {
  66. builder->AppendFormat(
  67. "%v:%v",
  68. location.GetFileName(),
  69. location.GetLine());
  70. } else {
  71. builder->AppendString("<unknown>");
  72. }
  73. }
  74. ////////////////////////////////////////////////////////////////////////////////
  75. } // namespace NYT