1/****************************************************************************************
2
3 Copyright (C) 2015 Autodesk, Inc.
4 All rights reserved.
5
6 Use of this software is subject to the terms of the Autodesk license agreement
7 provided at the time of installation or download, or which otherwise accompanies
8 this software in either electronic or hard copy form.
9
10****************************************************************************************/
11
12//! \file fbxeventhandler.h
13#ifndef _FBXSDK_CORE_EVENT_HANDLER_H_
14#define _FBXSDK_CORE_EVENT_HANDLER_H_
15
16#include <fbxsdk/fbxsdk_def.h>
17
18#include <fbxsdk/core/fbxevent.h>
19#include <fbxsdk/core/base/fbxintrusivelist.h>
20
21#include <fbxsdk/fbxsdk_nsbegin.h>
22
23class FbxListener;
24
25/** Event handler class contains a listener and a callback function.
26* Event handler is used to bind emitter and listener together. Its callback function can process event data.
27* To generate a valid event handler, you can create an event emitter and event listener first and then call FbxListener::Bind().
28* It will create an event handler automatically and bind the handler to the listener and the created emitter.
29* After that, the emitter and listener are bound together via event handler.
30* \remarks An object(emitter) can emit a certain type of event, the object(listener) who are listening to that type of event,
31* will receive a signal and take action to process the event data.
32* \par The whole process of event is:
33* \li 1. Create an emitter and a listener, then bind them together via the same event handler.
34* \li 2. Emitter can emit an event at certain conditions. The event could be handled by event handler.
35* \li 3. Once an event is emitted, the listener to this event will receive a signal.
36* \li 4. And then the listener could process the event data according to the types of event, by calling event handler.
37* \note The event data is process by the callback function of event handler.
38* \nosubgrouping
39* \see FbxListener FbxEventBase FbxEvent FbxEmitter
40*/
41class FbxEventHandler
42{
43public:
44 //! Event handler base type.
45 enum EType
46 {
47 eListener, //!< Listener event handler type.
48 eEmitter, //!< Emitter event handler type.
49 eCount //!< Count of different event handler types.
50 };
51
52 /** Get event type of current handler.
53 * \return The type ID of event. */
54 virtual int GetHandlerEventType()=0;
55
56 /** Call function that process event data.
57 * \param pEvent specify the event type. pEvent could be a specific class which derived from FbxEventBase.
58 * \see FbxEventBase */
59 virtual void FunctionCall(const FbxEventBase& pEvent)=0;
60
61 /** Get listener of current handler.
62 * \return A pointer to the listener object. */
63 virtual FbxListener* GetListener()=0;
64
65/*****************************************************************************************************************************
66** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
67*****************************************************************************************************************************/
68#ifndef DOXYGEN_SHOULD_SKIP_THIS
69 FbxEventHandler(){}
70 virtual ~FbxEventHandler(){}
71
72 FBXSDK_INTRUSIVE_LIST_NODE(FbxEventHandler, eCount);
73#endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
74};
75
76/*****************************************************************************************************************************
77** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
78*****************************************************************************************************************************/
79#ifndef DOXYGEN_SHOULD_SKIP_THIS
80
81template <typename EventType, typename ListenerType> class FbxMemberFuncEventHandler : public FbxEventHandler
82{
83 typedef void (ListenerType::*CallbackFnc)(const EventType*);
84
85public:
86 FbxMemberFuncEventHandler(ListenerType* pListenerInstance, CallbackFnc pFunction) : mListener(pListenerInstance), mFunction(pFunction){}
87 virtual int GetHandlerEventType(){ return EventType::GetStaticTypeId(); }
88 virtual void FunctionCall(const FbxEventBase& pEvent){ (*mListener.*mFunction)(reinterpret_cast<const EventType*>(&pEvent)); }
89 virtual FbxListener* GetListener(){ return mListener; }
90
91private:
92 ListenerType* mListener;
93 CallbackFnc mFunction;
94};
95
96template <typename EventType, typename ListenerType> class FbxConstMemberFuncEventHandler : public FbxEventHandler
97{
98 typedef void (ListenerType::*CallbackFnc)(const EventType*) const;
99
100public:
101 FbxConstMemberFuncEventHandler(ListenerType* pListenerInstance, CallbackFnc pFunction) : mListener(pListenerInstance), mFunction(pFunction){}
102 virtual int GetHandlerEventType(){ return EventType::GetStaticTypeId(); }
103 virtual void FunctionCall(const FbxEventBase& pEvent){ (*mListener.*mFunction)(reinterpret_cast<const EventType*>(&pEvent)); }
104 virtual FbxListener* GetListener(){ return mListener; }
105
106private:
107 ListenerType* mListener;
108 CallbackFnc mFunction;
109};
110
111template <typename EventType> class FbxFuncEventHandler : public FbxEventHandler
112{
113 typedef void (*CallbackFnc)(const EventType*, FbxListener*);
114
115public:
116 FbxFuncEventHandler(FbxListener* pListener, CallbackFnc pFunction) : mListener(pListener), mFunction(pFunction){}
117 virtual int GetHandlerEventType(){ return EventType::GetStaticTypeId(); }
118 virtual void FunctionCall(const FbxEventBase& pEvent){ (*mFunction)(reinterpret_cast<const EventType*>(&pEvent), mListener); }
119 virtual FbxListener* GetListener(){ return mListener; }
120
121private:
122 FbxListener* mListener;
123 CallbackFnc mFunction;
124};
125#endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
126
127#include <fbxsdk/fbxsdk_nsend.h>
128
129#endif /* _FBXSDK_CORE_EVENT_HANDLER_H_ */
130