MPD 0.17~git
src/db_plugin.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2003-2011 The Music Player Daemon Project
00003  * http://www.musicpd.org
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along
00016  * with this program; if not, write to the Free Software Foundation, Inc.,
00017  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00018  */
00019 
00026 #ifndef MPD_DB_PLUGIN_H
00027 #define MPD_DB_PLUGIN_H
00028 
00029 #include <glib.h>
00030 #include <assert.h>
00031 #include <stdbool.h>
00032 
00033 struct config_param;
00034 struct db_selection;
00035 struct db_visitor;
00036 
00037 struct db {
00038         const struct db_plugin *plugin;
00039 };
00040 
00041 struct db_plugin {
00042         const char *name;
00043 
00047         struct db *(*init)(const struct config_param *param, GError **error_r);
00048 
00052         void (*finish)(struct db *db);
00053 
00057         bool (*open)(struct db *db, GError **error_r);
00058 
00062         void (*close)(struct db *db);
00063 
00070         struct song *(*get_song)(struct db *db, const char *uri,
00071                                  GError **error_r);
00072 
00076         bool (*visit)(struct db *db, const struct db_selection *selection,
00077                       const struct db_visitor *visitor, void *ctx,
00078                       GError **error_r);
00079 };
00080 
00081 G_GNUC_MALLOC
00082 static inline struct db *
00083 db_plugin_new(const struct db_plugin *plugin, const struct config_param *param,
00084               GError **error_r)
00085 {
00086         assert(plugin != NULL);
00087         assert(plugin->init != NULL);
00088         assert(plugin->finish != NULL);
00089         assert(plugin->get_song != NULL);
00090         assert(plugin->visit != NULL);
00091         assert(error_r == NULL || *error_r == NULL);
00092 
00093         struct db *db = plugin->init(param, error_r);
00094         assert(db == NULL || db->plugin == plugin);
00095         assert(db != NULL || error_r == NULL || *error_r != NULL);
00096 
00097         return db;
00098 }
00099 
00100 static inline void
00101 db_plugin_free(struct db *db)
00102 {
00103         assert(db != NULL);
00104         assert(db->plugin != NULL);
00105         assert(db->plugin->finish != NULL);
00106 
00107         db->plugin->finish(db);
00108 }
00109 
00110 static inline bool
00111 db_plugin_open(struct db *db, GError **error_r)
00112 {
00113         assert(db != NULL);
00114         assert(db->plugin != NULL);
00115 
00116         return db->plugin->open != NULL
00117                 ? db->plugin->open(db, error_r)
00118                 : true;
00119 }
00120 
00121 static inline void
00122 db_plugin_close(struct db *db)
00123 {
00124         assert(db != NULL);
00125         assert(db->plugin != NULL);
00126 
00127         if (db->plugin->close != NULL)
00128                 db->plugin->close(db);
00129 }
00130 
00131 static inline struct song *
00132 db_plugin_get_song(struct db *db, const char *uri, GError **error_r)
00133 {
00134         assert(db != NULL);
00135         assert(db->plugin != NULL);
00136         assert(db->plugin->get_song != NULL);
00137         assert(uri != NULL);
00138 
00139         return db->plugin->get_song(db, uri, error_r);
00140 }
00141 
00142 static inline bool
00143 db_plugin_visit(struct db *db, const struct db_selection *selection,
00144                 const struct db_visitor *visitor, void *ctx,
00145                 GError **error_r)
00146 {
00147         assert(db != NULL);
00148         assert(db->plugin != NULL);
00149         assert(selection != NULL);
00150         assert(visitor != NULL);
00151         assert(error_r == NULL || *error_r == NULL);
00152 
00153         return db->plugin->visit(db, selection, visitor, ctx, error_r);
00154 }
00155 
00156 #endif