PMDK C++ bindings  1.13.0-git107.g7e59f08f
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-2021, 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 
42 template <typename T>
43 class v {
44 public:
45  static_assert(std::is_default_constructible<T>::value,
46  "Type T must be default constructible");
47 
51  v() noexcept : vlt{0}
52  {
53  }
54 
58  ~v()
59  {
60  /* Destructor of val should NOT be called */
61  }
62 
66  v &
67  operator=(const T &rhs)
68  {
69  /* make sure object is initialized */
70  (void)get();
71 
72  val = rhs;
73 
74  return *this;
75  }
76 
80  v &
81  operator=(v &rhs)
82  {
83  return *this = rhs.get();
84  }
85 
91  template <typename Y,
92  typename = typename std::enable_if<
93  std::is_convertible<Y, T>::value>::type>
94  v &
96  {
97  return *this = rhs.get();
98  }
99 
109  template <typename... Args>
110  T &
111  get(Args &&... args) noexcept
112  {
113  auto arg_pack =
114  std::forward_as_tuple(std::forward<Args>(args)...);
115 
116  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
117  if (pop == NULL)
118  return this->val;
119 
120  T *value = static_cast<T *>(pmemobj_volatile(
121  pop, &this->vlt, &this->val, sizeof(T),
122  pmem::detail::c_style_construct<T, decltype(arg_pack),
123  Args...>,
124  static_cast<void *>(&arg_pack)));
125 
126  return *value;
127  }
128 
137  T &
139  {
140  return val;
141  }
142 
146  operator T &() noexcept
147  {
148  return this->get();
149  }
150 
154  void
155  swap(v &other)
156  {
157  std::swap(get(), other.get());
158  }
159 
160 private:
161  struct pmemvlt vlt;
162 
163  /*
164  * Normally C++ requires all class members to be constructed during
165  * enclosing type construction. Holding a value inside of a union allows
166  * to bypass this requirement. val is only constructed by call to get().
167  */
168  union {
169  T val;
170  };
171 };
172 
181 template <class T>
182 inline void
183 swap(v<T> &a, v<T> &b)
184 {
185  a.swap(b);
186 }
187 
188 } /* namespace experimental */
189 
190 } /* namespace obj */
191 
192 } /* namespace pmem */
193 
194 #endif /* LIBPMEMOBJ_CPP_V_HPP */
Volatile residing on pmem class.
Definition: v.hpp:43
v & operator=(const T &rhs)
Assignment operator.
Definition: v.hpp:67
T & unsafe_get()
Retrieves reference to the object.
Definition: v.hpp:138
v & operator=(v &rhs)
Assignment operator.
Definition: v.hpp:81
void swap(v &other)
Swaps two v objects of the same type.
Definition: v.hpp:155
v() noexcept
Defaulted constructor.
Definition: v.hpp:51
void swap(v< T > &a, v< T > &b)
Swaps two v objects of the same type.
Definition: v.hpp:183
T & get(Args &&... args) noexcept
Retrieves reference to the object.
Definition: v.hpp:111
v & operator=(v< Y > &rhs)
Converting assignment operator from a different v<>.
Definition: v.hpp:95
~v()
Destructor.
Definition: v.hpp:58
Commonly used functionality.
Functions for lifetime management.
Persistent memory namespace.
Definition: allocation_flag.hpp:15