如何使用 Boost.Func

//z 2014-07-24 14:53:32 L.160'32788 BG57IV3@XCL T491489982 .K.F916674775 [T6,L181,R3,V64]
1. 概述
Boost.Func 允许你创建各种类型的函数指针变量。
Boost.Bind 能够动态bind一个函数到函数指针变量。

2. 声明一个boost::function 变量
声明变量的关键点是指明函数指针的返回值类型以及参数类型。
2.1 形式如下:
boost::function<RETURN_TYPE(PARAM_TYPE_1, PARAM_TYPE_N)> boostFunction; 
2.2 例子:
boost::function<void()> voidFunction; // returns void and
// takes no parameters 
boost::function<int(int, char**)> mainFunction; // same signature as 
// standard main() 
boost::function<void(std::string)> stringFunction;  // returns void and
// takes a string

3. 检查一个 boost::function 变量是否能够被安全调用
boost::function 可能包含或未包含一个可调用的函数指针。调用一个未包含合法可调用函数指针的function pointer将导致未定义的行为。下面演示了如何使用检查一个boost::function 变量的合法性:
boost::function<void()> boostFunction;  
if ( boostFunction ) // contains valid method


if ( boostFunction.empty() ) // does NOT contain valid method


3.1 empty() method 能够用于判断wrapper是否为null
3.2 使用完毕之后的清除 : fp = 0; 或者调用 fp.clear();

4. 调用 boost::function 变量
// some variables
int number = 0; 
std::string aString("text"); 
float aFloat = 0.f; 
// some function pointers
boost::function<void()> voidFunction; 
boost::function<int(std::string)> stringAndIntFunction; 
boost::function<void(float, int)> floatAndIntFunction; 
// call the functions
voidFunction(); 
number = stringAndIntFunction(aString); 
floatAndIntFunction(aFloat, number); 
//z 2014-07-24 14:53:32 L.160'32788 BG57IV3@XCL T491489982 .K.F916674775 [T6,L181,R3,V64]

5. 封装一个 command object
/// command object
class command
{
private:
    boost::function<void ()> _f;
public:
    command()
    {
        ;
    }
    command(boost::function<void ()> f) : _f(f)
    {
        ;
    }
    template <typename T> void setFunction (T t)
    {
        _f = t ;
    }
    void execute()
    {
        if(!_f.empty())
            _f();
    }
};

6. boost :: bind 初始化函数指针
能够为任何function object绑定参数。

理解bind的关键是placeholders,placeholders定义为_1到_9,你在你通常使用argument的地方使用它们。
6.1 bind到一个free fucntion (独立函数)
void comm (std::string str) {…}
(boost::bind(&comm, _1)) (“Isn’t this fun?”);
6.2 bind到一个member function
bind到一个member function也是可以的。只是必须考虑传递隐式的this reference到所有的非静态成员函数。
inFlight fly;
(boost::bind (&comm, _1,_2)) (fly, “Isn’t this fun?”);
6.2.1 绑定一个无参数的成员函数
boost::bind(&Final::decAlt, fa)
6.3 例子:
// PART 2
// create a series of commands and stuff them into a container to be sequenced later
vector<boost::shared_ptr<command> > v;
boost::shared_ptr<command> spCmd1(new command(boost::bind(&Final::incAlt, final)));
boost::shared_ptr<command> spCmd2(new command(boost::bind(&Final::decSpeed, final)));
boost::shared_ptr<command> spCmd3(new command(boost::bind(&Final::decAlt, final)));

v.push_back(spCmd1);
v.push_back(spCmd2);
v.push_back(spCmd3);

// run the command sequence
for_each(v.begin(), v.end(), boost::bind(&command::execute, _1));

return 0;
//z 2014-07-24 15:18:10 L.160'31310 BG57IV3@XCL T3039420616.K.F916674775 [T8,L231,R4,V79]

6.4 Using std::bind1st and std::mem_fun together one can bind the object of a pointer-to-member function for use with Boost.Function:
boost::function<int (int)> f;
X x;
f = std::bind1st( std::mem_fun(&X::foo), &x);
f(5); // Call x.foo(5)
6.5 references to function objects
stateful_type a_function_object;
boost::function<int (int)> f;
f = boost::ref(a_function_object);

boost::function<int (int)> f2(f);
f will not make a copy of a_function_object, nor will f2 when it is targeted to f's reference to a_function_object.
这里f和f2都不会为 a_function_object 做一份拷贝,如果其target是一个指向 a_function_object 的引用时。
//z 2014-07-24 15:55:35 L.160'29065 BG57IV3@XCL T3667519838.K.F916674775 [T9,L242,R4,V90]

posted @ 2014-07-24 14:54  BiG5  阅读(218)  评论(0编辑  收藏  举报