PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: DynEvent.h * 00003 * 00004 * Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/) 00005 * 00006 * This file is part of PixelLight. 00007 * 00008 * PixelLight is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU Lesser General Public License as published by 00010 * the Free Software Foundation, either version 3 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * PixelLight is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public License 00019 * along with PixelLight. If not, see <http://www.gnu.org/licenses/>. 00020 \*********************************************************/ 00021 00022 00023 #ifndef __PLCORE_DYNEVENT_H__ 00024 #define __PLCORE_DYNEVENT_H__ 00025 #pragma once 00026 00027 00028 //[-------------------------------------------------------] 00029 //[ Includes ] 00030 //[-------------------------------------------------------] 00031 #include "PLCore/Container/SimpleList.h" 00032 #include "PLCore/Base/Func/Signature.h" 00033 #include "PLCore/Base/Func/DynSignature.h" 00034 00035 00036 //[-------------------------------------------------------] 00037 //[ Namespace ] 00038 //[-------------------------------------------------------] 00039 namespace PLCore { 00040 00041 00042 //[-------------------------------------------------------] 00043 //[ Forward declarations ] 00044 //[-------------------------------------------------------] 00045 class EventDesc; 00046 class DynParams; 00047 class XmlElement; 00048 class DynEventHandler; 00049 00050 00051 //[-------------------------------------------------------] 00052 //[ Classes ] 00053 //[-------------------------------------------------------] 00054 /** 00055 * @brief 00056 * Virtual base class for events 00057 * 00058 * @remarks 00059 * This is the virtual base class to access events dynamically. 00060 * 00061 * @note 00062 * - Implementation of the observer design pattern (this class is the subject/observable, the source) 00063 */ 00064 class DynEvent : public DynSignature { 00065 00066 00067 //[-------------------------------------------------------] 00068 //[ Public definitions ] 00069 //[-------------------------------------------------------] 00070 public: 00071 typedef Signature<void, DynParams&, void*>::FuncType FUNC; 00072 00073 00074 //[-------------------------------------------------------] 00075 //[ Public functions ] 00076 //[-------------------------------------------------------] 00077 public: 00078 /** 00079 * @brief 00080 * Constructor 00081 */ 00082 PLCORE_API DynEvent(); 00083 00084 /** 00085 * @brief 00086 * Destructor 00087 */ 00088 PLCORE_API virtual ~DynEvent(); 00089 00090 /** 00091 * @brief 00092 * Connect event handler to the event 00093 * 00094 * @param[in] cHandler 00095 * Event handler 00096 */ 00097 PLCORE_API void Connect(DynEventHandler &cHandler); 00098 00099 /** 00100 * @brief 00101 * Disconnect event handler from the event 00102 * 00103 * @param[in] cHandler 00104 * Event handler 00105 */ 00106 PLCORE_API void Disconnect(DynEventHandler &cHandler); 00107 00108 /** 00109 * @brief 00110 * Return the number of connections 00111 * 00112 * @return 00113 * The number of connections 00114 * 00115 * @note 00116 * - Don't use this method within performance critical situations because due internal 00117 * implementation, this request has a linear, not constant runtime! 00118 */ 00119 inline uint32 GetNumOfConnects() const; 00120 00121 00122 //[-------------------------------------------------------] 00123 //[ Public virtual DynEvent functions ] 00124 //[-------------------------------------------------------] 00125 public: 00126 /** 00127 * @brief 00128 * Get event descriptor 00129 * 00130 * @return 00131 * Descriptor (can be a null pointer) 00132 */ 00133 PLCORE_API virtual const EventDesc *GetDesc() const; 00134 00135 /** 00136 * @brief 00137 * Create a generic event handler which is compatible with this dynamic event 00138 * 00139 * @param[in] pFunc 00140 * Function to be called by the dynamic event handler 00141 * @param[in] pUserData 00142 * Optional pointer to user data to pass on to the given generic function when the event was emitted, can be a null pointer 00143 * 00144 * @return 00145 * The generic event handler which is compatible with this dynamic event, null pointer on error (delete the created instance if you no longer need it) 00146 * 00147 * @note 00148 * - This method just creates an instance of the generic event handler and does not automatically connect it with this dynamic event 00149 * - Use typed instead of generic event handlers whenever possible, this method only exists for situations were no type information 00150 * is available at compile time 00151 */ 00152 PLCORE_API virtual DynEventHandler *CreateGenericEventHandler(const FUNC &pFunc, void *pUserData = nullptr) const; 00153 00154 /** 00155 * @brief 00156 * Emit event 00157 * 00158 * @param[in] cParams 00159 * Parameters 00160 */ 00161 PLCORE_API virtual void Emit(DynParams &cParams) const; 00162 00163 /** 00164 * @brief 00165 * Emit event 00166 * 00167 * @param[in] cParams 00168 * Parameters 00169 */ 00170 PLCORE_API virtual void Emit(const DynParams &cParams) const; 00171 00172 /** 00173 * @brief 00174 * Emit event 00175 * 00176 * @param[in] sParams 00177 * Parameters as string 00178 */ 00179 PLCORE_API virtual void Emit(const String &sParams) const; 00180 00181 /** 00182 * @brief 00183 * Emit event 00184 * 00185 * @param[in] cElement 00186 * Parameters as XML 00187 */ 00188 PLCORE_API virtual void Emit(const XmlElement &cElement) const; 00189 00190 00191 //[-------------------------------------------------------] 00192 //[ Protected data ] 00193 //[-------------------------------------------------------] 00194 protected: 00195 SimpleList<DynEventHandler*> m_lstHandlers; /**< List of event handlers */ 00196 00197 00198 }; 00199 00200 00201 //[-------------------------------------------------------] 00202 //[ Namespace ] 00203 //[-------------------------------------------------------] 00204 } // PLCore 00205 00206 00207 //[-------------------------------------------------------] 00208 //[ Implementation ] 00209 //[-------------------------------------------------------] 00210 #include "PLCore/Base/Event/DynEvent.inl" 00211 00212 00213 #endif // __PLCORE_DYNEVENT_H__
|