PMDK C++ bindings  1.13.0-git23.gf49772ac
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-2020, 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 
49 template <typename Iterator>
50 class slice {
51 public:
52  using size_type = std::size_t;
53  using iterator = Iterator;
54  using reverse_iterator = std::reverse_iterator<iterator>;
55  using reference = typename std::iterator_traits<iterator>::reference;
56 
63  slice(Iterator begin, Iterator end) : it_begin(begin), it_end(end)
64  {
65  static_assert(
66  std::is_pointer<Iterator>::value ||
67  (detail::has_indexing<Iterator>::value &&
68  detail::has_pre_decrement<Iterator>::value &&
69  detail::has_subtraction<Iterator>::value),
70  "Iterator should support: operator[], operator-(), operator--()");
71 
72  if (it_end - it_begin < 0)
73  throw std::out_of_range("pmem::obj::slice");
74  }
75 
79  slice(const slice &other) noexcept = default;
80 
84  slice &operator=(const slice &other) noexcept = default;
85 
89  iterator
90  begin() const noexcept
91  {
92  return it_begin;
93  }
94 
98  iterator
99  end() const noexcept
100  {
101  return it_end;
102  }
103 
107  reverse_iterator
108  rend() const noexcept
109  {
110  return reverse_iterator(it_begin);
111  }
112 
116  reverse_iterator
117  rbegin() const noexcept
118  {
119  return reverse_iterator(it_end);
120  }
121 
127  reference
128  at(size_type idx)
129  {
130  if (idx >= size())
131  throw std::out_of_range("pmem::obj::slice");
132 
133  return it_begin[static_cast<typename std::iterator_traits<
134  Iterator>::difference_type>(idx)];
135  }
136 
141  reference operator[](size_type idx)
142  {
143  return it_begin[static_cast<typename std::iterator_traits<
144  Iterator>::difference_type>(idx)];
145  }
146 
147  size_type
148  size() const
149  {
150  return static_cast<size_type>(it_end - it_begin);
151  }
152 
153 private:
154  iterator it_begin, it_end;
155 };
156 
157 } /* namespace obj */
158 
159 } /* namespace pmem */
160 
161 #endif /* LIBPMEMOBJ_CPP_SLICE_HPP */
pmem::obj::slice - provides interface to access sequence of objects.
Definition: slice.hpp:50
slice & operator=(const slice &other) noexcept=default
Defaulted assignment operator.
iterator begin() const noexcept
Returns iterator to the beginning of the range.
Definition: slice.hpp:90
reverse_iterator rbegin() const noexcept
Returns reverse iterator to the beginning.
Definition: slice.hpp:117
iterator end() const noexcept
Returns iterator to the end of the range.
Definition: slice.hpp:99
slice(const slice &other) noexcept=default
Defaulted copy constructor.
reverse_iterator rend() const noexcept
Returns reverse iterator to the end.
Definition: slice.hpp:108
reference at(size_type idx)
Element access operator.
Definition: slice.hpp:128
reference operator[](size_type idx)
Element access operator.
Definition: slice.hpp:141
slice(Iterator begin, Iterator end)
Constructor taking two iterators (iterators should support: operator[], operator-(),...
Definition: slice.hpp:63
Persistent memory namespace.
Definition: allocation_flag.hpp:15
Commonly used SFINAE helpers.