PMDK C++ bindings  1.11
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 
16 namespace pmem
17 {
18 
19 namespace obj
20 {
21 
26 template <typename Iterator>
27 class slice {
28 public:
29  using size_type = std::size_t;
30  using iterator = Iterator;
31  using reverse_iterator = std::reverse_iterator<iterator>;
32  using reference = typename std::iterator_traits<iterator>::reference;
33 
39  slice(Iterator begin, Iterator end) : it_begin(begin), it_end(end)
40  {
41  static_assert(
42  std::is_same<typename std::iterator_traits<
43  iterator>::iterator_category,
44  std::random_access_iterator_tag>::value,
45  "Iterator should have RandomAccessIterator tag");
46 
47  if (it_end < it_begin)
48  throw std::out_of_range("pmem::obj::slice");
49  }
50 
54  slice(const slice &other) noexcept = default;
55 
59  slice &operator=(const slice &other) noexcept = default;
60 
64  iterator
65  begin() const noexcept
66  {
67  return it_begin;
68  }
69 
73  iterator
74  end() const noexcept
75  {
76  return it_end;
77  }
78 
82  reverse_iterator
83  rend() const noexcept
84  {
85  return reverse_iterator(it_begin);
86  }
87 
91  reverse_iterator
92  rbegin() const noexcept
93  {
94  return reverse_iterator(it_end);
95  }
96 
102  reference
103  at(size_type idx)
104  {
105  if (idx >= size())
106  throw std::out_of_range("pmem::obj::slice");
107 
108  return it_begin[static_cast<typename std::iterator_traits<
109  Iterator>::difference_type>(idx)];
110  }
111 
116  reference operator[](size_type idx)
117  {
118  return it_begin[static_cast<typename std::iterator_traits<
119  Iterator>::difference_type>(idx)];
120  }
121 
122  size_type
123  size() const
124  {
125  return static_cast<size_type>(it_end - it_begin);
126  }
127 
128 private:
129  iterator it_begin, it_end;
130 };
131 
132 } /* namespace obj */
133 
134 } /* namespace pmem */
135 
136 #endif /* LIBPMEMOBJ_CPP_SLICE_HPP */
pmem
Persistent memory namespace.
Definition: allocation_flag.hpp:15
pmem::obj::slice::rbegin
reverse_iterator rbegin() const noexcept
Returns reverse iterator to the beginning.
Definition: slice.hpp:92
pmem::obj::slice::operator=
slice & operator=(const slice &other) noexcept=default
Defaulted assignment operator.
pmem::obj::slice::operator[]
reference operator[](size_type idx)
Element access operator.
Definition: slice.hpp:116
pmem::obj::slice::slice
slice(const slice &other) noexcept=default
Defaulted copy constructor.
pmem::obj::slice
pmem::obj::slice - provides interface to access sequence of objects.
Definition: slice.hpp:27
pmem::obj::slice::slice
slice(Iterator begin, Iterator end)
Constructor taking two RandomAccess iterators which define a range.
Definition: slice.hpp:39
pmem::obj::slice::begin
iterator begin() const noexcept
Returns iterator to the beginning of the range.
Definition: slice.hpp:65
pmem::obj::slice::end
iterator end() const noexcept
Returns iterator to the end of the range.
Definition: slice.hpp:74
pmem::obj::slice::at
reference at(size_type idx)
Element access operator.
Definition: slice.hpp:103
pmem::obj::slice::rend
reverse_iterator rend() const noexcept
Returns reverse iterator to the end.
Definition: slice.hpp:83