GME  13
apr_hooks.h
Go to the documentation of this file.
00001 /* Licensed to the Apache Software Foundation (ASF) under one or more
00002  * contributor license agreements.  See the NOTICE file distributed with
00003  * this work for additional information regarding copyright ownership.
00004  * The ASF licenses this file to You under the Apache License, Version 2.0
00005  * (the "License"); you may not use this file except in compliance with
00006  * the License.  You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef APR_HOOKS_H
00018 #define APR_HOOKS_H
00019 
00020 #include "apu.h"
00021 /* For apr_array_header_t */
00022 #include "apr_tables.h"
00023 
00029 #ifdef __cplusplus
00030 extern "C" {
00031 #endif
00032 
00052 #ifdef APR_HOOK_PROBES_ENABLED
00053 #define APR_HOOK_INT_DCL_UD void *ud = NULL
00054 #else
00055 
00058 #define APR_HOOK_INT_DCL_UD
00059 
00069 #define APR_HOOK_PROBE_ENTRY(ud,ns,name,args)
00070 
00081 #define APR_HOOK_PROBE_RETURN(ud,ns,name,rv,args)
00082 
00094 #define APR_HOOK_PROBE_INVOKE(ud,ns,name,src,args)
00095 
00108 #define APR_HOOK_PROBE_COMPLETE(ud,ns,name,src,rv,args)
00109 #endif
00110 
00114 #define APR_IMPLEMENT_HOOK_GET_PROTO(ns,link,name) \
00115 link##_DECLARE(apr_array_header_t *) ns##_hook_get_##name(void)
00116 
00118 #define APR_DECLARE_EXTERNAL_HOOK(ns,link,ret,name,args) \
00119 typedef ret ns##_HOOK_##name##_t args; \
00120 link##_DECLARE(void) ns##_hook_##name(ns##_HOOK_##name##_t *pf, \
00121                                       const char * const *aszPre, \
00122                                       const char * const *aszSucc, int nOrder); \
00123 link##_DECLARE(ret) ns##_run_##name args; \
00124 APR_IMPLEMENT_HOOK_GET_PROTO(ns,link,name); \
00125 typedef struct ns##_LINK_##name##_t \
00126     { \
00127     ns##_HOOK_##name##_t *pFunc; \
00128     const char *szName; \
00129     const char * const *aszPredecessors; \
00130     const char * const *aszSuccessors; \
00131     int nOrder; \
00132     } ns##_LINK_##name##_t;
00133 
00135 #define APR_HOOK_STRUCT(members) \
00136 static struct { members } _hooks;
00137 
00139 #define APR_HOOK_LINK(name) \
00140     apr_array_header_t *link_##name;
00141 
00143 #define APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \
00144 link##_DECLARE(void) ns##_hook_##name(ns##_HOOK_##name##_t *pf,const char * const *aszPre, \
00145                                       const char * const *aszSucc,int nOrder) \
00146     { \
00147     ns##_LINK_##name##_t *pHook; \
00148     if(!_hooks.link_##name) \
00149         { \
00150         _hooks.link_##name=apr_array_make(apr_hook_global_pool,1,sizeof(ns##_LINK_##name##_t)); \
00151         apr_hook_sort_register(#name,&_hooks.link_##name); \
00152         } \
00153     pHook=apr_array_push(_hooks.link_##name); \
00154     pHook->pFunc=pf; \
00155     pHook->aszPredecessors=aszPre; \
00156     pHook->aszSuccessors=aszSucc; \
00157     pHook->nOrder=nOrder; \
00158     pHook->szName=apr_hook_debug_current; \
00159     if(apr_hook_debug_enabled) \
00160         apr_hook_debug_show(#name,aszPre,aszSucc); \
00161     } \
00162     APR_IMPLEMENT_HOOK_GET_PROTO(ns,link,name) \
00163     { \
00164         return _hooks.link_##name; \
00165     }
00166 
00179 #define APR_IMPLEMENT_EXTERNAL_HOOK_VOID(ns,link,name,args_decl,args_use) \
00180 APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \
00181 link##_DECLARE(void) ns##_run_##name args_decl \
00182     { \
00183     ns##_LINK_##name##_t *pHook; \
00184     int n; \
00185     APR_HOOK_INT_DCL_UD; \
00186 \
00187     APR_HOOK_PROBE_ENTRY(ud, ns, name, args_use); \
00188 \
00189     if(_hooks.link_##name) \
00190         { \
00191         pHook=(ns##_LINK_##name##_t *)_hooks.link_##name->elts; \
00192         for(n=0 ; n < _hooks.link_##name->nelts ; ++n) \
00193             { \
00194             APR_HOOK_PROBE_INVOKE(ud, ns, name, (char *)pHook[n].szName, args_use); \
00195             pHook[n].pFunc args_use; \
00196             APR_HOOK_PROBE_COMPLETE(ud, ns, name, (char *)pHook[n].szName, 0, args_use); \
00197             } \
00198         } \
00199 \
00200     APR_HOOK_PROBE_RETURN(ud, ns, name, 0, args_use); \
00201 \
00202     }
00203 
00204 /* FIXME: note that this returns ok when nothing is run. I suspect it should
00205    really return decline, but that breaks Apache currently - Ben
00206 */
00222 #define APR_IMPLEMENT_EXTERNAL_HOOK_RUN_ALL(ns,link,ret,name,args_decl,args_use,ok,decline) \
00223 APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \
00224 link##_DECLARE(ret) ns##_run_##name args_decl \
00225     { \
00226     ns##_LINK_##name##_t *pHook; \
00227     int n; \
00228     ret rv = ok; \
00229     APR_HOOK_INT_DCL_UD; \
00230 \
00231     APR_HOOK_PROBE_ENTRY(ud, ns, name, args_use); \
00232 \
00233     if(_hooks.link_##name) \
00234         { \
00235         pHook=(ns##_LINK_##name##_t *)_hooks.link_##name->elts; \
00236         for(n=0 ; n < _hooks.link_##name->nelts ; ++n) \
00237             { \
00238             APR_HOOK_PROBE_INVOKE(ud, ns, name, (char *)pHook[n].szName, args_use); \
00239             rv=pHook[n].pFunc args_use; \
00240             APR_HOOK_PROBE_COMPLETE(ud, ns, name, (char *)pHook[n].szName, rv, args_use); \
00241             if(rv != ok && rv != decline) \
00242                 break; \
00243             rv = ok; \
00244             } \
00245         } \
00246 \
00247     APR_HOOK_PROBE_RETURN(ud, ns, name, rv, args_use); \
00248 \
00249     return rv; \
00250     }
00251 
00252 
00267 #define APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(ns,link,ret,name,args_decl,args_use,decline) \
00268 APR_IMPLEMENT_EXTERNAL_HOOK_BASE(ns,link,name) \
00269 link##_DECLARE(ret) ns##_run_##name args_decl \
00270     { \
00271     ns##_LINK_##name##_t *pHook; \
00272     int n; \
00273     ret rv = decline; \
00274     APR_HOOK_INT_DCL_UD; \
00275 \
00276     APR_HOOK_PROBE_ENTRY(ud, ns, name, args_use); \
00277 \
00278     if(_hooks.link_##name) \
00279         { \
00280         pHook=(ns##_LINK_##name##_t *)_hooks.link_##name->elts; \
00281         for(n=0 ; n < _hooks.link_##name->nelts ; ++n) \
00282             { \
00283             APR_HOOK_PROBE_INVOKE(ud, ns, name, (char *)pHook[n].szName, args_use); \
00284             rv=pHook[n].pFunc args_use; \
00285             APR_HOOK_PROBE_COMPLETE(ud, ns, name, (char *)pHook[n].szName, rv, args_use); \
00286 \
00287             if(rv != decline) \
00288                 break; \
00289             } \
00290         } \
00291 \
00292     APR_HOOK_PROBE_RETURN(ud, ns, name, rv, args_use); \
00293 \
00294     return rv; \
00295     }
00296 
00297     /* Hook orderings */
00299 #define APR_HOOK_REALLY_FIRST   (-10)
00300 
00301 #define APR_HOOK_FIRST          0
00302 
00303 #define APR_HOOK_MIDDLE         10
00304 
00305 #define APR_HOOK_LAST           20
00306 
00307 #define APR_HOOK_REALLY_LAST    30
00308 
00312 APU_DECLARE_DATA extern apr_pool_t *apr_hook_global_pool;
00313 
00318 APU_DECLARE_DATA extern int apr_hook_debug_enabled;
00319 
00323 APU_DECLARE_DATA extern const char *apr_hook_debug_current;
00324 
00330 APU_DECLARE(void) apr_hook_sort_register(const char *szHookName, 
00331                                         apr_array_header_t **aHooks);
00335 APU_DECLARE(void) apr_hook_sort_all(void);
00336 
00344 APU_DECLARE(void) apr_hook_debug_show(const char *szName,
00345                                       const char * const *aszPre,
00346                                       const char * const *aszSucc);
00347 
00351 APU_DECLARE(void) apr_hook_deregister_all(void);
00352 
00354 #ifdef __cplusplus
00355 }
00356 #endif
00357 
00358 #endif /* APR_HOOKS_H */