0

My question is very similar to

Apache module command parser prototype

However, I have more initializations in apr_cmd[], i.e. I have something like

 static const command_rec apr_cmds[] =
{
 AP_INIT_TAKE1("analytics_ip", apr_cfg_set_analytics_ip, NULL, OR_ALL, ""),
 AP_INIT_TAKE1("AuthenDenied", set_authen_denied,  (void*)APR_OFFSETOF(config_rec, authen_denied), OR_AUTHCFG, ""),
 AP_INIT_FLAG("RefreshCookies", ap_set_flag_slot, (void*)APR_OFFSETOF(config_rec, refresh_cookies), OR_AUTHCFG, ""),
 { NULL }
};

For each initialization, I got "cannot convert to cmd_func" compiling error.

Krystof suggests use

#ifdef __cplusplus 
typedef const char *(*cmd_func) (cmd_parms *cmd, void *dummy, const char *path); 
#else 
typedef const char *(*cmd_func) (); 
#endif

If I do this, the first error will be gone. However, the other errors remain since I can't redefine cmd_func again for second function set_authen_denied.

Any help is appreciated.

Community
  • 1
  • 1
NickOSU
  • 65
  • 2
  • 8

1 Answers1

0

remove hack code and make factory function.

namespace cmd_func_type{
    /** function to call for a no-args */
    using no_args_t = const char *(*) (cmd_parms *parms, void *mconfig);
    /** function to call for a raw-args */
    using raw_args_t = const char *(*) (cmd_parms *parms, void *mconfig, const char *args);
    /** function to call for a argv/argc */
    using take_argv_t = const char *(*) (cmd_parms *parms, void *mconfig, int argc, char *const argv[]);
    /** function to call for a take1 */
    using take1_t = const char *(*) (cmd_parms *parms, void *mconfig, const char *w);
    /** function to call for a take2 */
    using take2_t = const char *(*) (cmd_parms *parms, void *mconfig, const char *w, const char *w2);
    /** function to call for a take3 */
    using take3_t = const char *(*) (cmd_parms *parms, void *mconfig, const char *w, const char *w2, const char *w3);
    /** function to call for a flag */
    using flag_t = const char *(*) (cmd_parms *parms, void *mconfig, int on);
}
template<cmd_how args_how = NO_ARGS>
cmd_func make_cmd_func(cmd_func_type::no_args_t f){ cmd_func re; re.no_args = f; return re; }
template<cmd_how args_how = NO_ARGS> cmd_func make_cmd_func(cmd_func_type::raw_args_t);
template<>
cmd_func make_cmd_func<RAW_ARGS>(cmd_func_type::raw_args_t f){ cmd_func re; re.raw_args = f; return re; }
template<cmd_how args_how = TAKE_ARGV>
cmd_func make_cmd_func(cmd_func_type::take_argv_t f){ cmd_func re; re.take_argv = f; return re; }
template<>
cmd_func make_cmd_func<TAKE1>(cmd_func_type::take1_t f){ cmd_func re; re.take1 = f; return re; }
template<cmd_how args_how = TAKE2>
cmd_func make_cmd_func(cmd_func_type::take2_t f){ cmd_func re; re.take2 = f; return re; }
template<cmd_how args_how = TAKE3>
cmd_func make_cmd_func(cmd_func_type::take3_t f){ cmd_func re; re.take3 = f; return re; }
template<cmd_how args_how = FLAG>
cmd_func make_cmd_func(cmd_func_type::flag_t f){ cmd_func re; re.flag = f; return re; }

template<
    cmd_how args_how, typename DataPtr, typename CmdFuncType, 
    std::enable_if_t<std::is_pointer<DataPtr>::value || std::is_same<DataPtr, std::nullptr_t>::value, std::nullptr_t> = nullptr
>
command_rec make_command_rec(const char *name, CmdFuncType f, DataPtr cmd_data, int req_override, const char *errmsg){ 
    return {name, make_cmd_func<args_how>(f), (void*)(cmd_data), req_override, args_how, errmsg };
}

example
http://melpon.org/wandbox/permlink/6yeQNOptQHCJh5Wf

yumetodo
  • 1,147
  • 7
  • 19