libintrovirt v0.57.4
IntroVirt introspection library
Loading...
Searching...
No Matches
array_iterator.hh
Go to the documentation of this file.
1/*
2 * Copyright 2021 Assured Information Security, Inc.
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#pragma once
17
20
21#include <cassert>
22#include <memory>
23
24namespace introvirt {
25namespace windows {
26namespace nt {
27
34template <typename _T, typename _Container, bool _Const = false>
36 public:
37 using iterator_category = std::random_access_iterator_tag;
38 using difference_type = int32_t;
39 using value_type = _T;
40
41 /* deduce const qualifier from bool _Const parameter */
42 using reference = typename std::conditional_t<_Const, const _T&, _T&>;
43 using pointer = typename std::conditional_t<_Const, const _T*, _T*>;
44
45 public:
46 inline reference operator*() const { return container_[index_]; }
47 inline pointer operator->() const { return &(container_[index_]); }
48
49 inline bool operator==(const array_iterator<_T, _Container, _Const>& other) const {
50 return index_ == other.index_;
51 }
52 inline bool operator!=(const array_iterator<_T, _Container, _Const>& other) const {
53 return !(operator==(other));
54 }
55
56 reference operator[](difference_type offset) const { return container_[index_ + offset]; }
57
59 introvirt_assert((index_ + offset) <= container_->size(), "");
60 index_ += offset;
61 return *this;
62 }
63
65 auto result = *this;
66 result += offset;
67 return result;
68 }
69
71 introvirt_assert((index_ - offset) >= 0, "");
72 index_ -= offset;
73 return *this;
74 }
75
77 auto result = *this;
78 result += offset;
79 return result;
80 }
81
82 // Prefix increment operator
84 // Check if we're already at the end
85 introvirt_assert(index_ < container_.length(), "");
86 ++index_;
87 return *this;
88 }
89
90 // Postfix increment operator
92 auto copy = *this;
93 operator++();
94 return copy;
95 }
96
97 // Prefix decrement operator
99 // Check if we're at the start
100 introvirt_assert(index_ > 0, "");
101 --index_;
102 return *this;
103 }
104
105 // Postfix decrement operator
107 auto copy = *this;
108 operator--();
109 return copy;
110 }
111
112 uint32_t index() const { return index_; }
113
114 array_iterator(const _Container& container, uint32_t index)
115 : container_(container), index_(index) {}
116
117 // null/end constructor
118 array_iterator(const _Container& container)
119 : container_(container), index_(container.length()) {}
120
121 // Copy constructor to convert from iterator to const_iterator
122 template <bool Const_ = _Const, class = std::enable_if_t<Const_>>
124 : container_(src.container), index_(src.index_) {}
125
129
130 private:
131 const _Container& container_;
132 uint32_t index_;
133};
134
135} // namespace nt
136} // namespace windows
137} // namespace introvirt
Iterator helper for _INFORMATION types that have a fixed array result.
Definition array_iterator.hh:35
pointer operator->() const
Definition array_iterator.hh:47
array_iterator operator--(int)
Definition array_iterator.hh:106
reference operator[](difference_type offset) const
Definition array_iterator.hh:56
reference operator*() const
Definition array_iterator.hh:46
array_iterator(const array_iterator< _T, _Container, false > &src)
Definition array_iterator.hh:123
array_iterator & operator--()
Definition array_iterator.hh:98
uint32_t index() const
Definition array_iterator.hh:112
bool operator==(const array_iterator< _T, _Container, _Const > &other) const
Definition array_iterator.hh:49
bool operator!=(const array_iterator< _T, _Container, _Const > &other) const
Definition array_iterator.hh:52
array_iterator operator++(int)
Definition array_iterator.hh:91
array_iterator & operator-=(difference_type offset)
Definition array_iterator.hh:70
_T value_type
Definition array_iterator.hh:39
array_iterator & operator++()
Definition array_iterator.hh:83
typename std::conditional_t< _Const, const _T *, _T * > pointer
Definition array_iterator.hh:43
array_iterator< _T, _Container, _Const > & operator=(const array_iterator< _T, _Container, _Const > &)=default
array_iterator(const _Container &container)
Definition array_iterator.hh:118
array_iterator(const _Container &container, uint32_t index)
Definition array_iterator.hh:114
int32_t difference_type
Definition array_iterator.hh:38
std::random_access_iterator_tag iterator_category
Definition array_iterator.hh:37
array_iterator & operator+=(difference_type offset)
Definition array_iterator.hh:58
array_iterator operator+(difference_type offset) const
Definition array_iterator.hh:64
typename std::conditional_t< _Const, const _T &, _T & > reference
Definition array_iterator.hh:42
array_iterator operator-(difference_type offset) const
Definition array_iterator.hh:76
Type-safe guest virtual address pointer and guest_ptr template.
#define introvirt_assert(condition, msg)
Definition introvirt_assert.hh:32
Core IntroVirt classes.
Definition Cr0.hh:20