DART  6.10.1
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 
44 namespace dart {
45 namespace common {
46 
47 template <typename _Tp, typename... _Args>
48 std::shared_ptr<_Tp> make_aligned_shared(_Args&&... __args);
49 
51 template <typename T, typename... Args>
52 DART_DEPRECATED(6.9)
53 std::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 
57 template <typename _Tp>
58 using aligned_vector
59  = std::vector<_Tp, dart::common::detail::aligned_allocator_cpp11<_Tp>>;
60 
61 template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>>
62 using 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 
70 template <typename _Tp>
71 using aligned_vector = std::vector<_Tp, Eigen::aligned_allocator<_Tp>>;
72 
73 template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>>
74 using 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) \
101 private: \
102  struct private_structure \
103  { \
104  explicit private_structure() \
105  { \
106  } \
107  }; \
108  \
109 public: \
110  template <typename... Args> \
111  class_name(const private_structure&, Args&&... args) \
112  : class_name(std::forward<Args>(args)...) \
113  { \
114  } \
115  template <typename... Args> \
116  static ptr_type<class_name> func_name(Args&&... args) \
117  { \
118  return creator<class_name>( \
119  private_structure{}, std::forward<Args>(args)...); \
120  }
121 
122 // Define static creator function that returns a raw pointer to the object.
123 // This static functions will be defined: create()
124 #define DART_DEFINE_RAW_OBJECT_CREATOR(class_name) \
125  template <typename... Args> \
126  static class_name* DART_RAW_PTR_CREATOR_NAME(Args&&... args) \
127  { \
128  return new class_name(std::forward<Args>(args)...); \
129  }
130 
131 // Define static creator function that returns std::shared_ptr to the object.
132 // This static functions will be defined: createShared()
133 #define DART_DEFINE_SHARED_OBJECT_CREATOR(class_name) \
134  \
135  _DART_DEFINE_OBJECT_CREATOR( \
136  class_name, \
137  DART_SHARED_PTR_CREATOR_NAME, \
138  std::shared_ptr, \
139  std::make_shared)
140 
141 // Define static creator function that returns std::shared_ptr to the object
142 // where the constructor is protected.
143 // This static functions will be defined: createShared()
144 #define DART_DEFINE_SHARED_OBJECT_CREATOR_FOR_PROTECTED_CTOR(class_name) \
145  \
146  _DART_DEFINE_OBJECT_CREATOR_FOR_PROTECTED_CTOR( \
147  class_name, \
148  DART_SHARED_PTR_CREATOR_NAME, \
149  std::shared_ptr, \
150  std::make_shared)
151 
152 // Define static creator function that returns std::shared_ptr to the object
153 // requires aligned memory allocation.
154 // This static functions will be defined: createShared()
155 #define DART_DEFINE_ALIGNED_SHARED_OBJECT_CREATOR(class_name) \
156  \
157  _DART_DEFINE_OBJECT_CREATOR( \
158  class_name, \
159  DART_SHARED_PTR_CREATOR_NAME, \
160  std::shared_ptr, \
161  ::dart::common::make_aligned_shared)
162 
163 // Define static creator function that returns std::shared_ptr to the object
164 // requires aligned memory allocation where the constructor is protected.
165 // This static functions will be defined: createShared()
166 #define DART_DEFINE_ALIGNED_SHARED_OBJECT_CREATOR_FOR_PROTECTED_CTOR( \
167  class_name) \
168  \
169  _DART_DEFINE_OBJECT_CREATOR_FOR_PROTECTED_CTOR( \
170  class_name, \
171  DART_SHARED_PTR_CREATOR_NAME, \
172  std::shared_ptr, \
173  ::dart::common::make_aligned_shared)
174 
175 // Define static creator function that returns std::unique_ptr to the object
176 #define DART_DEFINE_UNIQUE_OBJECT_CREATOR(class_name) \
177  duke \
178  _DART_DEFINE_OBJECT_CREATOR( \
179  class_name, \
180  DART_UNIQUE_PTR_CREATOR_NAME, \
181  std::unique_ptr, \
182  ::std::make_unique)
183 
184 // Define static creator function that returns std::unique_ptr to the object
185 // where the constructor is protected
186 #define DART_DEFINE_UNIQUE_OBJECT_CREATOR_FOR_PROTECTED_CTOR(class_name) \
187  \
188  _DART_DEFINE_OBJECT_CREATOR_FOR_PROTECTED_CTOR( \
189  class_name, \
190  DART_UNIQUE_PTR_CREATOR_NAME, \
191  std::unique_ptr, \
192  ::std::make_unique)
193 
194 // Define two static creator functions that returns std::unique_ptr and
195 // std::unique_ptr, respectively, to the object
196 #define DART_DEFINE_OBJECT_CREATORS(class_name) \
197  DART_DEFINE_SHARED_OBJECT_CREATOR(class_name) \
198  DART_DEFINE_UNIQUE_OBJECT_CREATOR(class_name)
199 
200 // Define two static creator functions that returns std::unique_ptr and
201 // std::unique_ptr, respectively, to the object where the constructor is
202 // protected
203 #define DART_DEFINE_OBJECT_CREATORS_FOR_PROTECTED_CTOR(X) \
204  DART_DEFINE_SHARED_OBJECT_CREATOR_FOR_PROTECTED_CTOR(X) \
205  DART_DEFINE_UNIQUE_OBJECT_CREATOR_FOR_PROTECTED_CTOR(X)
206 
207 // Define two static creator functions that returns std::unique_ptr and
208 // std::unique_ptr, respectively, to the object
209 #if DART_ENABLE_SIMD
210 # define DART_DEFINE_ALIGNED_OBJECT_CREATORS(class_name) \
211  DART_DEFINE_ALIGNED_SHARED_OBJECT_CREATOR(class_name) \
212  DART_DEFINE_UNIQUE_OBJECT_CREATOR(class_name)
213 #else
214 # define DART_DEFINE_ALIGNED_OBJECT_CREATORS(class_name) \
215  DART_DEFINE_OBJECT_CREATORS(class_name)
216 #endif
217 
218 // Define two static creator functions that returns std::unique_ptr and
219 // std::unique_ptr, respectively, to the object where the constructor is
220 // protected
221 #if DART_ENABLE_SIMD
222 # define DART_DEFINE_ALIGNED_OBJECT_CREATORS_FOR_PROTECTED_CTOR(class_name) \
223  DART_DEFINE_CREATE_ALIGNED_PTR_SHARED_FOR_PROTECTED_CTOR(class_name) \
224  DART_DEFINE_UNIQUE_OBJECT_CREATOR_FOR_PROTECTED_CTOR(class_name)
225 #else
226 # define DART_DEFINE_ALIGNED_OBJECT_CREATORS_FOR_PROTECTED_CTOR(class_name) \
227  DART_DEFINE_OBJECT_CREATORS_FOR_PROTECTED_CTOR(class_name)
228 #endif
229 
231 
232 #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:51
std::unique_ptr< T > make_unique(Args &&... args)
Definition: Memory-impl.hpp:67
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:65
Definition: SharedLibraryManager.hpp:46