mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-05 05:37:29 +02:00
Add API to access DBCommand factories so you can observe automatically created instances
This commit is contained in:
@@ -52,6 +52,18 @@
|
|||||||
#define DEFAULT_WORKER_THREADS 4
|
#define DEFAULT_WORKER_THREADS 4
|
||||||
#define MAX_WORKER_THREADS 16
|
#define MAX_WORKER_THREADS 16
|
||||||
|
|
||||||
|
|
||||||
|
DatabaseCommand*
|
||||||
|
DatabaseCommandFactory::newInstance()
|
||||||
|
{
|
||||||
|
DatabaseCommand* command = create();
|
||||||
|
emit created( command );
|
||||||
|
return command;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Database* Database::s_instance = 0;
|
Database* Database::s_instance = 0;
|
||||||
|
|
||||||
|
|
||||||
@@ -248,9 +260,12 @@ void
|
|||||||
Database::registerCommand( DatabaseCommandFactory* commandFactory )
|
Database::registerCommand( DatabaseCommandFactory* commandFactory )
|
||||||
{
|
{
|
||||||
// this is ugly, but we don't have virtual static methods in C++ :(
|
// this is ugly, but we don't have virtual static methods in C++ :(
|
||||||
QScopedPointer<DatabaseCommand> command( commandFactory->create() );
|
QScopedPointer<DatabaseCommand> command( commandFactory->newInstance() );
|
||||||
|
|
||||||
const QString commandName = command->commandname();
|
const QString commandName = command->commandname();
|
||||||
|
const QString className = command->metaObject()->className();
|
||||||
|
|
||||||
|
tDebug() << "Registering command" << commandName << "from class" << className;
|
||||||
|
|
||||||
if( m_commandFactories.keys().contains( commandName ) )
|
if( m_commandFactories.keys().contains( commandName ) )
|
||||||
{
|
{
|
||||||
@@ -258,14 +273,31 @@ Database::registerCommand( DatabaseCommandFactory* commandFactory )
|
|||||||
}
|
}
|
||||||
Q_ASSERT( !m_commandFactories.keys().contains( commandName ) );
|
Q_ASSERT( !m_commandFactories.keys().contains( commandName ) );
|
||||||
|
|
||||||
|
m_commandNameClassNameMapping.insert( commandName, className );
|
||||||
m_commandFactories.insert( commandName, commandFactory );
|
m_commandFactories.insert( commandName, commandFactory );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
DatabaseCommandFactory*
|
||||||
|
Database::commandFactoryByClassName(const QString& className)
|
||||||
|
{
|
||||||
|
const QString commandName = m_commandNameClassNameMapping.key( className );
|
||||||
|
return commandFactoryByCommandName( commandName );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
DatabaseCommandFactory*
|
||||||
|
Database::commandFactoryByCommandName(const QString& commandName )
|
||||||
|
{
|
||||||
|
return m_commandFactories.value( commandName );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
DatabaseCommand*
|
DatabaseCommand*
|
||||||
Database::createCommandInstance( const QString& commandName )
|
Database::createCommandInstance( const QString& commandName )
|
||||||
{
|
{
|
||||||
DatabaseCommandFactory* factory = m_commandFactories.value( commandName );
|
DatabaseCommandFactory* factory = commandFactoryByCommandName( commandName );
|
||||||
|
|
||||||
if( !factory )
|
if( !factory )
|
||||||
{
|
{
|
||||||
@@ -273,7 +305,7 @@ Database::createCommandInstance( const QString& commandName )
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return factory->create();
|
return factory->newInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -36,14 +36,25 @@ class DatabaseWorker;
|
|||||||
class IdThreadWorker;
|
class IdThreadWorker;
|
||||||
|
|
||||||
|
|
||||||
struct DLLEXPORT DatabaseCommandFactory {
|
class DLLEXPORT DatabaseCommandFactory : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
virtual ~DatabaseCommandFactory() {};
|
virtual ~DatabaseCommandFactory() {};
|
||||||
|
DatabaseCommand* newInstance();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void created( DatabaseCommand* command );
|
||||||
|
|
||||||
|
protected:
|
||||||
virtual DatabaseCommand* create() const = 0;
|
virtual DatabaseCommand* create() const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class COMMAND>
|
template <class COMMAND>
|
||||||
struct DatabaseCommandFactoryImplementation : public DatabaseCommandFactory
|
class DatabaseCommandFactoryImplementation : public DatabaseCommandFactory
|
||||||
{
|
{
|
||||||
|
protected:
|
||||||
virtual COMMAND* create() const { return new COMMAND(); };
|
virtual COMMAND* create() const { return new COMMAND(); };
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -80,6 +91,11 @@ public:
|
|||||||
registerCommand( new DatabaseCommandFactoryImplementation<T>() );
|
registerCommand( new DatabaseCommandFactoryImplementation<T>() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T> DatabaseCommandFactory* commandFactory()
|
||||||
|
{
|
||||||
|
return commandFactoryByClassName( T::staticMetaObject.className() );
|
||||||
|
}
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void indexReady(); // search index
|
void indexReady(); // search index
|
||||||
void ready();
|
void ready();
|
||||||
@@ -96,6 +112,8 @@ private slots:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void registerCommand( DatabaseCommandFactory* commandFactory );
|
void registerCommand( DatabaseCommandFactory* commandFactory );
|
||||||
|
DatabaseCommandFactory* commandFactoryByClassName( const QString& className );
|
||||||
|
DatabaseCommandFactory* commandFactoryByCommandName( const QString& commandName );
|
||||||
DatabaseCommand* createCommandInstance( const QString& commandName );
|
DatabaseCommand* createCommandInstance( const QString& commandName );
|
||||||
|
|
||||||
bool m_ready;
|
bool m_ready;
|
||||||
@@ -105,7 +123,9 @@ private:
|
|||||||
QList< QPointer< DatabaseWorkerThread > > m_workerThreads;
|
QList< QPointer< DatabaseWorkerThread > > m_workerThreads;
|
||||||
IdThreadWorker* m_idWorker;
|
IdThreadWorker* m_idWorker;
|
||||||
int m_maxConcurrentThreads;
|
int m_maxConcurrentThreads;
|
||||||
|
|
||||||
QHash< QString, DatabaseCommandFactory* > m_commandFactories;
|
QHash< QString, DatabaseCommandFactory* > m_commandFactories;
|
||||||
|
QHash< QString, QString> m_commandNameClassNameMapping;
|
||||||
|
|
||||||
QHash< QThread*, DatabaseImpl* > m_implHash;
|
QHash< QThread*, DatabaseImpl* > m_implHash;
|
||||||
QMutex m_mutex;
|
QMutex m_mutex;
|
||||||
|
@@ -55,7 +55,7 @@ DatabaseCommand::_exec( DatabaseImpl* lib )
|
|||||||
{
|
{
|
||||||
//qDebug() << "RUNNING" << thread();
|
//qDebug() << "RUNNING" << thread();
|
||||||
m_state = RUNNING;
|
m_state = RUNNING;
|
||||||
emit running();
|
emitRunning();
|
||||||
exec( lib );
|
exec( lib );
|
||||||
m_state = FINISHED;
|
m_state = FINISHED;
|
||||||
//qDebug() << "FINISHED" << thread();
|
//qDebug() << "FINISHED" << thread();
|
||||||
|
@@ -64,7 +64,7 @@ public:
|
|||||||
|
|
||||||
// stuff to do once transaction applied ok.
|
// stuff to do once transaction applied ok.
|
||||||
// Don't change the database from in here, duh.
|
// Don't change the database from in here, duh.
|
||||||
void postCommit() { postCommitHook(); emit committed(); }
|
void postCommit() { postCommitHook(); emitCommitted(); }
|
||||||
virtual void postCommitHook(){};
|
virtual void postCommitHook(){};
|
||||||
|
|
||||||
void setSource( const Tomahawk::source_ptr& s );
|
void setSource( const Tomahawk::source_ptr& s );
|
||||||
@@ -87,12 +87,19 @@ public:
|
|||||||
}
|
}
|
||||||
void setGuid( const QString& g ) { m_guid = g; }
|
void setGuid( const QString& g ) { m_guid = g; }
|
||||||
|
|
||||||
void emitFinished() { emit finished(); }
|
void emitFinished() { emit finished(this); emit finished(); }
|
||||||
|
void emitCommitted() { emit committed(this); emit committed(); }
|
||||||
|
void emitRunning() { emit running(this); emit running(); }
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void running();
|
void running();
|
||||||
|
void running(DatabaseCommand*);
|
||||||
|
|
||||||
void finished();
|
void finished();
|
||||||
|
void finished(DatabaseCommand*);
|
||||||
|
|
||||||
void committed();
|
void committed();
|
||||||
|
void committed(DatabaseCommand*);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
State m_state;
|
State m_state;
|
||||||
|
Reference in New Issue
Block a user