在VC6中实现Boost::Bind/Function

来源:互联网 发布:如何查网络服务器端口 编辑:程序博客网 时间:2024/05/01 12:18

在VC6中实现Boost::Bind/Function
由于VC6不支持偏特化,所以直接使用Boost::Bind/Function只会导致编译错误。
下面来看看如何自行实现Bind/Function功能。
首先看看Bind/Function的使用方式:
1. 绑定函数并调用
int callback(int a, int b){return a + b;}
int result1 = bind(callback, 1, 2)();
int result2 = bind(callback, 1)(2);
int result3 = bind(callback)(1, 2);
2. 绑定函数对象并调用
class Callback
{
int operator()(int a, int b) {return a + b;}
}
Callback callback;
int result1 = bind(callback, 1, 2)();
int result2 = bind(callback, 1)(2);
int result3 = bind(callback)(1, 2);

bind的返回结果是一个函数对象,和上面的Callback类似,只是增加了一些额外的状态,用来保存原始的可调用对象以及绑定到该可调用对象的值。
1 int result1 = bind(callback, 1, 2)()中bind的返回值应当类似于:
class FunctionObject1
{
public:
 int operator()() {return m_callback(m_a, m_b);}
private:
 int (*m_callback)(int a, int b);或者Callback m_callback;//原始的可调用对象
 int m_a;//绑定的值
 int m_b;//绑定的值
}
2 int result1 = bind(callback, 1)(2)中bind的返回值应当类似于:
class FunctionObject2
{
public:
 int operator()(int b) {return m_callback(m_a, b);}
private:
 int (*m_callback)(int a, int b);或者Callback m_callback;//原始的可调用对象
 int m_a;//绑定的值
}
3 int result1 = bind(callback)(1, 2)中bind的返回值应当类似于:
class FunctionObject3
{
public:
 int operator()(int a, int b) {return m_callback(a, b);}
private:
 int (*m_callback)(int a, int b);或者Callback m_callback;//原始的可调用对象
}
现在的问题是bind是一个模板,其返回的类型必须用模板表示:
template<typename TReturn, typename TArg1, typename TArg2> class TFunctionObject1
{
public:
 TReturn operator()() {return m_callback(m_a, m_b);}
private:
 TReturn (*m_callback)(TArg1 a, TArg2 b);或者Callback m_callback;//原始的可调用对象
 TArg1 m_a;//绑定的值
 TArg2 m_b;//绑定的值
}
int callback(int a, int b){return a + b;}
bind(callback, 1, 2)();

