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 
56 /** The CallbackMethodCallerTemplate class
57  * O must be related to DisplayObject
58  */
59 template<class T, class O = DisplayObject>
61 {
62 public:
63  typedef int ( T::*MethodType1 )( O*, DisplayEvent* );
64  typedef void ( T::*MethodType2 )( DisplayEvent* ev, void* );
65  typedef void ( T::*MethodType3 )( void );
66 
67 protected:
68  int _methodType;
69  void* _callbackData; // Only used for MethodType2.
70  T* _container; // The object which contains the method to call
71  union
72  {
73  MethodType1 _1;
74  MethodType2 _2;
75  MethodType3 _3;
76  } _method;
77 
79  : _methodType( 0 )
80  , _callbackData( NULL )
81  , _container( NULL )
82  {}
83 
84 public:
85  CallbackMethodCallerTemplate( MethodType1 method, T* container = NULL )
86  : _methodType( 1 )
87  , _callbackData( NULL )
88  , _container( container )
89  {
90  _method._1 = method;
91  }
92  CallbackMethodCallerTemplate( MethodType2 method, T* container, void* callbackData = NULL )
93  : _methodType( 2 )
94  , _callbackData( callbackData )
95  , _container( container )
96  {
97  _method._2 = method;
98  }
99  CallbackMethodCallerTemplate( MethodType3 method, T* container )
100  : _methodType( 3 )
101  , _callbackData( NULL )
102  , _container( container )
103  {
104  _method._3 = method;
105  }
106 
107  virtual CallbackMethodCallerBase* Duplicate() const
108  {
110  newOne->_callbackData = _callbackData;
111  newOne->_method = _method;
112  newOne->_methodType = _methodType;
113  newOne->_container = _container;
114  return newOne;
115  }
116 
117  virtual void SetContainer( DisplayObject* container )
118  {
119  _container = dynamic_cast<T*>( container );
120  }
121 
122  // Call for MethodType1
123  int Call( DisplayObject* target, DisplayEvent* ev )
124  {
125  O* targetObject = dynamic_cast<O*>( target );
126  if( _method._1 && _methodType == 1 && targetObject )
127  {
128  T* obj = _container;
129 
130  // Try to extrach the frame from the object if needed
131  if( !obj )
132  obj = dynamic_cast<T*>( targetObject->DisplayObject::Parent() );
133 
134  if( obj )
135  return ( obj->*( _method._1 ) )( targetObject, ev );
136  }
137  return 0;
138  }
139  // Call for MethodType2
140  // Allows the caller to override callbackData with a non NULL value
141  void CallType2( DisplayEvent* ev, void* callbackData = NULL )
142  {
143  if( _method._2 && _methodType == 2 && _container )
144  {
145  if( callbackData == NULL )
146  callbackData = _callbackData;
147 
148  ( _container->*( _method._2 ) )( ev, callbackData );
149  }
150  }
151  // This calls the MethodType3 method
152  void CallType3()
153  {
154  if( _method._3 && _methodType == 3 && _container )
155  {
156  ( _container->*( _method._3 ) )();
157  }
158  }
159 };
160 
161 /** A CallbackMethodCallerBase that notifies when properties are changed, and also optionally calls a void callback
162  * Version only supports method type 3
163  */
164 template<class T>
166 {
167 public:
168  typedef void ( T::*MethodType )( void );
169 
170 public:
171  CallbackAttributeNotifier( T* container, const AttributeName& attributeName, MethodType method = NULL )
172  : _container( container )
173  , _attributeName( attributeName )
174  , _method( method )
175  {
176  }
177 
178  virtual CallbackMethodCallerBase* Duplicate() const
179  {
180  CallbackAttributeNotifier* newOne = new CallbackAttributeNotifier( _container, _attributeName, _method );
181  return newOne;
182  }
183 
184  virtual void SetContainer( DisplayObject* container )
185  {
186  _container = dynamic_cast<T*>( container );
187  }
188 
189  // Call for MethodType1
190  int Call( DisplayObject* /*target*/, DisplayEvent* /*ev*/ ) { return 0; }
191  // Call for MethodType2
192  void CallType2( DisplayEvent* /*ev*/, void* /*callbackData*/ = NULL ) {}
193 
194  // This calls the MethodType3 method
195  void CallType3()
196  {
197  if( _container )
198  {
199  _container->NotifyAttributeChanged( _attributeName );
200 
201  if( _method )
202  {
203  ( _container->*( _method ) )();
204  }
205  }
206  }
207 
208 protected:
209  T* _container; // The object which contains the method to call
210  AttributeName _attributeName;
211  MethodType _method;
212 };
213 
214 /** helper for class for emitting attribute changed events in generated code
215  * this class does two things:
216  * 1) It calls NotifyAttributeChanged if the attribute changed
217  * 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
218  * level display frames since they don't have a handle() method.
219  */
220 template<class T>
222 {
223 public:
224  /** ctor
225  * \param container container that has attribute
226  * \param val current value of attribute. Lifetime must extend pass the life of this class
227  * \param attribName name of attribute. Lifetime must extend pass the life of this class
228  * \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.
229  */
230  AttributeChangedEventEmitter( AttributeChangedNotifier* container, T& val, const AttributeName& attribName, bool emitEvent = false )
231  : _container( container )
232  , _prevVal( val )
233  , _curVal( val )
234  , _attribName( attribName )
235  , _emitEvent( emitEvent )
236  {
237  }
238 
239  /** dtor - emit attribute changed event if container is a DisplayObject and the value has changed */
241  {
242  if( NULL != _container )
243  {
245  }
246  }
247 
248 protected:
250  T _prevVal; /**< previous value for attribute */
251  T& _curVal; /**< ref to current value for attribute */
252  const AttributeName& _attribName; /**< attribute name */
253  bool _emitEvent; /** whether or not to emit an event */
254 
256  {
257  std::stringstream strmNew;
258  std::stringstream strmOld;
259 
260  std::string strNew;
261  std::string strOld;
262 
263  strmNew << _curVal;
264  strmOld << _prevVal;
265  strNew = strmNew.str();
266  strOld = strmOld.str();
267 
268  if( strNew.compare( strOld ) != 0 )
269  {
270  _container->NotifyAttributeChanged( _attribName );
271 
272  if( _emitEvent )
273  {
274  if( DisplayObject* displayObj = dynamic_cast<DisplayObject*>( _container ) )
275  {
276  std::string str = std::string( _attribName ) + " " + strNew;
277  ObjectEvent newEvent( displayObj, "AttributeChanged", str.c_str() );
278  displayObj->handle( &newEvent );
279  }
280  }
281  }
282  }
283 
284 private:
285  // disallow
288 };
289 
290 } // namespace disti
291 
292 #endif
Definition: events.h:320
void EmitAttributeChangedEventProperty()
Definition: callback_caller.h:255
AttributeChangedEventEmitter(AttributeChangedNotifier *container, T &val, const AttributeName &attribName, bool emitEvent=false)
Definition: callback_caller.h:230
Definition: display.h:98
~AttributeChangedEventEmitter()
Definition: callback_caller.h:240
T _prevVal
Definition: callback_caller.h:250
The disti::DisplayObject class and global enumerations.
AttributeChangedNotifier * _container
Definition: callback_caller.h:249
The CallbackMethodCallerBase class.
The standard Mouse and keyboard events and event structures.
const AttributeName & _attribName
Definition: callback_caller.h:252
Definition: callback_caller.h:221
Definition: events.h:111
T & _curVal
Definition: callback_caller.h:251
Definition: callback_caller_base.h:55
The disti::DisplayFrame class.
Definition: callback_caller.h:165
Definition: callback_caller.h:60
Definition: disti_metadata.h:453
virtual void NotifyAttributeChanged(const AttributeName &name)=0
Definition: disti_metadata.h:84
Definition: bmpimage.h:46