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