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

View File

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