#pragma once
#include
#include
#ifdef __APPLE__
#include
using std::tr1::function;
using std::tr1::bind;
using namespace std::tr1::placeholders;
namespace std {
template struct _Remove_reference
{
typedef _Ty _Type;
};
template inline typename _Remove_reference<_Ty>::_Type&& move(_Ty&& _Arg)
{
return ((typename _Remove_reference<_Ty>::_Type&&)_Arg);
}
};
#else
#include
using std::function;
using std::bind;
using namespace std::placeholders;
#endif
#include "MySingleton.h"
#include "MyThread.h"
class Parameter
{
public:
#define PARAM_IMPL_INIT(t) Parameter(t para) { m_data = para; }
#ifdef WIN32
// windows下面类型转换失败抛出异常
#define PARAM_IMPL_RET(t, it,value) operator t () { t ret = value; ret = boost::any_cast(m_data); return std::move(ret); };
#else
#define PARAM_IMPL_RET(t, it, value) operator t () { t ret = value; try { ret = boost::any_cast(m_data); } catch (...) {} return std::move(ret); };
#endif
PARAM_IMPL_INIT(int);
PARAM_IMPL_INIT(long long);
PARAM_IMPL_INIT(double);
PARAM_IMPL_INIT(const std::string&);
PARAM_IMPL_INIT(boost::any);
PARAM_IMPL_RET(bool, int, false);
PARAM_IMPL_RET(char, int, 0);
PARAM_IMPL_RET(unsigned char, int, 0);
PARAM_IMPL_RET(short, int, 0);
PARAM_IMPL_RET(unsigned short, int, 0);
PARAM_IMPL_RET(int, int, 0);
PARAM_IMPL_RET(unsigned int, int, 0);
PARAM_IMPL_RET(long, int, 0);
PARAM_IMPL_RET(unsigned long, int, 0);
PARAM_IMPL_RET(long long, long long, 0);
PARAM_IMPL_RET(unsigned long long, long long, 0);
PARAM_IMPL_RET(float, double, 0.0f);
PARAM_IMPL_RET(double, double, 0.0);
PARAM_IMPL_RET(std::string, std::string, "");
Parameter(const char* param)
{
m_data = std::string(param);
}
operator boost::any() {
return std::move(m_data);
}
private:
// boost::variant< std::string, double, float, int > m_data;
boost::any m_data;
};
typedef function< void(uint32, Parameter, Parameter, Parameter, Parameter) > FuncCallback;
#define LOCK_QUEUE MyLock l(&m_mutex)
class CommandQueue : public MySingleton
{
public:
CommandQueue() {};
~CommandQueue() {};
void registerHandler(uint32 command, FuncCallback callback)
{
m_eventMap[command] = callback;
}
void unRegisterHandler(uint32 command)
{
auto itr = m_eventMap.find(command);
if (itr != m_eventMap.end())
{
m_eventMap.erase(itr);
}
}
void post(uint32 dwCommand)
{
COMMAND_DATA cmdData;
cmdData.dwCommand = dwCommand;
LOCK_QUEUE;
m_queue.push_back(std::move(cmdData));
}
template
void post(uint32 dwCommand, Type data)
{
COMMAND_DATA cmdData;
cmdData.dwCommand = dwCommand;
cmdData.parameters.push_back(std::move(data));
LOCK_QUEUE;
m_queue.push_back(std::move(cmdData));
};
template
void post(uint32 dwCommand, Type1 data1, Type2 data2)
{
COMMAND_DATA cmdData;
cmdData.dwCommand = dwCommand;
cmdData.parameters.push_back(std::move(data1));
cmdData.parameters.push_back(std::move(data2));
LOCK_QUEUE;
m_queue.push_back(std::move(cmdData));
};
template< typename Type1, typename Type2, typename Type3 >
void post(uint32 dwCommand, Type1 data1, Type2 data2, Type3 data3)
{
COMMAND_DATA cmdData;
cmdData.dwCommand = dwCommand;
cmdData.parameters = {data1, data2, data3};
LOCK_QUEUE;
m_queue.push_back(std::move(cmdData));
};
template< typename Type1, typename Type2, typename Type3, typename Type4 >
void post(uint32 dwCommand, Type1 data1, Type2 data2, Type3 data3, Type4 data4)
{
COMMAND_DATA cmdData;
cmdData.dwCommand = dwCommand;
cmdData.parameters = {data1, data2, data3, data4};
LOCK_QUEUE;
m_queue.push_back(cmdData);
};
void send(uint32 dwCommand)
{
auto itr = m_eventMap.find(dwCommand);
if (itr != m_eventMap.end()) {
itr->second(dwCommand, 0, 0, 0, 0);
}
};
template
void send(uint32 dwCommand, Type data)
{
auto itr = m_eventMap.find(dwCommand);
if (itr != m_eventMap.end()) {
itr->second(dwCommand, data, 0, 0, 0);
}
};
template
void send(uint32 dwCommand, Type1 data1, Type2 data2)
{
auto itr = m_eventMap.find(dwCommand);
if (itr != m_eventMap.end()) {
itr->second(dwCommand, data1, data2, 0, 0);
}
};
template< typename Type1, typename Type2, typename Type3 >
void send(uint32 dwCommand, Type1 data1, Type2 data2, Type3 data3)
{
auto itr = m_eventMap.find(dwCommand);
if (itr != m_eventMap.end()) {
itr->second(dwCommand, data1, data2, data3, 0);
}
};
template< typename Type1, typename Type2, typename Type3, typename Type4 >
void send(uint32 dwCommand, Type1 data1, Type2 data2, Type3 data3, Type4 data4)
{
auto itr = m_eventMap.find(dwCommand);
if (itr != m_eventMap.end()) {
itr->second(dwCommand, data1, data2, data3, data4);
}
};
void dispatchAll()
{
LOCK_QUEUE;
while (!m_queue.empty()) {
COMMAND_DATA& cmdData = m_queue.front();
int size = cmdData.parameters.size();
auto itrCall = m_eventMap.find(cmdData.dwCommand);
switch (size)
{
case 0:
itrCall->second(cmdData.dwCommand, 0, 0, 0, 0);
break;
case 1:
itrCall->second(cmdData.dwCommand, cmdData.parameters[0], 0, 0, 0);
break;
case 2:
itrCall->second(cmdData.dwCommand, cmdData.parameters[0], cmdData.parameters[1], 0, 0);
break;
case 3:
itrCall->second(cmdData.dwCommand, cmdData.parameters[0], cmdData.parameters[1], cmdData.p arameters[2], 0);
break;
case 4:
itrCall->second(cmdData.dwCommand, cmdData.parameters[0], cmdData.parameters[1], cmdData.p arameters[2], cmdData.parameters[3]);
break;
}
m_queue.pop_front();
}
}
private:
struct COMMAND_DATA {
uint32 dwCommand;
std::vector parameters;
};
MyMutex m_mutex; // 递归锁,处理多线程异步消息抛送
std::deque m_queue; // post异步command处理队列(都在主线程处理,每帧结束的时候执行)
std::map< uint32, FuncCallback > m_eventMap; // 注册的命令和回调函数映射
};
inline void reg_command(uint32 command, FuncCallback callback)
{
CommandQueue::getSingleton().registerHandler(command, callback);
}
inline void unreg_command(uint32 command)
{
CommandQueue::getSingleton().unRegisterHandler(command);
}
inline void post_command(uint32 dwCommand)
{
CommandQueue::getSingleton().post(dwCommand);
}
template
inline void post_command(uint32 dwCommand, Type data)
{
CommandQueue::getSingleton().post(dwCommand, data);
}
template
inline void post_command(uint32 dwCommand, Type1 data1, Type2 data2)
{
CommandQueue::getSingleton().post(dwCommand, data1, data2);
}
template< typename Type1, typename Type2, typename Type3 >
inline void post_command(uint32 dwCommand, Type1 data1, Type2 data2, Type3 data3)
{
CommandQueue::getSingleton().post(dwCommand, data1, data2, data3);
}
template< typename Type1, typename Type2, typename Type3, typename Type4 >
inline void post_command(uint32 dwCommand, Type1 data1, Type2 data2, Type3 data3, Type4 data4)
{
CommandQueue::getSingleton().post(dwCommand, data1, data2, data3, data4);
}
inline void send_command(uint32 dwCommand)
{
CommandQueue::getSingleton().send(dwCommand);
}
template
inline void send_command(uint32 dwCommand, Type data)
{
CommandQueue::getSingleton().send(dwCommand, data);
}
template
inline void send_command(uint32 dwCommand, Type1 data1, Type2 data2)
{
CommandQueue::getSingleton().send(dwCommand, data1, data2);
}
template< typename Type1, typename Type2, typename Type3 >
inline void send_command(uint32 dwCommand, Type1 data1, Type2 data2, Type3 data3)
{
CommandQueue::getSingleton().send(dwCommand, data1, data2, data3);
}
template< typename Type1, typename Type2, typename Type3, typename Type4 >
inline void send_command(uint32 dwCommand, Type1 data1, Type2 data2, Type3 data3, Type4 data4)
{
CommandQueue::getSingleton().send(dwCommand, data1, data2, data3, data4);
}
|