boost::function

  • 概念

    function是一个函数对象的”容器”,概念上像是c/c++中函数指针类型的泛化。它以对象的形式封装了原始的函数指针或函数对象,能够容纳任意符合函数签名的可调用对象。因此,它可以被用于回调机制,暂时保管函数或函数对象,在之后需要的时机再调用,使回调机制拥有更多弹性。

  • 初比较

#include<iostream>
#include"boost/function.hpp"

void A()
{
    std::cout << "A" << std::endl;
}

typedef void(*func)();
typedef boost::function<void(void)> funct;//注意<void(void)>必须将入参和返回值写完整

int main(int argc,char* argv[])
{
    func f = A;
    f();

    funct ff = A;
    ff();
    return 0;
}
  • 优势对比
#include <iostream>
#include <vector>
#include <algorithm>
#include "boost/function.hpp"

void print_new_value(int i)
{
    std::cout << "The value has been updated and is now " << i << std::endl;
}

void interested_in_the_change(int i)
{
    std::cout << "Ah, the value has changed " << i << std::endl;
}

//函数指针实现
class notifier
{
    typedef void (*function_type)(int);
    std::vector<function_type> vec_;//将函数指针扔进容器内
    int value_;

public:
    void add_observer(function_type t)
    {
        vec_.push_back(t);
    }

    void change_value(int i)
    {
        value_=i;
        for (std::size_t i=0;i<vec_.size();++i)
        {
            (*vec_[i])(value_);//或vec_[i](value_);
        }
    }
};//该类的实现方式其局限在于只能使用函数指针类型

//函数包装器实现
class notifier
{
    typedef boost::function<void(int)> function_type;
    std::vector<function_type> vec_;
    int value_;

public:
    template <typename T> void add_observer(T t)//泛化
    {
        vec_.push_back(function_type(t));
    }

    void change_value(int i)
    {
        value_=i;
        for (std::size_t i=0;i<vec_.size();++i)
        {
            vec_[i](value_);
        }
    }
};//经过boost::function的扩展,可以使用函数、函数对象(见说明)以及boost::function实例。这里最大的优点不是放宽了对函数的要求(或者说,增加了对函数对象的支持),而是我们可以使用带状态的对象,这是非常需要的。

class knows_the_previous_value
{
    int last_value_;
public:
    void operator()(int i)//带状态的函数对象
    {
        static bool first_time=true;
        if(first_time)
        {
            last_value_=i;
            std::cout << "This is the first change of value, so I don't know the previous one" << std::endl;
            first_time=false;
            return;
        }
        std::cout << "Previous value was " << last_value_ << std::endl;
        last_value_=i;
    }
};

int main()
{
    notifier n;
    n.add_observer(&print_new_value);
  /*这里注意下函数名、&函数名、*函数名指向的地址都一样,但它们的实际类型是不一样的\
  类似数组名、&数组名、&数组首元素*/
    n.add_observer(&interested_in_the_change);
    n.add_observer(knows_the_previous_value());
  /*使用boost::function后的扩展功能,没看懂类名()干嘛的?临时无名的对象?\
  猜想knows_the_previous_value()传入的只是一种形式,因为在直接调\
  knows_the_previous_value()没有任何结果*/

    n.change_value(42);
    n.change_value(50);
}
class some_class
{
public:
    void do_stuff(int i) const
    {
        std::cout << "OK. Stuff is done. " << i << std::endl;
    }
};

int main()
{
    boost::function<void(some_class,int)> f;
    f=&some_class::do_stuff;
    f(some_class(),2);

    boost::function<void(some_class&,int)> ff;
    ff=&some_class::do_stuff;
    some_class s;
    f(s,1);

    boost::function<void(some_class*,int)> fff;
    fff=&some_class::do_stuff;
    some_class ss;
    f(ss,3);
}
//并不需要类的实体调用类成员
  • 带状态的函数对象
#include <iostream>
#include "boost/function.hpp"

class keeping_state
{
    int total_;
public:
    keeping_state():total_(0) {}

    int operator()(int i)
    {
        total_+=i;
        return total_;
    }

    int total() const
    {
        return total_;
    }
};

int main()
{
//    keeping_state ks;
//    boost::function<int(int)> f1;
//    f1=ks;

//    boost::function<int(int)> f2;
//    f2=ks;

    keeping_state ks;
    boost::function<int(int)> f1;
    f1=boost::ref(ks);

    boost::function<int(int)> f2;
    f2=boost::ref(ks);

    std::cout << "The current total is " << f1(5) << std::endl;
    std::cout << "The current total is " << f2(10) << std::endl;
    std::cout << "After adding two times, the total is " << ks.total() << std::endl;
}
发布了339 篇原创文章 · 获赞 46 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/u014644594/article/details/103870582