GL Studio C++ Runtime API
callback_caller.h
Go to the documentation of this file.
1 /*! \file
2  \brief The disti::CallbackMethodCallerTemplate class and global enumerations.
3 
4  \par Copyright Information
5 
6  Copyright (c) 2017 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_DISTI_CALLBACK_CALLER_H
41 #define INCLUDED_DISTI_CALLBACK_CALLER_H
42 
43 #include "callback_caller_base.h"
44 #include "display.h"
45 #include "display_frame.h"
46 #include "events.h"
47 #include "stdio.h"
48 
49 #ifndef WIN32
50 # define _snprintf snprintf
51 #endif
52 
53 namespace disti
54 {
55 /** The CallbackMethodCallerTemplate class
56  * O must be related to DisplayObject
57  */
58 template<class T, class O = DisplayObject>
60 {
61 public:
62  typedef int ( T::*MethodType1 )( O*, DisplayEvent* );
63  typedef void ( T::*MethodType2 )( DisplayEvent* ev, void* );
64  typedef void ( T::*MethodType3 )( void );
65 
66 protected:
67  int _methodType;
68  void* _callbackData; // Only used for MethodType2.
69  T* _container; // The object which contains the method to call
70  union
71  {
72  MethodType1 _1;
73  MethodType2 _2;
74  MethodType3 _3;
75  } _method;
76 
78  : _methodType( 0 )
79  , _callbackData( NULL )
80  , _container( NULL )
81  {}
82 
83 public:
84  CallbackMethodCallerTemplate( MethodType1 method, T* container = NULL )
85  : _methodType( 1 )
86  , _callbackData( NULL )
87  , _container( container )
88  {
89  _method._1 = method;
90  }
91  CallbackMethodCallerTemplate( MethodType2 method, T* container, void* callbackData = NULL )
92  : _methodType( 2 )
93  , _callbackData( callbackData )
94  , _container( container )
95  {
96  _method._2 = method;
97  }
98  CallbackMethodCallerTemplate( MethodType3 method, T* container )
99  : _methodType( 3 )
100  , _callbackData( NULL )
101  , _container( container )
102  {
103  _method._3 = method;
104  }
105 
106  virtual CallbackMethodCallerBase* Duplicate() const
107  {
109  newOne->_callbackData = _callbackData;
110  newOne->_method = _method;
111  newOne->_methodType = _methodType;
112  newOne->_container = _container;
113  return newOne;
114  }
115 
116  virtual void SetContainer( DisplayObject* container )
117  {
118  _container = dynamic_cast<T*>( container );
119  }
120 
121  // Call for MethodType1
122  int Call( DisplayObject* target, DisplayEvent* ev )
123  {
124  O* targetObject = dynamic_cast<O*>( target );
125  if( _method._1 && _methodType == 1 && targetObject )
126  {
127  T* obj = _container;
128 
129  // When an object is cloned, it will attempt to set the _container to itself. (See GLS-10164)
130  // This can be correct for some callbacks created via user code, but not for ones created in the editor.
131  // These are always the parent component, so check to see if the parent's type matches what we expect.
132  // If this is the case, we assume that we were cloned and this is our real container.
133  if( !obj )
134  {
135  if( ComponentBase* const targetComp = dynamic_cast<ComponentBase*>( targetObject ) )
136  {
137  obj = dynamic_cast<T*>( targetComp->ParentDisplayFrame() );
138  }
139  else
140  {
141  obj = dynamic_cast<T*>( targetObject->DisplayObject::Parent() );
142  }
143 
144  if( obj )
145  {
146  _container = obj;
147  }
148  }
149 
150  if( obj )
151  {
152  return ( obj->*( _method._1 ) )( targetObject, ev );
153  }
154  }
155  return 0;
156  }
157  // Call for MethodType2
158  // Allows the caller to override callbackData with a non NULL value
159  void CallType2( DisplayEvent* ev, void* callbackData = NULL )
160  {
161  if( _method._2 && _methodType == 2 && _container )
162  {
163  if( callbackData == NULL )
164  callbackData = _callbackData;
165 
166  ( _container->*( _method._2 ) )( ev, callbackData );
167  }
168  }
169  // This calls the MethodType3 method
170  void CallType3()
171  {
172  if( _method._3 && _methodType == 3 && _container )
173  {
174  ( _container->*( _method._3 ) )();
175  }
176  }
177 };
178 
179 /** A CallbackMethodCallerBase that notifies when properties are changed, and also optionally calls a void callback
180  * Version only supports method type 3
181  */
182 template<class T>
184 {
185 public:
186  typedef void ( T::*MethodType )( void );
187 
188 public:
189  CallbackAttributeNotifier( T* container, const AttributeName& attributeName, MethodType method = NULL )
190  : _container( container )
191  , _attributeName( attributeName )
192  , _method( method )
193  {
194  }
195 
196  virtual CallbackMethodCallerBase* Duplicate() const
197  {
198  CallbackAttributeNotifier* newOne = new CallbackAttributeNotifier( _container, _attributeName, _method );
199  return newOne;
200  }
201 
202  virtual void SetContainer( DisplayObject* container )
203  {
204  _container = dynamic_cast<T*>( container );
205  }
206 
207  // Call for MethodType1
208  int Call( DisplayObject* /*target*/, DisplayEvent* /*ev*/ ) { return 0; }
209  // Call for MethodType2
210  void CallType2( DisplayEvent* /*ev*/, void* /*callbackData*/ = NULL ) {}
211 
212  // This calls the MethodType3 method
213  void CallType3()
214  {
215  if( _container )
216  {
217  _container->NotifyAttributeChanged( _attributeName );
218 
219  if( _method )
220  {
221  ( _container->*( _method ) )();
222  }
223  }
224  }
225 
226 protected:
227  T* _container; // The object which contains the method to call
228  AttributeName _attributeName;
229  MethodType _method;
230 };
231 
232 /** helper for class for emitting attribute changed events in generated code
233  * this class does two things:
234  * 1) It calls NotifyAttributeChanged if the attribute changed
235  * 2) It emits an event if EmitEvent() was set to true and if the Container type is DisplayObject. It can't emit events for top
236  * level display frames since they don't have a handle() method.
237  */
238 template<class T>
240 {
241 public:
242  /** ctor
243  * \param container container that has attribute
244  * \param val current value of attribute. Lifetime must extend pass the life of this class
245  * \param attribName name of attribute. Lifetime must extend pass the life of this class
246  * \param emitEvent whether or not to emit an event. The container must be a DisplayObject, and the event will be passed to the object's handle() method.
247  */
248  AttributeChangedEventEmitter( AttributeChangedNotifier* container, T& val, const AttributeName& attribName, bool emitEvent = false )
249  : _container( container )
250  , _prevVal( val )
251  , _curVal( val )
252  , _attribName( attribName )
253  , _emitEvent( emitEvent )
254  {
255  }
256 
257  /** dtor - emit attribute changed event if container is a DisplayObject and the value has changed */
259  {
260  if( NULL != _container )
261  {
263  }
264  }
265 
266 protected:
268  T _prevVal; /**< previous value for attribute */
269  T& _curVal; /**< ref to current value for attribute */
270  const AttributeName& _attribName; /**< attribute name */
271  bool _emitEvent; /** whether or not to emit an event */
272 
274  {
275  std::stringstream strmNew;
276  std::stringstream strmOld;
277 
278  std::string strNew;
279  std::string strOld;
280 
281  strmNew << _curVal;
282  strmOld << _prevVal;
283  strNew = strmNew.str();
284  strOld = strmOld.str();
285 
286  if( strNew.compare( strOld ) != 0 )
287  {
288  _container->NotifyAttributeChanged( _attribName );
289 
290  if( _emitEvent )
291  {
292  if( DisplayObject* displayObj = dynamic_cast<DisplayObject*>( _container ) )
293  {
294  std::string str = std::string( _attribName ) + " " + strNew;
295  ObjectEvent newEvent( displayObj, "AttributeChanged", str.c_str() );
296  displayObj->handle( &newEvent );
297  }
298  }
299  }
300  }
301 
302 private:
303  // disallow
306 };
307 
308 } // namespace disti
309 
310 #endif
Definition: events.h:321
void EmitAttributeChangedEventProperty()
Definition: callback_caller.h:273
AttributeChangedEventEmitter(AttributeChangedNotifier *container, T &val, const AttributeName &attribName, bool emitEvent=false)
Definition: callback_caller.h:248
Definition: display.h:98
~AttributeChangedEventEmitter()
Definition: callback_caller.h:258
T _prevVal
Definition: callback_caller.h:268
The disti::DisplayObject class and global enumerations.
AttributeChangedNotifier * _container
Definition: callback_caller.h:267
Definition: component_base.h:67
The CallbackMethodCallerBase class.
The standard Mouse and keyboard events and event structures.
const AttributeName & _attribName
Definition: callback_caller.h:270
Definition: callback_caller.h:239
Definition: events.h:112
T & _curVal
Definition: callback_caller.h:269
Definition: callback_caller_base.h:55
The disti::DisplayFrame class.
Definition: callback_caller.h:183
Definition: callback_caller.h:59
Definition: disti_metadata.h:458
virtual void NotifyAttributeChanged(const AttributeName &name)=0
Definition: disti_metadata.h:85
Definition: bmpimage.h:46