33 #ifndef PMEMOBJ_PERSISTENT_POOL_PTR_HPP
34 #define PMEMOBJ_PERSISTENT_POOL_PTR_HPP
38 #include <type_traits>
49 class persistent_pool_ptr {
51 friend class persistent_pool_ptr;
53 typedef persistent_pool_ptr<T> this_type;
60 typedef typename pmem::detail::sp_element<T>::type element_type;
62 persistent_pool_ptr() : off(0)
70 persistent_pool_ptr(std::nullptr_t) noexcept : off(0)
82 persistent_pool_ptr(PMEMoid oid) noexcept : off(oid.off)
94 persistent_pool_ptr(uint64_t _off) noexcept : off(_off)
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
120 template <
typename Y,
121 typename =
typename std::enable_if<
122 std::is_convertible<Y *, T *>::value>::type>
134 persistent_pool_ptr(
const persistent_pool_ptr &r) noexcept : off(r.off)
153 persistent_pool_ptr(persistent_pool_ptr &&r) noexcept
154 : off(std::move(r.off))
162 persistent_pool_ptr &
163 operator=(persistent_pool_ptr &&r)
165 conditional_add_to_tx(
this);
166 this->off = std::move(r.off);
171 persistent_pool_ptr &operator=(std::nullptr_t)
173 conditional_add_to_tx(
this);
189 persistent_pool_ptr &
190 operator=(
const persistent_pool_ptr &r)
192 conditional_add_to_tx(
this);
208 persistent_pool_ptr &
211 conditional_add_to_tx(
this);
212 this->off = r.raw().off;
227 persistent_pool_ptr &
228 operator=(
const PMEMoid &oid)
230 conditional_add_to_tx(
this);
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)
252 conditional_add_to_tx(
this);
269 template <
typename Y,
270 typename =
typename std::enable_if<
271 std::is_convertible<Y *, T *>::value>::type>
272 persistent_pool_ptr &
275 conditional_add_to_tx(
this);
276 this->off = r.raw().off;
289 get(uint64_t pool_uuid)
const noexcept
291 PMEMoid oid = {pool_uuid, this->off};
292 return static_cast<element_type *
>(pmemobj_direct(oid));
296 operator()(uint64_t pool_uuid)
const noexcept
298 return get(pool_uuid);
309 get_persistent_ptr(uint64_t pool_uuid)
const noexcept
311 PMEMoid oid = {pool_uuid, this->off};
319 swap(persistent_pool_ptr &other)
321 conditional_add_to_tx(
this);
322 conditional_add_to_tx(&other);
329 explicit operator bool() const noexcept
331 return this->off != 0;
342 raw_oid(uint64_t pool_uuid)
const noexcept
344 PMEMoid oid = {pool_uuid, this->off};
357 conditional_add_to_tx(
this);
364 inline persistent_pool_ptr<T> &
367 conditional_add_to_tx(
this);
368 this->off +=
sizeof(T);
376 inline persistent_pool_ptr<T>
379 persistent_pool_ptr<T> ret(*
this);
388 inline persistent_pool_ptr<T> &
391 conditional_add_to_tx(
this);
392 this->off -=
sizeof(T);
400 inline persistent_pool_ptr<T>
403 persistent_pool_ptr<T> ret(*
this);
412 inline persistent_pool_ptr<T> &
415 conditional_add_to_tx(
this);
416 this->off += s *
sizeof(T);
424 inline persistent_pool_ptr<T> &
427 conditional_add_to_tx(
this);
428 this->off -= s *
sizeof(T);
433 inline persistent_pool_ptr<T>
436 persistent_pool_ptr<T> ret(*
this);
437 ret.off += s *
sizeof(T);
442 inline persistent_pool_ptr<T>
445 persistent_pool_ptr<T> ret(*
this);
446 ret.off -= s *
sizeof(T);
458 static_assert(!std::is_polymorphic<element_type>::value,
459 "Polymorphic types are not supported");
468 template <
typename T,
typename Y>
471 const persistent_pool_ptr<Y> &rhs) noexcept
473 return lhs.raw() == rhs.raw();
479 template <
typename T,
typename Y>
482 const persistent_pool_ptr<Y> &rhs) noexcept
484 return !(lhs == rhs);
490 template <
typename T>
492 operator!=(
const persistent_pool_ptr<T> &lhs, std::nullptr_t) noexcept
494 return lhs.raw() != 0;
500 template <
typename T>
502 operator!=(std::nullptr_t,
const persistent_pool_ptr<T> &lhs) noexcept
504 return lhs.raw() != 0;
510 template <
typename T>
512 operator==(
const persistent_pool_ptr<T> &lhs, std::nullptr_t) noexcept
514 return lhs.raw() == 0;
520 template <
typename T>
522 operator==(std::nullptr_t,
const persistent_pool_ptr<T> &lhs) noexcept
524 return lhs.raw() == 0;
527 template <
class T,
class U>
528 persistent_pool_ptr<T>
529 static_persistent_pool_pointer_cast(
const persistent_pool_ptr<U> &r)
531 static_assert(std::is_convertible<T *, U *>::value,
532 "Cannot cast persistent_pool_ptr");
533 return persistent_pool_ptr<T>(r.raw());
539 #endif // PMEMOBJ_PERSISTENT_POOL_PTR_HPP