Spicy
location.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <string>
6 #include <utility>
7 
8 #include <hilti/rt/filesystem.h>
9 
10 #include <hilti/base/util.h>
11 
12 namespace hilti {
13 
17 class Location {
18 public:
29  Location(hilti::rt::filesystem::path file = "", int from_line = -1, int to_line = -1, int from_character = -1,
30  int to_character = -1)
31  : _file(std::move(file)),
32  _from_line(from_line),
33  _to_line(to_line),
34  _from_character(from_character),
35  _to_character(to_character) {}
36 
37  Location(const Location&) = default;
38  Location(Location&&) = default;
39  Location& operator=(const Location&) = default;
40  Location& operator=(Location&&) = default;
41  ~Location() = default;
42 
43  auto file() const { return _file.generic_string(); }
44  auto from() const { return _from_line; }
45  auto to() const { return _to_line; }
46 
52  std::string render(bool no_path = false) const;
53 
58  explicit operator bool() const;
59 
61  operator std::string() const { return render(); }
62 
63  bool operator<(const Location& other) const {
64  return std::tie(_file, _from_line, _from_character, _to_line, _to_character) <
65  std::tie(other._file, other._from_line, other._from_character, other._to_line, other._to_character);
66  }
67 
68  bool operator==(const Location& other) const {
69  return std::tie(_file, _from_line, _from_character, _to_line, _to_character) ==
70  std::tie(other._file, other._from_line, other._from_character, other._to_line, other._to_character);
71  }
72 
73 private:
74  hilti::rt::filesystem::path _file;
75  int _from_line = -1;
76  int _to_line = -1;
77 
78  int _from_character = -1;
79  int _to_character = -1;
80 
81  friend struct std::hash<Location>;
82 };
83 
85 inline auto to_string(const Location& l) { return l.render(); }
86 
88 inline std::ostream& operator<<(std::ostream& out, const Location& l) {
89  out << l.render();
90  return out;
91 }
92 
93 namespace location {
95 extern const Location None;
96 } // namespace location
97 
98 } // namespace hilti
99 
100 namespace std {
101 template<>
102 struct hash<hilti::Location> {
103  size_t operator()(const hilti::Location& x) const {
104  auto hash = std::hash<const char*>()(x._file.c_str());
105  hash = hilti::rt::hashCombine(hash, x._from_line);
106  hash = hilti::rt::hashCombine(hash, x._to_line);
107  hash = hilti::rt::hashCombine(hash, x._from_character);
108  hash = hilti::rt::hashCombine(hash, x._to_character);
109 
110  return hash;
111  }
112 };
113 } // namespace std
Definition: optional.h:79
Location(hilti::rt::filesystem::path file="", int from_line=-1, int to_line=-1, int from_character=-1, int to_character=-1)
Definition: location.h:29
std::string render(bool no_path=false) const
Definition: location.cc:12
Definition: location.h:17
Definition: location.h:93