source_location.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. void FormatValue(TStringBuilderBase* builder, const TSourceLocation& location, TStringBuf /*spec*/)
  58. {
  59. if (location.GetFileName() != nullptr) {
  60. builder->AppendFormat(
  61. "%v:%v",
  62. location.GetFileName(),
  63. location.GetLine());
  64. } else {
  65. builder->AppendString("<unknown>");
  66. }
  67. }
  68. ////////////////////////////////////////////////////////////////////////////////
  69. } // namespace NYT