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