zoukankan      html  css  js  c++  java
  • boost::bind和boost::function使用示例

    C++11已支持bindfunction,之前的不支持,但可以借助boost达到同样目的。看如下两段代码:

    1) 创建HDFS目录

    void hdfs::init()

    {

        if (0 == hdfsExists(fs, data_dirpath.c_str()))

        {

            LOG(INFO) << data_dirpath << " exists";

        }

        else

        {

            if (0 == hdfsCreateDirectory(fs, data_dirpath.c_str()))

            {

                LOG(INFO) << "create " << data_dirpath << " SUCCESS";

            }

        }

    }

    2) 创建本地目录

    void local::init()

    {

        if (0 == access(data_dirpath.c_str(), R_OK | W_OK | X_OK))

        {

            LOG(INFO) << data_dirpath << " exists";

        }

        else

        {

            if (0 == hdfsCreateDirectory(data_dirpath.c_str(), S_IRWXU | S_IXGRP | S_IXOTH))

            {

                LOG(INFO) << "create " << data_dirpath << " SUCCESS";

            }

        }

    }

    不难看出上述两段代码逻辑是一样的,但是调用的函数名不同,而且函数的参数列表不同。下面利用boost::bindboost::function将它们统一成一个实现:

    void Xinit(boost::function<int (const char*)> exist_directory

             , boost::function<int (const char*)> create_directory)

    {

        if (0 == exist_directory(data_dirpath.c_str()))

        {

            LOG(INFO) << data_dirpath << " exists";

        }

        else

        {

            if (0 == create_directory(data_dirpath.c_str()))

            {

                LOG(INFO) << "create " << data_dirpath << " SUCCESS";

            }

        }

    }

    void hdfs::init()

    {

        Xinit(boost::bind(&hdfsExists, fs, _1)

            , boost::bind(&hdfsCreateDirectory, fs, _1));

    }

    void local::init()

    {

        Xinit(boost::bind(&access, _1, R_OK | W_OK | X_OK)

            , boost::bind(&mkdir, _1, S_IRWXU | S_IXGRP | S_IXOTH));

    }

    是不是看起来很舒服了?

    1) boost::function

    它的模板参数为函数原型,格式为:函数返回类型 (参数列表),其中的类型还可以为模板。

    2) boost:bind

    它可以带多个参数,第一个参数总是为函数地址,如果为非类成员函数,则后面跟参数列表,如果是类成员函数,则第二个参数为类对象的地址。

    其中“_1”和“_2”等,表示参数的占位符,对应于boost::function中的函数原型参数列表。像“fs”和“R_OK | W_OK | X_OK”,一看就知道是咋回事。

    有人说可以用它来替代C++中的虚拟函数,而且比虚拟函数更优雅,但我不这么认同,实际工作中,常常两者结合使用,以达到简化代码的目的。

  • 相关阅读:
    linux命令-定时任务at
    linux网络监控_网速测试
    Linux磁盘分区扩容
    Ubuntu配置SSH服务
    Ubuntu用户管理
    Ubuntu安装lrzsz
    Ubuntu系统配置apt-get软件更新源
    Ubuntu网络配置IP和DNS等,适用于14.04,16.04,17.10和18.04
    Ubuntu系统安装,适用于14.04,16.04和17.10
    使用nginx反向代理处理前后端跨域访问
  • 原文地址:https://www.cnblogs.com/aquester/p/9891601.html
Copyright © 2011-2022 走看看