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