Database{Plugin,Visitor}: pass references

This commit is contained in:
Max Kellermann 2012-08-07 21:32:08 +02:00
parent c6a0f5d3f9
commit 1a75abffa5
10 changed files with 95 additions and 91 deletions

View File

@ -31,6 +31,13 @@ extern "C" {
#include <functional> #include <functional>
static bool
AddSong(const char *playlist_path_utf8,
song &song, GError **error_r)
{
return spl_append_song(playlist_path_utf8, &song, error_r);
}
bool bool
addAllInToStoredPlaylist(const char *uri_utf8, const char *playlist_path_utf8, addAllInToStoredPlaylist(const char *uri_utf8, const char *playlist_path_utf8,
GError **error_r) GError **error_r)
@ -39,17 +46,17 @@ addAllInToStoredPlaylist(const char *uri_utf8, const char *playlist_path_utf8,
db_selection_init(&selection, uri_utf8, true); db_selection_init(&selection, uri_utf8, true);
using namespace std::placeholders; using namespace std::placeholders;
const auto f = std::bind(spl_append_song, playlist_path_utf8, _1, _2); const auto f = std::bind(AddSong, playlist_path_utf8, _1, _2);
return GetDatabase()->Visit(&selection, f, error_r); return GetDatabase()->Visit(selection, f, error_r);
} }
static bool static bool
SearchAddSong(const char *playlist_path_utf8, SearchAddSong(const char *playlist_path_utf8,
const struct locate_item_list *criteria, const struct locate_item_list *criteria,
struct song *song, GError **error_r) song &song, GError **error_r)
{ {
return !locate_song_search(song, criteria) || return !locate_song_search(&song, criteria) ||
spl_append_song(playlist_path_utf8, song, error_r); spl_append_song(playlist_path_utf8, &song, error_r);
} }
bool bool
@ -66,7 +73,7 @@ search_add_to_playlist(const char *uri, const char *playlist_path_utf8,
using namespace std::placeholders; using namespace std::placeholders;
const auto f = std::bind(SearchAddSong, playlist_path_utf8, const auto f = std::bind(SearchAddSong, playlist_path_utf8,
new_list, _1, _2); new_list, _1, _2);
bool success = GetDatabase()->Visit(&selection, f, error_r); bool success = GetDatabase()->Visit(selection, f, error_r);
locate_item_list_free(new_list); locate_item_list_free(new_list);

View File

@ -64,13 +64,13 @@ public:
/** /**
* Visit the selected entities. * Visit the selected entities.
*/ */
virtual bool Visit(const struct db_selection *selection, virtual bool Visit(const db_selection &selection,
VisitDirectory visit_directory, VisitDirectory visit_directory,
VisitSong visit_song, VisitSong visit_song,
VisitPlaylist visit_playlist, VisitPlaylist visit_playlist,
GError **error_r) const = 0; GError **error_r) const = 0;
bool Visit(const struct db_selection *selection, bool Visit(const db_selection &selection,
VisitDirectory visit_directory, VisitDirectory visit_directory,
VisitSong visit_song, VisitSong visit_song,
GError **error_r) const { GError **error_r) const {
@ -78,7 +78,7 @@ public:
VisitPlaylist(), error_r); VisitPlaylist(), error_r);
} }
bool Visit(const struct db_selection *selection, VisitSong visit_song, bool Visit(const db_selection &selection, VisitSong visit_song,
GError **error_r) const { GError **error_r) const {
return Visit(selection, VisitDirectory(), visit_song, error_r); return Visit(selection, VisitDirectory(), visit_song, error_r);
} }

View File

