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