1// Copyright 2010 Google Inc. All Rights Reserved.
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 Inc. 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.h: StaticMapIterator template class declaration.
30//
31// StaticMapIterator provides increment and decrement operators to iterate
32// through a StaticMap map. It does not provide *, -> operators, user should
33// use GetKeyPtr(), GetKey(), GetValuePtr() interfaces to retrieve data or
34// pointer to data. StaticMapIterator is essentially a const_iterator.
35//
36// Author: Siyang Xie (lambxsy@google.com)
37
38
39#ifndef PROCESSOR_STATIC_MAP_ITERATOR_H__
40#define PROCESSOR_STATIC_MAP_ITERATOR_H__
41
42#include "google_breakpad/common/breakpad_types.h"
43
44namespace google_breakpad {
45
46// Forward declaration.
47template<typename Key, typename Value, typename Compare> class StaticMap;
48
49// StaticMapIterator does not support operator*() or operator->(),
50// User should use GetKey(), GetKeyPtr(), GetValuePtr() instead;
51template<typename Key, typename Value, typename Compare>
52class StaticMapIterator {
53 public:
54 // Constructors.
55 StaticMapIterator(): index_(-1), base_(NULL) { }
56
57 // Increment & Decrement operators:
58 StaticMapIterator& operator++();
59 StaticMapIterator operator++(int post_fix_operator);
60
61 StaticMapIterator& operator--();
62 StaticMapIterator operator--(int post_fix_operator);
63
64 // Interface for retrieving data / pointer to data.
65 const Key* GetKeyPtr() const;
66
67 // Run time error will occur if GetKey() is called on an invalid iterator.
68 inline const Key GetKey() const { return *GetKeyPtr(); }
69
70 // return a raw memory pointer that points to the start address of value.
71 const char* GetValueRawPtr() const;
72
73 // return a reinterpret-casted pointer to the value.
74 inline const Value* GetValuePtr() const {
75 return reinterpret_cast<const Value*>(GetValueRawPtr());
76 }
77
78 bool operator==(const StaticMapIterator& x) const;
79 bool operator!=(const StaticMapIterator& x) const;
80
81 // Check if this iterator is valid.
82 // If iterator is invalid, user is forbidden to use ++/-- operator
83 // or interfaces for retrieving data / pointer to data.
84 bool IsValid() const;
85
86 private:
87 friend class StaticMap<Key, Value, Compare>;
88
89 // Only StaticMap can call this constructor.
90 explicit StaticMapIterator(const char* base, const int32_t& index);
91
92 // Index of node that the iterator is pointing to.
93 int32_t index_;
94
95 // Beginning address of the serialized map data.
96 const char* base_;
97
98 // Number of nodes in the map. Use it to identify end() iterator.
99 int32_t num_nodes_;
100
101 // offsets_ is an array of offset addresses of mapped values.
102 // For example:
103 // address_of_i-th_node_value = base_ + offsets_[i]
104 const uint32_t* offsets_;
105
106 // keys_[i] = key of i_th node.
107 const Key* keys_;
108};
109
110} // namespace google_breakpad
111
112#endif // PROCESSOR_STATIC_MAP_ITERATOR_H__
113