@ -40,79 +40,76 @@ extern "C" {
#include <set> #include <set>
static bool static bool
PrintDirectory(struct client *client, const struct directory *directory) PrintDirectory(struct client *client, const directory &directory)
{ {
if (!directory_is_root(directory)) if (!directory_is_root(&directory))
client_printf(client, "directory: %s\n", directory_get_path(directory)); client_printf(client, "directory: %s\n",
directory_get_path(&directory));
return true; return true;
} }
static void static void
print_playlist_in_directory(struct client *client, print_playlist_in_directory(struct client *client,
const struct directory *directory, const directory &directory,
const char *name_utf8) const char *name_utf8)
{ {
if (directory_is_root(directory)) if (directory_is_root(&directory))
client_printf(client, "playlist: %s\n", name_utf8); client_printf(client, "playlist: %s\n", name_utf8);
else else
client_printf(client, "playlist: %s/%s\n", client_printf(client, "playlist: %s/%s\n",
directory_get_path(directory), name_utf8); directory_get_path(&directory), name_utf8);
} }
static bool static bool
PrintSongBrief(struct client *client, struct song *song) PrintSongBrief(struct client *client, song &song)
{ {
assert(song != NULL); assert(song.parent != NULL);
assert(song->parent != NULL);
song_print_uri(client, song); song_print_uri(client, &song);
if (song->tag != NULL && song->tag->has_playlist) if (song.tag != NULL && song.tag->has_playlist)
/* this song file has an embedded CUE sheet */ /* this song file has an embedded CUE sheet */
print_playlist_in_directory(client, song->parent, print_playlist_in_directory(client, *song.parent, song.uri);
song->uri);
return true; return true;
} }
static bool static bool
PrintSongFull(struct client *client, struct song *song) PrintSongFull(struct client *client, song &song)
{ {
assert(song != NULL); assert(song.parent != NULL);
assert(song->parent != NULL);
song_print_info(client, song); song_print_info(client, &song);
if (song->tag != NULL && song->tag->has_playlist) if (song.tag != NULL && song.tag->has_playlist)
/* this song file has an embedded CUE sheet */ /* this song file has an embedded CUE sheet */
print_playlist_in_directory(client, song->parent, print_playlist_in_directory(client, *song.parent, song.uri);
song->uri);
return true; return true;
} }
static bool static bool
PrintPlaylistBrief(struct client *client, PrintPlaylistBrief(struct client *client,
const struct playlist_metadata *playlist, const playlist_metadata &playlist,
const struct directory *directory) const directory &directory)
{ {
print_playlist_in_directory(client, directory, playlist->name); print_playlist_in_directory(client, directory, playlist.name);
return true; return true;
} }
static bool static bool
PrintPlaylistFull(struct client *client, PrintPlaylistFull(struct client *client,
const struct playlist_metadata *playlist, const playlist_metadata &playlist,
const struct directory *directory) const directory &directory)
{ {
print_playlist_in_directory(client, directory, playlist->name); print_playlist_in_directory(client, directory, playlist.name);
#ifndef G_OS_WIN32 #ifndef G_OS_WIN32
struct tm tm; struct tm tm;
#endif #endif
char timestamp[32]; char timestamp[32];
time_t t = playlist->mtime; time_t t = playlist.mtime;
strftime(timestamp, sizeof(timestamp), strftime(timestamp, sizeof(timestamp),
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
"%Y-%m-%dT%H:%M:%SZ", "%Y-%m-%dT%H:%M:%SZ",
@ -138,15 +135,15 @@ db_selection_print(struct client *client, const struct db_selection *selection,
const auto p = std::bind(full ? PrintPlaylistFull : PrintPlaylistBrief, const auto p = std::bind(full ? PrintPlaylistFull : PrintPlaylistBrief,
client, _1, _2); client, _1, _2);
return GetDatabase()->Visit(selection, d, s, p, error_r); return GetDatabase()->Visit(*selection, d, s, p, error_r);
} }
static bool static bool
SearchPrintSong(struct client *client, const struct locate_item_list *criteria, SearchPrintSong(struct client *client, const struct locate_item_list *criteria,
struct song *song) song &song)
{ {
if (locate_song_search(song, criteria)) if (locate_song_search(&song, criteria))
song_print_info(client, song); song_print_info(client, &song);
return true; return true;
} }
@ -164,7 +161,7 @@ searchForSongsIn(struct client *client, const char *uri,
using namespace std::placeholders; using namespace std::placeholders;
const auto f = std::bind(SearchPrintSong, client, new_list, _1); const auto f = std::bind(SearchPrintSong, client, new_list, _1);
bool success = GetDatabase()->Visit(&selection, f, error_r); bool success = GetDatabase()->Visit(selection, f, error_r);
locate_item_list_free(new_list); locate_item_list_free(new_list);
@ -173,10 +170,10 @@ searchForSongsIn(struct client *client, const char *uri,
static bool static bool
MatchPrintSong(struct client *client, const struct locate_item_list *criteria, MatchPrintSong(struct client *client, const struct locate_item_list *criteria,
struct song *song) song &song)
{ {
if (locate_song_match(song, criteria)) if (locate_song_match(&song, criteria))
song_print_info(client, song); song_print_info(client, &song);
return true; return true;
} }
@ -191,7 +188,7 @@ findSongsIn(struct client *client, const char *uri,
using namespace std::placeholders; using namespace std::placeholders;
const auto f = std::bind(MatchPrintSong, client, criteria, _1); const auto f = std::bind(MatchPrintSong, client, criteria, _1);
return GetDatabase()->Visit(&selection, f, error_r); return GetDatabase()->Visit(selection, f, error_r);
} }
struct SearchStats { struct SearchStats {
@ -207,11 +204,11 @@ static void printSearchStats(struct client *client, SearchStats *stats)
static bool static bool
stats_visitor_song(SearchStats &stats, const struct locate_item_list *criteria, stats_visitor_song(SearchStats &stats, const struct locate_item_list *criteria,
struct song *song) song &song)
{ {
if (locate_song_match(song, criteria)) { if (locate_song_match(&song, criteria)) {
stats.numberOfSongs++; stats.numberOfSongs++;
stats.playTime += song_get_duration(song); stats.playTime += song_get_duration(&song);
} }
return true; return true;
@ -232,7 +229,7 @@ searchStatsForSongsIn(struct client *client, const char *name,
using namespace std::placeholders; using namespace std::placeholders;
const auto f = std::bind(stats_visitor_song, std::ref(stats), criteria, const auto f = std::bind(stats_visitor_song, std::ref(stats), criteria,
_1); _1);
if (!GetDatabase()->Visit(&selection, f, error_r)) if (!GetDatabase()->Visit(selection, f, error_r))
return false; return false;
printSearchStats(client, &stats); printSearchStats(client, &stats);
@ -267,13 +264,13 @@ typedef std::set<const char *, StringLess> StringSet;
static void static void
visitTag(struct client *client, StringSet &set, visitTag(struct client *client, StringSet &set,
struct song *song, enum tag_type tagType) song &song, enum tag_type tagType)
{ {
struct tag *tag = song->tag; struct tag *tag = song.tag;
bool found = false; bool found = false;
if (tagType == LOCATE_TAG_FILE_TYPE) { if (tagType == LOCATE_TAG_FILE_TYPE) {
song_print_uri(client, song); song_print_uri(client, &song);
return; return;
} }
@ -295,9 +292,9 @@ static bool
unique_tags_visitor_song(struct client *client, unique_tags_visitor_song(struct client *client,
enum tag_type tag_type, enum tag_type tag_type,
const struct locate_item_list *criteria, const struct locate_item_list *criteria,
StringSet &set, struct song *song) StringSet &set, song &song)
{ {
if (locate_song_match(song, criteria)) if (locate_song_match(&song, criteria))
visitTag(client, set, song, tag_type); visitTag(client, set, song, tag_type);
return true; return true;
@ -317,7 +314,7 @@ listAllUniqueTags(struct client *client, int type,
const auto f = std::bind(unique_tags_visitor_song, client, const auto f = std::bind(unique_tags_visitor_song, client,
(enum tag_type)type, criteria, std::ref(set), (enum tag_type)type, criteria, std::ref(set),
_1); _1);
if (!GetDatabase()->Visit(&selection, f, error_r)) if (!GetDatabase()->Visit(selection, f, error_r))
return false; return false;
if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES)

View File

@ -32,11 +32,10 @@ extern "C" {
#include <functional> #include <functional>
static bool static bool
AddToQueue(struct player_control *pc, struct song *song, AddToQueue(struct player_control *pc, song &song, GError **error_r)
GError **error_r)
{ {
enum playlist_result result = enum playlist_result result =
playlist_append_song(&g_playlist, pc, song, NULL); playlist_append_song(&g_playlist, pc, &song, NULL);
if (result != PLAYLIST_RESULT_SUCCESS) { if (result != PLAYLIST_RESULT_SUCCESS) {
g_set_error(error_r, playlist_quark(), result, g_set_error(error_r, playlist_quark(), result,
"Playlist error"); "Playlist error");
@ -54,15 +53,15 @@ addAllIn(struct player_control *pc, const char *uri, GError **error_r)
using namespace std::placeholders; using namespace std::placeholders;
const auto f = std::bind(AddToQueue, pc, _1, _2); const auto f = std::bind(AddToQueue, pc, _1, _2);
return GetDatabase()->Visit(&selection, f, error_r); return GetDatabase()->Visit(selection, f, error_r);
} }
static bool static bool
MatchAddSong(struct player_control *pc, MatchAddSong(struct player_control *pc,
const struct locate_item_list *criteria, const struct locate_item_list *criteria,
struct song *song, GError **error_r) song &song, GError **error_r)
{ {
return !locate_song_match(song, criteria) || return !locate_song_match(&song, criteria) ||
AddToQueue(pc, song, error_r); AddToQueue(pc, song, error_r);
} }
@ -75,15 +74,15 @@ findAddIn(struct player_control *pc, const char *uri,
using namespace std::placeholders; using namespace std::placeholders;
const auto f = std::bind(MatchAddSong, pc, criteria, _1, _2); const auto f = std::bind(MatchAddSong, pc, criteria, _1, _2);
return GetDatabase()->Visit(&selection, f, error_r); return GetDatabase()->Visit(selection, f, error_r);
} }
static bool static bool
SearchAddSong(struct player_control *pc, SearchAddSong(struct player_control *pc,
const struct locate_item_list *criteria, const struct locate_item_list *criteria,
struct song *song, GError **error_r) song &song, GError **error_r)
{ {
return !locate_song_search(song, criteria) || return !locate_song_search(&song, criteria) ||
AddToQueue(pc, song, error_r); AddToQueue(pc, song, error_r);
} }
@ -100,7 +99,7 @@ search_add_songs(struct player_control *pc, const char *uri,
using namespace std::placeholders; using namespace std::placeholders;
const auto f = std::bind(SearchAddSong, pc, new_list, _1, _2); const auto f = std::bind(SearchAddSong, pc, new_list, _1, _2);
bool success = GetDatabase()->Visit(&selection, f, error_r); bool success = GetDatabase()->Visit(selection, f, error_r);
locate_item_list_free(new_list); locate_item_list_free(new_list);

View File

@ -28,8 +28,9 @@ struct directory;
struct song; struct song;
struct playlist_metadata; struct playlist_metadata;
typedef std::function<bool(const struct directory *, GError **)> VisitDirectory; typedef std::function<bool(const directory &, GError **)> VisitDirectory;
typedef std::function<bool(struct song *, GError **)> VisitSong; typedef std::function<bool(struct song &, GError **)> VisitSong;
typedef std::function<bool(const struct playlist_metadata *, const struct directory *, GError **)> VisitPlaylist; typedef std::function<bool(const playlist_metadata &, const directory &,
GError **)> VisitPlaylist;
#endif #endif

View File

@ -291,21 +291,21 @@ directory::Walk(bool recursive,
if (visit_song) { if (visit_song) {
struct song *song; struct song *song;
directory_for_each_song(song, this) directory_for_each_song(song, this)
if (!visit_song(song, error_r)) if (!visit_song(*song, error_r))
return false; return false;
} }
if (visit_playlist) { if (visit_playlist) {
struct playlist_metadata *i; struct playlist_metadata *i;
directory_for_each_playlist(i, this) directory_for_each_playlist(i, this)
if (!visit_playlist(i, this, error_r)) if (!visit_playlist(*i, *this, error_r))
return false; return false;
} }
struct directory *child; struct directory *child;
directory_for_each_child(child, this) { directory_for_each_child(child, this) {
if (visit_directory && if (visit_directory &&
!visit_directory(child, error_r)) !visit_directory(*child, error_r))
return false; return false;
if (recursive && if (recursive &&

View File

@ -83,12 +83,12 @@ visit_tag(StringSet &artists, StringSet &albums, const struct tag *tag)
} }
static bool static bool
collect_stats_song(StringSet &artists, StringSet &albums, struct song *song) collect_stats_song(StringSet &artists, StringSet &albums, song &song)
{ {
++stats.song_count; ++stats.song_count;
if (song->tag != NULL) if (song.tag != NULL)
visit_tag(artists, albums, song->tag); visit_tag(artists, albums, song.tag);
return true; return true;
} }
@ -106,7 +106,7 @@ void stats_update(void)
using namespace std::placeholders; using namespace std::placeholders;
const auto f = std::bind(collect_stats_song, const auto f = std::bind(collect_stats_song,
std::ref(artists), std::ref(albums), _1); std::ref(artists), std::ref(albums), _1);
GetDatabase()->Visit(&selection, f, NULL); GetDatabase()->Visit(selection, f, NULL);
stats.artist_count = artists.size(); stats.artist_count = artists.size();
stats.album_count = albums.size(); stats.album_count = albums.size();

View File

@ -237,30 +237,30 @@ SimpleDatabase::LookupDirectory(const char *uri) const
} }
bool bool
SimpleDatabase::Visit(const struct db_selection *selection, SimpleDatabase::Visit(const db_selection &selection,
VisitDirectory visit_directory, VisitDirectory visit_directory,
VisitSong visit_song, VisitSong visit_song,
VisitPlaylist visit_playlist, VisitPlaylist visit_playlist,
GError **error_r) const GError **error_r) const
{ {
const struct directory *directory = LookupDirectory(selection->uri); const struct directory *directory = LookupDirectory(selection.uri);
if (directory == NULL) { if (directory == NULL) {
struct song *song; struct song *song;
if (visit_song && if (visit_song &&
(song = GetSong(selection->uri, NULL)) != NULL) (song = GetSong(selection.uri, NULL)) != NULL)
return visit_song(song, error_r); return visit_song(*song, error_r);
g_set_error(error_r, db_quark(), DB_NOT_FOUND, g_set_error(error_r, db_quark(), DB_NOT_FOUND,
"No such directory"); "No such directory");
return false; return false;
} }
if (selection->recursive && visit_directory && if (selection.recursive && visit_directory &&
!visit_directory(directory, error_r)) !visit_directory(*directory, error_r))
return false; return false;
db_lock(); db_lock();
bool ret = directory->Walk(selection->recursive, bool ret = directory->Walk(selection.recursive,
visit_directory, visit_song, visit_playlist, visit_directory, visit_song, visit_playlist,
error_r); error_r);
db_unlock(); db_unlock();

View File

@ -60,7 +60,7 @@ public:
virtual void Close() override; virtual void Close() override;
virtual struct song *GetSong(const char *uri_utf8, virtual struct song *GetSong(const char *uri_utf8,
GError **error_r) const override; GError **error_r) const override;
virtual bool Visit(const struct db_selection *selection, virtual bool Visit(const db_selection &selection,
VisitDirectory visit_directory, VisitDirectory visit_directory,
VisitSong visit_song, VisitSong visit_song,
VisitPlaylist visit_playlist, VisitPlaylist visit_playlist,

View File

@ -48,24 +48,24 @@ my_log_func(const gchar *log_domain, G_GNUC_UNUSED GLogLevelFlags log_level,
} }
static bool static bool
DumpDirectory(const struct directory *directory, GError **) DumpDirectory(const directory &directory, GError **)
{ {
cout << "D " << directory->path << endl; cout << "D " << directory.path << endl;
return true; return true;
} }
static bool static bool
DumpSong(struct song *song, GError **) DumpSong(song &song, GError **)
{ {
cout << "S " << song->parent->path << "/" << song->uri << endl; cout << "S " << song.parent->path << "/" << song.uri << endl;
return true; return true;
} }
static bool static bool
DumpPlaylist(const struct playlist_metadata *playlist, DumpPlaylist(const playlist_metadata &playlist,
const struct directory *directory, GError **) const directory &directory, GError **)
{ {
cout << "P " << directory->path << "/" << playlist->name << endl; cout << "P " << directory.path << "/" << playlist.name << endl;
return true; return true;
} }
@ -133,7 +133,7 @@ main(int argc, char **argv)
db_selection selection; db_selection selection;
db_selection_init(&selection, "", true); db_selection_init(&selection, "", true);
if (!db->Visit(&selection, DumpDirectory, DumpSong, DumpPlaylist, if (!db->Visit(selection, DumpDirectory, DumpSong, DumpPlaylist,
&error)) { &error)) {
db->Close(); db->Close();
delete db; delete db;