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++中的虚拟函数,而且比虚拟函数更优雅,但我不这么认同,实际工作中,常常两者结合使用,以达到简化代码的目的。

  • 相关阅读:
    vue实例讲解之axios的使用
    实例讲解webpack的基本使用第四篇
    实例讲解webpack的基本使用第三篇
    实例讲解webpack的基本使用第二篇
    写好一篇技术博客的正确姿势是什么
    实例讲解js正则表达式的使用
    一个综合实例讲解vue的基础知识点。
    vue实例讲解之vue-router的使用
    .NET 串口通信
    textarea赋值时换行符无效的解决方法
  • 原文地址:https://www.cnblogs.com/aquester/p/9891601.html
Copyright © 2011-2022 走看看