PMDK C++ bindings  1.5.2
This is the C++ bindings documentation for PMDK's libpmemobj.
slice.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018-2020, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
38 #ifndef LIBPMEMOBJ_CPP_SLICE_HPP
39 #define LIBPMEMOBJ_CPP_SLICE_HPP
40 
41 #include <iterator>
42 #include <type_traits>
43 
44 namespace pmem
45 {
46 
47 namespace obj
48 {
49 
50 namespace experimental
51 {
52 
57 template <typename Iterator>
58 class slice {
59 public:
60  using size_type = std::size_t;
61  using iterator = Iterator;
62  using reverse_iterator = std::reverse_iterator<iterator>;
63  using reference = typename std::iterator_traits<iterator>::reference;
64 
70  slice(Iterator begin, Iterator end) : it_begin(begin), it_end(end)
71  {
72  static_assert(
73  std::is_same<typename std::iterator_traits<
74  iterator>::iterator_category,
75  std::random_access_iterator_tag>::value,
76  "Iterator should have RandomAccessIterator tag");
77 
78  if (it_end < it_begin)
79  throw std::out_of_range("pmem::obj::slice");
80  }
81 
85  slice(const slice &other) noexcept = default;
86 
90  slice &operator=(const slice &other) noexcept = default;
91 
95  iterator
96  begin() const noexcept
97  {
98  return it_begin;
99  }
100 
104  iterator
105  end() const noexcept
106  {
107  return it_end;
108  }
109 
113  reverse_iterator
114  rend() const noexcept
115  {
116  return reverse_iterator(it_begin);
117  }
118 
122  reverse_iterator
123  rbegin() const noexcept
124  {
125  return reverse_iterator(it_end);
126  }
127 
133  reference
134  at(size_type idx)
135  {
136  if (idx >= size())
137  throw std::out_of_range("pmem::obj::slice");
138 
139  return it_begin[static_cast<typename std::iterator_traits<
140  Iterator>::difference_type>(idx)];
141  }
142 
147  reference operator[](size_type idx)
148  {
149  return it_begin[static_cast<typename std::iterator_traits<
150  Iterator>::difference_type>(idx)];
151  }
152 
153  size_type
154  size() const
155  {
156  return static_cast<size_type>(it_end - it_begin);
157  }
158 
159 private:
160  iterator it_begin, it_end;
161 };
162 
163 } /* namespace experimental */
164 
165 } /* namespace obj */
166 
167 } /* namespace pmem */
168 
169 #endif /* LIBPMEMOBJ_CPP_SLICE_HPP */
pmem::obj::experimental::slice::operator=
slice & operator=(const slice &other) noexcept=default
Defaulted assignment operator.
pmem::obj::experimental::slice::operator[]
reference operator[](size_type idx)
Element access operator.
Definition: slice.hpp:147
pmem::obj::experimental::slice
pmem::obj::experimental::slice - provides interface to access sequence of objects.
Definition: slice.hpp:58
pmem::obj::experimental::slice::rbegin
reverse_iterator rbegin() const noexcept
Returns reverse iterator to the beginning.
Definition: slice.hpp:123
pmem::obj::experimental::slice::slice
slice(Iterator begin, Iterator end)
Constructor taking two RandomAccess iterators which define a range.
Definition: slice.hpp:70
pmem::obj::experimental::slice::at
reference at(size_type idx)
Element access operator.
Definition: slice.hpp:134
pmem::obj::experimental::slice::slice
slice(const slice &other) noexcept=default
Defaulted copy constructor.
pmem::obj::experimental::slice::end
iterator end() const noexcept
Returns iterator to the end of the range.
Definition: slice.hpp:105
pmem::obj::experimental::slice::begin
iterator begin() const noexcept
Returns iterator to the beginning of the range.
Definition: slice.hpp:96
pmem::obj::experimental::slice::rend
reverse_iterator rend() const noexcept
Returns reverse iterator to the end.
Definition: slice.hpp:114