blob: 319528e0ea1b53c108b6f74ae1fa816b88bea6e4 [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 Lesinskia5870652015-11-20 15:32:30 -080061inline bool operator==(const Source& lhs, const Source& rhs) {
62 return lhs.path == rhs.path && lhs.line == rhs.line;
63}
64
Adam Lesinski1ab598f2015-08-14 14:26:04 -070065inline 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 Lesinskia1ad4a82015-06-08 11:41:09 -070076}
77
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078} // namespace aapt
79
80#endif // AAPT_SOURCE_H