tahoma2d/toonz/sources/include/tfunctorinvoker.h

112 lines
2.8 KiB
C
Raw Normal View History

2016-05-17 03:04:11 +12:00
#pragma once
2016-03-19 06:57:51 +13:00
#ifndef TFUNCTORINVOKER_H
#define TFUNCTORINVOKER_H
// TnzCore includes
#include "tcommon.h"
// Qt includes
#include <QObject>
#undef DVAPI
#undef DVVAR
#ifdef TNZCORE_EXPORTS
#define DVAPI DV_EXPORT_API
#define DVVAR DV_EXPORT_VAR
#else
#define DVAPI DV_IMPORT_API
#define DVVAR DV_IMPORT_VAR
#endif
//********************************************************************************
// TFunctorInvoker declaration
//********************************************************************************
/*!
2016-06-15 18:43:10 +12:00
\brief The TFunctorInvoker is a singleton class that can be used to invoke
functions
2016-03-19 06:57:51 +13:00
as Qt slots without having to inherit from QObject.
\details \par Rationale
2016-06-15 18:43:10 +12:00
Signal and slots are an important part of the Qt framework -
however, using them
requires to explicitly inherit from QObject. This singleton object
is provided
to avoid the memory overhead of inheriting from QObject in the
case of simple
function calls - notably functions that need to synchronize with
Qt's main event
2016-03-19 06:57:51 +13:00
loop.
\par Usage
2016-06-15 18:43:10 +12:00
Inherit from TFunctorInvoker::BaseFunctor and allocate a new
instance on the heap,
2016-03-19 06:57:51 +13:00
to be supplied to the TFunctorInvoker::invoke() slot.
\n
The following code exemplifies a correct usage for this class:
\n
\code
class MyFunctor final : public TFunctorInvoker::BaseFunctor
2016-03-19 06:57:51 +13:00
{
2016-06-15 18:43:10 +12:00
MyParams m_params; // Function
parameters
2016-03-19 06:57:51 +13:00
public:
MyFunctor(const MyParams& params) : m_params(params) {}
void operator()()
{
// The function body, operating on m_params
}
};
//------------------------------------------------------------------
void invokeMyFunctor_queued(const MyParams& params)
{
2016-06-15 18:43:10 +12:00
QMetaObject::invokeMethod(TFunctorInvoker::instance(), "invoke",
Qt::QueuedConnection,
2016-03-19 06:57:51 +13:00
Q_ARG(void*, new MyFunctor(params))
);
}
\endcode
*/
class DVAPI TFunctorInvoker final : public QObject {
2016-06-15 18:43:10 +12:00
Q_OBJECT
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
/*!
\brief Polymorphic base \a zerary functor for TFunctorInvoker-compatible
functors.
*/
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
class BaseFunctor {
public:
virtual ~BaseFunctor() {}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
virtual void operator()() = 0;
};
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
static TFunctorInvoker *instance();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void invokeQueued(BaseFunctor *functor);
2016-03-19 06:57:51 +13:00
public Q_SLOTS:
2016-06-15 18:43:10 +12:00
void invoke(void *vPtr) {
BaseFunctor *func = (BaseFunctor *)vPtr;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
(*func)(); // Invoke functor ...
delete func; // ... and then delete it
}
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
TFunctorInvoker() {}
2016-03-19 06:57:51 +13:00
};
2016-06-15 18:43:10 +12:00
#endif // TFUNCTORINVOKER_H