• 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基础知识[2]【延迟及定时机制】
    Linux基础知识[1]【ACL权限】
    docker 入门学习篇【基本命令与操作】
    centos7.1下 Docker环境搭建
    RHEL6.5下更新python至2.7版本
    Github初学者探索
    vmware下linux虚拟机传文件解决方案之 xftp
    mysql 常用操作命令
    常用DNS记录
    SDNU 1481.纪念品分组(水题)
  • 原文地址:https://www.cnblogs.com/aquester/p/9891601.html
Copyright © 2020-2023  润新知