2010年11月25日 星期四

[好code分享] 利用掛function pointer 對同一堆data structure做不同的操作

int ecmpIprtNextHopIterate(
    void    (*exefunc)(ecmpRtNhEntry_t* ,ecmpNhEntry_t *,void*),
    void * data
){
    ecmpRtNhEntry_t* ecmpRtNhEntry_p = NULL;
    ecmpNhEntry_t* ecmpNhEntry_p = NULL;
    int32 bits,hashVal;
    for ( bits = IPRT_ECMP_NETMASK-1; bits >= 0; bits--)
    {
        for(hashVal = HASHMOD-1; hashVal >= 0; hashVal--){
            TAILQ_FOREACH(ecmpRtNhEntry_p, &ecmpRtNhTable[bits][hashVal],link) 
            {
                TAILQ_FOREACH(ecmpNhEntry_p, &(ecmpRtNhEntry_p->nhList),link){
                    if(exefunc){
                        exefunc(ecmpRtNhEntry_p, ecmpNhEntry_p,data);
                    }
                }
            }
        }
    }
    return ZYNOS_E_NONE;
}

要一直對相同的一堆data structure做不同的處理時用的, 感覺還不錯

沒有留言:

張貼留言