CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
source_location.h
Go to the documentation of this file.
1
9#ifndef CPPCOMMON_SYSTEM_SOURCE_LOCATION_H
10#define CPPCOMMON_SYSTEM_SOURCE_LOCATION_H
11
12#include "string/format.h"
13
14#include <sstream>
15#include <string>
16
17namespace CppCommon {
18
20
23#define __LOCATION__ CppCommon::SourceLocation(__FILE__, __LINE__)
24
26
32{
33 friend class Exception;
34
35public:
37
41 explicit SourceLocation(const char* filename, int line) noexcept : _filename(filename), _line(line) {}
42 SourceLocation(const SourceLocation&) noexcept = default;
43 SourceLocation(SourceLocation&&) noexcept = default;
44 ~SourceLocation() noexcept = default;
45
46 SourceLocation& operator=(const SourceLocation&) noexcept = default;
47 SourceLocation& operator=(SourceLocation&&) noexcept = default;
48
50 const char* filename() const noexcept { return _filename; }
52 int line() const noexcept { return _line; }
53
55 std::string string() const
56 { std::stringstream ss; ss << *this; return ss.str(); }
57
59 friend std::ostream& operator<<(std::ostream& os, const SourceLocation& source_location);
60
61private:
62 const char* _filename;
63 int _line;
64
65 SourceLocation() noexcept : SourceLocation(nullptr, 0) {}
66};
67
70} // namespace CppCommon
71
72#include "source_location.inl"
73
74#endif // CPPCOMMON_SYSTEM_SOURCE_LOCATION_H
const char * filename() const noexcept
Get file name.
SourceLocation(const char *filename, int line) noexcept
Create a new source location with the given file name and line number.
SourceLocation(const SourceLocation &) noexcept=default
SourceLocation(SourceLocation &&) noexcept=default
int line() const noexcept
Get line number.
std::string string() const
Get the string from the current source location.
friend std::ostream & operator<<(std::ostream &os, const SourceLocation &source_location)
Output source location into the given output stream.
Format string definition.
C++ Common project definitions.
Source location inline implementation.