/ src / processor / static_map_iterator-inl.h
static_map_iterator-inl.h
  1  // Copyright 2010 Google LLC
  2  //
  3  // Redistribution and use in source and binary forms, with or without
  4  // modification, are permitted provided that the following conditions are
  5  // met:
  6  //
  7  //     * Redistributions of source code must retain the above copyright
  8  // notice, this list of conditions and the following disclaimer.
  9  //     * Redistributions in binary form must reproduce the above
 10  // copyright notice, this list of conditions and the following disclaimer
 11  // in the documentation and/or other materials provided with the
 12  // distribution.
 13  //     * Neither the name of Google LLC nor the names of its
 14  // contributors may be used to endorse or promote products derived from
 15  // this software without specific prior written permission.
 16  //
 17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 21  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 22  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 23  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 24  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 25  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 27  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28  
 29  // static_map_iterator-inl.h: StaticMapIterator implementation.
 30  //
 31  // See static_map_iterator.h for documentation.
 32  //
 33  // Author: Siyang Xie (lambxsy@google.com)
 34  
 35  #ifndef PROCESSOR_STATIC_MAP_ITERATOR_INL_H__
 36  #define PROCESSOR_STATIC_MAP_ITERATOR_INL_H__
 37  
 38  #include "processor/static_map_iterator.h"
 39  
 40  #include "processor/logging.h"
 41  
 42  namespace google_breakpad {
 43  
 44  template<typename Key, typename Value, typename Compare>
 45  StaticMapIterator<Key, Value, Compare>::StaticMapIterator(const char* base,
 46                                                              const int& index):
 47        index_(index), base_(base) {
 48    // See static_map.h for documentation on
 49    // bytes format of serialized StaticMap data.
 50    num_nodes_ = *(reinterpret_cast<const int32_t*>(base_));
 51    offsets_ = reinterpret_cast<const uint32_t*>(base_ + sizeof(num_nodes_));
 52    keys_ = reinterpret_cast<const Key*>(
 53        base_ + (1 + num_nodes_) * sizeof(num_nodes_));
 54  }
 55  
 56  // Increment & Decrement operators:
 57  template<typename Key, typename Value, typename Compare>
 58  StaticMapIterator<Key, Value, Compare>&
 59  StaticMapIterator<Key, Value, Compare>::operator++() {
 60    if (!IsValid()) {
 61      BPLOG(ERROR) << "operator++ on invalid iterator";
 62      return *this;
 63    }
 64    if (++index_ > num_nodes_) index_ = num_nodes_;
 65    return *this;
 66  }
 67  
 68  template<typename Key, typename Value, typename Compare>
 69  StaticMapIterator<Key, Value, Compare>
 70  StaticMapIterator<Key, Value, Compare>::operator++(int postfix_operator) {
 71    if (!IsValid()) {
 72      BPLOG(ERROR) << "operator++ on invalid iterator";
 73      return *this;
 74    }
 75    StaticMapIterator<Key, Value, Compare> tmp = *this;
 76    if (++index_ > num_nodes_) index_ = num_nodes_;
 77    return tmp;
 78  }
 79  
 80  template<typename Key, typename Value, typename Compare>
 81  StaticMapIterator<Key, Value, Compare>&
 82  StaticMapIterator<Key, Value, Compare>::operator--() {
 83    if (!IsValid()) {
 84      BPLOG(ERROR) << "operator++ on invalid iterator";
 85      return *this;
 86    }
 87  
 88    if (--index_ < 0) index_ = 0;
 89    return *this;
 90  }
 91  
 92  template<typename Key, typename Value, typename Compare>
 93  StaticMapIterator<Key, Value, Compare>
 94  StaticMapIterator<Key, Value, Compare>::operator--(int postfix_operator) {
 95    if (!IsValid()) {
 96      BPLOG(ERROR) << "operator++ on invalid iterator";
 97      return *this;
 98    }
 99    StaticMapIterator<Key, Value, Compare> tmp = *this;
100  
101    if (--index_ < 0) index_ = 0;
102    return tmp;
103  }
104  
105  template<typename Key, typename Value, typename Compare>
106  const Key* StaticMapIterator<Key, Value, Compare>::GetKeyPtr() const {
107    if (!IsValid()) {
108      BPLOG(ERROR) << "call GetKeyPtr() on invalid iterator";
109      return NULL;
110    }
111    return &(keys_[index_]);
112  }
113  
114  template<typename Key, typename Value, typename Compare>
115  const char* StaticMapIterator<Key, Value, Compare>::GetValueRawPtr() const {
116    if (!IsValid()) {
117      BPLOG(ERROR) << "call GetValuePtr() on invalid iterator";
118      return NULL;
119    }
120    return base_ + offsets_[index_];
121  }
122  
123  template<typename Key, typename Value, typename Compare>
124  bool StaticMapIterator<Key, Value, Compare>::operator==(
125      const StaticMapIterator<Key, Value, Compare>& x) const {
126    return base_ == x.base_ && index_ == x.index_;
127  }
128  
129  template<typename Key, typename Value, typename Compare>
130  bool StaticMapIterator<Key, Value, Compare>::operator!=(
131      const StaticMapIterator<Key, Value, Compare>& x) const {
132    // Only need to compare base_ and index_.
133    // Other data members are auxiliary.
134    return base_ != x.base_ || index_ != x.index_;
135  }
136  
137  template<typename Key, typename Value, typename Compare>
138  bool StaticMapIterator<Key, Value, Compare>::IsValid() const {
139    if (!base_ || index_ < 0 || index_ > num_nodes_)
140      return false;
141  
142    return true;
143  }
144  
145  }  // namespace google_breakpad
146  
147  #endif  // PROCESSOR_STATIC_MAP_ITERATOR_INL_H__