33 #ifndef PMEMOBJ_PERSISTENT_POOL_PTR_HPP
34 #define PMEMOBJ_PERSISTENT_POOL_PTR_HPP
38 #include <type_traits>
49 using namespace pmem::obj;
52 class persistent_pool_ptr {
54 friend class persistent_pool_ptr;
56 typedef persistent_pool_ptr<T> this_type;
63 typedef typename pmem::detail::sp_element<T>::type element_type;
65 persistent_pool_ptr() : off(0)
73 persistent_pool_ptr(std::nullptr_t) noexcept : off(0)
85 persistent_pool_ptr(PMEMoid oid) noexcept : off(oid.off)
97 persistent_pool_ptr(uint64_t _off) noexcept : off(_off)
108 template <
typename Y,
109 typename =
typename std::enable_if<
110 std::is_convertible<Y *, T *>::value>::type>
111 persistent_pool_ptr(
const persistent_pool_ptr<Y> &r) noexcept
123 template <
typename Y,
124 typename =
typename std::enable_if<
125 std::is_convertible<Y *, T *>::value>::type>
137 persistent_pool_ptr(
const persistent_pool_ptr &r) noexcept : off(r.off)
156 persistent_pool_ptr(persistent_pool_ptr &&r) noexcept
157 : off(std::move(r.off))
165 persistent_pool_ptr &
166 operator=(persistent_pool_ptr &&r)
168 conditional_add_to_tx(
this);
169 this->off = std::move(r.off);
174 persistent_pool_ptr &operator=(std::nullptr_t)
176 conditional_add_to_tx(
this);
192 persistent_pool_ptr &
193 operator=(
const persistent_pool_ptr &r)
195 conditional_add_to_tx(
this);
211 persistent_pool_ptr &
214 conditional_add_to_tx(
this);
215 this->off = r.raw().off;
230 persistent_pool_ptr &
231 operator=(
const PMEMoid &oid)
233 conditional_add_to_tx(
this);
249 template <
typename Y,
250 typename =
typename std::enable_if<
251 std::is_convertible<Y *, T *>::value>::type>
252 persistent_pool_ptr &
253 operator=(
const persistent_pool_ptr<Y> &r)
255 conditional_add_to_tx(
this);
272 template <
typename Y,
273 typename =
typename std::enable_if<
274 std::is_convertible<Y *, T *>::value>::type>
275 persistent_pool_ptr &
278 conditional_add_to_tx(
this);
279 this->off = r.raw().off;
292 get(uint64_t pool_uuid)
const noexcept
294 PMEMoid oid = {pool_uuid, this->off};
295 return static_cast<element_type *
>(pmemobj_direct(oid));
299 operator()(uint64_t pool_uuid)
const noexcept
301 return get(pool_uuid);
312 get_persistent_ptr(uint64_t pool_uuid)
const noexcept
314 PMEMoid oid = {pool_uuid, this->off};
322 swap(persistent_pool_ptr &other) noexcept
324 conditional_add_to_tx(
this);
325 conditional_add_to_tx(&other);
326 std::swap(this->off, other.off);
332 explicit operator bool() const noexcept
334 return this->off != 0;
345 raw_oid(uint64_t pool_uuid)
const noexcept
347 PMEMoid oid = {pool_uuid, this->off};
360 conditional_add_to_tx(
this);
367 inline persistent_pool_ptr<T> &
370 conditional_add_to_tx(
this);
371 this->off +=
sizeof(T);
379 inline persistent_pool_ptr<T>
382 persistent_pool_ptr<T> ret(*
this);
391 inline persistent_pool_ptr<T> &
394 conditional_add_to_tx(
this);
395 this->off -=
sizeof(T);
403 inline persistent_pool_ptr<T>
406 persistent_pool_ptr<T> ret(*
this);
415 inline persistent_pool_ptr<T> &
418 conditional_add_to_tx(
this);
419 this->off += s *
sizeof(T);
427 inline persistent_pool_ptr<T> &
430 conditional_add_to_tx(
this);
431 this->off -= s *
sizeof(T);
436 inline persistent_pool_ptr<T>
439 persistent_pool_ptr<T> ret(*
this);
440 ret.off += s *
sizeof(T);
445 inline persistent_pool_ptr<T>
448 persistent_pool_ptr<T> ret(*
this);
449 ret.off -= s *
sizeof(T);
461 static_assert(!std::is_polymorphic<element_type>::value,
462 "Polymorphic types are not supported");
471 template <
typename T,
typename Y>
474 const persistent_pool_ptr<Y> &rhs) noexcept
476 return lhs.raw() == rhs.raw();
482 template <
typename T,
typename Y>
485 const persistent_pool_ptr<Y> &rhs) noexcept
487 return !(lhs == rhs);
493 template <
typename T>
495 operator!=(
const persistent_pool_ptr<T> &lhs, std::nullptr_t) noexcept
497 return lhs.raw() != 0;
503 template <
typename T>
505 operator!=(std::nullptr_t,
const persistent_pool_ptr<T> &lhs) noexcept
507 return lhs.raw() != 0;
513 template <
typename T>
515 operator==(
const persistent_pool_ptr<T> &lhs, std::nullptr_t) noexcept
517 return lhs.raw() == 0;
523 template <
typename T>
525 operator==(std::nullptr_t,
const persistent_pool_ptr<T> &lhs) noexcept
527 return lhs.raw() == 0;
530 template <
class T,
class U>
531 persistent_pool_ptr<T>
532 static_persistent_pool_pointer_cast(
const persistent_pool_ptr<U> &r)
534 static_assert(std::is_convertible<T *, U *>::value,
535 "Cannot cast persistent_pool_ptr");
536 return persistent_pool_ptr<T>(r.raw());
542 #endif // PMEMOBJ_PERSISTENT_POOL_PTR_HPP