Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 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> |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 22 | #include <tuple> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 23 | |
| 24 | namespace aapt { |
| 25 | |
| 26 | struct SourceLineColumn; |
| 27 | struct SourceLine; |
| 28 | |
| 29 | /** |
| 30 | * Represents a file on disk. Used for logging and |
| 31 | * showing errors. |
| 32 | */ |
| 33 | struct 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 | */ |
| 43 | struct 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 | */ |
| 54 | struct SourceLineColumn { |
| 55 | std::string path; |
| 56 | size_t line; |
| 57 | size_t column; |
| 58 | }; |
| 59 | |
| 60 | // |
| 61 | // Implementations |
| 62 | // |
| 63 | |
| 64 | SourceLine Source::line(size_t line) const { |
| 65 | return SourceLine{ path, line }; |
| 66 | } |
| 67 | |
| 68 | SourceLineColumn SourceLine::column(size_t column) const { |
| 69 | return SourceLineColumn{ path, line, column }; |
| 70 | } |
| 71 | |
| 72 | inline ::std::ostream& operator<<(::std::ostream& out, const Source& source) { |
| 73 | return out << source.path; |
| 74 | } |
| 75 | |
| 76 | inline ::std::ostream& operator<<(::std::ostream& out, const SourceLine& source) { |
| 77 | return out << source.path << ":" << source.line; |
| 78 | } |
| 79 | |
| 80 | inline ::std::ostream& operator<<(::std::ostream& out, const SourceLineColumn& source) { |
| 81 | return out << source.path << ":" << source.line << ":" << source.column; |
| 82 | } |
| 83 | |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 84 | inline bool operator<(const SourceLine& lhs, const SourceLine& rhs) { |
| 85 | return std::tie(lhs.path, lhs.line) < std::tie(rhs.path, rhs.line); |
| 86 | } |
| 87 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 88 | } // namespace aapt |
| 89 | |
| 90 | #endif // AAPT_SOURCE_H |