PMDK C++ bindings  1.2.0
This is the C++ bindings documentation for PMDK's libpmemobj.
v.hpp
Go to the documentation of this file.
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 
38 #ifndef LIBPMEMOBJ_CPP_V_HPP
39 #define LIBPMEMOBJ_CPP_V_HPP
40 
41 #include <memory>
42 
45 
46 namespace pmem
47 {
48 
49 namespace obj
50 {
51 
52 namespace experimental
53 {
54 
66 template <typename T>
67 class v {
68 public:
69  static_assert(std::is_default_constructible<T>::value,
70  "Type T must be default constructible");
71 
75  v() noexcept : vlt{0}
76  {
77  }
78 
82  ~v()
83  {
84  /* Destructor of val should NOT be called */
85  }
86 
90  v &
91  operator=(const T &rhs)
92  {
93  /* make sure object is initialized */
94  (void)get();
95 
96  val = rhs;
97 
98  return *this;
99  }
100 
104  v &
105  operator=(v &rhs)
106  {
107  return *this = rhs.get();
108  }
109 
115  template <typename Y,
116  typename = typename std::enable_if<
117  std::is_convertible<Y, T>::value>::type>
118  v &
120  {
121  return *this = rhs.get();
122  }
123 
133  template <typename... Args>
134  T &
135  get(Args &&... args) noexcept
136  {
137  auto arg_pack =
138  std::forward_as_tuple(std::forward<Args>(args)...);
139 
140  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
141  if (pop == NULL)
142  return this->val;
143 
144  T *value = static_cast<T *>(pmemobj_volatile(
145  pop, &this->vlt, &this->val, sizeof(T),
146  pmem::detail::c_style_construct<T, decltype(arg_pack),
147  Args...>,
148  static_cast<void *>(&arg_pack)));
149 
150  return *value;
151  }
152 
161  T &
163  {
164  return val;
165  }
166 
170  operator T &() noexcept
171  {
172  return this->get();
173  }
174 
178  void
179  swap(v &other)
180  {
181  std::swap(get(), other.get());
182  }
183 
184 private:
185  struct pmemvlt vlt;
186 
187  /*
188  * Normally C++ requires all class members to be constructed during
189  * enclosing type construction. Holding a value inside of a union allows
190  * to bypass this requirement. val is only constructed by call to get().
191  */
192  union {
193  T val;
194  };
195 };
196 
203 template <class T>
204 inline void
205 swap(v<T> &a, v<T> &b)
206 {
207  a.swap(b);
208 }
209 
210 } /* namespace experimental */
211 
212 } /* namespace obj */
213 
214 } /* namespace pmem */
215 
216 #endif /* LIBPMEMOBJ_CPP_V_HPP */
v & operator=(const T &rhs)
Assignment operator.
Definition: v.hpp:91
T & unsafe_get()
Retrieves reference to the object.
Definition: v.hpp:162
T & get(Args &&... args) noexcept
Retrieves reference to the object.
Definition: v.hpp:135
pmem::obj::experimental::v - volatile resides on pmem class.
Definition: v.hpp:67
Functions for destroying arrays.
Commonly used functionality.
v & operator=(v< Y > &rhs)
Converting assignment operator from a different v<>.
Definition: v.hpp:119
v & operator=(v &rhs)
Assignment operator.
Definition: v.hpp:105
~v()
Destructor.
Definition: v.hpp:82
Definition: allocation_flag.hpp:43
void swap(v &other)
Swaps two v objects of the same type.
Definition: v.hpp:179
v() noexcept
Defaulted constructor.
Definition: v.hpp:75