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