string callback2(string a, string b){return a + b;}
bind(callback2, "1", "2")();
bind实现如下:
template<typename TReturn, typename TArg1, typename TArg2>
 TFunctionObject1<TReturn, TArg1, TArg2>
 bind(TReturn (*function)(TArg1, TArg2), TArg1 a, TArg2 b)
{
 return TFunctionObject1<TReturn, TArg1, TArg2>(function, a, b);
}
上面的这个实现有两个问题:
1. 传入的第一个参数必须是回调函数(要想支持更多数目的参数,可以重载bind),而不能是函数对象;
2. 函数的返回值不能为void,这由TFunctionObject1的operator()的实现“return m_callback(m_a, m_b);”决定。
第二个问题在vc7及其以上可以很方便地通过偏特化来解决:
template<typename TArg1, typename TArg2> class TFunctionObject1<void>
{
public:
 void operator()() {m_callback(m_a, m_b);}
private:
 void (*m_callback)(TArg1 a, TArg2 b);或者Callback m_callback;//原始的可调用对象
 TArg1 m_a;//绑定的值
 TArg2 m_b;//绑定的值
}
可惜不巧的是,VC6不支持偏特化,所以只能将有返回值和无返回值的区别对待:
1. 有返回值:
template<typename TReturn, typename TArg1, typename TArg2> class TFunctionObject1_Return
{
public:
 TReturn operator()() {return m_callback(m_a, m_b);}
private:
 TReturn (*m_callback)(TArg1 a, TArg2 b);
 TArg1 m_a;//绑定的值
 TArg2 m_b;//绑定的值
}
template<typename TReturn, typename TArg1, typename TArg2>
 TFunctionObject1_Return<TReturn, TArg1, TArg2>
 bind_Return(TReturn (*function)(TArg1, TArg2), TArg1 a, TArg2 b)
{
 return TFunctionObject1_Return<TReturn, TArg1, TArg2>(function, a, b);
}
2. 无返回值:
template<typename TArg1, typename TArg2> class TFunctionObject1_NoReturn
{
public:
 void operator()() {m_callback(m_a, m_b);}
private:
 void (*m_callback)(TArg1 a, TArg2 b);
 TArg1 m_a;//绑定的值
 TArg2 m_b;//绑定的值
}
template<typename TArg1, typename TArg2>
 TFunctionObject1_NoReturn<TArg1, TArg2>
 bind_NoReturn(void (*function)(TArg1, TArg2), TArg1 a, TArg2 b)
{
 return TFunctionObject1_NoReturn<TArg1, TArg2>(function, a, b);
}
向bind传递函数对象时,如何确定函数对象的返回值类型成为一个。在较新版本的C++编译器中,可以简单地通过下面的办法解决:
template<typename TType> ResultOf
{
 typedef TType Type;
};
class MyFunctionObject
{
public:
 string operator()(string a, string b){return a + b;}
};
ResultOf<MyFunctionObject(string, string)>::Type value = MyFunctionObject(...)("a", "b");
可惜vc6仍然不支持,所以,需要走点弯路。
有两条弯路:
1. 为MyFunctionObject特化ResultOf:
template<> ResultOf<MyFunctionObject>
{
 typedef string Type;
};
这样,在需要使用MyFunctionObject函数调用返回值类型的地方使用ResultOf<MyFunctionObject>::Type即可。这样做有一个缺点,即,在operator()有多个重载的情况下,它们的返回值都必须为同一类型。
2. 使用成员函数指针。向bind传入函数时,推导出函数的返回值类型是相当方便的;那么,在传入函数对象时,如果能够同时传入要函数指针(即string operator()(string a, string b)的指针),问题就解决了。可惜的是,正常情况下,无法取得操作符重载函数的指针(通过玩弄内存的方式可以取得,不过这儿就不讨论了)。所以,我们需要作出一些改变,不再使用operator()作为函数调用操作符,而使用其它普通成员函数。
class MyFunctionObject
{
public:
 string MyFunctionCall(string a, string b){return a + b;}
 int MyFunctionCall(int a, int b){return a + b;}
};
向bind传递函数对象的实现
1. 特化ResultOf
template<typename TType> ResultOf
{
 typedef TType Type;
};
template<typename TFunctionObject, typename TArg1, typename TArg2> class TFunctionObject1_Return
{
public:
 ResultOf<TFunctionObject>::Type operator()() {return m_callback(m_a, m_b);}
 //下面这两个函数(可随意增加)是使用这个方法的附加奖励:只要在TFunctionObject中实现了相应的operator(),就可以很方便地进行重载调用
 template<typename TArg3> ResultOf<TFunctionObject>::Type operator()(TArg3 c) {return m_callback(m_a, m_b, c);}
 template<typename TArg3, typename TArg4> ResultOf<TFunctionObject>::Type operator()(TArg3 c, TArg4 d) {return m_callback(m_a, m_b, c, d);}
private:
 TFunctionObject m_callback;
 TArg1 m_a;//绑定的值
 TArg2 m_b;//绑定的值
}
template<typename TFunctionObject, typename TArg1, typename TArg2>
 TFunctionObject1_Return<TFunctionObject, TArg1, TArg2>
 bind_Return(TFunctionObject fo, TArg1 a, TArg2 b)
{
 return TFunctionObject1_Return<TFunctionObject, TArg1, TArg2>(fo, a, b);
}
/*这个是示例:
class MyFunctionObject
{
public:
 string operator()(string a, string b){return a + b;}
 string operator()(string a, string b, int c){return a + b;}
 string operator()(string a, string b, bool c){return a + b;}
 string operator()(string a, string b, bool c, const CTime& d){return a + b;}
};
template<> ResultOf<MyFunctionObject>
{
 typedef string Type;
};
MyFunctionObject mfo;
bind_Return(mfo, "a", "b")();//调用string operator()(string a, string b)
bind_Return(mfo, "a", "b")(0);//调用string operator()(string a, string b, int c)
bind_Return(mfo, "a", "b")(true);//调用string operator()(string a, string b, bool c)
bind_Return(mfo, "a", "b")(true, CTime::GetCurrentTime());//调用string operator()(string a, string b, bool c, const CTime& d)
//*/
2. 使用成员函数指针。
template<typename TFunctionObject, typename TReturn, typename TArg1, typename TArg2> class TFunctionObject1_Return
{
public:
 TReturn operator()() {return (m_callback.*m_function)(m_a, m_b);}
private:
 TFunctionObject m_callback;
 TReturn (TFunctionObject::m_function)(TArg1, TArg2);
 TArg1 m_a;//绑定的值
 TArg2 m_b;//绑定的值
}
template<typename TFunctionObject, typename TReturn, typename TArg1, typename TArg2>
 TFunctionObject1_Return<TFunctionObject, TReturn, TArg1, TArg2>
 bind_Return(TFunctionObject fo, TReturn (TFunctionObject::function)(TArg1, TArg2), TArg1 a, TArg2 b)
{
 return TFunctionObject1_Return<TFunctionObject, function, TArg1, TArg2>(fo, a, b);
}
/*这个是示例:
class MyFunctionObject
{
public:
 string MyFunctionCall(string a, string b){return a + b;}
 int MyFunctionCall2(int a, int b){return a + b;}
};
MyFunctionObject mfo;
bind_Return(mfo, &MyFunctionObject::MyFunctionCall, "a", "b")();//调用string MyFunctionCall(string a, string b)
bind_Return(mfo, &MyFunctionObject::MyFunctionCall2, 1, 2)();//调用int MyFunctionCall2(int a, int b)
//*/
注意,和“1. 特化ResultOf”中不同的是,本方法中传入bind的成员函数在不能有重载,所以在示例中才用MyFunctionCall和MyFunctionCall2加以区分

原创粉丝点击