| GL Studio C++ Runtime API

weak_referenceable_mixin.h
Go to the documentation of this file.
1/*! \file
2 \brief weak reference and related classes
3
4 \par Copyright Information
5
6 Copyright (c) 2016 by The DiSTI Corporation.<br>
7 11301 Corporate Blvd; Suite 100<br>
8 Orlando, Florida 32817<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*/
40#ifndef INCLUDED_WEAK_REFERENCEABLE_MIXIN_H
41#define INCLUDED_WEAK_REFERENCEABLE_MIXIN_H
42
43#include "dynamic_array.h"
44#include "gls_cpp_lang_support.h"
45#include "weak_reference.h"
46
47 namespace disti
48{
49/** utility helper mixin class for simplifying implementing WeakReferenceable.
50 * Classes that want to implement WeakReferenceable can simply inherit this class
51 */
52 class WeakReferenceableMixin : virtual public WeakReferenceable
53{
54 public:
55 void AddWeakReference( WeakReference* weakRef ) DISTI_METHOD_OVERRIDE
56 {
57 // reduce storage by only allocating the dynamic array when asked for a weak reference
58 if( !_weakRefs )
59 {
60 _weakRefs = new DynamicArray<WeakReference*>();
61 }
62
63 // store the weak reference so we can notify it when we are deleted
64 _weakRefs->PushBack( weakRef );
65 }
66
67 void NotifyWeakReferenceDestroyed( WeakReference* ref ) DISTI_METHOD_OVERRIDE
68 {
69 // When a weak reference is deleted, remove it from our list so that we don't call a method on a deleted object
70 // in our destructor
71 if( _weakRefs )
72 {
73 for( unsigned int index = 0u; index < _weakRefs->Count(); ++index )
74 {
75 if( ref == ( *_weakRefs )[ index ] )
76 {
77 _weakRefs->EraseAt( index );
78 return;
79 }
80 }
81 }
82 }
83
84 protected:
85 /** constructor */
86 WeakReferenceableMixin()
87 : _weakRefs( NULL )
88 {
89 }
90
91 /** destructor. Notifies all weak references that this object is being deleted */
92 virtual ~WeakReferenceableMixin()
93 {
94 if( _weakRefs )
95 {
96 for( unsigned int index = 0u; index < _weakRefs->Count(); ++index )
97 {
98 ( *_weakRefs )[ index ]->NotifyReferentDestroyed();
99 }
100
101 delete _weakRefs;
102 _weakRefs = NULL;
103 }
104 }
105
106 /** pointer to array of weak refs. Allocated on the first call to GetWeakReference() */
107 DynamicArray<WeakReference*>* _weakRefs;
108};
109
110} // namespace disti
111
112#endif
disti::DynamicArray
Definition: dynamic_array.h:79
disti::WeakReference
Definition: weak_reference.h:52
disti::WeakReferenceableMixin
Definition: weak_referenceable_mixin.h:53
disti::WeakReferenceableMixin::~WeakReferenceableMixin
virtual ~WeakReferenceableMixin()
Definition: weak_referenceable_mixin.h:92
disti::WeakReferenceableMixin::_weakRefs
DynamicArray< WeakReference * > * _weakRefs
Definition: weak_referenceable_mixin.h:107
disti::WeakReferenceableMixin::AddWeakReference
void AddWeakReference(WeakReference *weakRef) override
Definition: weak_referenceable_mixin.h:55
disti::WeakReferenceableMixin::WeakReferenceableMixin
WeakReferenceableMixin()
Definition: weak_referenceable_mixin.h:86
disti::WeakReferenceableMixin::NotifyWeakReferenceDestroyed
void NotifyWeakReferenceDestroyed(WeakReference *ref) override
Definition: weak_referenceable_mixin.h:67
disti::WeakReferenceable
Definition: weak_reference.h:65
dynamic_array.h
The disti::DynamicArray class. A templated array of objects capable of dynamically growing.
gls_cpp_lang_support.h
Macros and helper code to determine what subset of C++11/14/17 is available.
DISTI_METHOD_OVERRIDE
#define DISTI_METHOD_OVERRIDE
Macro to wrap the override keyword, removed on compilers that don't support it.
Definition: gls_cpp_lang_support.h:222
disti
Definition: bmpimage.h:47
weak_reference.h
weak reference and related classes