2010-10-20 23:50:49 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
/**
|
|
|
|
* \file InGuiThread.h
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Peter Kümmel
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INGUITHREAD_H
|
|
|
|
#define INGUITHREAD_H
|
|
|
|
|
2010-10-21 21:29:08 +00:00
|
|
|
#include <QMutex>
|
2010-10-20 23:50:49 +00:00
|
|
|
#include <QObject>
|
2010-10-21 21:29:08 +00:00
|
|
|
#include <QWaitCondition>
|
2010-10-20 23:50:49 +00:00
|
|
|
|
2010-11-01 19:37:59 +00:00
|
|
|
#include "frontends/Application.h"
|
2010-10-20 23:50:49 +00:00
|
|
|
#include "support/bind.h"
|
|
|
|
#include "support/functional.h"
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
|
|
|
|
|
|
|
class IntoGuiThreadMover : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
IntoGuiThreadMover();
|
|
|
|
|
|
|
|
void callInGuiThread();
|
|
|
|
|
|
|
|
Q_SIGNALS:
|
2010-10-21 21:29:08 +00:00
|
|
|
void triggerFunctionCall();
|
2010-10-20 23:50:49 +00:00
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void doFunctionCall();
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual void synchronousFunctionCall() = 0;
|
2010-10-21 21:29:08 +00:00
|
|
|
|
|
|
|
QWaitCondition condition_;
|
|
|
|
QMutex sync_mutex_;
|
2010-10-20 23:50:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<class R>
|
|
|
|
class InGuiThread : private IntoGuiThreadMover
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
InGuiThread() {}
|
|
|
|
|
|
|
|
template<class F>
|
|
|
|
R call(F f)
|
|
|
|
{
|
|
|
|
func_ = f;
|
2010-11-01 19:37:59 +00:00
|
|
|
if (theApp() == 0)
|
|
|
|
synchronousFunctionCall();
|
|
|
|
else
|
|
|
|
callInGuiThread();
|
2010-10-20 23:50:49 +00:00
|
|
|
return return_value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class F, class P1>
|
2010-10-23 00:06:22 +00:00
|
|
|
R call(F f, P1& p1)
|
2010-10-20 23:50:49 +00:00
|
|
|
{
|
2010-10-23 00:06:22 +00:00
|
|
|
return call(bind(f, ref(p1)));
|
2010-10-20 23:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class F, class P1, class P2>
|
2010-10-23 00:06:22 +00:00
|
|
|
R call(F f, P1& p1, P2& p2)
|
2010-10-20 23:50:49 +00:00
|
|
|
{
|
2010-10-23 00:06:22 +00:00
|
|
|
return call(bind(f, ref(p1), ref(p2)));
|
2010-10-20 23:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class F, class P1, class P2, class P3>
|
2010-10-23 00:06:22 +00:00
|
|
|
R call(F f, P1& p1, P2& p2, P3& p3)
|
2010-10-20 23:50:49 +00:00
|
|
|
{
|
2010-10-23 00:06:22 +00:00
|
|
|
return call(bind(f, ref(p1), ref(p2), ref(p3)));
|
2010-10-20 23:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class F, class P1, class P2, class P3, class P4>
|
2010-10-23 00:06:22 +00:00
|
|
|
R call(F f, P1& p1, P2& p2, P3& p3, P4& p4)
|
2010-10-20 23:50:49 +00:00
|
|
|
{
|
2010-10-23 00:06:22 +00:00
|
|
|
return call(bind(f, ref(p1), ref(p2), ref(p3), ref(p4)));
|
2010-10-20 23:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
...
|
|
|
|
*/
|
|
|
|
|
|
|
|
template<class F, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8>
|
2010-10-23 00:06:22 +00:00
|
|
|
R call(F f, P1& p1, P2& p2, P3& p3, P4& p4, P5& p5, P6& p6, P7& p7, P8& p8)
|
2010-10-20 23:50:49 +00:00
|
|
|
{
|
2010-10-23 00:06:22 +00:00
|
|
|
return call(bind(f, ref(p1), ref(p2), ref(p3), ref(p4), ref(p5), ref(p6), ref(p7), ref(p8)));
|
2010-10-20 23:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void synchronousFunctionCall()
|
|
|
|
{
|
|
|
|
return_value_ = func_();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
R return_value_;
|
|
|
|
function<R()> func_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// void specialisation
|
|
|
|
template<>
|
|
|
|
class InGuiThread<void> : private IntoGuiThreadMover
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
InGuiThread() {}
|
|
|
|
|
|
|
|
template<class F>
|
|
|
|
void call(F f)
|
|
|
|
{
|
|
|
|
func_ = f;
|
2010-11-01 19:37:59 +00:00
|
|
|
if (theApp() == 0)
|
|
|
|
synchronousFunctionCall();
|
|
|
|
else
|
|
|
|
callInGuiThread();
|
2010-10-20 23:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class F, class P1>
|
2010-10-23 00:06:22 +00:00
|
|
|
void call(F f, P1& p1)
|
2010-10-20 23:50:49 +00:00
|
|
|
{
|
2010-10-23 00:06:22 +00:00
|
|
|
call(bind(f, ref(p1)));
|
2010-10-20 23:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class F, class P1, class P2>
|
2010-10-23 00:06:22 +00:00
|
|
|
void call(F f, P1& p1, P2& p2)
|
2010-10-20 23:50:49 +00:00
|
|
|
{
|
2010-10-23 00:06:22 +00:00
|
|
|
call(bind(f, ref(p1), ref(p2)));
|
2010-10-20 23:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class F, class P1, class P2, class P3>
|
2010-10-23 00:06:22 +00:00
|
|
|
void call(F f, P1& p1, P2& p2, P3& p3)
|
2010-10-20 23:50:49 +00:00
|
|
|
{
|
2010-10-23 00:06:22 +00:00
|
|
|
call(bind(f, ref(p1), ref(p2), ref(p3)));
|
2010-10-20 23:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class F, class P1, class P2, class P3, class P4>
|
2010-10-23 00:06:22 +00:00
|
|
|
void call(F f, P1& p1, P2& p2, P3& p3, P4& p4)
|
2010-10-20 23:50:49 +00:00
|
|
|
{
|
2010-10-23 00:06:22 +00:00
|
|
|
call(bind(f, ref(p1), ref(p2), ref(p3), ref(p4)));
|
2010-10-20 23:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
...
|
|
|
|
*/
|
|
|
|
|
|
|
|
template<class F, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8>
|
2010-10-23 00:06:22 +00:00
|
|
|
void call(F f, P1& p1, P2& p2, P3& p3, P4& p4, P5& p5, P6& p6, P7& p7, P8& p8)
|
2010-10-20 23:50:49 +00:00
|
|
|
{
|
2010-10-23 00:06:22 +00:00
|
|
|
call(bind(f, ref(p1), ref(p2), ref(p3), ref(p4), ref(p5), ref(p6), ref(p7), ref(p8)));
|
2010-10-20 23:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void synchronousFunctionCall()
|
|
|
|
{
|
|
|
|
func_();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
function<void()> func_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
|
|
|
|
2010-10-21 22:04:06 +00:00
|
|
|
#endif // INGUITHREAD_H
|