PMDK C++ bindings  1.9
This is the C++ bindings documentation for PMDK's libpmemobj.
persistent_pool_ptr.hpp
1 /*
2  * Copyright 2018-2019, 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 
33 #ifndef PMEMOBJ_PERSISTENT_POOL_PTR_HPP
34 #define PMEMOBJ_PERSISTENT_POOL_PTR_HPP
35 
36 #include <cassert>
37 #include <cstddef>
38 #include <type_traits>
39 
42 
43 namespace pmem
44 {
45 namespace detail
46 {
47 
48 template <typename T>
49 class persistent_pool_ptr {
50  template <typename Y>
51  friend class persistent_pool_ptr;
52 
53  typedef persistent_pool_ptr<T> this_type;
54 
55 public:
60  typedef typename pmem::detail::sp_element<T>::type element_type;
61 
62  persistent_pool_ptr() : off(0)
63  {
64  verify_type();
65  }
66 
70  persistent_pool_ptr(std::nullptr_t) noexcept : off(0)
71  {
72  verify_type();
73  }
74 
82  persistent_pool_ptr(PMEMoid oid) noexcept : off(oid.off)
83  {
84  verify_type();
85  }
86 
94  persistent_pool_ptr(uint64_t _off) noexcept : off(_off)
95  {
96  verify_type();
97  }
98 
105  template <typename Y,
106  typename = typename std::enable_if<
107  std::is_convertible<Y *, T *>::value>::type>
108  persistent_pool_ptr(const persistent_pool_ptr<Y> &r) noexcept
109  : off(r.off)
110  {
111  verify_type();
112  }
113 
120  template <typename Y,
121  typename = typename std::enable_if<
122  std::is_convertible<Y *, T *>::value>::type>
123  persistent_pool_ptr(const pmem::obj::persistent_ptr<Y> &r) noexcept
124  : off(r.raw().off)
125  {
126  verify_type();
127  }
128 
129  /*
130  * Copy constructor.
131  *
132  * @param r Persistent pool pointer to the same type.
133  */
134  persistent_pool_ptr(const persistent_pool_ptr &r) noexcept : off(r.off)
135  {
136  verify_type();
137  }
138 
139  /*
140  * Copy constructor from a persistent_ptr.
141  *
142  * @param r Persistent pointer to the same type.
143  */
144  persistent_pool_ptr(const pmem::obj::persistent_ptr<T> &r) noexcept
145  : off(r.raw().off)
146  {
147  verify_type();
148  }
149 
153  persistent_pool_ptr(persistent_pool_ptr &&r) noexcept
154  : off(std::move(r.off))
155  {
156  verify_type();
157  }
158 
162  persistent_pool_ptr &
163  operator=(persistent_pool_ptr &&r)
164  {
165  conditional_add_to_tx(this);
166  this->off = std::move(r.off);
167 
168  return *this;
169  }
170 
171  persistent_pool_ptr &operator=(std::nullptr_t)
172  {
173  conditional_add_to_tx(this);
174  this->off = 0;
175 
176  return *this;
177  }
178 
189  persistent_pool_ptr &
190  operator=(const persistent_pool_ptr &r)
191  {
192  conditional_add_to_tx(this);
193  this->off = r.off;
194 
195  return *this;
196  }
197 
208  persistent_pool_ptr &
209  operator=(const pmem::obj::persistent_ptr<T> &r)
210  {
211  conditional_add_to_tx(this);
212  this->off = r.raw().off;
213 
214  return *this;
215  }
216 
227  persistent_pool_ptr &
228  operator=(const PMEMoid &oid)
229  {
230  conditional_add_to_tx(this);
231  this->off = oid.off;
232  return *this;
233  }
234 
246  template <typename Y,
247  typename = typename std::enable_if<
248  std::is_convertible<Y *, T *>::value>::type>
249  persistent_pool_ptr &
250  operator=(const persistent_pool_ptr<Y> &r)
251  {
252  conditional_add_to_tx(this);
253  this->off = r.off;
254 
255  return *this;
256  }
257 
269  template <typename Y,
270  typename = typename std::enable_if<
271  std::is_convertible<Y *, T *>::value>::type>
272  persistent_pool_ptr &
273  operator=(const pmem::obj::persistent_ptr<Y> &r)
274  {
275  conditional_add_to_tx(this);
276  this->off = r.raw().off;
277 
278  return *this;
279  }
280 
288  element_type *
289  get(uint64_t pool_uuid) const noexcept
290  {
291  PMEMoid oid = {pool_uuid, this->off};
292  return static_cast<element_type *>(pmemobj_direct(oid));
293  }
294 
295  element_type *
296  operator()(uint64_t pool_uuid) const noexcept
297  {
298  return get(pool_uuid);
299  }
300 
309  get_persistent_ptr(uint64_t pool_uuid) const noexcept
310  {
311  PMEMoid oid = {pool_uuid, this->off};
312  return pmem::obj::persistent_ptr<T>(oid);
313  }
314 
318  void
319  swap(persistent_pool_ptr &other)
320  {
321  conditional_add_to_tx(this);
322  conditional_add_to_tx(&other);
323  std::swap(this->off, other.off);
324  }
325 
326  /*
327  * Bool conversion operator.
328  */
329  explicit operator bool() const noexcept
330  {
331  return this->off != 0;
332  }
333 
341  PMEMoid
342  raw_oid(uint64_t pool_uuid) const noexcept
343  {
344  PMEMoid oid = {pool_uuid, this->off};
345  return oid;
346  }
347 
348  const uint64_t &
349  raw() const noexcept
350  {
351  return this->off;
352  }
353 
354  uint64_t &
355  raw()
356  {
357  conditional_add_to_tx(this);
358  return this->off;
359  }
360 
364  inline persistent_pool_ptr<T> &
365  operator++()
366  {
367  conditional_add_to_tx(this);
368  this->off += sizeof(T);
369 
370  return *this;
371  }
372 
376  inline persistent_pool_ptr<T>
377  operator++(int)
378  {
379  persistent_pool_ptr<T> ret(*this);
380  ++(*this);
381 
382  return ret;
383  }
384 
388  inline persistent_pool_ptr<T> &
389  operator--()
390  {
391  conditional_add_to_tx(this);
392  this->off -= sizeof(T);
393 
394  return *this;
395  }
396 
400  inline persistent_pool_ptr<T>
401  operator--(int)
402  {
403  persistent_pool_ptr<T> ret(*this);
404  --(*this);
405 
406  return ret;
407  }
408 
412  inline persistent_pool_ptr<T> &
413  operator+=(std::ptrdiff_t s)
414  {
415  conditional_add_to_tx(this);
416  this->off += s * sizeof(T);
417 
418  return *this;
419  }
420 
424  inline persistent_pool_ptr<T> &
425  operator-=(std::ptrdiff_t s)
426  {
427  conditional_add_to_tx(this);
428  this->off -= s * sizeof(T);
429 
430  return *this;
431  }
432 
433  inline persistent_pool_ptr<T>
434  operator+(std::ptrdiff_t s)
435  {
436  persistent_pool_ptr<T> ret(*this);
437  ret.off += s * sizeof(T);
438 
439  return ret;
440  }
441 
442  inline persistent_pool_ptr<T>
443  operator-(std::ptrdiff_t s)
444  {
445  persistent_pool_ptr<T> ret(*this);
446  ret.off -= s * sizeof(T);
447 
448  return ret;
449  }
450 
451 private:
452  /* offset of persistent object in a persistent memory pool*/
453  uint64_t off;
454 
455  void
456  verify_type()
457  {
458  static_assert(!std::is_polymorphic<element_type>::value,
459  "Polymorphic types are not supported");
460  }
461 };
462 
468 template <typename T, typename Y>
469 inline bool
470 operator==(const persistent_pool_ptr<T> &lhs,
471  const persistent_pool_ptr<Y> &rhs) noexcept
472 {
473  return lhs.raw() == rhs.raw();
474 }
475 
479 template <typename T, typename Y>
480 inline bool
481 operator!=(const persistent_pool_ptr<T> &lhs,
482  const persistent_pool_ptr<Y> &rhs) noexcept
483 {
484  return !(lhs == rhs);
485 }
486 
490 template <typename T>
491 inline bool
492 operator!=(const persistent_pool_ptr<T> &lhs, std::nullptr_t) noexcept
493 {
494  return lhs.raw() != 0;
495 }
496 
500 template <typename T>
501 inline bool
502 operator!=(std::nullptr_t, const persistent_pool_ptr<T> &lhs) noexcept
503 {
504  return lhs.raw() != 0;
505 }
506 
510 template <typename T>
511 inline bool
512 operator==(const persistent_pool_ptr<T> &lhs, std::nullptr_t) noexcept
513 {
514  return lhs.raw() == 0;
515 }
516 
520 template <typename T>
521 inline bool
522 operator==(std::nullptr_t, const persistent_pool_ptr<T> &lhs) noexcept
523 {
524  return lhs.raw() == 0;
525 }
526 
527 template <class T, class U>
528 persistent_pool_ptr<T>
529 static_persistent_pool_pointer_cast(const persistent_pool_ptr<U> &r)
530 {
531  static_assert(std::is_convertible<T *, U *>::value,
532  "Cannot cast persistent_pool_ptr");
533  return persistent_pool_ptr<T>(r.raw());
534 }
535 
536 } // namespace detail
537 } // namespace pmem
538 
539 #endif // PMEMOBJ_PERSISTENT_POOL_PTR_HPP
pmem::obj::operator+
persistent_ptr< T > operator+(persistent_ptr< T > const &lhs, std::ptrdiff_t s)
Addition operator for persistent pointers.
Definition: persistent_ptr.hpp:868
pmem
Persistent memory namespace.
Definition: allocation_flag.hpp:44
pmem::detail::operator!=
bool operator!=(const persistent_pool_ptr< T > &lhs, const persistent_pool_ptr< Y > &rhs) noexcept
Inequality operator.
Definition: persistent_pool_ptr.hpp:481
pmem::obj::operator++
p< T > & operator++(p< T > &pp)
Prefix increment operator overload.
Definition: pext.hpp:77
pmem::detail::operator==
bool operator==(const persistent_pool_ptr< T > &lhs, const persistent_pool_ptr< Y > &rhs) noexcept
Equality operator.
Definition: persistent_pool_ptr.hpp:470
pmem::obj::operator-
persistent_ptr< T > operator-(persistent_ptr< T > const &lhs, std::ptrdiff_t s)
Subtraction operator for persistent pointers.
Definition: persistent_ptr.hpp:882
pmem::obj::swap
void swap(pmem::obj::array< T, N > &lhs, pmem::obj::array< T, N > &rhs)
Non-member swap function.
Definition: array.hpp:913
pmem::obj::operator+=
p< T > & operator+=(p< T > &lhs, const p< Y > &rhs)
Addition assignment operator overload.
Definition: pext.hpp:123
pmem::obj::operator-=
p< T > & operator-=(p< T > &lhs, const p< Y > &rhs)
Subtraction assignment operator overload.
Definition: pext.hpp:145
specialization.hpp
Helper template for persistent ptr specialization.
pmem::obj::persistent_ptr
Persistent pointer class.
Definition: persistent_ptr.hpp:212
pmem::obj::get
T & get(pmem::obj::array< T, N > &a)
Non-member get function.
Definition: array.hpp:923
persistent_ptr.hpp
Persistent smart pointer.
pmem::obj::operator--
p< T > & operator--(p< T > &pp)
Prefix decrement operator overload.
Definition: pext.hpp:88