Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2012 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_RUNTIME_SAFE_MAP_H_
     18 #define ART_RUNTIME_SAFE_MAP_H_
     19 
     20 #include <map>
     21 #include <memory>
     22 
     23 #include "base/allocator.h"
     24 #include "base/logging.h"
     25 
     26 namespace art {
     27 
     28 // Equivalent to std::map, but without operator[] and its bug-prone semantics (in particular,
     29 // the implicit insertion of a default-constructed value on failed lookups).
     30 template <typename K, typename V, typename Comparator = std::less<K>,
     31           typename Allocator = TrackingAllocator<std::pair<const K, V>, kAllocatorTagSafeMap>>
     32 class SafeMap {
     33  private:
     34   typedef SafeMap<K, V, Comparator, Allocator> Self;
     35 
     36  public:
     37   typedef typename ::std::map<K, V, Comparator, Allocator>::key_compare key_compare;
     38   typedef typename ::std::map<K, V, Comparator, Allocator>::value_compare value_compare;
     39   typedef typename ::std::map<K, V, Comparator, Allocator>::allocator_type allocator_type;
     40   typedef typename ::std::map<K, V, Comparator, Allocator>::iterator iterator;
     41   typedef typename ::std::map<K, V, Comparator, Allocator>::const_iterator const_iterator;
     42   typedef typename ::std::map<K, V, Comparator, Allocator>::size_type size_type;
     43   typedef typename ::std::map<K, V, Comparator, Allocator>::key_type key_type;
     44   typedef typename ::std::map<K, V, Comparator, Allocator>::value_type value_type;
     45 
     46   SafeMap() = default;
     47   SafeMap(const SafeMap&) = default;
     48   explicit SafeMap(const key_compare& cmp, const allocator_type& allocator = allocator_type())
     49     : map_(cmp, allocator) {
     50   }
     51 
     52   Self& operator=(const Self& rhs) {
     53     map_ = rhs.map_;
     54     return *this;
     55   }
     56 
     57   allocator_type get_allocator() const { return map_.get_allocator(); }
     58   key_compare key_comp() const { return map_.key_comp(); }
     59   value_compare value_comp() const { return map_.value_comp(); }
     60 
     61   iterator begin() { return map_.begin(); }
     62   const_iterator begin() const { return map_.begin(); }
     63   iterator end() { return map_.end(); }
     64   const_iterator end() const { return map_.end(); }
     65 
     66   bool empty() const { return map_.empty(); }
     67   size_type size() const { return map_.size(); }
     68 
     69   void swap(Self& other) { map_.swap(other.map_); }
     70   void clear() { map_.clear(); }
     71   iterator erase(iterator it) { return map_.erase(it); }
     72   size_type erase(const K& k) { return map_.erase(k); }
     73 
     74   iterator find(const K& k) { return map_.find(k); }
     75   const_iterator find(const K& k) const { return map_.find(k); }
     76 
     77   iterator lower_bound(const K& k) { return map_.lower_bound(k); }
     78   const_iterator lower_bound(const K& k) const { return map_.lower_bound(k); }
     79 
     80   size_type count(const K& k) const { return map_.count(k); }
     81 
     82   // Note that unlike std::map's operator[], this doesn't return a reference to the value.
     83   V Get(const K& k) const {
     84     const_iterator it = map_.find(k);
     85     DCHECK(it != map_.end());
     86     return it->second;
     87   }
     88 
     89   // Used to insert a new mapping.
     90   iterator Put(const K& k, const V& v) {
     91     std::pair<iterator, bool> result = map_.emplace(k, v);
     92     DCHECK(result.second);  // Check we didn't accidentally overwrite an existing value.
     93     return result.first;
     94   }
     95 
     96   // Used to insert a new mapping at a known position for better performance.
     97   iterator PutBefore(iterator pos, const K& k, const V& v) {
     98     // Check that we're using the correct position and the key is not in the map.
     99     DCHECK(pos == map_.end() || map_.key_comp()(k, pos->first));
    100     DCHECK(pos == map_.begin() || map_.key_comp()((--iterator(pos))->first, k));
    101     return map_.emplace_hint(pos, k, v);
    102   }
    103 
    104   // Used to insert a new mapping or overwrite an existing mapping. Note that if the value type
    105   // of this container is a pointer, any overwritten pointer will be lost and if this container
    106   // was the owner, you have a leak.
    107   void Overwrite(const K& k, const V& v) {
    108     std::pair<iterator, bool> result = map_.insert(std::make_pair(k, v));
    109     if (!result.second) {
    110       // Already there - update the value for the existing key
    111       result.first->second = v;
    112     }
    113   }
    114 
    115   bool Equals(const Self& rhs) const {
    116     return map_ == rhs.map_;
    117   }
    118 
    119  private:
    120   ::std::map<K, V, Comparator, Allocator> map_;
    121 };
    122 
    123 template <typename K, typename V, typename Comparator, typename Allocator>
    124 bool operator==(const SafeMap<K, V, Comparator, Allocator>& lhs,
    125                 const SafeMap<K, V, Comparator, Allocator>& rhs) {
    126   return lhs.Equals(rhs);
    127 }
    128 
    129 template <typename K, typename V, typename Comparator, typename Allocator>
    130 bool operator!=(const SafeMap<K, V, Comparator, Allocator>& lhs,
    131                 const SafeMap<K, V, Comparator, Allocator>& rhs) {
    132   return !(lhs == rhs);
    133 }
    134 
    135 template<class Key, class T, AllocatorTag kTag, class Compare = std::less<Key>>
    136 class AllocationTrackingSafeMap : public SafeMap<
    137     Key, T, Compare, TrackingAllocator<std::pair<Key, T>, kTag>> {
    138 };
    139 
    140 }  // namespace art
    141 
    142 #endif  // ART_RUNTIME_SAFE_MAP_H_
    143