DART  6.7.3
NodePtr.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_DYNAMICS_DETAIL_NODEPTR_HPP_
34 #define DART_DYNAMICS_DETAIL_NODEPTR_HPP_
35 
37 
38 namespace dart {
39 namespace dynamics {
40 
41 class NodeDestructor;
42 
48 template <class NodeT, class BodyNodeT>
50 {
51 public:
52 
53  template<class, class> friend class TemplateNodePtr;
54 
55  using element_type = NodeT;
56 
58  TemplateNodePtr() : mNode(nullptr) { }
59 
62  TemplateNodePtr(NodeT* _ptr) { set(_ptr); }
63 
65  template <class OtherNodeT, class OtherBodyNodeT>
68  {
69  set(_ptr.get());
70  }
71 
74  {
75  set(_ptr);
76  return *this;
77  }
78 
80  template <class OtherNodeT, class OtherBodyNodeT>
83  {
84  set(_ptr.get());
85  return *this;
86  }
87 
89  operator NodeT*() const { return get(); }
90 
92  NodeT& operator*() const { return *get(); }
93 
95  NodeT* operator->() const { return get(); }
96 
98  NodeT* get() const
99  {
100  return mNode;
101  }
102 
104  void set(NodeT* _ptr)
105  {
106  if(nullptr == _ptr)
107  {
108  mBodyNodePtr = nullptr;
109  mDestructor = nullptr;
110  mNode = nullptr;
111  return;
112  }
113 
114  mBodyNodePtr = _ptr->getBodyNodePtr();
115  mDestructor = _ptr->mDestructor.lock();
116  mNode = _ptr;
117  }
118 
119 protected:
120 
122  NodeT* mNode;
123 
126  std::shared_ptr<NodeDestructor> mDestructor;
127 
131 };
132 
138 template <class NodeT, class BodyNodeT>
140 {
141 public:
142 
143  template <class, class> friend class TemplateWeakNodePtr;
144 
146  TemplateWeakNodePtr() : mNode(nullptr) { }
147 
150  TemplateWeakNodePtr(NodeT* _ptr) { set(_ptr); }
151 
153  template <class OtherNodeT, class OtherBodyNodeT>
156  {
157  set(_weakPtr);
158  }
159 
161  template <class OtherNodeT, class OtherBodyNodeT>
164  {
165  set(_strongPtr.get());
166  }
167 
170  {
171  set(_ptr);
172  return *this;
173  }
174 
176  template <class OtherNodeT, class OtherBodyNodeT>
179  {
180  set(_weakPtr);
181  return *this;
182  }
183 
185  template <class OtherNodeT, class OtherBodyNodeT>
188  {
189  set(_strongPtr.get());
190  return *this;
191  }
192 
197  {
199  if(nullptr == bodyNode)
200  return nullptr;
201 
202  std::shared_ptr<NodeDestructor> destructor = mWeakDestructor.lock();
203  if(nullptr == destructor)
204  return nullptr;
205 
207  }
208 
210  void set(NodeT* _ptr)
211  {
212  if(nullptr == _ptr)
213  {
214  mNode = nullptr;
215  mWeakDestructor.reset();
216  mWeakBodyNodePtr = nullptr;
217  return;
218  }
219 
220  mWeakBodyNodePtr = _ptr->getBodyNodePtr();
221  mWeakDestructor = _ptr->getOrCreateDestructor();
222  mNode = _ptr;
223  }
224 
226  template <class OtherNodeT, class OtherBodyNodeT>
228  {
229  mNode = _weakPtr.mNode;
230  mWeakDestructor = _weakPtr.mWeakDestructor;
232  }
233 
234 protected:
235 
237  NodeT* mNode;
238 
240  std::weak_ptr<NodeDestructor> mWeakDestructor;
241 
244 };
245 
246 
247 
248 } // namespace dynamics
249 } // namespace dart
250 
251 #endif // DART_DYNAMICS_DETAIL_NODEPTR_HPP_
TemplateBodyNodePtr is a templated class that enables users to create a reference-counting BodyNodePt...
Definition: BodyNodePtr.hpp:111
TemplateNodePtr is a templated class that enables users to create a strong reference-counting NodePtr...
Definition: NodePtr.hpp:50
TemplateBodyNodePtr< BodyNodeT > mBodyNodePtr
Hold onto a BodyNodePtr to the Node's associated BodyNode to make sure that the BodyNode stays alive.
Definition: NodePtr.hpp:130
TemplateNodePtr(const TemplateNodePtr< OtherNodeT, OtherBodyNodeT > &_ptr)
Constructor that takes in a strong NodePtr.
Definition: NodePtr.hpp:66
TemplateNodePtr()
Default constructor.
Definition: NodePtr.hpp:58
NodeT element_type
Definition: NodePtr.hpp:55
std::shared_ptr< NodeDestructor > mDestructor
Hold onto a shared_ptr to the Node's Destructor to make sure the Node stays alive.
Definition: NodePtr.hpp:126
TemplateNodePtr(NodeT *_ptr)
Typical constructor.
Definition: NodePtr.hpp:62
NodeT * operator->() const
Dereferencing operation.
Definition: NodePtr.hpp:95
NodeT * get() const
Get the raw Node pointer.
Definition: NodePtr.hpp:98
NodeT & operator*() const
Dereferencing operator.
Definition: NodePtr.hpp:92
NodeT * mNode
Node that this NodePtr refers to.
Definition: NodePtr.hpp:122
void set(NodeT *_ptr)
Set the Node for this NodePtr.
Definition: NodePtr.hpp:104
TemplateNodePtr & operator=(NodeT *_ptr)
Assignment operator.
Definition: NodePtr.hpp:73
TemplateWeakBodyNodePtr is a templated class that enables users to create a non-reference-holding Wea...
Definition: BodyNodePtr.hpp:199
TemplateWeakNodePtr is a templated class that enables users to create a weak non-reference-holding We...
Definition: NodePtr.hpp:140
NodeT * mNode
Node that this pointer references.
Definition: NodePtr.hpp:237
std::weak_ptr< NodeDestructor > mWeakDestructor
Destructor for the Node.
Definition: NodePtr.hpp:240
TemplateWeakNodePtr()
Default constructor.
Definition: NodePtr.hpp:146
TemplateWeakNodePtr(NodeT *_ptr)
Typical constructor.
Definition: NodePtr.hpp:150
TemplateWeakNodePtr(const TemplateNodePtr< OtherNodeT, OtherBodyNodeT > &_strongPtr)
Constructor that takes in a strong NodePtr.
Definition: NodePtr.hpp:162
void set(const TemplateWeakNodePtr< OtherNodeT, OtherBodyNodeT > &_weakPtr)
Set the Node for this WeakNodePtr based on another WeakNodePtr.
Definition: NodePtr.hpp:227
TemplateNodePtr< NodeT, BodyNodeT > lock() const
Locks the Node reference to ensure that the referenced Node is currently still available.
Definition: NodePtr.hpp:196
void set(NodeT *_ptr)
Set the Node for this WeakNodePtr.
Definition: NodePtr.hpp:210
TemplateWeakNodePtr & operator=(NodeT *_ptr)
Assignment operator for raw Node pointers.
Definition: NodePtr.hpp:169
TemplateWeakBodyNodePtr< BodyNodeT > mWeakBodyNodePtr
Pointer to the BodyNode that the Node is attached to.
Definition: NodePtr.hpp:243
TemplateWeakNodePtr(const TemplateWeakNodePtr< OtherNodeT, OtherBodyNodeT > &_weakPtr)
Constructor that takes in a WeakNodePtr.
Definition: NodePtr.hpp:154
Definition: BulletCollisionDetector.cpp:63