TypeWrappers.h revision f6113af2d6f6eebee68d3ac510fe96d38a7a39e9
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#ifndef __TYPE_WRAPPERS_H 18#define __TYPE_WRAPPERS_H 19 20#include <androidfw/ResourceTypes.h> 21#include <utils/ByteOrder.h> 22 23namespace android { 24 25struct TypeVariant { 26 TypeVariant(const ResTable_type* data) 27 : data(data) {} 28 29 class iterator { 30 public: 31 iterator& operator=(const iterator& rhs) { 32 mTypeVariant = rhs.mTypeVariant; 33 mIndex = rhs.mIndex; 34 return *this; 35 } 36 37 bool operator==(const iterator& rhs) const { 38 return mTypeVariant == rhs.mTypeVariant && mIndex == rhs.mIndex; 39 } 40 41 bool operator!=(const iterator& rhs) const { 42 return mTypeVariant != rhs.mTypeVariant || mIndex != rhs.mIndex; 43 } 44 45 iterator operator++(int) { 46 uint32_t prevIndex = mIndex; 47 operator++(); 48 return iterator(mTypeVariant, prevIndex); 49 } 50 51 const ResTable_entry* operator->() const { 52 return operator*(); 53 } 54 55 uint32_t index() const { 56 return mIndex; 57 } 58 59 iterator& operator++(); 60 const ResTable_entry* operator*() const; 61 62 private: 63 friend struct TypeVariant; 64 iterator(const TypeVariant* tv, uint32_t index) 65 : mTypeVariant(tv), mIndex(index) {} 66 const TypeVariant* mTypeVariant; 67 uint32_t mIndex; 68 }; 69 70 iterator beginEntries() const { 71 return iterator(this, 0); 72 } 73 74 iterator endEntries() const { 75 return iterator(this, dtohl(data->entryCount)); 76 } 77 78 const ResTable_type* data; 79}; 80 81} // namespace android 82 83#endif // __TYPE_WRAPPERS_H 84