ccls/src/position.hh

83 lines
1.9 KiB
C++
Raw Normal View History

2018-08-21 05:27:52 +00:00
// Copyright 2017-2018 ccls Authors
// SPDX-License-Identifier: Apache-2.0
2017-04-05 08:06:18 +00:00
#pragma once
2018-10-29 04:21:21 +00:00
#include "utils.hh"
2017-05-19 07:02:01 +00:00
#include <stdint.h>
#include <string>
namespace ccls {
struct Pos {
uint16_t line = 0;
int16_t column = -1;
2017-04-05 08:06:18 +00:00
static Pos FromString(const std::string &encoded);
2017-04-05 08:06:18 +00:00
bool Valid() const { return column >= 0; }
2017-04-05 08:06:18 +00:00
std::string ToString();
// Compare two Positions and check if they are equal. Ignores the value of
// |interesting|.
bool operator==(const Pos &o) const {
return line == o.line && column == o.column;
}
bool operator<(const Pos &o) const {
if (line != o.line)
return line < o.line;
return column < o.column;
}
bool operator<=(const Pos &o) const { return !(o < *this); }
2017-04-05 08:06:18 +00:00
};
struct Range {
Pos start;
Pos end;
2017-04-05 08:06:18 +00:00
2018-08-09 17:08:14 +00:00
static Range FromString(const std::string &encoded);
2017-04-05 08:06:18 +00:00
bool Valid() const { return start.Valid(); }
bool Contains(int line, int column) const;
2017-04-05 08:06:18 +00:00
std::string ToString();
2018-08-09 17:08:14 +00:00
bool operator==(const Range &o) const {
return start == o.start && end == o.end;
}
2018-08-09 17:08:14 +00:00
bool operator<(const Range &o) const {
return !(start == o.start) ? start < o.start : end < o.end;
}
2017-05-19 07:02:01 +00:00
};
// Reflection
struct JsonReader;
struct JsonWriter;
struct BinaryReader;
struct BinaryWriter;
void Reflect(JsonReader &visitor, Pos &value);
void Reflect(JsonReader &visitor, Range &value);
void Reflect(JsonWriter &visitor, Pos &value);
void Reflect(JsonWriter &visitor, Range &value);
void Reflect(BinaryReader &visitor, Pos &value);
void Reflect(BinaryReader &visitor, Range &value);
void Reflect(BinaryWriter &visitor, Pos &value);
void Reflect(BinaryWriter &visitor, Range &value);
} // namespace ccls
2018-05-05 03:40:52 +00:00
namespace std {
template <> struct hash<ccls::Range> {
std::size_t operator()(ccls::Range x) const {
union {
ccls::Range range;
2018-05-05 03:40:52 +00:00
uint64_t u64;
} u{x};
static_assert(sizeof(ccls::Range) == 8);
2018-05-05 03:40:52 +00:00
return hash<uint64_t>()(u.u64);
}
};
2018-08-09 17:08:14 +00:00
} // namespace std
2018-05-05 03:40:52 +00:00
MAKE_HASHABLE(ccls::Pos, t.line, t.column);