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

  • 相关阅读:
    Using Resource File on DotNet
    C++/CLI VS CSharp
    JIT VS NGen
    [Tip: disable vc intellisense]VS2008 VC Intelisense issue
    UVa 10891 Game of Sum(经典博弈区间DP)
    UVa 10723 Cyborg Genes(LCS变种)
    UVa 607 Scheduling Lectures(简单DP)
    UVa 10401 Injured Queen Problem(简单DP)
    UVa 10313 Pay the Price(类似数字分解DP)
    UVa 10635 Prince and Princess(LCS N*logN)
  • 原文地址:https://www.cnblogs.com/aquester/p/9891601.html
Copyright © 2011-2022 走看看