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