Add lazy initialisation helper.

This commit is contained in:
John Maguire 2016-02-11 13:45:50 +00:00
parent ea12cfacac
commit 4c8b1d8143
3 changed files with 61 additions and 42 deletions

View File

@ -0,0 +1,25 @@
#include <functional>
#include <memory>
template <typename T>
class Lazy {
public:
explicit Lazy(std::function<T*()> init) : init_(init) {}
T* get() {
if (!ptr_) {
ptr_.reset(init_());
}
return ptr_.get();
}
typename std::add_lvalue_reference<T>::type operator*() const {
return *ptr_;
}
T* operator->() const { return ptr_.get(); }
private:
std::function<T*()> init_;
std::unique_ptr<T> ptr_;
};

View File

@ -42,10 +42,25 @@ InternetService::InternetService(const QString& name, Application* app,
app_(app), app_(app),
model_(model), model_(model),
name_(name), name_(name),
append_to_playlist_(nullptr), append_to_playlist_([]() {
replace_playlist_(nullptr), return new QAction(
open_in_new_playlist_(nullptr), IconLoader::Load("media-playback-start", IconLoader::Base),
separator_(nullptr) {} tr("Append to current playlist"), nullptr);
}),
replace_playlist_([]() {
return new QAction(
IconLoader::Load("media-playback-start", IconLoader::Base),
tr("Replace current playlist"), nullptr);
}),
open_in_new_playlist_([]() {
return new QAction(IconLoader::Load("document-new", IconLoader::Base),
tr("Open in new playlist"), nullptr);
}),
separator_([]() {
QAction* action = new QAction(nullptr);
action->setSeparator(true);
return action;
}) {}
void InternetService::ShowUrlBox(const QString& title, const QString& url) { void InternetService::ShowUrlBox(const QString& title, const QString& url) {
QMessageBox url_box; QMessageBox url_box;
@ -64,50 +79,27 @@ void InternetService::ShowUrlBox(const QString& title, const QString& url) {
} }
QList<QAction*> InternetService::GetPlaylistActions() { QList<QAction*> InternetService::GetPlaylistActions() {
if (!separator_) {
separator_ = new QAction(this);
separator_->setSeparator(true);
}
return QList<QAction*>() << GetAppendToPlaylistAction() return QList<QAction*>() << GetAppendToPlaylistAction()
<< GetReplacePlaylistAction() << GetReplacePlaylistAction()
<< GetOpenInNewPlaylistAction() << separator_; << GetOpenInNewPlaylistAction() << separator_.get();
} }
QAction* InternetService::GetAppendToPlaylistAction() { QAction* InternetService::GetAppendToPlaylistAction() {
if (!append_to_playlist_) { connect(append_to_playlist_.get(), SIGNAL(triggered()), this,
append_to_playlist_ = new QAction(IconLoader::Load("media-playback-start", SLOT(AppendToPlaylist()));
IconLoader::Base), return append_to_playlist_.get();
tr("Append to current playlist"), this);
connect(append_to_playlist_, SIGNAL(triggered()), this,
SLOT(AppendToPlaylist()));
}
return append_to_playlist_;
} }
QAction* InternetService::GetReplacePlaylistAction() { QAction* InternetService::GetReplacePlaylistAction() {
if (!replace_playlist_) { connect(replace_playlist_.get(), SIGNAL(triggered()), this,
replace_playlist_ = new QAction(IconLoader::Load("media-playback-start", SLOT(ReplacePlaylist()));
IconLoader::Base), return replace_playlist_.get();
tr("Replace current playlist"), this);
connect(replace_playlist_, SIGNAL(triggered()), this,
SLOT(ReplacePlaylist()));
}
return replace_playlist_;
} }
QAction* InternetService::GetOpenInNewPlaylistAction() { QAction* InternetService::GetOpenInNewPlaylistAction() {
if (!open_in_new_playlist_) { connect(open_in_new_playlist_.get(), SIGNAL(triggered()), this,
open_in_new_playlist_ = new QAction(IconLoader::Load("document-new", SLOT(OpenInNewPlaylist()));
IconLoader::Base), return open_in_new_playlist_.get();
tr("Open in new playlist"), this);
connect(open_in_new_playlist_, SIGNAL(triggered()), this,
SLOT(OpenInNewPlaylist()));
}
return open_in_new_playlist_;
} }
void InternetService::AddItemToPlaylist(const QModelIndex& index, void InternetService::AddItemToPlaylist(const QModelIndex& index,

View File

@ -23,10 +23,12 @@
#ifndef INTERNET_CORE_INTERNETSERVICE_H_ #ifndef INTERNET_CORE_INTERNETSERVICE_H_
#define INTERNET_CORE_INTERNETSERVICE_H_ #define INTERNET_CORE_INTERNETSERVICE_H_
#include <QAction>
#include <QObject> #include <QObject>
#include <QList> #include <QList>
#include <QUrl> #include <QUrl>
#include "core/lazy.h"
#include "core/song.h" #include "core/song.h"
#include "playlist/playlistitem.h" #include "playlist/playlistitem.h"
#include "smartplaylists/generator.h" #include "smartplaylists/generator.h"
@ -83,7 +85,7 @@ class InternetService : public QObject {
virtual QString Icon() { return QString(); } virtual QString Icon() { return QString(); }
signals: signals:
void StreamError(const QString& message); void StreamError(const QString& message);
void StreamMetadataFound(const QUrl& original_url, const Song& song); void StreamMetadataFound(const QUrl& original_url, const Song& song);
@ -135,10 +137,10 @@ class InternetService : public QObject {
InternetModel* model_; InternetModel* model_;
QString name_; QString name_;
QAction* append_to_playlist_; Lazy<QAction> append_to_playlist_;
QAction* replace_playlist_; Lazy<QAction> replace_playlist_;
QAction* open_in_new_playlist_; Lazy<QAction> open_in_new_playlist_;
QAction* separator_; Lazy<QAction> separator_;
}; };
Q_DECLARE_METATYPE(InternetService*); Q_DECLARE_METATYPE(InternetService*);