PMDK C++ bindings  1.10
This is the C++ bindings documentation for PMDK's libpmemobj.
v.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 /* Copyright 2018-2019, Intel Corporation */
3 
9 #ifndef LIBPMEMOBJ_CPP_V_HPP
10 #define LIBPMEMOBJ_CPP_V_HPP
11 
12 #include <memory>
13 #include <tuple>
14 
17 
18 namespace pmem
19 {
20 
21 namespace obj
22 {
23 
24 namespace experimental
25 {
26 
38 template <typename T>
39 class v {
40 public:
41  static_assert(std::is_default_constructible<T>::value,
42  "Type T must be default constructible");
43 
47  v() noexcept : vlt{0}
48  {
49  }
50 
54  ~v()
55  {
56  /* Destructor of val should NOT be called */
57  }
58 
62  v &
63  operator=(const T &rhs)
64  {
65  /* make sure object is initialized */
66  (void)get();
67 
68  val = rhs;
69 
70  return *this;
71  }
72 
76  v &
77  operator=(v &rhs)
78  {
79  return *this = rhs.get();
80  }
81 
87  template <typename Y,
88  typename = typename std::enable_if<
89  std::is_convertible<Y, T>::value>::type>
90  v &
92  {
93  return *this = rhs.get();
94  }
95 
105  template <typename... Args>
106  T &
107  get(Args &&... args) noexcept
108  {
109  auto arg_pack =
110  std::forward_as_tuple(std::forward<Args>(args)...);
111 
112  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
113  if (pop == NULL)
114  return this->val;
115 
116  T *value = static_cast<T *>(pmemobj_volatile(
117  pop, &this->vlt, &this->val, sizeof(T),
118  pmem::detail::c_style_construct<T, decltype(arg_pack),
119  Args...>,
120  static_cast<void *>(&arg_pack)));
121 
122  return *value;
123  }
124 
133  T &
135  {
136  return val;
137  }
138 
142  operator T &() noexcept
143  {
144  return this->get();
145  }
146 
150  void
151  swap(v &other)
152  {
153  std::swap(get(), other.get());
154  }
155 
156 private:
157  struct pmemvlt vlt;
158 
159  /*
160  * Normally C++ requires all class members to be constructed during
161  * enclosing type construction. Holding a value inside of a union allows
162  * to bypass this requirement. val is only constructed by call to get().
163  */
164  union {
165  T val;
166  };
167 };
168 
175 template <class T>
176 inline void
177 swap(v<T> &a, v<T> &b)
178 {
179  a.swap(b);
180 }
181 
182 } /* namespace experimental */
183 
184 } /* namespace obj */
185 
186 } /* namespace pmem */
187 
188 #endif /* LIBPMEMOBJ_CPP_V_HPP */
pmem::obj::experimental::v::swap
void swap(v &other)
Swaps two v objects of the same type.
Definition: v.hpp:151
pmem
Persistent memory namespace.
Definition: allocation_flag.hpp:15
pmem::obj::experimental::v::operator=
v & operator=(const T &rhs)
Assignment operator.
Definition: v.hpp:63
pmem::obj::experimental::v::unsafe_get
T & unsafe_get()
Retrieves reference to the object.
Definition: v.hpp:134
common.hpp
Commonly used functionality.
pmem::obj::experimental::swap
void swap(v< T > &a, v< T > &b)
Swaps two v objects of the same type.
Definition: v.hpp:177
pmem::obj::experimental::v::get
T & get(Args &&... args) noexcept
Retrieves reference to the object.
Definition: v.hpp:107
pmem::obj::experimental::v::operator=
v & operator=(v< Y > &rhs)
Converting assignment operator from a different v<>.
Definition: v.hpp:91
pmem::obj::experimental::v::~v
~v()
Destructor.
Definition: v.hpp:54
life.hpp
Functions for destroying arrays.
pmem::obj::experimental::v
pmem::obj::experimental::v - volatile resides on pmem class.
Definition: v.hpp:39
pmem::obj::experimental::v::v
v() noexcept
Defaulted constructor.
Definition: v.hpp:47
pmem::obj::experimental::v::operator=
v & operator=(v &rhs)
Assignment operator.
Definition: v.hpp:77