muduo网络库——关于boost::function和boost::bind的使用

1. 基础用法

boost::function就像C#里面的delegate,可以指向任何函数,包括成员函数。当用bind把某个成员函数绑定到某个对象上时,我们就得到了一个闭包(closure)。

class Foo
{
public:
    void methodA();
    void methodInt(int a);
    void methodString(const string& str);
};

class Bar
{
public:
    void methodB();
}

boost::function<void()> f1; //无参数,无返回值

Foo foo;
f1 = boost::bind(&Foo::methodA, &foo);
f1();  //执行methodA()

Bar bar;
f1 = boost::bind(&Bar::methodB, &bar);
f1();  //执行methodB()

f1 = boost::bind(&Foo::methodInt, &foo, 5);
f1();  //执行methodInt(5)

f1 = boost::bind(&Foo::methodString, &foo, "Hello");
f1();  //执行methodString("Hello")

auto f2 = boost::bind(&Foo::methodInt, &foo, _1);
f2(12);  //执行methodInt(12)

2. 基于boost::function的线程设计

class Thread
{
public:
    typedef boost:function<void()> ThreadCallback;

    Thread(ThreadCallback cb)
        : cb_(cb)
    {

    }

    void start()
    {
        
    }

private:
    void run()
    {
        cb_();
    }

    ThreadCallback cb_;
}

使用方式:

class Foo   //无需继承
{
public:
    void runInThread();
    void runInAnotherThread(int);
}

Foo foo;
Thread t1(boost::bind(&Foo::runInThread, &foo));
Thread t2(boost::bind(&Foo::runInAnotherThread, &foo, 12));

t1.start();
t2.start();

个人理解:

这里主要是为了解耦,继承往往带来的问题就是强耦合。如果你熟悉Qt,马上可以联想到QThread的使用。一个类继承Qthread,然后重写虚函数run()。

class childThread : public QThread
{
public:
    ...
    virtual void run();
    ...
}

class Foo
{
    ...
private:
    std::shared_ptr<childThread > m_thread = nullptr; //定义一个智能指针类使用
}

3. muduo网络库的设计

书中给出了如下示例:

class connection;
class NetServer : boost::noncopyable
{
public:
    typedef boost::function<void (Connection*)> ConnectionCallback;
    typedef boost::function<void (Connection*, const void*, int len)> MessageCallback;

    NetServer(uint16_t port);
    ~NetServer();

    void registerConnectionCallback(const ConnectionCallback&);
    void registerMessageCallback(const MessageCallback&);
    void sendMessage(Connection*, const void* buf, int len);

private:
    ...
};

使用:

class EchoService
{
public:
    typedef boost::function<void(Connection*, const void*, int)> SendMessageCallback;
    
    EchoService(const SendMessageCallback& sendMsgCb)
        : sendMessageCb_(sendMsgCb)
    {

    }

    void onMessage(Connection* conn, const void* buf, int size)
    {
        sendMessageCb_(conn, buf, size);
    }

    void onConnection(Connection* conn)
    {
        
    }

private:
    SendMessageCallback sendMessageCb_;
};

int main()
{
    NetServer server(7);
    EchoService echo(boost::bind(&NetServer::sendMessage, &server, _1, _2, _3));
    server.registerMessageCallback(boost::bind(&EchoService::onMessage, &echo, _1, _2, _3));
    server.registerConnectionCallback(boost::bind(&EchoService::onConnection, &echo, _1));

    server.run();
}

说明:

boost库是muduo网络库依赖的第三方库,在c++11标准出来之后,新增了std::function和std::bind,功能与boost::function和boost::bind相同。详细介绍请参考我之前的文章:https://blog.csdn.net/www_dong/article/details/107946889

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值