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