DART  6.10.1
Composite.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_COMPOSITE_HPP_
34 #define DART_COMMON_COMPOSITE_HPP_
35 
38 
39 namespace dart {
40 namespace common {
41 
52 class Composite
53 {
54 public:
57 
58  using AspectMap = std::map<std::type_index, std::unique_ptr<Aspect> >;
59  using RequiredAspectSet = std::unordered_set<std::type_index>;
60 
61  template <typename... Aspects>
63 
64  template <typename... Aspects>
66 
68  virtual ~Composite() = default;
69 
71  Composite() = default;
72 
74  // TODO(MXG): Consider making this safe by cloning Aspects into the new copy
75  Composite(const Composite&) = delete;
76 
78  Composite(Composite&&) = delete;
79 
81  Composite& operator=(const Composite&) = delete;
82 
85 
87  template <class T>
88  bool has() const;
89 
91  template <class T>
92  T* get();
93 
95  template <class T>
96  const T* get() const;
97 
101  template <class T>
102  void set(const T* aspect);
103 
107  template <class T>
108  void set(std::unique_ptr<T>&& aspect);
109 
111  template <class T, typename... Args>
112  T* createAspect(Args&&... args);
113 
115  template <class T>
116  void removeAspect();
117 
121  template <class T>
122  std::unique_ptr<T> releaseAspect();
123 
127  template <class T>
128  static constexpr bool isSpecializedFor();
129 
131  template <class T>
132  bool requiresAspect() const;
133 
137  void setCompositeState(const State& newStates);
138 
140  State getCompositeState() const;
141 
143  void copyCompositeStateTo(State& outgoingStates) const;
144 
148  void setCompositeProperties(const Properties& newProperties);
149 
152 
155  void copyCompositePropertiesTo(Properties& outgoingProperties) const;
156 
158  void duplicateAspects(const Composite* fromComposite);
159 
163  void matchAspects(const Composite* otherComposite);
164 
165 protected:
168  void addToComposite(Aspect* aspect);
169 
172  void removeFromComposite(Aspect* aspect);
173 
175  void _set(std::type_index type_idx, const Aspect* aspect);
176 
178  void _set(std::type_index type_idx, std::unique_ptr<Aspect> aspect);
179 
182 
186 };
187 
188 //==============================================================================
190 // TODO(MXG): Consider putting this functionality into the Composite class
191 // itself. Also consider allowing the user to specify arguments for the
192 // constructors of the Aspects.
193 template <class T>
194 void createAspects(T* /*comp*/);
195 
196 //==============================================================================
197 template <class T, class NextAspect, class... Aspects>
198 void createAspects(T* comp);
199 
200 } // namespace common
201 } // namespace dart
202 
204 
205 #endif // DART_COMMON_COMPOSITE_HPP_
Definition: Aspect.hpp:48
Composite is a base class that should be virtually inherited by any class that wants to be able to ma...
Definition: Composite.hpp:53
std::unordered_set< std::type_index > RequiredAspectSet
Definition: Composite.hpp:59
RequiredAspectSet mRequiredAspects
A set containing type information for Aspects which are not allowed to leave this composite.
Definition: Composite.hpp:185
State getCompositeState() const
Get the states of the aspects inside of this Composite.
Definition: Composite.cpp:161
void duplicateAspects(const Composite *fromComposite)
Give this Composite a copy of each Aspect from otherComposite.
Definition: Composite.cpp:206
static constexpr bool isSpecializedFor()
Check if this Composite is specialized for a specific type of Aspect.
Definition: Composite.hpp:131
void copyCompositeStateTo(State &outgoingStates) const
Fill outgoingStates with the states of the aspects inside this Composite.
Definition: Composite.cpp:170
std::map< std::type_index, std::unique_ptr< Aspect > > AspectMap
Definition: Composite.hpp:58
void removeAspect()
Remove an Aspect from this Composite.
Definition: Composite.hpp:102
bool requiresAspect() const
Check if this Composite requires this specific type of Aspect.
Definition: Composite.hpp:138
void _set(std::type_index type_idx, const Aspect *aspect)
Non-templated version of set(const T*)
Definition: Composite.cpp:287
T * get()
Get a certain type of Aspect from this Composite.
Definition: Composite.hpp:59
Composite & operator=(const Composite &)=delete
It is currently unsafe to copy an Composite.
Properties getCompositeProperties() const
Get the properties of the aspects inside of this Composite.
Definition: Composite.cpp:187
void addToComposite(Aspect *aspect)
Add this Aspect to the Composite.
Definition: Composite.cpp:273
void removeFromComposite(Aspect *aspect)
Remove this Aspect from the Composite.
Definition: Composite.cpp:280
Composite(Composite &&)=delete
It is currently unsafe to move an Composite.
Composite & operator=(Composite &&)=delete
It is currently unsafe to move an Composite.
Composite()=default
Default constructor.
std::unique_ptr< T > releaseAspect()
Remove an Aspect from this Composite, but return its unique_ptr instead of letting it be deleted.
Definition: Composite.hpp:115
void setCompositeProperties(const Properties &newProperties)
Set the properties of the aspects in this Composite based on the given Composite::Properties.
Definition: Composite.cpp:178
bool has() const
Check if this Composite currently has a certain type of Aspect.
Definition: Composite.hpp:52
void copyCompositePropertiesTo(Properties &outgoingProperties) const
Fill outgoingProperties with the properties of the aspects inside this Composite.
Definition: Composite.cpp:196
Composite(const Composite &)=delete
It is currently unsafe to copy an Composite.
void setCompositeState(const State &newStates)
Set the states of the aspects in this Composite based on the given Composite::State.
Definition: Composite.cpp:154
T * createAspect(Args &&... args)
Construct an Aspect inside of this Composite.
Definition: Composite.hpp:91
AspectMap mAspectMap
A map that relates the type of Aspect to its pointer.
Definition: Composite.hpp:181
virtual ~Composite()=default
Virtual destructor.
void matchAspects(const Composite *otherComposite)
Make the Aspects of this Composite match the Aspects of otherComposite.
Definition: Composite.cpp:256
void set(const T *aspect)
Make a clone of the aspect and place the clone into this Composite.
Definition: Composite.hpp:77
Definition: CompositeData.hpp:186
Definition: CompositeData.hpp:107
CompositeData< CompositeStateMap, GetState > CompositeState
Definition: CompositeData.hpp:176
CompositeData< CompositePropertiesMap, GetProperties > CompositeProperties
Definition: CompositeData.hpp:178
void createAspects(T *)
Attach an arbitrary number of Aspects to the specified Composite type.
Definition: Composite.hpp:145
Definition: BulletCollisionDetector.cpp:65