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