1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef AAPT_SOURCE_H
18#define AAPT_SOURCE_H
19
20#include <ostream>
21#include <string>
22#include <tuple>
23
24namespace aapt {
25
26struct SourceLineColumn;
27struct SourceLine;
28
29/**
30 * Represents a file on disk. Used for logging and
31 * showing errors.
32 */
33struct Source {
34    std::string path;
35
36    inline SourceLine line(size_t line) const;
37};
38
39/**
40 * Represents a file on disk and a line number in that file.
41 * Used for logging and showing errors.
42 */
43struct SourceLine {
44    std::string path;
45    size_t line;
46
47    inline SourceLineColumn column(size_t column) const;
48};
49
50/**
51 * Represents a file on disk and a line:column number in that file.
52 * Used for logging and showing errors.
53 */
54struct SourceLineColumn {
55    std::string path;
56    size_t line;
57    size_t column;
58};
59
60//
61// Implementations
62//
63
64SourceLine Source::line(size_t line) const {
65    return SourceLine{ path, line };
66}
67
68SourceLineColumn SourceLine::column(size_t column) const {
69    return SourceLineColumn{ path, line, column };
70}
71
72inline ::std::ostream& operator<<(::std::ostream& out, const Source& source) {
73    return out << source.path;
74}
75
76inline ::std::ostream& operator<<(::std::ostream& out, const SourceLine& source) {
77    return out << source.path << ":" << source.line;
78}
79
80inline ::std::ostream& operator<<(::std::ostream& out, const SourceLineColumn& source) {
81    return out << source.path << ":" << source.line << ":" << source.column;
82}
83
84inline bool operator<(const SourceLine& lhs, const SourceLine& rhs) {
85    return std::tie(lhs.path, lhs.line) < std::tie(rhs.path, rhs.line);
86}
87
88} // namespace aapt
89
90#endif // AAPT_SOURCE_H
91