PMDK C++ bindings  1.8.1
This is the C++ bindings documentation for PMDK's libpmemobj.
life.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-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_DESTROYER_HPP
39 #define LIBPMEMOBJ_CPP_DESTROYER_HPP
40 
41 #include <cstddef>
42 #include <type_traits>
43 #include <utility>
44 
47 
48 namespace pmem
49 {
50 
51 namespace detail
52 {
53 
54 /*
55  * Template for checking if T is not an array.
56  */
57 template <typename T>
58 struct if_not_array {
59  typedef T type;
60 };
61 
62 /*
63  * Template for checking if T is not an array.
64  */
65 template <typename T>
66 struct if_not_array<T[]>;
67 
68 /*
69  * Template for checking if T is not an array.
70  */
71 template <typename T, size_t N>
72 struct if_not_array<T[N]>;
73 
74 /*
75  * Template for checking if T is an array.
76  */
77 template <typename T>
78 struct if_size_array;
79 
80 /*
81  * Template for checking if T is an array.
82  */
83 template <typename T>
84 struct if_size_array<T[]>;
85 
86 /*
87  * Template for checking if T is an array.
88  */
89 template <typename T, size_t N>
90 struct if_size_array<T[N]> {
91  typedef T type[N];
92 };
93 
94 /*
95  * Calls object's constructor.
96  *
97  * Supports aggregate initialization since C++17
98  */
99 template <typename T, typename... Args>
100 void
101 create(typename if_not_array<T>::type *ptr, Args &&... args)
102 {
103 #if __cpp_lib_is_aggregate
104  if constexpr (std::is_aggregate_v<T>)
105  new (static_cast<void *>(ptr)) T{std::forward<Args>(args)...};
106  else
107  new (static_cast<void *>(ptr)) T(std::forward<Args>(args)...);
108 #else
109  new (static_cast<void *>(ptr)) T(std::forward<Args>(args)...);
110 #endif
111 }
112 
113 /*
114  * Recursively calls array's elements' constructors.
115  */
116 template <typename T, typename... Args>
117 void
118 create(typename if_size_array<T>::type *ptr, Args &&... args)
119 {
120  typedef typename detail::pp_array_type<T>::type I;
121  enum { N = pp_array_elems<T>::elems };
122 
123  for (std::size_t i = 0; i < N; ++i)
124  create<I>(&(*ptr)[i], std::forward<Args>(args)...);
125 }
126 
127 /*
128  * Calls the objects constructor.
129  *
130  * Unpacks the tuple to get constructor's parameters.
131  */
132 template <typename T, size_t... Indices, typename Tuple>
133 void
134 create_from_tuple(void *ptr, index_sequence<Indices...>, Tuple tuple)
135 {
136  new (ptr) T(std::get<Indices>(std::move(tuple))...);
137 }
138 
139 /*
140  * C-style function which calls T constructor with arguments packed in a tuple.
141  *
142  * The arg is a tuple containing constructor parameters.
143  */
144 template <typename T, typename Tuple, typename... Args>
145 int
146 c_style_construct(void *ptr, void *arg)
147 {
148  auto *arg_pack = static_cast<Tuple *>(arg);
149 
150  typedef typename make_index_sequence<Args...>::type index;
151  try {
152  create_from_tuple<T>(ptr, index(), std::move(*arg_pack));
153  } catch (...) {
154  return -1;
155  }
156 
157  return 0;
158 }
159 
160 /*
161  * Calls object's destructor.
162  */
163 template <typename T,
164  typename = typename std::enable_if<!std::is_pod<T>::value>::type>
165 void
166 destroy(typename if_not_array<T>::type &arg)
167 {
168  arg.~T();
169 }
170 
171 /*
172  * Don't call destructors for POD types.
173  */
174 template <typename T, typename dummy = void,
175  typename = typename std::enable_if<std::is_pod<T>::value>::type>
176 void
177 destroy(typename if_not_array<T>::type &)
178 {
179 }
180 
181 /*
182  * Recursively calls array's elements' destructors.
183  */
184 template <typename T>
185 void
186 destroy(typename if_size_array<T>::type &arg)
187 {
188  typedef typename detail::pp_array_type<T>::type I;
189  enum { N = pp_array_elems<T>::elems };
190 
191  for (std::size_t i = 0; i < N; ++i)
192  destroy<I>(arg[N - 1 - i]);
193 }
194 
195 } /* namespace detail */
196 
197 } /* namespace pmem */
198 
199 #endif /* LIBPMEMOBJ_CPP_DESTROYER_HPP */
pmem
A persistent version of concurrent hash map implementation Ref: https://arxiv.org/abs/1509....
Definition: allocation_flag.hpp:44
integer_sequence.hpp
Create c++14 style index sequence.
array_traits.hpp
Common array traits.