PMDK C++ bindings 1.13.0
This is the C++ bindings documentation for PMDK's libpmemobj.
Loading...
Searching...
No Matches
v.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2/* Copyright 2018-2020, Intel Corporation */
3
9#ifndef LIBPMEMOBJ_CPP_V_HPP
10#define LIBPMEMOBJ_CPP_V_HPP
11
12#include <memory>
13#include <tuple>
14
17
18namespace pmem
19{
20
21namespace obj
22{
23
24namespace experimental
25{
26
41template <typename T>
42class v {
43public:
44 static_assert(std::is_default_constructible<T>::value,
45 "Type T must be default constructible");
46
50 v() noexcept : vlt{0}
51 {
52 }
53
58 {
59 /* Destructor of val should NOT be called */
60 }
61
65 v &
66 operator=(const T &rhs)
67 {
68 /* make sure object is initialized */
69 (void)get();
70
71 val = rhs;
72
73 return *this;
74 }
75
79 v &
81 {
82 return *this = rhs.get();
83 }
84
90 template <typename Y,
91 typename = typename std::enable_if<
92 std::is_convertible<Y, T>::value>::type>
93 v &
95 {
96 return *this = rhs.get();
97 }
98
108 template <typename... Args>
109 T &
110 get(Args &&... args) noexcept
111 {
112 auto arg_pack =
113 std::forward_as_tuple(std::forward<Args>(args)...);
114
115 PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
116 if (pop == NULL)
117 return this->val;
118
119 T *value = static_cast<T *>(pmemobj_volatile(
120 pop, &this->vlt, &this->val, sizeof(T),
121 pmem::detail::c_style_construct<T, decltype(arg_pack),
122 Args...>,
123 static_cast<void *>(&arg_pack)));
124
125 return *value;
126 }
127
136 T &
138 {
139 return val;
140 }
141
145 operator T &() noexcept
146 {
147 return this->get();
148 }
149
153 void
154 swap(v &other)
155 {
156 std::swap(get(), other.get());
157 }
158
159private:
160 struct pmemvlt vlt;
161
162 /*
163 * Normally C++ requires all class members to be constructed during
164 * enclosing type construction. Holding a value inside of a union allows
165 * to bypass this requirement. val is only constructed by call to get().
166 */
167 union {
168 T val;
169 };
170};
171
178template <class T>
179inline void
181{
182 a.swap(b);
183}
184
185} /* namespace experimental */
186
187} /* namespace obj */
188
189} /* namespace pmem */
190
191#endif /* LIBPMEMOBJ_CPP_V_HPP */
Volatile residing on pmem class.
Definition: v.hpp:42
v & operator=(v< Y > &rhs)
Converting assignment operator from a different v<>.
Definition: v.hpp:94
v & operator=(v &rhs)
Assignment operator.
Definition: v.hpp:80
void swap(v &other)
Swaps two v objects of the same type.
Definition: v.hpp:154
v & operator=(const T &rhs)
Assignment operator.
Definition: v.hpp:66
v() noexcept
Defaulted constructor.
Definition: v.hpp:50
~v()
Destructor.
Definition: v.hpp:57
T & unsafe_get()
Retrieves reference to the object.
Definition: v.hpp:137
T & get(Args &&... args) noexcept
Retrieves reference to the object.
Definition: v.hpp:110
Commonly used functionality.
Functions for destroying arrays.
void swap(concurrent_map< Key, Value, Comp, Allocator > &lhs, concurrent_map< Key, Value, Comp, Allocator > &rhs)
Non-member swap.
Definition: concurrent_map.hpp:151
Persistent memory namespace.
Definition: allocation_flag.hpp:15