blob: d7f2a668477c8aff7803e3a6263d02f8acdbb962 [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
20#include <ostream>
21#include <string>
22
Adam Lesinskid5083f62017-01-16 15:07:21 -080023#include "androidfw/StringPiece.h"
24
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025#include "util/Maybe.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027namespace aapt {
28
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080029/**
30 * Represents a file on disk. Used for logging and
31 * showing errors.
32 */
33struct Source {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 std::string path;
35 Maybe<size_t> line;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 Source() = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080038
Adam Lesinskid5083f62017-01-16 15:07:21 -080039 inline Source(const android::StringPiece& path) : path(path.to_string()) { // NOLINT(implicit)
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080041
Adam Lesinskid5083f62017-01-16 15:07:21 -080042 inline Source(const android::StringPiece& path, size_t line)
43 : path(path.to_string()), line(line) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044
Adam Lesinskice5e56e2016-10-21 17:56:45 -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