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 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | #include "util/Maybe.h" |
| 21 | #include "util/StringPiece.h" |
| 22 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 23 | #include <ostream> |
| 24 | #include <string> |
| 25 | |
| 26 | namespace aapt { |
| 27 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 28 | /** |
| 29 | * Represents a file on disk. Used for logging and |
| 30 | * showing errors. |
| 31 | */ |
| 32 | struct Source { |
| 33 | std::string path; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 34 | Maybe<size_t> line; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 35 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 36 | Source() = default; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 37 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 38 | inline Source(const StringPiece& path) : path(path.toString()) { |
| 39 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 40 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 41 | inline Source(const StringPiece& path, size_t line) : path(path.toString()), line(line) { |
| 42 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 43 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 44 | inline Source withLine(size_t line) const { |
| 45 | return Source(path, line); |
| 46 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | // |
| 50 | // Implementations |
| 51 | // |
| 52 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 53 | inline ::std::ostream& operator<<(::std::ostream& out, const Source& source) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 54 | out << source.path; |
| 55 | if (source.line) { |
| 56 | out << ":" << source.line.value(); |
| 57 | } |
| 58 | return out; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 61 | inline bool operator==(const Source& lhs, const Source& rhs) { |
| 62 | return lhs.path == rhs.path && lhs.line == rhs.line; |
| 63 | } |
| 64 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 65 | inline bool operator<(const Source& lhs, const Source& rhs) { |
| 66 | int cmp = lhs.path.compare(rhs.path); |
| 67 | if (cmp < 0) return true; |
| 68 | if (cmp > 0) return false; |
| 69 | if (lhs.line) { |
| 70 | if (rhs.line) { |
| 71 | return lhs.line.value() < rhs.line.value(); |
| 72 | } |
| 73 | return false; |
| 74 | } |
| 75 | return bool(rhs.line); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 78 | } // namespace aapt |
| 79 | |
| 80 | #endif // AAPT_SOURCE_H |