代码拉取完成,页面将自动刷新
同步操作将从 启家/QEvent 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#pragma once
#include <memory>
#include <list>
#include <thread>
#include "QJMutex.h"
namespace qijia
{
struct IDataBase
{
virtual ~IDataBase() {}
virtual void run() = 0;
};
template <typename FunCall>
struct CDataBase : public IDataBase
{
CDataBase(FunCall&& f)
: m_fun(std::forward<FunCall>(f))
{}
void run()
{
m_fun();
}
FunCall m_fun;
};
class QJEventData
{
public:
template <typename FunCall, typename ... Args>
QJEventData(FunCall&& f, Args&& ... args)
{
m_pFun = makeCDataBase(std::bind(std::forward<FunCall>(f), std::forward<Args>(args)...));
}
void run()
{
m_pFun->run();
}
private:
template <typename FunCall>
std::shared_ptr<CDataBase<FunCall>> makeCDataBase(FunCall&& f)
{
return std::make_shared<CDataBase<FunCall>>(std::forward<FunCall>(f));
}
std::shared_ptr<IDataBase> m_pFun;
};
// this loop for other thread sendevent to main thread
class QJEventLoop
{
typedef std::list<std::shared_ptr<QJEventData>> eventDataList;
typedef std::shared_ptr<eventDataList> eventDataListPtr;
public:
void init()
{
m_mainId = (std::this_thread::get_id ());
}
bool loop()
{
if ( std::this_thread::get_id () != m_mainId)
{
//error not run in main thread
return false;
}
eventDataListPtr eventList;
{
qijia::QJMutex lock (mtx);
eventList.reset (new eventDataList(*m_eventList));
m_eventList->clear();
}
auto iter = eventList->begin();
while(iter != eventList->end())
{
(*iter)->run();
iter++;
}
return true;
}
static QJEventLoop& App()
{
static QJEventLoop instance;
return instance;
}
void addEventList(std::shared_ptr<QJEventData> event)
{
qijia::QJMutex lock (mtx);
if (!m_eventList.unique ())
{
m_eventList.reset (new eventDataList(*m_eventList));
}
m_eventList->push_back(event);
}
private:
QJEventLoop()
:m_mainId(std::this_thread::get_id ())
{
m_eventList.reset(new eventDataList);
}
std::thread::id m_mainId;
eventDataListPtr m_eventList;
std::mutex mtx;
};
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。