PMDK C++ bindings  1.2.0
This is the C++ bindings documentation for PMDK's libpmemobj.
p.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2017, 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 PMEMOBJ_P_HPP
39 #define PMEMOBJ_P_HPP
40 
41 #include <memory>
42 
45 
46 namespace pmem
47 {
48 
49 namespace obj
50 {
63 template <typename T>
64 class p {
65  typedef p<T> this_type;
66 
67 public:
75  p(const T &_val) noexcept : val{_val}
76  {
77  }
78 
82  p() = default;
83 
94  p &
95  operator=(const p &rhs)
96  {
97  this_type(rhs).swap(*this);
98 
99  return *this;
100  }
101 
112  template <typename Y, typename = typename std::enable_if<
113  std::is_convertible<Y, T>::value>::type>
114  p &
115  operator=(const p<Y> &rhs)
116  {
117  this_type(rhs).swap(*this);
118 
119  return *this;
120  }
121 
125  operator T() const noexcept
126  {
127  return this->val;
128  }
129 
140  T &
142  {
143  detail::conditional_add_to_tx(this);
144 
145  return this->val;
146  }
147 
155  const T &
156  get_ro() const noexcept
157  {
158  return this->val;
159  }
160 
167  void
168  swap(p &other)
169  {
170  detail::conditional_add_to_tx(this);
171  detail::conditional_add_to_tx(&other);
172  std::swap(this->val, other.val);
173  }
174 
175 private:
176  T val;
177 };
178 
185 template <class T>
186 inline void
187 swap(p<T> &a, p<T> &b)
188 {
189  a.swap(b);
190 }
191 
192 } /* namespace obj */
193 
194 } /* namespace pmem */
195 
196 #endif /* PMEMOBJ_P_HPP */
Helper template for persistent ptr specialization.
T & get_rw()
Retrieves read-write reference of the object.
Definition: p.hpp:141
Commonly used functionality.
p(const T &_val) noexcept
Value constructor.
Definition: p.hpp:75
p & operator=(const p &rhs)
Assignment operator.
Definition: p.hpp:95
const T & get_ro() const noexcept
Retrieves read-only const reference of the object.
Definition: p.hpp:156
void swap(p &other)
Swaps two p objects of the same type.
Definition: p.hpp:168
Resides on pmem class.
Definition: p.hpp:64
Definition: allocator.hpp:48
p()=default
Defaulted constructor.
p & operator=(const p< Y > &rhs)
Converting assignment operator from a different p<>.
Definition: p.hpp:115
void swap(p< T > &a, p< T > &b)
Swaps two p objects of the same type.
Definition: p.hpp:187