#include "sideband.h"
#include "command.h"
-static struct afs_table afs_tables[] = {
- {.init = aft_init, .name = "audio_files"},
- {.init = attribute_init, .name = "attributes"},
- {.init = score_init, .name = "scores"},
- {.init = moods_init, .name = "moods"},
- {.init = lyrics_init, .name = "lyrics"},
- {.init = images_init, .name = "images"},
- {.init = playlists_init, .name = "playlists"},
+/**
+ * The array of tables of the audio file selector.
+ *
+ * We organize them in an array to be able to loop over all tables.
+ */
+static const struct afs_table {
+ /** The name is no table operation, so define it here. */
+ const char * const name;
+ /** The only way to invoke the ops is via this pointer. */
+ const struct afs_table_operations *ops;
+} afs_tables[] = {
+ {.name = "audio_files", .ops = &aft_ops},
+ {.name = "attributes", .ops = &attr_ops},
+ {.name = "scores", .ops = &score_ops},
+ {.name = "moods", .ops = &moods_ops},
+ {.name = "lyrics", .ops = &lyrics_ops},
+ {.name = "images", .ops = &images_ops},
+ {.name = "playlists", .ops = &playlists_ops},
};
-
+/** Used to loop over the afs tables. */
#define NUM_AFS_TABLES ARRAY_SIZE(afs_tables)
struct command_task {
int i;
PARA_NOTICE_LOG("closing afs tables\n");
for (i = 0; i < NUM_AFS_TABLES; i++)
- afs_tables[i].close();
+ afs_tables[i].ops->close();
free(database_dir);
database_dir = NULL;
}
PARA_NOTICE_LOG("opening %zu osl tables in %s\n", NUM_AFS_TABLES,
database_dir);
for (i = 0; i < NUM_AFS_TABLES; i++) {
- ret = afs_tables[i].open(database_dir);
+ ret = afs_tables[i].ops->open(database_dir);
if (ret >= 0)
continue;
PARA_ERROR_LOG("could not open %s\n", afs_tables[i].name);
if (ret >= 0)
return ret;
while (i)
- afs_tables[--i].close();
+ afs_tables[--i].ops->close();
return ret;
}
__noreturn void afs_init(int socket_fd)
{
static struct sched s;
- int i, ret;
+ int ret;
register_signal_task(&s);
init_list_head(&afs_client_list);
- for (i = 0; i < NUM_AFS_TABLES; i++)
- afs_tables[i].init(&afs_tables[i]);
ret = open_afs_tables();
if (ret < 0)
goto out;
close_afs_tables();
get_database_dir();
for (i = 0; i < NUM_AFS_TABLES; i++) {
- struct afs_table *t = &afs_tables[i];
+ const struct afs_table *t = afs_tables + i;
if (!(table_mask & (1 << i)))
continue;
- if (!t->create)
+ if (!t->ops->create)
continue;
- ret = t->create(database_dir);
+ ret = t->ops->create(database_dir);
if (ret < 0) {
para_printf(&aca->pbout, "cannot create table %s\n",
t->name);
table_mask = 0;
for (i = 0; i < num_inputs; i++) {
for (j = 0; j < NUM_AFS_TABLES; j++) {
- struct afs_table *t = &afs_tables[j];
+ const struct afs_table *t = afs_tables + j;
if (strcmp(lls_input(i, lpr), t->name))
continue;
int i, ret;
for (i = 0; i < NUM_AFS_TABLES; i++) {
- struct afs_table *t = &afs_tables[i];
- if (!t->event_handler)
+ const struct afs_table *t = afs_tables + i;
+ if (!t->ops->event_handler)
continue;
- ret = t->event_handler(event, pb, data);
+ ret = t->ops->event_handler(event, pb, data);
if (ret < 0) {
PARA_CRIT_LOG("table %s, event %u: %s\n", t->name,
event, para_strerror(-ret));
struct afs_info *old_afsi;
};
-/** Function pointers for table handling. */
-struct afs_table {
- /** Initializes the other pointers in this struct. */
- void (*init)(struct afs_table *t);
- /** The name of this table. */
- const char *name;
- /** Gets called on startup and on \p SIGHUP. */
+/** Methods for table startup/shutdown and event handling. */
+struct afs_table_operations {
+ /** Gets called on startup and on SIGHUP. */
int (*open)(const char *base_dir);
- /** Gets called on shutdown and on \p SIGHUP. */
+ /** Gets called on shutdown and on SIGHUP. */
void (*close)(void);
- /** Called by the \a init afs command. */
+ /** Called from the init command. */
int (*create)(const char *);
- /** Handles afs events. */
+ /** Handle events generated by other tables. See enum \ref afs_events. */
int (*event_handler)(enum afs_events event, struct para_buffer *pb,
void *data);
};
int for_each_matching_row(struct pattern_match_data *pmd);
/* score */
-void score_init(struct afs_table *t);
+extern const struct afs_table_operations score_ops;
int score_loop(osl_rbtree_loop_func *func, void *data);
int score_get_best(struct osl_row **aft_row, long *score);
int get_score_and_aft_row(struct osl_row *score_row, long *score, struct osl_row **aft_row);
bool row_belongs_to_score_table(const struct osl_row *aft_row);
/* attribute */
-void attribute_init(struct afs_table *t);
+extern const struct afs_table_operations attr_ops;
void get_attribute_bitmap(const uint64_t *atts, char *buf); /* needed by com_ls() */
int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum);
int get_attribute_text(uint64_t *atts, const char *delim, char **text);
int attribute_check_callback(struct afs_callback_arg *aca);
/* aft */
-void aft_init(struct afs_table *t);
+extern const struct afs_table_operations aft_ops;
int aft_get_row_of_path(const char *path, struct osl_row **row);
int aft_check_attributes(uint64_t att_mask, struct para_buffer *pb);
int open_and_update_audio_file(int *fd);
/** Define exported functions and a table pointer for an osl blob table. */
#define DECLARE_BLOB_SYMBOLS(table_name, cmd_prefix) \
- void table_name ## _init(struct afs_table *t); \
int cmd_prefix ## _get_name_by_id(uint32_t id, char **name); \
int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def); \
int cmd_prefix ## _get_def_by_name(const char *name, struct osl_object *def); \
char **name, struct osl_object *def); \
int table_name ##_event_handler(enum afs_events event, \
struct para_buffer *pb, void *data); \
- extern struct osl_table *table_name ## _table;
+ extern struct osl_table *table_name ## _table; \
+ extern const struct afs_table_operations table_name ## _ops;
/** \cond blob_symbols */
DECLARE_BLOB_SYMBOLS(lyrics, lyr);
}
}
-/**
- * Initialize the audio file table.
- *
- * \param t Pointer to the structure to be initialized.
- */
-void aft_init(struct afs_table *t)
-{
- t->open = aft_open;
- t->close = aft_close;
- t->create = aft_create;
- t->event_handler = aft_event_handler;
-}
+/** The audio file table contains information about known audio files. */
+const struct afs_table_operations aft_ops = {
+ .open = aft_open,
+ .close = aft_close,
+ .create = aft_create,
+ .event_handler = aft_event_handler,
+};
return osl(osl_create_table(&attribute_table_desc));
}
-/**
- * Initialize the attribute table structure.
- *
- * \param t The table structure to initialize.
- */
-void attribute_init(struct afs_table *t)
-{
- t->open = attribute_open;
- t->close = attribute_close;
- t->create = attribute_create;
-}
+/** The attribute table stores name/bitnum pairs. */
+const struct afs_table_operations attr_ops = { /* no event handler */
+ .open = attribute_open,
+ .close = attribute_close,
+ .create = attribute_create,
+};
&table_name ## _table_desc, dir); \
}
-/** Define the \p init function for this blob type. */
-#define DEFINE_BLOB_INIT(table_name) \
- void table_name ## _init(struct afs_table *t) \
- { \
- t->open = table_name ## _open; \
- t->close = table_name ## _close; \
- t->create = table_name ## _create;\
- t->event_handler = table_name ##_event_handler; \
- }
+/** Blob tables map integers to blobs. */
+#define DEFINE_BLOB_AFS_TABLE_OPS(table_name) \
+ const struct afs_table_operations table_name ## _ops = { \
+ .open = table_name ## _open, \
+ .close = table_name ## _close, \
+ .create = table_name ## _create, \
+ .event_handler = table_name ##_event_handler, \
+ };
/** Define all functions for this blob type. */
#define DEFINE_BLOB_FUNCTIONS(table_name, short_name, c_short_name) \
DEFINE_BLOB_OPEN(table_name) \
DEFINE_BLOB_CLOSE(table_name) \
DEFINE_BLOB_CREATE(table_name) \
- DEFINE_BLOB_INIT(table_name) \
+ DEFINE_BLOB_AFS_TABLE_OPS(table_name) \
DEFINE_BLOB_COMMAND(ls, LS, table_name, short_name, c_short_name) \
DEFINE_BLOB_COMMAND(cat, CAT, table_name, short_name, c_short_name) \
DEFINE_BLOB_COMMAND(add, ADD, table_name, short_name, c_short_name) \
return score_open(NULL);
}
-/**
- * Initialize the scoring subsystem.
- *
- * \param t The members of \a t are filled in by the function.
- */
-void score_init(struct afs_table *t)
-{
- t->name = score_table_desc.name;
- t->open = score_open;
- t->close = score_close;
-}
+/** The score table stores (aft row, score) pairs in memory. */
+const struct afs_table_operations score_ops = {
+ .open = score_open,
+ .close = score_close,
+};