【问题标题】:Union initialization for Apache module with C++使用 C++ 对 Apache 模块进行联合初始化
【发布时间】:2016-08-01 02:36:32
【问题描述】:

我的问题与

非常相似

Apache module command parser prototype

但是,我在 apr_cmd[] 中有更多的初始化,即我有类似的东西

 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 }
};

对于每次初始化,我都会收到“无法转换为 cmd_func”的编译错误。

Krystof 建议使用

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

如果我这样做,第一个错误就会消失。但是,其他错误仍然存​​在,因为我无法再次为第二个函数 set_authen_denied 重新定义 cmd_func。

感谢任何帮助。

【问题讨论】:

    标签: c++ apache visual-studio


    【解决方案1】:

    删除 hack 代码并制作工厂功能。

    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 };
    }
    

    示例
    http://melpon.org/wandbox/permlink/6yeQNOptQHCJh5Wf

    【讨论】:

    • 非常感谢,梦堂。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 2015-01-03
    相关资源
    最近更新 更多