GL Studio 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) 2015 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 _CALLBACK_CALLER_H
41 #define _CALLBACK_CALLER_H
42 
43 #include "display.h"
44 #include "events.h"
45 #include "display_frame.h"
46 #include "callback_caller_base.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  protected:
67  int _methodType;
68  union {
69  MethodType1 _1;
70  MethodType2 _2;
71  MethodType3 _3;
72  } _method;
73  void* _callbackData; // Only used for MethodType2.
74  //DisplayFrame* _frame;
75  //DisplayObject* _object;
76  T* _container; // The object which contains the method to call
78  _methodType(0),
79  _callbackData(NULL),
80  _container(NULL){}
81 
82  public:
83 
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  // Try to extrach the frame from the object if needed
130  if (!obj)
131  obj = dynamic_cast<T*>(targetObject->DisplayObject::Parent());
132 
133  if (obj)
134  return (obj->*(_method._1))(targetObject, ev);
135  }
136  return 0;
137  }
138  // Call for MethodType2
139  // Allows the caller to override callbackData with a non NULL value
140  void CallType2(DisplayEvent* ev, void* callbackData = NULL)
141  {
142  if (_method._2 && _methodType == 2 && _container)
143  {
144  if (callbackData == NULL)
145  callbackData = _callbackData;
146 
147  (_container->*(_method._2))(ev, callbackData);
148  }
149  }
150  // This calls the MethodType3 method
151  void CallType3()
152  {
153  if (_method._3 && _methodType == 3 && _container)
154  {
155  (_container->*(_method._3))();
156  }
157  }
158 };
159 
160 /** A CallbackMethodCallerBase that notifies when properties are changed, and also optionally calls a void callback
161  * Version only supports method type 3
162  */
163 template <class T>
165 {
166 public:
167  typedef void ( T::*MethodType )( void );
168 
169 public:
170  CallbackAttributeNotifier( T* container, const AttributeName& attributeName, MethodType method = NULL )
171  : _container( container )
172  , _attributeName( attributeName )
173  , _method( method )
174  {
175  }
176 
177  virtual CallbackMethodCallerBase* Duplicate() const
178  {
179  CallbackAttributeNotifier* newOne = new CallbackAttributeNotifier( _container, _attributeName, _method );
180  return newOne;
181  }
182 
183  virtual void SetContainer( DisplayObject* container )
184  {
185  _container = dynamic_cast<T*>( container );
186  }
187 
188  // Call for MethodType1
189  int Call( DisplayObject* /*target*/, DisplayEvent* /*ev*/ ) { return 0; }
190  // Call for MethodType2
191  void CallType2( DisplayEvent* /*ev*/, void* callbackData = NULL ) {}
192 
193  // This calls the MethodType3 method
194  void CallType3()
195  {
196  if( _container )
197  {
198  _container->NotifyAttributeChanged( _attributeName );
199 
200  if( _method )
201  {
202  ( _container->*( _method ) )();
203  }
204  }
205  }
206 
207 protected:
208  T* _container; // The object which contains the method to call
209  AttributeName _attributeName;
210  MethodType _method;
211 };
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:56
The disti::DisplayFrame class.
Definition: callback_caller.h:164
Definition: callback_caller.h:60
Definition: disti_metadata.h:432
virtual void NotifyAttributeChanged(const AttributeName &name)=0
Definition: disti_metadata.h:83
Definition: bmpimage.h:46