aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/misc/source_location.h
blob: 286527887ae13e969f2cababce09f810c8a1854f (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
#pragma once

#include <util/generic/string.h>

#ifdef __cpp_lib_source_location
#include <source_location>
#endif // __cpp_lib_source_location

namespace NYT {

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

class TStringBuilderBase;

// TODO(dgolear): Drop when LLVM-14 is eradicated.
#ifdef __cpp_lib_source_location
void FormatValue(TStringBuilderBase* builder, const std::source_location& location, TStringBuf /*spec*/);
#endif // __cpp_lib_source_location

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

class TSourceLocation
{
public:
    TSourceLocation() = default;
    TSourceLocation(const char* fileName, int line);
#ifdef __cpp_lib_source_location
    explicit TSourceLocation(const std::source_location& location);
#endif // __cpp_lib_source_location

    const char* GetFileName() const;
    int GetLine() const;
    bool IsValid() const;

    bool operator<(const TSourceLocation& other) const;
    bool operator==(const TSourceLocation& other) const;

private:
    const char* FileName_ = nullptr;
    int Line_ = -1;
};

//! Defines a macro to record the current source location.
#ifdef __cpp_lib_source_location
#define YT_CURRENT_SOURCE_LOCATION ::NYT::TSourceLocation(std::source_location::current())
#else
#define YT_CURRENT_SOURCE_LOCATION ::NYT::TSourceLocation(__FILE__, __LINE__)
#endif // __cpp_lib_source_location

void FormatValue(TStringBuilderBase* builder, const TSourceLocation& location, TStringBuf spec);

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

} // namespace NYT

#define SOURCE_LOCATION_INL_H_
#include "source_location-inl.h"
#undef SOURCE_LOCATION_INL_H_