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