PMDK C++ bindings  1.5.2
This is the C++ bindings documentation for PMDK's libpmemobj.
life.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-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_DESTROYER_HPP
39 #define LIBPMEMOBJ_CPP_DESTROYER_HPP
40 
41 #include <stddef.h>
42 #include <type_traits>
43 #include <utility>
44 
46 
47 namespace pmem
48 {
49 
50 namespace detail
51 {
52 
53 /*
54  * Template for checking if T is not an array.
55  */
56 template <typename T>
57 struct if_not_array {
58  typedef T type;
59 };
60 
61 /*
62  * Template for checking if T is not an array.
63  */
64 template <typename T>
65 struct if_not_array<T[]>;
66 
67 /*
68  * Template for checking if T is not an array.
69  */
70 template <typename T, size_t N>
71 struct if_not_array<T[N]>;
72 
73 /*
74  * Template for checking if T is an array.
75  */
76 template <typename T>
77 struct if_size_array;
78 
79 /*
80  * Template for checking if T is an array.
81  */
82 template <typename T>
83 struct if_size_array<T[]>;
84 
85 /*
86  * Template for checking if T is an array.
87  */
88 template <typename T, size_t N>
89 struct if_size_array<T[N]> {
90  typedef T type[N];
91 };
92 
93 /*
94  * Calls object's constructor.
95  *
96  * Supports aggregate initialization since C++17
97  */
98 template <typename T, typename... Args>
99 void
100 create(typename if_not_array<T>::type *ptr, Args &&... args)
101 {
102 #if __cpp_lib_is_aggregate
103  if constexpr (std::is_aggregate_v<T>)
104  new (static_cast<void *>(ptr)) T{std::forward<Args>(args)...};
105  else
106  new (static_cast<void *>(ptr)) T(std::forward<Args>(args)...);
107 #else
108  new (static_cast<void *>(ptr)) T(std::forward<Args>(args)...);
109 #endif
110 }
111 
112 /*
113  * Recursively calls array's elements' constructors.
114  */
115 template <typename T, typename... Args>
116 void
117 create(typename if_size_array<T>::type *ptr, Args &&... args)
118 {
119  typedef typename detail::pp_array_type<T>::type I;
120  enum { N = pp_array_elems<T>::elems };
121 
122  for (std::size_t i = 0; i < N; ++i)
123  create<I>(&(*ptr)[i], std::forward<Args>(args)...);
124 }
125 
126 /*
127  * Calls object's destructor.
128  */
129 template <typename T,
130  typename = typename std::enable_if<!std::is_pod<T>::value>::type>
131 void
132 destroy(typename if_not_array<T>::type &arg)
133 {
134  arg.~T();
135 }
136 
137 /*
138  * Don't call destructors for POD types.
139  */
140 template <typename T, typename dummy = void,
141  typename = typename std::enable_if<std::is_pod<T>::value>::type>
142 void
143 destroy(typename if_not_array<T>::type &)
144 {
145 }
146 
147 /*
148  * Recursively calls array's elements' destructors.
149  */
150 template <typename T>
151 void
152 destroy(typename if_size_array<T>::type &arg)
153 {
154  typedef typename detail::pp_array_type<T>::type I;
155  enum { N = pp_array_elems<T>::elems };
156 
157  for (std::size_t i = 0; i < N; ++i)
158  destroy<I>(arg[N - 1 - i]);
159 }
160 
161 } /* namespace detail */
162 
163 } /* namespace pmem */
164 
165 #endif /* LIBPMEMOBJ_CPP_DESTROYER_HPP */
array_traits.hpp
Common array traits.