From 513731227550737966bf05f724aef4ff26c0b9a7 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Tue, 15 Mar 2022 21:59:12 +0100 Subject: [PATCH] afs: Replace ->init of afs tables by table operations. This is simpler, avoids the run-time initialization, and allows us to mark the instances of the operations structures constant. Improve the documentation a bit while at it. --- afs.c | 52 ++++++++++++++++++++++++++++++---------------------- afs.h | 26 +++++++++++--------------- aft.c | 19 +++++++------------ attribute.c | 17 ++++++----------- blob.c | 19 +++++++++---------- score.c | 16 +++++----------- 6 files changed, 68 insertions(+), 81 deletions(-) diff --git a/afs.c b/afs.c index 36c679fc..b017a7af 100644 --- a/afs.c +++ b/afs.c @@ -33,16 +33,26 @@ #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 { @@ -620,7 +630,7 @@ static void close_afs_tables(void) 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; } @@ -659,7 +669,7 @@ static int open_afs_tables(void) 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); @@ -668,7 +678,7 @@ static int open_afs_tables(void) if (ret >= 0) return ret; while (i) - afs_tables[--i].close(); + afs_tables[--i].ops->close(); return ret; } @@ -960,12 +970,10 @@ static int afs_poll(struct pollfd *fds, nfds_t nfds, int timeout) __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; @@ -1008,13 +1016,13 @@ static int com_init_callback(struct afs_callback_arg *aca) 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); @@ -1046,7 +1054,7 @@ static int com_init(struct command_context *cc, struct lls_parse_result *lpr) 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; @@ -1120,10 +1128,10 @@ __must_check int afs_event(enum afs_events event, struct para_buffer *pb, 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)); diff --git a/afs.h b/afs.h index 52aad791..0983692e 100644 --- a/afs.h +++ b/afs.h @@ -73,19 +73,15 @@ struct afsi_change_event_data { 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); }; @@ -233,7 +229,7 @@ int string_compare(const struct osl_object *obj1, const struct osl_object *obj2) 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); @@ -244,14 +240,14 @@ int clear_score_table(void); 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); @@ -279,7 +275,6 @@ int playlist_check_callback(struct afs_callback_arg *aca); /** 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); \ @@ -287,7 +282,8 @@ int playlist_check_callback(struct afs_callback_arg *aca); 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); diff --git a/aft.c b/aft.c index 1f1e1198..b4fabbc8 100644 --- a/aft.c +++ b/aft.c @@ -2648,15 +2648,10 @@ static int aft_event_handler(enum afs_events event, struct para_buffer *pb, } } -/** - * 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, +}; diff --git a/attribute.c b/attribute.c index 3ff70cb2..8c87fa30 100644 --- a/attribute.c +++ b/attribute.c @@ -491,14 +491,9 @@ static int attribute_create(const char *dir) 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, +}; diff --git a/blob.c b/blob.c index 70d55236..20d8cba8 100644 --- a/blob.c +++ b/blob.c @@ -625,22 +625,21 @@ static int blob_open(struct osl_table **table, &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) \ diff --git a/score.c b/score.c index c7a2411b..fa12663c 100644 --- a/score.c +++ b/score.c @@ -293,14 +293,8 @@ int clear_score_table(void) 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, +}; -- 2.39.5