PMDK C++ bindings  1.13.0-git107.g7e59f08f
This is the C++ bindings documentation for PMDK's libpmemobj.
slice.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 /* Copyright 2018-2021, Intel Corporation */
3 
9 #ifndef LIBPMEMOBJ_CPP_SLICE_HPP
10 #define LIBPMEMOBJ_CPP_SLICE_HPP
11 
12 #include <iterator>
13 #include <stdexcept>
14 #include <type_traits>
15 
17 
18 namespace pmem
19 {
20 
21 namespace detail
22 {
23 template <typename Iterator>
24 using subtraction =
25  decltype(std::declval<Iterator>() - std::declval<Iterator>());
26 template <typename Iterator>
27 using has_subtraction = supports<Iterator, subtraction>;
28 
29 template <typename Iterator>
30 using pre_decrement = decltype(std::declval<Iterator>().operator--());
31 template <typename Iterator>
32 using has_pre_decrement = supports<Iterator, pre_decrement>;
33 
34 template <typename Iterator>
35 using indexing = decltype(std::declval<Iterator>().operator[](
36  std::declval<
37  typename std::iterator_traits<Iterator>::difference_type>()));
38 template <typename Iterator>
39 using has_indexing = supports<Iterator, indexing>;
40 } /* namespace detail */
41 
42 namespace obj
43 {
44 
60 template <typename Iterator>
61 class slice {
62 public:
63  using size_type = std::size_t;
64  using iterator = Iterator;
65  using reverse_iterator = std::reverse_iterator<iterator>;
66  using reference = typename std::iterator_traits<iterator>::reference;
67 
76  slice(Iterator begin, Iterator end) : it_begin(begin), it_end(end)
77  {
78  static_assert(
79  std::is_pointer<Iterator>::value ||
80  (detail::has_indexing<Iterator>::value &&
81  detail::has_pre_decrement<Iterator>::value &&
82  detail::has_subtraction<Iterator>::value),
83  "Iterator should support: operator[], operator-(), operator--()");
84 
85  if (it_end - it_begin < 0)
86  throw std::out_of_range("pmem::obj::slice");
87  }
88 
92  slice(const slice &other) noexcept = default;
93 
97  slice &operator=(const slice &other) noexcept = default;
98 
102  iterator
103  begin() const noexcept
104  {
105  return it_begin;
106  }
107 
111  iterator
112  end() const noexcept
113  {
114  return it_end;
115  }
116 
125  reverse_iterator
126  rbegin() const noexcept
127  {
128  return reverse_iterator(it_end);
129  }
130 
138  reverse_iterator
139  rend() const noexcept
140  {
141  return reverse_iterator(it_begin);
142  }
143 
152  reference
153  at(size_type idx)
154  {
155  if (idx >= size())
156  throw std::out_of_range("pmem::obj::slice");
157 
158  return it_begin[static_cast<typename std::iterator_traits<
159  Iterator>::difference_type>(idx)];
160  }
161 
171  reference operator[](size_type idx)
172  {
173  return it_begin[static_cast<typename std::iterator_traits<
174  Iterator>::difference_type>(idx)];
175  }
176 
182  size_type
183  size() const
184  {
185  return static_cast<size_type>(it_end - it_begin);
186  }
187 
188 private:
189  iterator it_begin, it_end;
190 };
191 
192 } /* namespace obj */
193 
194 } /* namespace pmem */
195 
196 #endif /* LIBPMEMOBJ_CPP_SLICE_HPP */
Provides interface to access sequence of objects.
Definition: slice.hpp:61
slice & operator=(const slice &other) noexcept=default
Defaulted assignment operator.
iterator begin() const noexcept
Definition: slice.hpp:103
reverse_iterator rbegin() const noexcept
Returns a reverse_iterator to the last element in the range.
Definition: slice.hpp:126
iterator end() const noexcept
Definition: slice.hpp:112
slice(const slice &other) noexcept=default
Defaulted copy constructor.
reverse_iterator rend() const noexcept
Returns a reverse_iterator to the first element in the range.
Definition: slice.hpp:139
size_type size() const
Returns total number of elements within slice's range.
Definition: slice.hpp:183
reference at(size_type idx)
Access operator for a single element of slice.
Definition: slice.hpp:153
reference operator[](size_type idx)
Subscript operator, providing access to a single element of the slice.
Definition: slice.hpp:171
slice(Iterator begin, Iterator end)
Constructor taking two iterators, which define a range.
Definition: slice.hpp:76
Persistent memory namespace.
Definition: allocation_flag.hpp:15
Commonly used SFINAE helpers.