Commit 4be33502 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

Cache: Remove INTF template parameter

Instead, have the caller specify which type they want.
This allows us avoid using those ugly std::static_pointer_cast
parent e6960e3e
......@@ -58,7 +58,7 @@ class IMedia
virtual bool removeLabel( LabelPtr label ) = 0;
virtual MoviePtr movie() = 0;
virtual const std::string& artist() const = 0;
virtual std::vector<std::shared_ptr<ILabel> > labels() = 0;
virtual std::vector<LabelPtr> labels() = 0;
virtual std::vector<VideoTrackPtr> videoTracks() = 0;
virtual std::vector<AudioTrackPtr> audioTracks() = 0;
// Returns the location of this file snapshot.
......
......@@ -151,7 +151,7 @@ std::vector<MediaPtr> Album::tracks() const
" LEFT JOIN " + policy::AlbumTrackTable::Name + " att ON att.media_id = med.id_media "
" WHERE att.album_id = ? ORDER BY att.disc_number, att.track_number";
m_tracks = Media::fetchAll( m_dbConnection, req, m_id );
m_tracks = Media::fetchAll<IMedia>( m_dbConnection, req, m_id );
m_tracksCached = true;
return m_tracks;
}
......@@ -214,7 +214,7 @@ std::vector<ArtistPtr> Album::artists() const
static const std::string req = "SELECT art.* FROM " + policy::ArtistTable::Name + " art "
"LEFT JOIN AlbumArtistRelation aar ON aar.id_artist = art.id_artist "
"WHERE aar.id_album = ?";
return Artist::fetchAll( m_dbConnection, req, m_id );
return Artist::fetchAll<IArtist>( m_dbConnection, req, m_id );
}
bool Album::addArtist( std::shared_ptr<Artist> artist )
......
......@@ -47,10 +47,10 @@ struct AlbumTable
};
}
class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable>
class Album : public IAlbum, public Cache<Album, policy::AlbumTable>
{
private:
typedef Cache<Album, IAlbum, policy::AlbumTable> _Cache;
using _Cache = Cache<Album, policy::AlbumTable>;
public:
Album( DBConnection dbConnection, sqlite::Row& row );
Album( const std::string& title );
......@@ -103,7 +103,7 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable>
mutable bool m_tracksCached;
mutable std::mutex m_tracksLock;
friend class Cache<Album, IAlbum, policy::AlbumTable>;
friend _Cache;
friend struct policy::AlbumTable;
};
......
......@@ -44,10 +44,10 @@ struct AlbumTrackTable
};
}
class AlbumTrack : public IAlbumTrack, public Cache<AlbumTrack, IAlbumTrack, policy::AlbumTrackTable>
class AlbumTrack : public IAlbumTrack, public Cache<AlbumTrack, policy::AlbumTrackTable>
{
private:
typedef Cache<AlbumTrack, IAlbumTrack, policy::AlbumTrackTable> _Cache;
using _Cache = Cache<AlbumTrack, policy::AlbumTrackTable>;
public:
AlbumTrack( DBConnection dbConnection, sqlite::Row& row );
AlbumTrack(Media* media, unsigned int trackNumber, unsigned int albumId , unsigned int discNumber);
......@@ -80,7 +80,7 @@ class AlbumTrack : public IAlbumTrack, public Cache<AlbumTrack, IAlbumTrack, pol
std::shared_ptr<Album> m_album;
friend class Cache<AlbumTrack, IAlbumTrack, policy::AlbumTrackTable>;
friend _Cache;
friend struct policy::AlbumTrackTable;
};
......
......@@ -80,7 +80,7 @@ std::vector<AlbumPtr> Artist::albums() const
return {};
static const std::string req = "SELECT * FROM " + policy::AlbumTable::Name + " alb "
"WHERE artist_id = ? ORDER BY release_year, title";
return Album::fetchAll( m_dbConnection, req, m_id );
return Album::fetchAll<IAlbum>( m_dbConnection, req, m_id );
}
std::vector<MediaPtr> Artist::media() const
......@@ -90,7 +90,7 @@ std::vector<MediaPtr> Artist::media() const
static const std::string req = "SELECT med.* FROM " + policy::MediaTable::Name + " med "
"LEFT JOIN MediaArtistRelation mar ON mar.id_media = med.id_media "
"WHERE mar.id_artist = ?";
return Media::fetchAll( m_dbConnection, req, m_id );
return Media::fetchAll<IMedia>( m_dbConnection, req, m_id );
}
else
{
......@@ -99,7 +99,7 @@ std::vector<MediaPtr> Artist::media() const
static const std::string req = "SELECT med.* FROM " + policy::MediaTable::Name + " med "
"LEFT JOIN MediaArtistRelation mar ON mar.id_media = med.id_media "
"WHERE mar.id_artist IS NULL";
return Media::fetchAll( m_dbConnection, req );
return Media::fetchAll<IMedia>( m_dbConnection, req );
}
}
......
......@@ -40,10 +40,10 @@ struct ArtistTable
};
}
class Artist : public IArtist, public Cache<Artist, IArtist, policy::ArtistTable>
class Artist : public IArtist, public Cache<Artist, policy::ArtistTable>
{
private:
using _Cache = Cache<Artist, IArtist, policy::ArtistTable>;
using _Cache = Cache<Artist, policy::ArtistTable>;
public:
Artist( DBConnection dbConnection, sqlite::Row& row );
......
......@@ -39,8 +39,10 @@ struct AudioTrackTable
};
}
class AudioTrack : public IAudioTrack, public Cache<AudioTrack, IAudioTrack, policy::AudioTrackTable>
class AudioTrack : public IAudioTrack, public Cache<AudioTrack, policy::AudioTrackTable>
{
using _Cache = Cache<AudioTrack, policy::AudioTrackTable>;
public:
AudioTrack( DBConnection dbConnection, sqlite::Row& row );
AudioTrack(const std::string& codec, unsigned int bitrate, unsigned int sampleRate, unsigned int nbChannels, const std::string& language, const std::string& desc , unsigned int mediaId);
......@@ -71,7 +73,6 @@ class AudioTrack : public IAudioTrack, public Cache<AudioTrack, IAudioTrack, pol
private:
typedef Cache<AudioTrack, IAudioTrack, policy::AudioTrackTable> _Cache;
friend struct policy::AudioTrackTable;
};
......
......@@ -104,14 +104,14 @@ std::vector<MediaPtr> Folder::files()
{
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name +
" WHERE folder_id = ?";
return Media::fetchAll( m_dbConection, req, m_id );
return Media::fetchAll<IMedia>( m_dbConection, req, m_id );
}
std::vector<FolderPtr> Folder::folders()
{
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name +
" WHERE id_parent = ?";
return fetchAll( m_dbConection, req, m_id );
return fetchAll<IFolder>( m_dbConection, req, m_id );
}
FolderPtr Folder::parent()
......
......@@ -52,9 +52,9 @@ struct FolderCache
}
class Folder : public IFolder, public Cache<Folder, IFolder, policy::FolderTable, policy::FolderCache>
class Folder : public IFolder, public Cache<Folder, policy::FolderTable, policy::FolderCache>
{
using _Cache = Cache<Folder, IFolder, policy::FolderTable, policy::FolderCache>;
using _Cache = Cache<Folder, policy::FolderTable, policy::FolderCache>;
public:
Folder( DBConnection dbConnection, sqlite::Row& row );
......
......@@ -60,7 +60,7 @@ std::vector<MediaPtr> Label::files()
static const std::string req = "SELECT f.* FROM " + policy::MediaTable::Name + " f "
"LEFT JOIN LabelFileRelation lfr ON lfr.id_media = f.id_media "
"WHERE lfr.id_label = ?";
return Media::fetchAll( m_dbConnection, req, m_id );
return Media::fetchAll<IMedia>( m_dbConnection, req, m_id );
}
LabelPtr Label::create(DBConnection dbConnection, const std::string& name )
......
......@@ -50,10 +50,10 @@ struct LabelCachePolicy
}
class Label : public ILabel, public Cache<Label, ILabel, policy::LabelTable, policy::LabelCachePolicy>
class Label : public ILabel, public Cache<Label, policy::LabelTable, policy::LabelCachePolicy>
{
private:
typedef Cache<Label, ILabel, policy::LabelTable, policy::LabelCachePolicy> _Cache;
using _Cache = Cache<Label, policy::LabelTable, policy::LabelCachePolicy>;
public:
Label( DBConnection dbConnection, sqlite::Row& row );
Label( const std::string& name );
......@@ -70,7 +70,7 @@ class Label : public ILabel, public Cache<Label, ILabel, policy::LabelTable, pol
unsigned int m_id;
std::string m_name;
friend class Cache<Label, ILabel, policy::LabelTable>;
friend _Cache;
friend struct policy::LabelTable;
};
......
......@@ -158,12 +158,12 @@ bool Media::setShowEpisode(ShowEpisodePtr showEpisode)
return true;
}
std::vector<std::shared_ptr<ILabel> > Media::labels()
std::vector<LabelPtr> Media::labels()
{
static const std::string req = "SELECT l.* FROM " + policy::LabelTable::Name + " l "
"LEFT JOIN LabelFileRelation lfr ON lfr.id_label = l.id_label "
"WHERE lfr.id_media = ?";
return Label::fetchAll( m_dbConnection, req, m_id );
return Label::fetchAll<ILabel>( m_dbConnection, req, m_id );
}
int Media::playCount() const
......@@ -205,7 +205,7 @@ std::vector<VideoTrackPtr> Media::videoTracks()
{
static const std::string req = "SELECT * FROM " + policy::VideoTrackTable::Name +
" WHERE media_id = ?";
return VideoTrack::fetchAll( m_dbConnection, req, m_id );
return VideoTrack::fetchAll<IVideoTrack>( m_dbConnection, req, m_id );
}
bool Media::addAudioTrack( const std::string& codec, unsigned int bitrate,
......@@ -219,7 +219,7 @@ std::vector<AudioTrackPtr> Media::audioTracks()
{
static const std::string req = "SELECT * FROM " + policy::AudioTrackTable::Name +
" WHERE media_id = ?";
return AudioTrack::fetchAll( m_dbConnection, req, m_id );
return AudioTrack::fetchAll<IAudioTrack>( m_dbConnection, req, m_id );
}
const std::string &Media::snapshot()
......
......@@ -57,10 +57,10 @@ struct MediaCache
};
}
class Media : public IMedia, public Cache<Media, IMedia, policy::MediaTable, policy::MediaCache>
class Media : public IMedia, public Cache<Media, policy::MediaTable, policy::MediaCache>
{
private:
typedef Cache<Media, IMedia, policy::MediaTable, policy::MediaCache> _Cache;
using _Cache = Cache<Media, policy::MediaTable, policy::MediaCache>;
public:
// Those should be private, however the standard states that the expression
......@@ -88,7 +88,7 @@ class Media : public IMedia, public Cache<Media, IMedia, policy::MediaTable, pol
bool setShowEpisode( ShowEpisodePtr showEpisode );
virtual bool addLabel( LabelPtr label ) override;
virtual bool removeLabel( LabelPtr label ) override;
virtual std::vector<std::shared_ptr<ILabel> > labels() override;
virtual std::vector<LabelPtr> labels() override;
virtual int playCount() const override;
virtual const std::string& mrl() const override;
virtual MoviePtr movie() override;
......@@ -135,7 +135,7 @@ class Media : public IMedia, public Cache<Media, IMedia, policy::MediaTable, pol
ShowEpisodePtr m_showEpisode;
MoviePtr m_movie;
friend class Cache<Media, IMedia, policy::MediaTable, policy::MediaCache>;
friend _Cache;
friend struct policy::MediaTable;
};
......
......@@ -173,19 +173,19 @@ void MediaLibrary::setVerbosity(LogLevel v)
std::vector<MediaPtr> MediaLibrary::files()
{
return Media::fetchAll( m_dbConnection.get() );
return Media::fetchAll<IMedia>( m_dbConnection.get() );
}
std::vector<MediaPtr> MediaLibrary::audioFiles()
{
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name + " WHERE type = ? ORDER BY title";
return Media::fetchAll( m_dbConnection.get(), req, IMedia::Type::AudioType );
return Media::fetchAll<IMedia>( m_dbConnection.get(), req, IMedia::Type::AudioType );
}
std::vector<MediaPtr> MediaLibrary::videoFiles()
{
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name + " WHERE type = ? ORDER BY title";
return Media::fetchAll( m_dbConnection.get(), req, IMedia::Type::VideoType );
return Media::fetchAll<IMedia>( m_dbConnection.get(), req, IMedia::Type::VideoType );
}
MediaPtr MediaLibrary::file( const std::string& path )
......@@ -290,7 +290,7 @@ std::vector<AlbumPtr> MediaLibrary::albums()
{
static const std::string req = "SELECT * FROM " + policy::AlbumTable::Name +
" ORDER BY title ASC";
return Album::fetchAll( m_dbConnection.get(), req );
return Album::fetchAll<IAlbum>( m_dbConnection.get(), req );
}
ShowPtr MediaLibrary::show(const std::string& name)
......@@ -338,7 +338,7 @@ std::vector<ArtistPtr> MediaLibrary::artists() const
{
static const std::string req = "SELECT * FROM " + policy::ArtistTable::Name +
" WHERE nb_albums > 0";
return Artist::fetchAll( m_dbConnection.get(), req );
return Artist::fetchAll<IArtist>( m_dbConnection.get(), req );
}
void MediaLibrary::addMetadataService(std::unique_ptr<IMetadataService> service)
......
......@@ -121,7 +121,7 @@ std::vector<MediaPtr> Movie::files()
{
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name
+ " WHERE movie_id = ?";
return Media::fetchAll( m_dbConnection, req, m_id );
return Media::fetchAll<IMedia>( m_dbConnection, req, m_id );
}
bool Movie::createTable( DBConnection dbConnection )
......
......@@ -39,8 +39,10 @@ struct MovieTable
};
}
class Movie : public IMovie, public Cache<Movie, IMovie, policy::MovieTable>
class Movie : public IMovie, public Cache<Movie, policy::MovieTable>
{
using _Cache = Cache<Movie, policy::MovieTable>;
public:
Movie( DBConnection dbConnection, sqlite::Row& row );
Movie( const std::string& title );
......@@ -69,7 +71,6 @@ class Movie : public IMovie, public Cache<Movie, IMovie, policy::MovieTable>
std::string m_artworkUrl;
std::string m_imdbId;
typedef Cache<Movie, IMovie, policy::MovieTable> _Cache;
friend struct policy::MovieTable;
};
......
......@@ -142,12 +142,11 @@ void Parser::restore()
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name
+ " WHERE parsed = 0";
auto media = Media::fetchAll( m_dbConnection, req );
auto media = Media::fetchAll<Media>( m_dbConnection, req );
std::lock_guard<std::mutex> lock( m_lock );
for ( auto& it : media )
for ( auto& m : media )
{
auto m = std::static_pointer_cast<Media>( it );
m_tasks.push( new Task( m, m_services, m_callback ) );
}
}
......
......@@ -132,7 +132,7 @@ std::vector<ShowEpisodePtr> Show::episodes()
{
static const std::string req = "SELECT * FROM " + policy::ShowEpisodeTable::Name
+ " WHERE show_id = ?";
return ShowEpisode::fetchAll( m_dbConnection, req, m_id );
return ShowEpisode::fetchAll<IShowEpisode>( m_dbConnection, req, m_id );
}
bool Show::createTable(DBConnection dbConnection)
......
......@@ -42,8 +42,10 @@ struct ShowTable
};
}
class Show : public IShow, public Cache<Show, IShow, policy::ShowTable>
class Show : public IShow, public Cache<Show, policy::ShowTable>
{
using _Cache = Cache<Show, policy::ShowTable>;
public:
Show( DBConnection dbConnection, sqlite::Row& row );
Show( const std::string& name );
......@@ -75,9 +77,8 @@ class Show : public IShow, public Cache<Show, IShow, policy::ShowTable>
time_t m_lastSyncDate;
std::string m_tvdbId;
friend class Cache<Show, IShow, policy::ShowTable>;
friend _Cache;
friend struct policy::ShowTable;
typedef Cache<Show, IShow, policy::ShowTable> _Cache;
};
#endif // SHOW_H
......@@ -145,7 +145,7 @@ std::vector<MediaPtr> ShowEpisode::files()
{
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name
+ " WHERE show_episode_id = ?";
return Media::fetchAll( m_dbConnection, req, m_id );
return Media::fetchAll<IMedia>( m_dbConnection, req, m_id );
}
bool ShowEpisode::createTable( DBConnection dbConnection )
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment