1 /* 2 * Copyright (C) 2014 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 #pragma once 18 19 #include <androidfw/ResourceTypes.h> 20 #include <utils/ByteOrder.h> 21 22 namespace android { 23 24 struct TypeVariant { 25 explicit TypeVariant(const ResTable_type* data); 26 27 class iterator { 28 public: 29 bool operator==(const iterator& rhs) const { 30 return mTypeVariant == rhs.mTypeVariant && mIndex == rhs.mIndex; 31 } 32 33 iterator operator++(int) { 34 iterator prev = *this; 35 operator++(); 36 return prev; 37 } 38 39 const ResTable_entry* operator->() const { 40 return operator*(); 41 } 42 indexTypeVariant43 uint32_t index() const { 44 return mIndex; 45 } 46 47 iterator& operator++(); 48 const ResTable_entry* operator*() const; 49 50 private: 51 friend struct TypeVariant; 52 53 enum class Kind { Begin, End }; iteratorTypeVariant54 iterator(const TypeVariant* tv, Kind kind) 55 : mTypeVariant(tv) { 56 mSparseIndex = mIndex = (kind == Kind::Begin ? 0 : tv->mLength); 57 // mSparseIndex here is technically past the number of sparse entries, but it is still 58 // ok as it is enough to infer that this is the end iterator. 59 } 60 61 const TypeVariant* mTypeVariant; 62 uint32_t mIndex; 63 uint32_t mSparseIndex; 64 }; 65 beginEntriesTypeVariant66 iterator beginEntries() const { 67 return iterator(this, iterator::Kind::Begin); 68 } 69 endEntriesTypeVariant70 iterator endEntries() const { 71 return iterator(this, iterator::Kind::End); 72 } 73 74 const ResTable_type* data; 75 76 private: 77 // For a dense table, this is the number of the elements. 78 // For a sparse table, this is the index of the last element + 1. 79 // In both cases, it can be used for iteration as the upper loop bound as in |i < mLength|. 80 uint32_t mLength; 81 bool mSparse; 82 }; 83 84 } // namespace android 85