GL Studio C++ Runtime API
weak_reference.h
Go to the documentation of this file.
1/// \file weak_reference.h
2/// \brief Weak reference and related classes
3///
4/// \par Copyright Information
5///
6/// Copyright (c) 2016 by The DiSTI Corporation.<br>
7/// 12249 Science Drive; Suite 300<br>
8/// Orlando, Florida 32826<br>
9/// USA<br>
10/// <br>
11/// All rights reserved.<br>
12///
13/// This Software contains proprietary trade secrets of DiSTI and may not be
14/// reproduced, in whole or part, in any form, or by any means of electronic,
15/// mechanical, or otherwise, without the written permission of DiSTI. Said
16/// permission may be derived through the purchase of applicable DiSTI product
17/// licenses which detail the distribution rights of this content and any
18/// Derivative Works based on this or other copyrighted DiSTI Software.
19///
20/// NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21/// AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22/// PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23/// AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24/// IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25/// PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26///
27/// LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28/// IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29/// INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30/// DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31/// INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32/// INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33/// OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34/// EXCEED FIVE DOLLARS (US$5.00).
35///
36/// The aforementioned terms and restrictions are governed by the laws of the
37/// State of Florida and the United States of America.
38
39#ifndef INCLUDED_WEAK_REFERENCE_H
40#define INCLUDED_WEAK_REFERENCE_H
41
42#include "disti_assert.h"
43#include "scoped_ptr.h"
44
45namespace disti
46{
47class WeakReferenceable;
48
49/** Interface for holding a weak reference to an object. This class should generally not be used directly. Use WeakRef instead */
51{
52public:
53 /// \return A pointer to the object. Returns NULL if the object was deleted.
54 virtual WeakReferenceable* GetReferent() const = 0;
55
56 /// Notify this weak reference that the object destroyed.
57 virtual void NotifyReferentDestroyed() = 0;
58
59 virtual ~WeakReference() {}
60};
61
62/** Interface for getting a weak reference to an object. To implement this class, inherit from WeakReferenceableMixin (see DisplayObject as an example) */
64{
65public:
66 /// Add a weak reference to this object, that gets notified when this object is destroyed. Does not take ownership of weakRef.
67 /// \param weakRef the weak reference to add.
68 virtual void AddWeakReference( WeakReference* weakRef ) = 0;
69
70 /// Notify this object that its weak reference is being destroyed.
71 /// \param weakReference The weak reference to notify.
72 virtual void NotifyWeakReferenceDestroyed( WeakReference* weakReference ) = 0;
73
74 virtual ~WeakReferenceable() {}
75};
76
77/** helper class to simplify working with a weak reference
78 * Ex Usage:
79 * DisplayObject* obj = GetSomeDisplayObject();
80 * WeakRef< DisplayObject > weakRef = obj;
81 * ...
82 * ... some code that may or may not have deleted obj
83 * ...
84 * if( !weakRef.IsNull() )
85 * {
86 * weakRef.Get()->Location( 0, 0, 0 );
87 * }
88 */
89template<class T>
91{
92private:
93 /** Implementation for a weak reference */
94 class WeakReferenceImpl : virtual public WeakReference
95 {
96 public:
97 /** constructor
98 * \param referent a pointer to the WeakReferenceable object
99 */
100 WeakReferenceImpl( WeakReferenceable* referent )
101 : _referent( referent )
102 {
103 if( referent )
104 {
105 referent->AddWeakReference( this );
106 }
107 }
108
109 /** destructor. Notifies the WeakReferenceable that this WeakReference is being deleted */
110 ~WeakReferenceImpl()
111 {
112 if( _referent )
113 {
114 _referent->NotifyWeakReferenceDestroyed( this );
115 }
116 }
117
118 /** \see WeakReference */
119 WeakReferenceable* GetReferent() const DISTI_METHOD_OVERRIDE { return _referent; }
120
121 /** notifies the WeakReference that the WeakReferenceable object is being destroyed so it can set its referent to NULL */
122 void NotifyReferentDestroyed() DISTI_METHOD_OVERRIDE
123 {
124 _referent = NULL;
125 }
126
127 private:
128 /** pointer to the WeakReferenceable object */
129 WeakReferenceable* _referent;
130 };
131
132public:
133 /** default ctor -- initializes weak reference to NULL
134 */
136 {
137 GLS_ASSERT( IsNull() );
138 }
139
140 /** construct from a WeakReferenceable pointer
141 * \param[in] weakReferenceable pointer to an WeakReferenceable interface ( can be NULL )
142 */
143 WeakRef( WeakReferenceable* weakReferenceable )
144 {
145 SetWeakReference( weakReferenceable );
146 }
147
148 /** construct from an WeakRef< T >
149 * \param[in] w WeakRef< T > ( can be NULL )
150 */
151 WeakRef( const WeakRef& w )
152 {
153 SetWeakReference( w.GetWeakReferenceable() );
154 }
155
156 /** construct from an WeakRef< U >
157 * \param[in] w WeakRef< U > ( can be NULL )
158 */
159 template<class U>
160 WeakRef( const WeakRef<U>& w )
161 {
162 SetWeakReference( w.GetWeakReferenceable() );
163 }
164
165 /** assign this weak ref to reference the given WeakReferenceable*
166 * \param weakReferenceable WeakReferenceable* in question (can be NULL)
167 * \return a ref to this
168 */
169 WeakRef& operator=( WeakReferenceable* weakReferenceable )
170 {
171 SetWeakReference( weakReferenceable );
172 return *this;
173 }
174
175 /** assign this pointer to point to the given WeakRef< T >
176 * \param w WeakRef< T > in question (can be NULL)
177 * \return a ref to this
178 */
180 {
181 SetWeakReference( w.GetWeakReferenceable() );
182 return *this;
183 }
184
185 /** assign this pointer to point to the given WeakRef< U >
186 * \param w WeakRef< U > in question (can be NULL)
187 * \return a ref to this
188 */
189 template<class U>
191 {
192 SetWeakReference( w.GetWeakReferenceable() );
193 return *this;
194 }
195
196 /** determine if weak reference is NULL
197 * \return true if weak ref is NULL else false
198 */
199 bool IsNull() const
200 {
201 return ( _weakReference.IsNull() || NULL == _weakReference->GetReferent() );
202 }
203
204 /** Get a T*
205 * \return if referent exists and supports interface T then return a pointer to T, else return NULL
206 */
207 T* Get() const
208 {
209 T* ret = NULL;
210
211 // if there is still a weak ref, see if it has a referent that supports interface T
212 if( !IsNull() )
213 {
214 ret = dynamic_cast<T*>( _weakReference->GetReferent() );
215 }
216
217 return ret;
218 }
219
220 /** get the WeakReferenceable* contained in this weak ref
221 * \return the WeakReferenceable pointer contained in this weak ref ( can be NULL )
222 */
224 {
225 return !_weakReference.IsNull() ? _weakReference->GetReferent() : NULL;
226 }
227
228private:
229 void SetWeakReference( WeakReferenceable* weakReferenceable )
230 {
231 WeakReference* weakRef = NULL;
232 if( weakReferenceable )
233 {
234 // Important - this weak reference must be allocated in the same unit that will delete it, which is
235 // why its only allocated here, right before we store it in the object that will delete it.
236 weakRef = new WeakReferenceImpl( weakReferenceable );
237 GLS_ASSERT( NULL != weakRef->GetReferent() );
238 }
239
240 _weakReference.Reset( weakRef );
241 }
242
243 ScopedPtr<WeakReference> _weakReference; /**< weak reference else NULL */
244};
245
246} // namespace disti
247
248#endif
Definition: weak_reference.h:91
WeakRef & operator=(const WeakRef< U > &w)
Definition: weak_reference.h:190
WeakRef(WeakReferenceable *weakReferenceable)
Definition: weak_reference.h:143
T * Get() const
Definition: weak_reference.h:207
WeakRef(const WeakRef< U > &w)
Definition: weak_reference.h:160
WeakRef()
Definition: weak_reference.h:135
WeakRef & operator=(const WeakRef &w)
Definition: weak_reference.h:179
bool IsNull() const
Definition: weak_reference.h:199
WeakReferenceable * GetWeakReferenceable() const
Definition: weak_reference.h:223
WeakRef(const WeakRef &w)
Definition: weak_reference.h:151
WeakRef & operator=(WeakReferenceable *weakReferenceable)
Definition: weak_reference.h:169
Definition: weak_reference.h:51
virtual void NotifyReferentDestroyed()=0
Notify this weak reference that the object destroyed.
virtual WeakReferenceable * GetReferent() const =0
Definition: weak_reference.h:64
virtual void AddWeakReference(WeakReference *weakRef)=0
virtual void NotifyWeakReferenceDestroyed(WeakReference *weakReference)=0
Contains the DistiAssert macro.
#define GLS_ASSERT(exp)
Definition: disti_assert.h:149
#define DISTI_METHOD_OVERRIDE
Macro to wrap the override keyword, removed on compilers that don't support it.
Definition: gls_cpp_lang_support.h:221
Definition: bmpimage.h:46
A smart pointer with unique ownership – poor man's std::unique_ptr.