From 7aae399239d02e48de9060dded1d0232310d2141 Mon Sep 17 00:00:00 2001 From: wm4 Date: Mon, 6 Aug 2012 17:45:17 +0200 Subject: m_config: support auto-allocated sub-structs Given your option struct has a field that is a pointer to another struct, this commit allows you to declare options that write into that other struct. The code in m_config will dereference the pointer field on its own if such an option is accessed. If the field is NULL on initialization of the containing m_config, the struct is automatically allocated. OPT_SUBSTRUCT() can be used to declare such a field. struct m_sub_options is used to describe the pointed-to struct, and includes size and defaults if the struct has to be allocated by m_config. --- m_option.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'm_option.h') diff --git a/m_option.h b/m_option.h index e0321d1de1..ab83027a85 100644 --- a/m_option.h +++ b/m_option.h @@ -52,6 +52,7 @@ extern const m_option_type_t m_option_type_print; extern const m_option_type_t m_option_type_print_func; extern const m_option_type_t m_option_type_print_func_param; extern const m_option_type_t m_option_type_subconfig; +extern const m_option_type_t m_option_type_subconfig_struct; extern const m_option_type_t m_option_type_imgfmt; extern const m_option_type_t m_option_type_afmt; @@ -148,6 +149,12 @@ struct m_opt_choice_alternatives { int value; }; +// m_option.priv points to this if M_OPT_TYPE_USE_SUBSTRUCT is used +struct m_sub_options { + const struct m_option *opts; + size_t size; + const void *defaults; +}; // FIXME: backward compatibility #define CONF_TYPE_FLAG (&m_option_type_flag) @@ -338,6 +345,10 @@ struct m_option { // takes no parameter. #define M_OPT_TYPE_OLD_SYNTAX_NO_PARAM (1 << 3) +// modify M_OPT_TYPE_HAS_CHILD so that m_option::p points to +// struct m_sub_options, instead of a direct m_option array. +#define M_OPT_TYPE_USE_SUBSTRUCT (1 << 4) + ///////////////////////////// Parser flags ///////////////////////////////// // On success parsers return the number of arguments consumed: 0 or 1. @@ -461,6 +472,12 @@ static inline void m_option_free(const m_option_t *opt, void *dst) #define OPT_CHOICE_(optname, varname, flags, choices, ...) OPT_GENERAL(optname, varname, flags, .priv = (void *)&(const struct m_opt_choice_alternatives[]){OPT_HELPER_REMOVEPAREN choices, {NULL}}, __VA_ARGS__) #define OPT_TIME(...) OPT_GENERAL(__VA_ARGS__, .type = &m_option_type_time) +// subconf must have the type struct m_sub_options. +// flagv should be M_OPT_MERGE or M_OPT_FLATTEN. +// varname refers to the field, that must be a pointer to a field described by +// the subconf struct. +#define OPT_SUBSTRUCT(varname, subconf, flagv) OPT_GENERAL("-", varname, flagv, .type = &m_option_type_subconfig_struct, .priv = (void*)&subconf) + #define OPT_BASE_STRUCT struct MPOpts #endif /* MPLAYER_M_OPTION_H */ -- cgit v1.2.3