PMDK C++ bindings  1.2.0
This is the C++ bindings documentation for PMDK's libpmemobj.
 All Classes Files Functions Variables Typedefs Friends Pages
v.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018, 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 
69 public:
77  v(const T &_val) noexcept : vlt{0}, val{_val}
78  {
79  }
80 
84  v() noexcept : vlt{0}, val()
85  {
86  }
87 
91  v &
92  operator=(const v &rhs)
93  {
94  this_type(rhs).swap(*this);
95 
96  return *this;
97  }
98 
104  template <typename Y,
105  typename = typename std::enable_if<
106  std::is_convertible<Y, T>::value>::type>
107  v &
108  operator=(const v<Y> &rhs)
109  {
110  this_type(rhs).swap(*this);
111 
112  return *this;
113  }
114 
121  T &
122  get() noexcept
123  {
124  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
125  if (pop == NULL)
126  return this->val;
127 
128  T *value = static_cast<T *>(pmemobj_volatile(
129  pop, &this->vlt, &this->val, sizeof(T),
130  pmem::detail::instantiate_volatile_object<T>, NULL));
131 
132  return *value;
133  }
134 
138  operator T() const noexcept
139  {
140  return this->get();
141  }
142 
146  void
147  swap(v &other)
148  {
149  std::swap(this->val, other.val);
150  }
151 
152 private:
153  struct pmemvlt vlt;
154  T val;
155 };
156 
163 template <class T>
164 inline void
165 swap(v<T> &a, v<T> &b)
166 {
167  a.swap(b);
168 }
169 
170 } /* namespace experimental */
171 
172 } /* namespace obj */
173 
174 } /* namespace pmem */
175 
176 #endif /* LIBPMEMOBJ_CPP_V_HPP */
v & operator=(const v< Y > &rhs)
Converting assignment operator from a different v<>.
Definition: v.hpp:108
Implementation details of volatile variables implementation.
pmem::obj::experimental::v - volatile resides on pmem class.
Definition: v.hpp:67
Commonly used functionality.
v & operator=(const v &rhs)
Assignment operator.
Definition: v.hpp:92
v(const T &_val) noexcept
Value constructor.
Definition: v.hpp:77
Definition: allocator.hpp:48
void swap(v &other)
Swaps two v objects of the same type.
Definition: v.hpp:147
v() noexcept
Defaulted constructor.
Definition: v.hpp:84