Home | History | Annotate | Download | only in dex
      1 /*
      2  * Copyright (C) 2016 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 ART_LIBDEXFILE_DEX_DEX_FILE_TYPES_H_
     18 #define ART_LIBDEXFILE_DEX_DEX_FILE_TYPES_H_
     19 
     20 #include <limits>
     21 #include <ostream>
     22 
     23 namespace art {
     24 namespace dex {
     25 
     26 constexpr uint32_t kDexNoIndex = 0xFFFFFFFF;
     27 
     28 class StringIndex {
     29  public:
     30   uint32_t index_;
     31 
     32   constexpr StringIndex() : index_(std::numeric_limits<decltype(index_)>::max()) {}
     33   explicit constexpr StringIndex(uint32_t idx) : index_(idx) {}
     34 
     35   bool IsValid() const {
     36     return index_ != std::numeric_limits<decltype(index_)>::max();
     37   }
     38   static StringIndex Invalid() {
     39     return StringIndex(std::numeric_limits<decltype(index_)>::max());
     40   }
     41 
     42   bool operator==(const StringIndex& other) const {
     43     return index_ == other.index_;
     44   }
     45   bool operator!=(const StringIndex& other) const {
     46     return index_ != other.index_;
     47   }
     48   bool operator<(const StringIndex& other) const {
     49     return index_ < other.index_;
     50   }
     51   bool operator<=(const StringIndex& other) const {
     52     return index_ <= other.index_;
     53   }
     54   bool operator>(const StringIndex& other) const {
     55     return index_ > other.index_;
     56   }
     57   bool operator>=(const StringIndex& other) const {
     58     return index_ >= other.index_;
     59   }
     60 };
     61 std::ostream& operator<<(std::ostream& os, const StringIndex& index);
     62 
     63 class TypeIndex {
     64  public:
     65   uint16_t index_;
     66 
     67   constexpr TypeIndex() : index_(std::numeric_limits<decltype(index_)>::max()) {}
     68   explicit constexpr TypeIndex(uint16_t idx) : index_(idx) {}
     69 
     70   bool IsValid() const {
     71     return index_ != std::numeric_limits<decltype(index_)>::max();
     72   }
     73   static TypeIndex Invalid() {
     74     return TypeIndex(std::numeric_limits<decltype(index_)>::max());
     75   }
     76 
     77   bool operator==(const TypeIndex& other) const {
     78     return index_ == other.index_;
     79   }
     80   bool operator!=(const TypeIndex& other) const {
     81     return index_ != other.index_;
     82   }
     83   bool operator<(const TypeIndex& other) const {
     84     return index_ < other.index_;
     85   }
     86   bool operator<=(const TypeIndex& other) const {
     87     return index_ <= other.index_;
     88   }
     89   bool operator>(const TypeIndex& other) const {
     90     return index_ > other.index_;
     91   }
     92   bool operator>=(const TypeIndex& other) const {
     93     return index_ >= other.index_;
     94   }
     95 };
     96 std::ostream& operator<<(std::ostream& os, const TypeIndex& index);
     97 
     98 }  // namespace dex
     99 }  // namespace art
    100 
    101 namespace std {
    102 
    103 template<> struct hash<art::dex::StringIndex> {
    104   size_t operator()(const art::dex::StringIndex& index) const {
    105     return hash<uint32_t>()(index.index_);
    106   }
    107 };
    108 
    109 template<> struct hash<art::dex::TypeIndex> {
    110   size_t operator()(const art::dex::TypeIndex& index) const {
    111     return hash<uint16_t>()(index.index_);
    112   }
    113 };
    114 
    115 }  // namespace std
    116 
    117 #endif  // ART_LIBDEXFILE_DEX_DEX_FILE_TYPES_H_
    118