aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/misc/source_location.cpp
blob: f3f396ee90e63a170cb9d3685f495b9b02dd04cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "source_location.h"

#include <library/cpp/yt/string/format.h>

#include <string.h>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

#ifdef __cpp_lib_source_location

void FormatValue(TStringBuilderBase* builder, const std::source_location& location, TStringBuf /*spec*/)
{
    if (location.file_name() != nullptr) {
        builder->AppendFormat(
            "%v:%v:%v",
            location.file_name(),
            location.line(),
            location.column());
    } else {
        builder->AppendString("<unknown>");
    }
}

#endif // __cpp_lib_source_location

////////////////////////////////////////////////////////////////////////////////

const char* TSourceLocation::GetFileName() const
{
    return FileName_;
}

int TSourceLocation::GetLine() const
{
    return Line_;
}

bool TSourceLocation::IsValid() const
{
    return FileName_ != nullptr;
}

bool TSourceLocation::operator<(const TSourceLocation& other) const
{
    const char* fileName = FileName_ ? FileName_ : "";
    const char* otherFileName = other.FileName_ ? other.FileName_ : "";
    int fileNameResult = strcmp(fileName, otherFileName);
    if (fileNameResult != 0) {
        return fileNameResult < 0;
    }

    if (Line_ < other.Line_) {
        return true;
    }
    if (Line_ > other.Line_) {
        return false;
    }

    return false;
}

bool TSourceLocation::operator==(const TSourceLocation& other) const
{
    const char* fileName = FileName_ ? FileName_ : "";
    const char* otherFileName = other.FileName_ ? other.FileName_ : "";
    return
        strcmp(fileName, otherFileName) == 0 &&
        Line_ == other.Line_;
}

#ifdef __cpp_lib_source_location
TSourceLocation TSourceLocation::FromStd(const std::source_location& location)
{
    return TSourceLocation(location.file_name(), location.line());
}
#endif // __cpp_lib_source_location

void FormatValue(TStringBuilderBase* builder, const TSourceLocation& location, TStringBuf /*spec*/)
{
    if (location.GetFileName() != nullptr) {
        builder->AppendFormat(
            "%v:%v",
            location.GetFileName(),
            location.GetLine());
    } else {
        builder->AppendString("<unknown>");
    }
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT