source: mainline/uspace/lib/cpp/include/internal/insert_iterator.hpp@ f62f1ee

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f62f1ee was bdc55009, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: implemented an auxiliary insert iterator for replicative inserts

  • Property mode set to 100644
File size: 4.1 KB
Line 
1/*
2 * Copyright (c) 2018 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef LIBCPP_INTERNAL_INSERT_ITERATOR
30#define LIBCPP_INTERNAL_INSERT_ITERATOR
31
32namespace std
33{
34 struct forward_iterator_tag;
35}
36
37namespace std::aux
38{
39 /**
40 * Note: Commonly the data structures in this library
41 * contain a set of insert member functions. These
42 * are often both iterator based and copy based.
43 * This iterator can change the copy version into
44 * an iterator version by creating a count based copy
45 * iterator.
46 * Usage:
47 * first == insert_iterator{value}
48 * last == insert_iterator{count}
49 *
50 * So the following code:
51 * while (first != last)
52 * *data_++ = *first++;
53 *
54 * Will insert a copy of value into the data_
55 * iterator exactly count times.
56 * TODO: Apply this to existing containers?
57 */
58 template<class T>
59 class insert_iterator
60 {
61 public:
62 using difference_type = unsigned;
63 using value_type = T;
64 using iterator_category = forward_iterator_tag;
65 using reference = value_type&;
66 using pointer = value_type*;
67
68 explicit insert_iterator(const value_type& val)
69 : value_{val}, count_{}
70 { /* DUMMY BODY */ }
71
72 explicit insert_iterator(difference_type count)
73 : value_{}, count_{count}
74 { /* DUMMY BODY */ }
75
76 explicit insert_iterator(const value_type& val, difference_type count)
77 : value_{val}, count_{count}
78 { /* DUMMY BODY */ }
79
80 insert_iterator(const insert_iterator&) = default;
81 insert_iterator& operator=(const insert_iterator&) = default;
82
83 insert_iterator(insert_iterator&&) = default;
84 insert_iterator& operator=(insert_iterator&&) = default;
85
86 const value_type& operator*() const
87 {
88 return value_;
89 }
90
91 const value_type* operator->() const
92 {
93 return &value_;
94 }
95
96 insert_iterator& operator++()
97 {
98 ++count_;
99
100 return *this;
101 }
102
103 insert_iterator operator++(int)
104 {
105 ++count_;
106
107 return insert_iterator{value_, count_ - 1};
108 }
109
110 bool operator==(const insert_iterator& other)
111 {
112 return count_ == other.count_;
113 }
114
115 bool operator!=(const insert_iterator& other)
116 {
117 return count_ != other.count_;
118 }
119
120 private:
121 value_type value_;
122 difference_type count_;
123 };
124}
125
126#endif
Note: See TracBrowser for help on using the repository browser.