DART  6.7.3
SpecializedForAspect.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2019, 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_DETAIL_SPECIALIZEDFORASPECT_HPP_
34 #define DART_COMMON_DETAIL_SPECIALIZEDFORASPECT_HPP_
35 
37 
38 // This preprocessor token should only be used by the unittest that is
39 // responsible for checking that the specialized routines are being used to
40 // access specialized Aspects
41 #ifdef DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
42 bool usedSpecializedAspectAccess;
43 #endif // DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
44 
45 namespace dart {
46 namespace common {
47 
48 //==============================================================================
49 template <class SpecAspect>
51 {
52  mSpecAspectIterator = mAspectMap.insert(
53  std::make_pair<std::type_index, std::unique_ptr<Aspect>>(
54  typeid(SpecAspect), nullptr)).first;
55 }
56 
57 //==============================================================================
58 template <class SpecAspect>
59 template <class T>
61 {
62  return _has(type<T>());
63 }
64 
65 //==============================================================================
66 template <class SpecAspect>
67 template <class T>
69 {
70  return _get(type<T>());
71 }
72 
73 //==============================================================================
74 template <class SpecAspect>
75 template <class T>
77 {
78  return _get(type<T>());
79 }
80 
81 //==============================================================================
82 template <class SpecAspect>
83 template <class T>
85 {
86  _set(type<T>(), aspect);
87 }
88 
89 //==============================================================================
90 template <class SpecAspect>
91 template <class T>
92 void SpecializedForAspect<SpecAspect>::set(std::unique_ptr<T>&& aspect)
93 {
94  _set(type<T>(), std::move(aspect));
95 }
96 
97 //==============================================================================
98 template <class SpecAspect>
99 template <class T, typename ...Args>
101 {
102  return _createAspect(type<T>(), std::forward<Args>(args)...);
103 }
104 
105 //==============================================================================
106 template <class SpecAspect>
107 template <class T>
109 {
110  _removeAspect(type<T>());
111 }
112 
113 //==============================================================================
114 template <class SpecAspect>
115 template <class T>
117 {
118  return _releaseAspect(type<T>());
119 }
120 
121 //==============================================================================
122 template <class SpecAspect>
123 template <class T>
125 {
126  return _isSpecializedFor(type<T>());
127 }
128 
129 //==============================================================================
130 template <class SpecAspect>
131 template <class T>
133 {
134 #ifdef DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
135  usedSpecializedAspectAccess = true;
136 #endif // DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
137 
138  return Composite::has<T>();
139 }
140 
141 //==============================================================================
142 template <class SpecAspect>
143 bool SpecializedForAspect<SpecAspect>::_has(type<SpecAspect>) const
144 {
145 #ifdef DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
146  usedSpecializedAspectAccess = true;
147 #endif // DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
148 
149  return (mSpecAspectIterator->second.get() != nullptr);
150 }
151 
152 //==============================================================================
153 template <class SpecAspect>
154 template <class T>
156 {
157  return Composite::get<T>();
158 }
159 
160 //==============================================================================
161 template <class SpecAspect>
162 SpecAspect* SpecializedForAspect<SpecAspect>::_get(type<SpecAspect>)
163 {
164 #ifdef DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
165  usedSpecializedAspectAccess = true;
166 #endif // DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
167 
168  return static_cast<SpecAspect*>(mSpecAspectIterator->second.get());
169 }
170 
171 //==============================================================================
172 template <class SpecAspect>
173 template <class T>
175 {
176  return Composite::get<T>();
177 }
178 
179 //==============================================================================
180 template <class SpecAspect>
181 const SpecAspect* SpecializedForAspect<SpecAspect>::_get(type<SpecAspect>) const
182 {
183 #ifdef DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
184  usedSpecializedAspectAccess = true;
185 #endif // DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
186 
187  return static_cast<SpecAspect*>(mSpecAspectIterator->second.get());
188 }
189 
190 //==============================================================================
191 template <class SpecAspect>
192 template <class T>
193 void SpecializedForAspect<SpecAspect>::_set(type<T>, const T* aspect)
194 {
195  Composite::set<T>(aspect);
196 }
197 
198 //==============================================================================
199 template <class SpecAspect>
201  type<SpecAspect>, const SpecAspect* aspect)
202 {
203 #ifdef DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
204  usedSpecializedAspectAccess = true;
205 #endif // DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
206 
207  if(aspect)
208  {
209  mSpecAspectIterator->second = aspect->cloneAspect();
210  addToComposite(mSpecAspectIterator->second.get());
211  }
212  else
213  {
214  mSpecAspectIterator->second = nullptr;
215  }
216 }
217 
218 //==============================================================================
219 template <class SpecAspect>
220 template <class T>
221 void SpecializedForAspect<SpecAspect>::_set(type<T>, std::unique_ptr<T>&& aspect)
222 {
223  Composite::set<T>(std::move(aspect));
224 }
225 
226 //==============================================================================
227 template <class SpecAspect>
229  type<SpecAspect>, std::unique_ptr<SpecAspect>&& aspect)
230 {
231 #ifdef DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
232  usedSpecializedAspectAccess = true;
233 #endif // DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
234 
235  mSpecAspectIterator->second = std::move(aspect);
236  addToComposite(mSpecAspectIterator->second.get());
237 }
238 
239 //==============================================================================
240 template <class SpecAspect>
241 template <class T, typename ...Args>
243 {
244  return Composite::createAspect<T>(std::forward<Args>(args)...);
245 }
246 
247 //==============================================================================
248 template <class SpecAspect>
249 template <typename ...Args>
251  type<SpecAspect>, Args&&... args)
252 {
253 #ifdef DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
254  usedSpecializedAspectAccess = true;
255 #endif // DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
256 
257  SpecAspect* aspect = new SpecAspect(std::forward<Args>(args)...);
258  mSpecAspectIterator->second = std::unique_ptr<SpecAspect>(aspect);
259  addToComposite(aspect);
260 
261  return aspect;
262 }
263 
264 //==============================================================================
265 template <class SpecAspect>
266 template <class T>
268 {
269  Composite::removeAspect<T>();
270 }
271 
272 //==============================================================================
273 template <class SpecAspect>
275 {
276 #ifdef DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
277  usedSpecializedAspectAccess = true;
278 #endif // DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
279 
281 
282  removeFromComposite(mSpecAspectIterator->second.get());
283  mSpecAspectIterator->second = nullptr;
284 }
285 
286 //==============================================================================
287 template <class SpecAspect>
288 template <class T>
290 {
291  return Composite::releaseAspect<T>();
292 }
293 
294 //==============================================================================
295 template <class SpecAspect>
297  type<SpecAspect>)
298 {
299 #ifdef DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
300  usedSpecializedAspectAccess = true;
301 #endif // DART_UNITTEST_SPECIALIZED_ASPECT_ACCESS
302 
303  DART_COMMON_CHECK_ILLEGAL_ASPECT_ERASE(release, SpecAspect, nullptr);
304 
305  removeFromComposite(mSpecAspectIterator->second.get());
306  std::unique_ptr<SpecAspect> extraction(
307  static_cast<SpecAspect*>(mSpecAspectIterator->second.release()));
308 
309  return extraction;
310 }
311 
312 //==============================================================================
313 template <class SpecAspect>
314 template <class T>
316 {
317  return false;
318 }
319 
320 //==============================================================================
321 template <class SpecAspect>
323 {
324  return true;
325 }
326 
327 } // namespace common
328 } // namespace dart
329 
330 #endif // DART_COMMON_DETAIL_SPECIALIZEDFORASPECT_HPP_
#define DART_BLANK
Definition: NoOp.hpp:55
Declaration of the variadic template.
Definition: SpecializedForAspect.hpp:46
#define DART_COMMON_CHECK_ILLEGAL_ASPECT_ERASE(Func, T, ReturnType)
Definition: Composite.hpp:38
Definition: BulletCollisionDetector.cpp:63