PMDK C++ bindings  1.7
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 <stdexcept>
43 #include <type_traits>
44 
45 namespace pmem
46 {
47 
48 namespace obj
49 {
50 
51 namespace experimental
52 {
53 
58 template <typename Iterator>
59 class slice {
60 public:
61  using size_type = std::size_t;
62  using iterator = Iterator;
63  using reverse_iterator = std::reverse_iterator<iterator>;
64  using reference = typename std::iterator_traits<iterator>::reference;
65 
71  slice(Iterator begin, Iterator end) : it_begin(begin), it_end(end)
72  {
73  static_assert(
74  std::is_same<typename std::iterator_traits<
75  iterator>::iterator_category,
76  std::random_access_iterator_tag>::value,
77  "Iterator should have RandomAccessIterator tag");
78 
79  if (it_end < it_begin)
80  throw std::out_of_range("pmem::obj::slice");
81  }
82 
86  slice(const slice &other) noexcept = default;
87 
91  slice &operator=(const slice &other) noexcept = default;
92 
96  iterator
97  begin() const noexcept
98  {
99  return it_begin;
100  }
101 
105  iterator
106  end() const noexcept
107  {
108  return it_end;
109  }
110 
114  reverse_iterator
115  rend() const noexcept
116  {
117  return reverse_iterator(it_begin);
118  }
119 
123  reverse_iterator
124  rbegin() const noexcept
125  {
126  return reverse_iterator(it_end);
127  }
128 
134  reference
135  at(size_type idx)
136  {
137  if (idx >= size())
138  throw std::out_of_range("pmem::obj::slice");
139 
140  return it_begin[static_cast<typename std::iterator_traits<
141  Iterator>::difference_type>(idx)];
142  }
143 
148  reference operator[](size_type idx)
149  {
150  return it_begin[static_cast<typename std::iterator_traits<
151  Iterator>::difference_type>(idx)];
152  }
153 
154  size_type
155  size() const
156  {
157  return static_cast<size_type>(it_end - it_begin);
158  }
159 
160 private:
161  iterator it_begin, it_end;
162 };
163 
164 } /* namespace experimental */
165 
166 } /* namespace obj */
167 
168 } /* namespace pmem */
169 
170 #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:148
pmem::obj::experimental::slice
pmem::obj::experimental::slice - provides interface to access sequence of objects.
Definition: slice.hpp:59
pmem::obj::experimental::slice::rbegin
reverse_iterator rbegin() const noexcept
Returns reverse iterator to the beginning.
Definition: slice.hpp:124
pmem::obj::experimental::slice::slice
slice(Iterator begin, Iterator end)
Constructor taking two RandomAccess iterators which define a range.
Definition: slice.hpp:71
pmem::obj::experimental::slice::at
reference at(size_type idx)
Element access operator.
Definition: slice.hpp:135
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:106
pmem::obj::experimental::slice::begin
iterator begin() const noexcept
Returns iterator to the beginning of the range.
Definition: slice.hpp:97
pmem::obj::experimental::slice::rend
reverse_iterator rend() const noexcept
Returns reverse iterator to the end.
Definition: slice.hpp:115