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