Home | History | Annotate | Download | only in vintf
      1 /*
      2  * Copyright (C) 2017 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 
     18 #ifndef ANDROID_VINTF_MAP_VALUE_ITERATOR_H
     19 #define ANDROID_VINTF_MAP_VALUE_ITERATOR_H
     20 
     21 #include <iterator>
     22 #include <map>
     23 
     24 namespace android {
     25 namespace vintf {
     26 
     27 template<typename Map>
     28 struct MapIterTypes {
     29     using K = typename Map::key_type;
     30     using V = typename Map::mapped_type;
     31 
     32     // Iterator over all values of a Map
     33     template<bool is_const>
     34     struct IteratorImpl : public std::iterator <
     35             std::bidirectional_iterator_tag, /* Category */
     36             V,
     37             ptrdiff_t, /* Distance */
     38             typename std::conditional<is_const, const V *, V *>::type /* Pointer */,
     39             typename std::conditional<is_const, const V &, V &>::type /* Reference */
     40         >
     41     {
     42         using traits = std::iterator_traits<IteratorImpl>;
     43         using ptr_type = typename traits::pointer;
     44         using ref_type = typename traits::reference;
     45         using diff_type = typename traits::difference_type;
     46 
     47         using map_iter = typename std::conditional<is_const,
     48                 typename Map::const_iterator, typename Map::iterator>::type;
     49 
     50         IteratorImpl(map_iter i) : mIter(i) {}
     51 
     52         inline IteratorImpl &operator++()    {
     53             mIter++;
     54             return *this;
     55         };
     56         inline IteratorImpl  operator++(int) {
     57             IteratorImpl i = *this;
     58             mIter++;
     59             return i;
     60         }
     61         inline IteratorImpl &operator--()    {
     62             mIter--;
     63             return *this;
     64         }
     65         inline IteratorImpl  operator--(int) {
     66             IteratorImpl i = *this;
     67             mIter--;
     68             return i;
     69         }
     70         inline ref_type operator*() const  { return mIter->second; }
     71         inline ptr_type operator->() const { return &(mIter->second); }
     72         inline bool operator==(const IteratorImpl &rhs) const { return mIter == rhs.mIter; }
     73         inline bool operator!=(const IteratorImpl &rhs) const { return mIter != rhs.mIter; }
     74 
     75     private:
     76         map_iter mIter;
     77     };
     78 
     79     using ValueIterator = IteratorImpl<false>;
     80     using ConstValueIterator = IteratorImpl<true>;
     81 
     82     template<bool is_const>
     83     struct IterableImpl {
     84         using map_ref = typename std::conditional<is_const, const Map &, Map &>::type;
     85         IterableImpl(map_ref map) : mMap(map) {}
     86 
     87         IteratorImpl<is_const> begin() const {
     88             return IteratorImpl<is_const>(mMap.begin());
     89         }
     90 
     91         IteratorImpl<is_const> end() const {
     92             return IteratorImpl<is_const>(mMap.end());
     93         }
     94 
     95     private:
     96         map_ref mMap;
     97     };
     98 
     99     using ValueIterable = IterableImpl<false>;
    100     using ConstValueIterable = IterableImpl<true>;
    101 };
    102 
    103 template<typename K, typename V>
    104 using ConstMapValueIterable = typename MapIterTypes<std::map<K, V>>::ConstValueIterable;
    105 template<typename K, typename V>
    106 using ConstMultiMapValueIterable = typename MapIterTypes<std::multimap<K, V>>::ConstValueIterable;
    107 
    108 template<typename K, typename V>
    109 ConstMapValueIterable<K, V> iterateValues(const std::map<K, V> &map) {
    110     return map;
    111 }
    112 template<typename K, typename V>
    113 ConstMultiMapValueIterable<K, V> iterateValues(const std::multimap<K, V> &map) {
    114     return map;
    115 }
    116 
    117 } // namespace vintf
    118 } // namespace android
    119 
    120 #endif // ANDROID_VINTF_MAP_VALUE_ITERATOR_H
    121