libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
iterator.hh
Go to the documentation of this file.
1/*
2 * libkeepass - KeePass key database importer/exporter
3 * Copyright (C) 2014 Christian Kindahl
4 * Copyright (C) 2024 Dominik Krümpelmann
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
22#pragma once
23#include <exception>
24#include <iterator>
25
26namespace keepass {
27
36template <typename C> class bounds_checked_iterator {
37protected:
38 typename C::iterator first_;
39 typename C::iterator last_;
40
41public:
42 using iterator_category = std::output_iterator_tag;
43 using value_type = void;
44 using difference_type = void;
45 using pointer = void;
46 using reference = void;
47
52 explicit bounds_checked_iterator(C& container)
53 : first_(container.begin()), last_(container.end()) {}
54
56 bounds_checked_iterator& operator=(const typename C::value_type& value) {
57 if (first_ == last_)
58 throw std::out_of_range("assigning outside container limits.");
59
60 *first_ = value;
61 return *this;
62 }
63
65 bounds_checked_iterator& operator=(typename C::value_type&& value) {
66 if (first_ == last_)
67 throw std::out_of_range("assigning outside container limits.");
68
69 *first_ = std::move(value);
70 return *this;
71 }
72
73 bounds_checked_iterator& operator*() { return *this; }
74
75 bounds_checked_iterator& operator++() {
76 first_++;
77 return *this;
78 }
79
80 bounds_checked_iterator operator++(int) {
81 ++first_;
82 return *this;
83 }
84};
85
92template <typename C> inline bounds_checked_iterator<C> bounds_checked(C& container) {
93 return bounds_checked_iterator<C>(container);
94}
95
96} // namespace keepass
An output iterator that throws on writing past the end of a container.
Definition iterator.hh:36
bounds_checked_iterator & operator=(typename C::value_type &&value)
Move-assigns a value through the iterator, throwing if the end is exceeded.
Definition iterator.hh:65
bounds_checked_iterator & operator=(const typename C::value_type &value)
Assigns a value through the iterator, throwing if the end is exceeded.
Definition iterator.hh:56
bounds_checked_iterator(C &container)
Definition iterator.hh:52
bounds_checked_iterator< C > bounds_checked(C &container)
Convenience factory that creates a bounds-checked iterator.
Definition iterator.hh:92