blob: 8af203cdad0e39c4d0826caeeb417346251c54a0 [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 {
33 std::string path;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034 Maybe<size_t> line;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036 Source() = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038 inline Source(const StringPiece& path) : path(path.toString()) {
39 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041 inline Source(const StringPiece& path, size_t line) : path(path.toString()), line(line) {
42 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044 inline Source withLine(size_t line) const {
45 return Source(path, line);
46 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047};
48
49//
50// Implementations
51//
52
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053inline ::std::ostream& operator<<(::std::ostream& out, const Source& source) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054 out << source.path;
55 if (source.line) {
56 out << ":" << source.line.value();
57 }
58 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059}
60
Adam Lesinski1ab598f2015-08-14 14:26:04 -070061inline bool operator<(const Source& lhs, const Source& rhs) {
62 int cmp = lhs.path.compare(rhs.path);
63 if (cmp < 0) return true;
64 if (cmp > 0) return false;
65 if (lhs.line) {
66 if (rhs.line) {
67 return lhs.line.value() < rhs.line.value();
68 }
69 return false;
70 }
71 return bool(rhs.line);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070072}
73
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074} // namespace aapt
75
76#endif // AAPT_SOURCE_H