PMDK C++ bindings  1.9
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 
55 template <typename Iterator>
56 class slice {
57 public:
58  using size_type = std::size_t;
59  using iterator = Iterator;
60  using reverse_iterator = std::reverse_iterator<iterator>;
61  using reference = typename std::iterator_traits<iterator>::reference;
62 
68  slice(Iterator begin, Iterator end) : it_begin(begin), it_end(end)
69  {
70  static_assert(
71  std::is_same<typename std::iterator_traits<
72  iterator>::iterator_category,
73  std::random_access_iterator_tag>::value,
74  "Iterator should have RandomAccessIterator tag");
75 
76  if (it_end < it_begin)
77  throw std::out_of_range("pmem::obj::slice");
78  }
79 
83  slice(const slice &other) noexcept = default;
84 
88  slice &operator=(const slice &other) noexcept = default;
89 
93  iterator
94  begin() const noexcept
95  {
96  return it_begin;
97  }
98 
102  iterator
103  end() const noexcept
104  {
105  return it_end;
106  }
107 
111  reverse_iterator
112  rend() const noexcept
113  {
114  return reverse_iterator(it_begin);
115  }
116 
120  reverse_iterator
121  rbegin() const noexcept
122  {
123  return reverse_iterator(it_end);
124  }
125 
131  reference
132  at(size_type idx)
133  {
134  if (idx >= size())
135  throw std::out_of_range("pmem::obj::slice");
136 
137  return it_begin[static_cast<typename std::iterator_traits<
138  Iterator>::difference_type>(idx)];
139  }
140 
145  reference operator[](size_type idx)
146  {
147  return it_begin[static_cast<typename std::iterator_traits<
148  Iterator>::difference_type>(idx)];
149  }
150 
151  size_type
152  size() const
153  {
154  return static_cast<size_type>(it_end - it_begin);
155  }
156 
157 private:
158  iterator it_begin, it_end;
159 };
160 
161 } /* namespace obj */
162 
163 } /* namespace pmem */
164 
165 #endif /* LIBPMEMOBJ_CPP_SLICE_HPP */
pmem
Persistent memory namespace.
Definition: allocation_flag.hpp:44
pmem::obj::slice::rbegin
reverse_iterator rbegin() const noexcept
Returns reverse iterator to the beginning.
Definition: slice.hpp:121
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:145
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:56
pmem::obj::slice::slice
slice(Iterator begin, Iterator end)
Constructor taking two RandomAccess iterators which define a range.
Definition: slice.hpp:68
pmem::obj::slice::begin
iterator begin() const noexcept
Returns iterator to the beginning of the range.
Definition: slice.hpp:94
pmem::obj::slice::end
iterator end() const noexcept
Returns iterator to the end of the range.
Definition: slice.hpp:103
pmem::obj::slice::at
reference at(size_type idx)
Element access operator.
Definition: slice.hpp:132
pmem::obj::slice::rend
reverse_iterator rend() const noexcept
Returns reverse iterator to the end.
Definition: slice.hpp:112