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 20#include <ostream> 21#include <string> 22 23#include "androidfw/StringPiece.h" 24 25#include "util/Maybe.h" 26 27namespace aapt { 28 29/** 30 * Represents a file on disk. Used for logging and 31 * showing errors. 32 */ 33struct Source { 34 std::string path; 35 Maybe<size_t> line; 36 37 Source() = default; 38 39 inline Source(const android::StringPiece& path) : path(path.to_string()) { // NOLINT(implicit) 40 } 41 42 inline Source(const android::StringPiece& path, size_t line) 43 : path(path.to_string()), line(line) {} 44 45 inline Source WithLine(size_t line) const { return Source(path, line); } 46}; 47 48// 49// Implementations 50// 51 52inline ::std::ostream& operator<<(::std::ostream& out, const Source& source) { 53 out << source.path; 54 if (source.line) { 55 out << ":" << source.line.value(); 56 } 57 return out; 58} 59 60inline bool operator==(const Source& lhs, const Source& rhs) { 61 return lhs.path == rhs.path && lhs.line == rhs.line; 62} 63 64inline bool operator<(const Source& lhs, const Source& rhs) { 65 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(); 71 } 72 return false; 73 } 74 return bool(rhs.line); 75} 76 77} // namespace aapt 78 79#endif // AAPT_SOURCE_H 80