DART 6.12.2
Loading...
Searching...
No Matches
Memory.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2021, The DART development contributors
3 * All rights reserved.
4 *
5 * The list of contributors can be found at:
6 * https://github.com/dartsim/dart/blob/master/LICENSE
7 *
8 * This file is provided under the following "BSD-style" License:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef DART_COMMON_MEMORY_HPP_
34#define DART_COMMON_MEMORY_HPP_
35
36#include <map>
37#include <memory>
38#include <vector>
39
42#include "dart/config.hpp"
43
44namespace dart {
45namespace common {
46
47template <typename _Tp, typename... _Args>
48std::shared_ptr<_Tp> make_aligned_shared(_Args&&... __args);
49
51template <typename T, typename... Args>
53std::unique_ptr<T> make_unique(Args&&... args);
54
55#if EIGEN_VERSION_AT_LEAST(3, 2, 1) && EIGEN_VERSION_AT_MOST(3, 2, 8)
56
57template <typename _Tp>
59 = std::vector<_Tp, dart::common::detail::aligned_allocator_cpp11<_Tp>>;
60
61template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>>
62using aligned_map = std::map<
63 _Key,
64 _Tp,
65 _Compare,
66 dart::common::detail::aligned_allocator_cpp11<std::pair<const _Key, _Tp>>>;
67
68#else
69
70template <typename _Tp>
71using aligned_vector = std::vector<_Tp, Eigen::aligned_allocator<_Tp>>;
72
73template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>>
74using aligned_map = std::map<
75 _Key,
76 _Tp,
77 _Compare,
78 Eigen::aligned_allocator<std::pair<const _Key, _Tp>>>;
79
80#endif
81
82} // namespace common
83} // namespace dart
84
85#define DART_RAW_PTR_CREATOR_NAME create
86#define DART_SHARED_PTR_CREATOR_NAME createShared
87#define DART_UNIQUE_PTR_CREATOR_NAME createUnique
88
89// Define static creator function that returns a smart pointer to an object
90#define _DART_DEFINE_OBJECT_CREATOR(class_name, func_name, ptr_type, creator) \
91 template <typename... Args> \
92 static ptr_type<class_name> func_name(Args&&... args) \
93 { \
94 return creator<class_name>(std::forward<Args>(args)...); \
95 }
96
97// Define static creator function that returns a smart pointer to an object with
98// protected constructor
99#define _DART_DEFINE_OBJECT_CREATOR_FOR_PROTECTED_CTOR( \
100 class_name, func_name, ptr_type, creator) \
101private: \
102 struct private_structure \
103 { \
104 explicit private_structure() {} \
105 }; \
106 \
107public: \
108 template <typename... Args> \
109 class_name(const private_structure&, Args&&... args) \
110 : class_name(std::forward<Args>(args)...) \
111 { \
112 } \
113 template <typename... Args> \
114 static ptr_type<class_name> func_name(Args&&... args) \
115 { \
116 return creator<class_name>( \
117 private_structure{}, std::forward<Args>(args)...); \
118 }
119
120// Define static creator function that returns a raw pointer to the object.
121// This static functions will be defined: create()
122#define DART_DEFINE_RAW_OBJECT_CREATOR(class_name) \
123 template <typename... Args> \
124 static class_name* DART_RAW_PTR_CREATOR_NAME(Args&&... args) \
125 { \
126 return new class_name(std::forward<Args>(args)...); \
127 }
128
129// Define static creator function that returns std::shared_ptr to the object.
130// This static functions will be defined: createShared()
131#define DART_DEFINE_SHARED_OBJECT_CREATOR(class_name) \
132 \
133 _DART_DEFINE_OBJECT_CREATOR( \
134 class_name, \
135 DART_SHARED_PTR_CREATOR_NAME, \
136 std::shared_ptr, \
137 std::make_shared)
138
139// Define static creator function that returns std::shared_ptr to the object
140// where the constructor is protected.
141// This static functions will be defined: createShared()
142#define DART_DEFINE_SHARED_OBJECT_CREATOR_FOR_PROTECTED_CTOR(class_name) \
143 \
144 _DART_DEFINE_OBJECT_CREATOR_FOR_PROTECTED_CTOR( \
145 class_name, \
146 DART_SHARED_PTR_CREATOR_NAME, \
147 std::shared_ptr, \
148 std::make_shared)
149
150// Define static creator function that returns std::shared_ptr to the object
151// requires aligned memory allocation.
152// This static functions will be defined: createShared()
153#define DART_DEFINE_ALIGNED_SHARED_OBJECT_CREATOR(class_name) \
154 \
155 _DART_DEFINE_OBJECT_CREATOR( \
156 class_name, \
157 DART_SHARED_PTR_CREATOR_NAME, \
158 std::shared_ptr, \
159 ::dart::common::make_aligned_shared)
160
161// Define static creator function that returns std::shared_ptr to the object
162// requires aligned memory allocation where the constructor is protected.
163// This static functions will be defined: createShared()
164#define DART_DEFINE_ALIGNED_SHARED_OBJECT_CREATOR_FOR_PROTECTED_CTOR( \
165 class_name) \
166 \
167 _DART_DEFINE_OBJECT_CREATOR_FOR_PROTECTED_CTOR( \
168 class_name, \
169 DART_SHARED_PTR_CREATOR_NAME, \
170 std::shared_ptr, \
171 ::dart::common::make_aligned_shared)
172
173// Define static creator function that returns std::unique_ptr to the object
174#define DART_DEFINE_UNIQUE_OBJECT_CREATOR(class_name) \
175 duke \
176 _DART_DEFINE_OBJECT_CREATOR( \
177 class_name, \
178 DART_UNIQUE_PTR_CREATOR_NAME, \
179 std::unique_ptr, \
180 ::std::make_unique)
181
182// Define static creator function that returns std::unique_ptr to the object
183// where the constructor is protected
184#define DART_DEFINE_UNIQUE_OBJECT_CREATOR_FOR_PROTECTED_CTOR(class_name) \
185 \
186 _DART_DEFINE_OBJECT_CREATOR_FOR_PROTECTED_CTOR( \
187 class_name, \
188 DART_UNIQUE_PTR_CREATOR_NAME, \
189 std::unique_ptr, \
190 ::std::make_unique)
191
192// Define two static creator functions that returns std::unique_ptr and
193// std::unique_ptr, respectively, to the object
194#define DART_DEFINE_OBJECT_CREATORS(class_name) \
195 DART_DEFINE_SHARED_OBJECT_CREATOR(class_name) \
196 DART_DEFINE_UNIQUE_OBJECT_CREATOR(class_name)
197
198// Define two static creator functions that returns std::unique_ptr and
199// std::unique_ptr, respectively, to the object where the constructor is
200// protected
201#define DART_DEFINE_OBJECT_CREATORS_FOR_PROTECTED_CTOR(X) \
202 DART_DEFINE_SHARED_OBJECT_CREATOR_FOR_PROTECTED_CTOR(X) \
203 DART_DEFINE_UNIQUE_OBJECT_CREATOR_FOR_PROTECTED_CTOR(X)
204
205// Define two static creator functions that returns std::unique_ptr and
206// std::unique_ptr, respectively, to the object
207#if DART_ENABLE_SIMD
208 #define DART_DEFINE_ALIGNED_OBJECT_CREATORS(class_name) \
209 DART_DEFINE_ALIGNED_SHARED_OBJECT_CREATOR(class_name) \
210 DART_DEFINE_UNIQUE_OBJECT_CREATOR(class_name)
211#else
212 #define DART_DEFINE_ALIGNED_OBJECT_CREATORS(class_name) \
213 DART_DEFINE_OBJECT_CREATORS(class_name)
214#endif
215
216// Define two static creator functions that returns std::unique_ptr and
217// std::unique_ptr, respectively, to the object where the constructor is
218// protected
219#if DART_ENABLE_SIMD
220 #define DART_DEFINE_ALIGNED_OBJECT_CREATORS_FOR_PROTECTED_CTOR(class_name) \
221 DART_DEFINE_CREATE_ALIGNED_PTR_SHARED_FOR_PROTECTED_CTOR(class_name) \
222 DART_DEFINE_UNIQUE_OBJECT_CREATOR_FOR_PROTECTED_CTOR(class_name)
223#else
224 #define DART_DEFINE_ALIGNED_OBJECT_CREATORS_FOR_PROTECTED_CTOR(class_name) \
225 DART_DEFINE_OBJECT_CREATORS_FOR_PROTECTED_CTOR(class_name)
226#endif
227
229
230#endif // DART_COMMON_MEMORY_HPP_
#define DART_DEPRECATED(version)
Definition Deprecated.hpp:51
std::shared_ptr< _Tp > make_aligned_shared(_Args &&... __args)
Definition Memory-impl.hpp:53
std::unique_ptr< T > make_unique(Args &&... args)
Definition Memory-impl.hpp:69
std::vector< _Tp, Eigen::aligned_allocator< _Tp > > aligned_vector
Definition Memory.hpp:71
std::map< _Key, _Tp, _Compare, Eigen::aligned_allocator< std::pair< const _Key, _Tp > > > aligned_map
Definition Memory.hpp:78
Definition BulletCollisionDetector.cpp:60
Definition SharedLibraryManager.hpp:46