Fichier source en cours : FW - Callback.h






Voici le code source cpp :




	#ifndef CALL_BACK_H
#define CALL_BACK_H

namespace fw
{
	class Callback
	{
	public:
		friend class ThreadsWrapper;

		typedef uint32 CallbackSlot;

		class Parameter : public DataParameters
		{
			friend class Callback;

		public:
			Parameter();
			virtual ~Parameter();

#define CALLBACK_RETURN_PARAM_NAME "ReturnData"

			template<typename T>
			T GetReturnValue() const
			{
				if (GetParametersCount() > 0)
				{
					return (T)GetParameterAsPointer<T>(CALLBACK_RETURN_PARAM_NAME);
				}
				else
				{
					return (T) nullptr;
				}
			}

			template<typename T>
			void SetReturnValue(T value)
			{
				SetParam(CALLBACK_RETURN_PARAM_NAME, value);
			}
		};

		Callback(const std::string& sName = UNKNOWN);
		virtual ~Callback();

		template<typename C, typename ThisPointer>
		void AddCallbackFunction(bool (C::*f) (const Parameter&), ThisPointer p)
		{
			std::function <bool(const Parameter&)> func = std::bind(f, p, std::placeholders::_1);

			m_callbacksParam.push_back(func);
		}

		template<typename C, typename ThisPointer>
		void AddCallbackFunction(bool (C::*f) (), ThisPointer p)
		{
			std::function <bool()> func = std::bind(f, p);

			m_callbacksSimple.push_back(func);
		}

		void operator() ();

		void operator() (const Parameter& parameter);

		bool InvokeCallbacks(const Parameter& parameter) const
		{
			if (m_callbacksParam.size() == 0 && m_callbacksSimple.size() == 0)
			{
				return false;
			}

			bool bOK = false;

			for (uint32 i = 0; i < m_callbacksParam.size(); i++)
			{
				std::function<bool(Parameter)> func = m_callbacksParam[i];

				bool bSuccess = func(parameter);

				bOK |= bSuccess;
			}

			for (uint32 i = 0; i < m_callbacksSimple.size(); i++)
			{
				std::function<bool()> func = m_callbacksSimple[i];

				bool bSuccess = func();

				bOK |= bSuccess;
			}

			return bOK;
		}

	private:
		std::string m_sName;

		std::vector<std::function<bool()>> m_callbacksSimple;
		std::vector<std::function<bool(const Parameter&)>> m_callbacksParam;
	};
}

#endif