Updated the Service interface

This commit is contained in:
emeric
2019-04-19 13:39:08 +02:00
parent 0aef48f3bc
commit 3df9a2ad54
11 changed files with 54 additions and 75 deletions
+26 -16
View File
@@ -19,26 +19,36 @@
#include <memory>
namespace CoverArt
template <typename T>
class ServiceProvider
{
class Grabber;
}
public:
namespace Scanner {
class MediaScanner;
}
template <class ...Args>
static
T&
create(Args&&... args)
{
assign(std::make_unique<T>(std::forward<Args>(args)...));
return *get();
}
static void assign(std::unique_ptr<T> service) { _service = std::move(service); }
static void clear() { _service.reset(); }
namespace Similarity {
class Searcher;
}
static T* get() { return _service.get(); }
struct Services
{
std::unique_ptr<CoverArt::Grabber> coverArtGrabber;
std::unique_ptr<Scanner::MediaScanner> mediaScanner;
std::unique_ptr<Similarity::Searcher> similaritySearcher;
private:
static std::unique_ptr<T> _service;
};
Services& getServices();
template <typename T>
std::unique_ptr<T> ServiceProvider<T>::_service = {};
template <typename T>
T*
getService()
{
return ServiceProvider<T>::get();
}
-31
View File
@@ -1,31 +0,0 @@
/*
* Copyright (C) 2019 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Services.hpp"
#include "cover/CoverArtGrabber.hpp"
#include "scanner/MediaScanner.hpp"
#include "similarity/SimilaritySearcher.hpp"
Services& getServices()
{
static Services services;
return services;
}
+9 -8
View File
@@ -34,7 +34,7 @@
#include "ui/LmsApplication.hpp"
#include "utils/Config.hpp"
#include "utils/Logger.hpp"
#include "Services.hpp"
#include "Service.hpp"
std::vector<std::string> generateWtConfig(std::string execPath)
{
@@ -127,15 +127,16 @@ int main(int argc, char* argv[])
UserInterface::LmsApplicationGroupContainer appGroups;
// Service initialization order is important
getServices().mediaScanner = std::make_unique<Scanner::MediaScanner>(*connectionPool);
Scanner::MediaScanner& mediaScanner {ServiceProvider<Scanner::MediaScanner>::create(*connectionPool)};
Similarity::FeaturesScannerAddon similarityFeaturesScannerAddon(*connectionPool);
getServices().mediaScanner->setAddon(similarityFeaturesScannerAddon);
getServices().coverArtGrabber = std::make_unique<CoverArt::Grabber>();
getServices().coverArtGrabber->setDefaultCover(server.appRoot() + "/images/unknown-cover.jpg");
mediaScanner.setAddon(similarityFeaturesScannerAddon);
getServices().similaritySearcher = std::make_unique<Similarity::Searcher>(similarityFeaturesScannerAddon);
CoverArt::Grabber& coverArtGrabber {ServiceProvider<CoverArt::Grabber>::create()};
coverArtGrabber.setDefaultCover(server.appRoot() + "/images/unknown-cover.jpg");
ServiceProvider<Similarity::Searcher>::create(similarityFeaturesScannerAddon);
API::Subsonic::SubsonicResource subsonicResource {*connectionPool};
@@ -153,7 +154,7 @@ int main(int argc, char* argv[])
// Start
LMS_LOG(MAIN, INFO) << "Starting media scanner...";
getServices().mediaScanner->start();
mediaScanner.start();
LMS_LOG(MAIN, INFO) << "Starting server...";
server.start();
@@ -167,7 +168,7 @@ int main(int argc, char* argv[])
server.stop();
LMS_LOG(MAIN, INFO) << "Stopping media scanner...";
getServices().mediaScanner->stop();
mediaScanner.stop();
LMS_LOG(MAIN, INFO) << "Clean stop!";
res = EXIT_SUCCESS;