Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/Platform.cpp

Issue 29535600: Issue 5617 - Provide with async executor bound to Platform in the future (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created Sept. 4, 2017, 12:03 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/Platform.cpp
diff --git a/src/Platform.cpp b/src/Platform.cpp
index 0e80c142086086fe21e9b78123a1d04e7c8809fa..35859444a8db9ae5e5899b9ef82d7ec5bcf7f217 100644
--- a/src/Platform.cpp
+++ b/src/Platform.cpp
@@ -21,6 +21,7 @@
#include "DefaultTimer.h"
#include "DefaultWebRequest.h"
#include "DefaultFileSystem.h"
+#include <stdexcept>
using namespace AdblockPlus;
@@ -30,36 +31,23 @@ namespace
{
std::thread(task).detach();
}
-}
-TimerPtr AdblockPlus::CreateDefaultTimer()
-{
- return TimerPtr(new DefaultTimer());
-}
-
-FileSystemPtr AdblockPlus::CreateDefaultFileSystem(const Scheduler& scheduler, const std::string& basePath)
-{
- return FileSystemPtr(new DefaultFileSystem(scheduler, std::unique_ptr<DefaultFileSystemSync>(new DefaultFileSystemSync(basePath))));
-}
-
-WebRequestPtr AdblockPlus::CreateDefaultWebRequest(const Scheduler& scheduler, WebRequestSyncPtr syncImpl)
-{
- if (!syncImpl)
- syncImpl.reset(new DefaultWebRequestSync());
- return WebRequestPtr(new DefaultWebRequest(scheduler, std::move(syncImpl)));
+ template<typename T>
+ void ValidatePlatformCreationParameter(const std::unique_ptr<T>& param, const char* paramName)
+ {
+ if (!param)
+ throw std::logic_error(paramName + std::string(" must not be nullptr"));
+ }
}
-LogSystemPtr AdblockPlus::CreateDefaultLogSystem()
-{
- return LogSystemPtr(new DefaultLogSystem());
-}
+#define ASSIGN_PLATFORM_PARAM(param) ValidatePlatformCreationParameter(param = std::move(creationParameters.param), #param)
Platform::Platform(CreationParameters&& creationParameters)
{
- logSystem = creationParameters.logSystem ? std::move(creationParameters.logSystem) : CreateDefaultLogSystem();
- timer = creationParameters.timer ? std::move(creationParameters.timer) : CreateDefaultTimer();
- fileSystem = creationParameters.fileSystem ? std::move(creationParameters.fileSystem) : CreateDefaultFileSystem(::DummyScheduler);
- webRequest = creationParameters.webRequest ? std::move(creationParameters.webRequest) : CreateDefaultWebRequest(::DummyScheduler);
+ ASSIGN_PLATFORM_PARAM(logSystem);
+ ASSIGN_PLATFORM_PARAM(timer);
+ ASSIGN_PLATFORM_PARAM(fileSystem);
+ ASSIGN_PLATFORM_PARAM(webRequest);
}
Platform::~Platform()
@@ -125,4 +113,78 @@ IWebRequest& Platform::GetWebRequest()
LogSystem& Platform::GetLogSystem()
{
return *logSystem;
+}
+
+namespace
+{
+ class DefaultPlatform : public Platform
+ {
+ public:
+ typedef std::shared_ptr<Scheduler> AsyncExecutorPtr;
+ explicit DefaultPlatform(const AsyncExecutorPtr& asyncExecutor, CreationParameters&& creationParams)
+ : Platform(std::move(creationParams)), asyncExecutor(asyncExecutor)
+ {
+ }
+ ~DefaultPlatform()
+ {
+ asyncExecutor.reset();
sergei 2017/09/04 12:16:27 While the actual executor is not ready yet (https:
+ }
+ private:
+ AsyncExecutorPtr asyncExecutor;
+ };
+}
+
+Scheduler DefaultPlatformBuilder::GetDefaultAsyncExecutor()
+{
+ if (!defaultScheduler)
+ {
+ asyncExecutor = std::make_shared<Scheduler>(::DummyScheduler);
+ std::weak_ptr<Scheduler> weakExecutor = asyncExecutor;
+ defaultScheduler = [weakExecutor](const SchedulerTask& task)
+ {
+ if (auto executor = weakExecutor.lock())
+ {
+ (*executor)(task);
+ }
+ };
+ }
+ return defaultScheduler;
+}
+
+void DefaultPlatformBuilder::CreateDefaultTimer()
+{
+ timer.reset(new DefaultTimer());
+}
+
+void DefaultPlatformBuilder::CreateDefaultFileSystem(const std::string& basePath)
+{
+ fileSystem.reset(new DefaultFileSystem(GetDefaultAsyncExecutor(), std::unique_ptr<DefaultFileSystemSync>(new DefaultFileSystemSync(basePath))));
+}
+
+void DefaultPlatformBuilder::CreateDefaultWebRequest(std::unique_ptr<IWebRequestSync> webRequest)
+{
+ if (!webRequest)
+ webRequest.reset(new DefaultWebRequestSync());
+ this->webRequest.reset(new DefaultWebRequest(GetDefaultAsyncExecutor(), std::move(webRequest)));
+}
+
+void DefaultPlatformBuilder::CreateDefaultLogSystem()
+{
+ logSystem.reset(new DefaultLogSystem());
+}
+
+std::unique_ptr<Platform> DefaultPlatformBuilder::CreatePlatform()
+{
+ if (!logSystem)
+ CreateDefaultLogSystem();
+ if (!timer)
+ CreateDefaultTimer();
+ if (!fileSystem)
+ CreateDefaultFileSystem();
+ if (!webRequest)
+ CreateDefaultWebRequest();
+
+ std::unique_ptr<Platform> platform(new DefaultPlatform(asyncExecutor, std::move(*this)));
+ asyncExecutor.reset();
sergei 2017/09/04 12:16:27 This `reset` is merely to ensure that async execut
+ return platform;
}
« no previous file with comments | « shell/src/Main.cpp ('k') | test/DefaultFileSystem.cpp » ('j') | test/WebRequest.cpp » ('J')

Powered by Google App Engine
This is Rietveld