blob: 422b36112660a00b2046aa96418da7be6e26bfb2 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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 Lesinski1ab598f2015-08-14 14:26:04 -070020#include "util/Maybe.h"
21#include "util/StringPiece.h"
22
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023#include <ostream>
24#include <string>
25
26namespace aapt {
27
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028/**
29 * Represents a file on disk. Used for logging and
30 * showing errors.
31 */
32struct Source {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 std::string path;
34 Maybe<size_t> line;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 Source() = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 inline Source(const StringPiece& path)
39 : path(path.toString()) { // NOLINT(implicit)
40 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080041
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 inline Source(const StringPiece& path, size_t line)
43 : path(path.toString()), line(line) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 inline Source withLine(size_t line) const { return Source(path, line); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046};
47
48//
49// Implementations
50//
51
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052inline ::std::ostream& operator<<(::std::ostream& out, const Source& source) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 out << source.path;
54 if (source.line) {
55 out << ":" << source.line.value();
56 }
57 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080058}
59
Adam Lesinskia5870652015-11-20 15:32:30 -080060inline bool operator==(const Source& lhs, const Source& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 return lhs.path == rhs.path && lhs.line == rhs.line;
Adam Lesinskia5870652015-11-20 15:32:30 -080062}
63
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064inline bool operator<(const Source& lhs, const Source& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 int cmp = lhs.path.compare(rhs.path);
66 if (cmp < 0) return true;
67 if (cmp > 0) return false;
68 if (lhs.line) {
69 if (rhs.line) {
70 return lhs.line.value() < rhs.line.value();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 return false;
73 }
74 return bool(rhs.line);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070075}
76
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079#endif // AAPT_SOURCE_H