aboutsummaryrefslogtreecommitdiffstats
path: root/src/playlist.h
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2008-09-16 19:11:39 +0200
committerMax Kellermann <max@duempel.org>2008-09-16 19:11:39 +0200
commit3ac0e9238308d8acea506fc95679fe8d5ae44f45 (patch)
tree189238754a1c09e66dcdd811efb32aef9c5dfe1a /src/playlist.h
parenta6858acfdead6285a010a31b1fc7c8a730e91788 (diff)
downloadmpd-3ac0e9238308d8acea506fc95679fe8d5ae44f45.tar.gz
mpd-3ac0e9238308d8acea506fc95679fe8d5ae44f45.tar.xz
mpd-3ac0e9238308d8acea506fc95679fe8d5ae44f45.zip
playlist: hide direct accesses in inline functions
Added functions like playlist_length(), playlist_get(), playlist_replace(), playlist_remove(). Don't access the mpdclient_playlist struct directly.
Diffstat (limited to '')
-rw-r--r--src/playlist.h73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/playlist.h b/src/playlist.h
index 946ca631c..377ed84a2 100644
--- a/src/playlist.h
+++ b/src/playlist.h
@@ -24,6 +24,7 @@
#include "libmpdclient.h"
+#include <assert.h>
#include <glib.h>
struct mpdclient;
@@ -49,6 +50,78 @@ playlist_clear(struct mpdclient_playlist *playlist);
/* free a playlist */
gint mpdclient_playlist_free(mpdclient_playlist_t *playlist);
+static inline guint
+playlist_length(const struct mpdclient_playlist *playlist)
+{
+ assert(playlist != NULL);
+ assert(playlist->list != NULL);
+
+ return playlist->list->len;
+}
+
+static inline gboolean
+playlist_is_empty(const struct mpdclient_playlist *playlist)
+{
+ return playlist_length(playlist) == 0;
+}
+
+static inline struct mpd_song *
+playlist_get(const struct mpdclient_playlist *playlist, guint idx)
+{
+ assert(idx < playlist_length(playlist));
+
+ return g_array_index(playlist->list, struct mpd_song *, idx);
+}
+
+static inline void
+playlist_append(struct mpdclient_playlist *playlist, const mpd_Song *song)
+{
+ mpd_Song *song2 = mpd_songDup(song);
+ g_array_append_val(playlist->list, song2);
+}
+
+static inline void
+playlist_set(const struct mpdclient_playlist *playlist, guint idx,
+ const mpd_Song *song)
+{
+ assert(idx < playlist_length(playlist));
+
+ g_array_index(playlist->list, mpd_Song *, idx) = mpd_songDup(song);
+}
+
+static inline void
+playlist_replace(struct mpdclient_playlist *playlist, guint idx,
+ const mpd_Song *song)
+{
+ mpd_freeSong(playlist_get(playlist, idx));
+ playlist_set(playlist, idx, song);
+}
+
+static inline void
+playlist_remove(struct mpdclient_playlist *playlist, guint idx)
+{
+ mpd_Song *song = playlist_get(playlist, idx);
+ mpd_freeSong(song);
+ g_array_remove_index(playlist->list, idx);
+}
+
+static inline void
+playlist_swap(struct mpdclient_playlist *playlist, guint idx1, guint idx2)
+{
+ mpd_Song *song1 = playlist_get(playlist, idx1);
+ mpd_Song *song2 = playlist_get(playlist, idx2);
+ gint n;
+
+ /* update the songs position field */
+ n = song1->pos;
+ song1->pos = song2->pos;
+ song2->pos = n;
+
+ /* update the array */
+ g_array_index(playlist->list, struct mpd_song *, idx1) = song2;
+ g_array_index(playlist->list, struct mpd_song *, idx2) = song1;
+}
+
struct mpd_song *playlist_lookup_song(struct mpdclient *c, gint id);
struct mpd_song *playlist_get_song(struct mpdclient *c, gint index);