类的成员函数作为回调函数的方法

21 篇文章 0 订阅
16 篇文章 0 订阅

“类中的成员” 函数不能作为本身的回调,如果非要这样弄可以有几个办法,

例如静态方式声明,提供this指针等,

针对static的话我想失去了回调在类中使用的意义,

网上可以搜索下提供this指针方式,之前搜索时候看到过


另外就是间接将类中成员变成回调,其过程就是利用间接的一个方法调用这个回调,然后设置回调函数的地方使用这个中间的方法(在此谢过给过帮助的同学)


如下我以使用zookeeper来说明(其中需要声明回调用于监测//)


/*

author: programer.moo@gamil.com

name: zk_frameword.h
 */

#ifndef _ZK_FRAMEWORK_H
#define _ZK_FRAMEWORK_H

#include <iostream>
#include <stdlib.h>
#include <tr1/functional>
#include "zookeeper/zookeeper.h"


namespace detail { class ZooKeeperWatcher; }

class ZKOP{
        public:
                ZKOP();
                ~ZKOP();

        public:
                typedef std::tr1::function<void(const char* path, int type, int state)> wrapFunc;
                typedef String_vector zkVector;

        public:

                bool zkInit(const std::string& host, const wrapFunc& func, const int timeout);
                void zkWatcher(const char* path, int type, int state);

                /*others*/


        private:
                zhandle_t* zkhandle;
                detail::ZooKeeperWatcher* context_;
                clientid_t clientid_;
};

#endif//endif zk_frame.h          


/*

name=zk_frame.cpp

author=programer.moo@gmail.com

*/

/**
 * we must wrap a class to call member function in ZKOP
 * because member function cannot be callback function
*/
namespace detail
{//这个就是包的中间层
        class ZooKeeperWatcher
        {/*{{{*/
                public:
                        typedef ZKOP::wrapFunc wrapFunc;

                public:
                        explicit ZooKeeperWatcher(const wrapFunc& func)
                                : func_(func)
                        {}  
                        void RunInWatcher(int type, int state, const char* path) {
                                func_(path, type, state);
                        }   

                private:
                        wrapFunc func_;
        }/*}}}*/;

        void watcher(zhandle_t* zh, int type, int state, const char* path, void* ctx)
        {/*{{{*/
                ZooKeeperWatcher* watcher = static_cast<ZooKeeperWatcher*>(ctx);
                watcher->RunInWatcher(type, state, path);
        }/*}}}*/
}//namespace detail
/


/**
 * 1,set debug level of log
 * 2,init zookeeper(connect host and set timeout and callback func)
 * 3,if fail then false else true
 * \param host indicate zookeeper server lists
 * \param fun indicate callback func
 * \param timeout indicate timeout of zookeeper
*/
bool ZKOP::zkInit(const std::string& host, const wrapFunc& func, const int timeout)
{/*{{{*/
        zoo_set_debug_level(ZOO_LOG_LEVEL_WARN);
        context_ = new detail::ZooKeeperWatcher(func);                使用中间层
        zkhandle = zookeeper_init(host.data(),
                        detail::watcher, 中间层的回调
                        timeout,
                        &clientid_,
                        context_,
                        0);

       if (NULL == zkhandle) {
                LOG(INFO) << "init zookeeper handle fail!\t"<< "host:" << host;
                return false;
        }else{
                LOG(INFO) << "init zookeeper successful!";
                return true;
        }
}/*}}}*/

//

/*

name=main.cpp

*/

/*

省略代码

pzoo = std::tr1::shared_ptr<ZKOP>(new ZKOP());

*/                                                                                                                                                                                           

bool init_zk(){
        bool ret_init = pzoo->zkInit(FLAGS_zk_host,
                        std::tr1::bind(&ZKOP::zkWatcher,               绑定回调(像正常类中成员函数一样使用只不过通过中间层弄封的)
                                *pzoo,
                                std::tr1::placeholders::_1,
                                std::tr1::placeholders::_2,
                                std::tr1::placeholders::_3
                                ),  
                        FLAGS_zk_timeout);
        if(false == ret_init){
                return false;
        }   
        return true;
}



以上代码仅为片断,可以简单说明使用方法,有问题请指正,多谢

供参考交流





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值