From 1fd051516811eb17f89d8406138437360eb7882c Mon Sep 17 00:00:00 2001 From: John Maguire Date: Wed, 21 Nov 2012 16:03:50 +0100 Subject: [PATCH] Make variadic version of ConcurrentRun::Run() --- ext/libclementine-common/core/concurrentrun.h | 262 +++--------------- src/core/songloader.cpp | 33 +-- 2 files changed, 55 insertions(+), 240 deletions(-) diff --git a/ext/libclementine-common/core/concurrentrun.h b/ext/libclementine-common/core/concurrentrun.h index 6f46f9369..fedb21f2d 100644 --- a/ext/libclementine-common/core/concurrentrun.h +++ b/ext/libclementine-common/core/concurrentrun.h @@ -18,10 +18,9 @@ #ifndef CONCURRENTRUN_H #define CONCURRENTRUN_H -#include +#include -#include -#include +#include #include #include @@ -74,217 +73,39 @@ class ThreadFunctorBase : public QFutureInterface, public QRunnable virtual void run() = 0; }; -// Base implemenation for functions having a result to be returned -template +template class ThreadFunctor : public ThreadFunctorBase { public: - ThreadFunctor() {} + ThreadFunctor(std::function function, + Args... args) + : function_(std::bind(function, args...)) { + } - virtual void run() = 0; - - void End() { - this->reportResult(result_); + virtual void run() { + this->reportResult(function_()); this->reportFinished(); } - protected: - ReturnType result_; + private: + std::function function_; }; -// Base implementation for functions with void result -template -class ThreadFunctor >::type> - : public ThreadFunctorBase { +// Partial specialisation for void return type. +template +class ThreadFunctor : public ThreadFunctorBase { public: - ThreadFunctor() {} + ThreadFunctor(std::function function, + Args... args) + : function_(std::bind(function, args...)) { + } - virtual void run() = 0; - - void End() { + virtual void run() { + function_(); this->reportFinished(); } -}; -/* - ThreadFunctor with no arguments: -*/ -// Non-void result -template -class ThreadFunctor0 : public ThreadFunctor { -public: - ThreadFunctor0(std::tr1::function function) - : function_(function) - { } - - void run() { - this->result_ = function_(); - ThreadFunctor::End(); - } - -private: - std::tr1::function function_; -}; - -// Void result -template -class ThreadFunctor0 >::type> - : public ThreadFunctor { -public: - ThreadFunctor0(std::tr1::function function) - : function_(function) - { } - - void run() { - function_(); - ThreadFunctor::End(); - } - -private: - std::tr1::function function_; -}; - -/* - ThreadFunctor with one argument: -*/ -// Non-void result -template -class ThreadFunctor1 : public ThreadFunctor { -public: - ThreadFunctor1(std::tr1::function function, - const Arg& arg) - : function_(function), - arg_(arg) - { } - - void run() { - this->result_ = function_(arg_); - ThreadFunctor::End(); - } - -private: - std::tr1::function function_; - Arg arg_; -}; - -// Void result -template -class ThreadFunctor1 >::type> - : public ThreadFunctor { -public: - ThreadFunctor1(std::tr1::function function, - const Arg& arg) - : function_(function), - arg_(arg) - { } - - void run() { - function_(arg_); - ThreadFunctor::End(); - } - -private: - std::tr1::function function_; - Arg arg_; -}; - -/* - ThreadFunctor with two arguments: -*/ -// Non-void result -template -class ThreadFunctor2 : public ThreadFunctor { -public: - ThreadFunctor2(std::tr1::function function, - const Arg1& arg1, const Arg2& arg2) - : function_(function), - arg1_(arg1), - arg2_(arg2) - { } - - void run() { - this->result_ = function_(arg1_, arg2_); - ThreadFunctor::End(); - } - -private: - std::tr1::function function_; - Arg1 arg1_; - Arg2 arg2_; -}; - -// Void result -template -class ThreadFunctor2 >::type> - : public ThreadFunctor { -public: - ThreadFunctor2(std::tr1::function function, - const Arg1& arg1, const Arg2& arg2) - : function_(function), - arg1_(arg1), - arg2_(arg2) - { } - - void run() { - function_(arg1_, arg2_); - ThreadFunctor::End(); - } - -private: - std::tr1::function function_; - Arg1 arg1_; - Arg2 arg2_; -}; - -/* - ThreadFunctor with three arguments: -*/ -// Non-void result -template -class ThreadFunctor3 : public ThreadFunctor { -public: - ThreadFunctor3(std::tr1::function function, - const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) - : function_(function), - arg1_(arg1), - arg2_(arg2), - arg3_(arg3) - { } - - void run() { - this->result_ = function_(arg1_, arg2_, arg3_); - ThreadFunctor::End(); - } - -private: - std::tr1::function function_; - Arg1 arg1_; - Arg2 arg2_; - Arg3 arg3_; -}; - -// Void result -template -class ThreadFunctor3 >::type> - : public ThreadFunctor { -public: - ThreadFunctor3(std::tr1::function function, - const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) - : function_(function), - arg1_(arg1), - arg2_(arg2), - arg3_(arg3) - { } - - void run() { - function_(arg1_, arg2_, arg3_); - ThreadFunctor::End(); - } - -private: - std::tr1::function function_; - Arg1 arg1_; - Arg2 arg2_; - Arg3 arg3_; + private: + std::function function_; }; @@ -293,39 +114,32 @@ private: */ namespace ConcurrentRun { - template + // Empty argument form. + template QFuture Run( QThreadPool* threadpool, - std::tr1::function function) { - - return (new ThreadFunctor0(function))->Start(threadpool); + std::function function) { + return (new ThreadFunctor(function))->Start(threadpool); } - template + // Function object with arguments form. + template QFuture Run( QThreadPool* threadpool, - std::tr1::function function, - const Arg& arg) { - - return (new ThreadFunctor1(function, arg))->Start(threadpool); + std::function function, + const Args&... args) { + return (new ThreadFunctor( + function, args...))->Start(threadpool); } - template + // Support passing C function pointers instead of function objects. + template QFuture Run( QThreadPool* threadpool, - std::tr1::function function, - const Arg1& arg1, const Arg2& arg2) { - - return (new ThreadFunctor2(function, arg1, arg2))->Start(threadpool); - } - - template - QFuture Run( - QThreadPool* threadpool, - std::tr1::function function, - const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) { - - return (new ThreadFunctor3(function, arg1, arg2, arg3))->Start(threadpool); + ReturnType (*function) (Args...), + const Args&... args) { + return Run( + threadpool, std::function(function), args...); } } diff --git a/src/core/songloader.cpp b/src/core/songloader.cpp index f7303533b..069f22e86 100644 --- a/src/core/songloader.cpp +++ b/src/core/songloader.cpp @@ -15,8 +15,22 @@ along with Clementine. If not, see . */ -#include "config.h" #include "songloader.h" + +#include + +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_AUDIOCD +# include +#endif + +#include "config.h" #include "core/concurrentrun.h" #include "core/logging.h" #include "core/song.h" @@ -34,19 +48,6 @@ #include "podcasts/podcastservice.h" #include "podcasts/podcasturlloader.h" -#include -#include -#include -#include -#include -#include - -#include - -#ifdef HAVE_AUDIOCD -# include -#endif - QSet SongLoader::sRawUriSchemes; const int SongLoader::kDefaultTimeout = 5000; @@ -230,7 +231,7 @@ SongLoader::Result SongLoader::LoadLocal(const QString& filename, bool block, if (QFileInfo(filename).isDir()) { if (!block) { ConcurrentRun::Run(&thread_pool_, - boost::bind(&SongLoader::LoadLocalDirectoryAndEmit, this, filename)); + std::bind(&SongLoader::LoadLocalDirectoryAndEmit, this, filename)); return WillLoadAsync; } else { LoadLocalDirectory(filename); @@ -263,7 +264,7 @@ SongLoader::Result SongLoader::LoadLocal(const QString& filename, bool block, // It's a playlist! if (!block) { ConcurrentRun::Run(&thread_pool_, - boost::bind(&SongLoader::LoadPlaylistAndEmit, this, parser, filename)); + std::bind(&SongLoader::LoadPlaylistAndEmit, this, parser, filename)); return WillLoadAsync; } else { LoadPlaylist(parser, filename);