异步线程任务封装可以参考Chrome中Callback和tuple实现。
Callback.h
#ifndef __callback_h__
#define __callback_h__
#pragma once
#include "base/tuple.h"
#include "raw_scoped_refptr_mismatch_checker.h"
// Callback跟Task很像, 但没有参数限制, 实质上是面向对象的函数指针.
//
// Callbacks被设计和Tuples共同工作. 有一系列的辅助函数和类用于对使用者隐藏
// Tuple的细节. 用户代码只需要和CallbackRunner基类打交道, CallbackRunner只
// 有Run方法, 通过New*函数创建. 这样用户不必关心哪个类实现了回调, 只需要知
// 道参数个数以及类型.
//
// 这些实现是通过CallbackImpl完成的, CallbackImpl派生自CallbackStorage, 具体
// 的存储类型对用户透明, 用户只需要调用即可.
//
// 注意callbacks目前不能取消或者放弃自己的操作, 现在是在上层进行处理的.
// callback中的指针在调用结束前都必须是有效的.
//
// 类似Task, 一旦回调执行结束, 回调执行者负责删除回调指针.
//
// 客户端用法示例:
// void Object::DoStuff(int, string);
// Callback2::Type* callback =
// NewCallback(obj, &Object::DoStuff);
// callback->Run(5, string("hello"));
// delete callback;
// 或者直接使用元组:
// CallbackRunner >* callback =
// NewCallback(obj, &Object::DoStuff);
// callback->RunWithParams(MakeTuple(5, string("hello")));
//
// 有一个不带参数有返回值的版本. 示例:
// int Object::GetNextInt();
// CallbackWithReturnValue::Type* callback =
// NewCallbackWithReturnValue(obj, &Object::GetNextInt);
// int next_int = callback->Run();
// delete callback;
// 所有Callbacks的基类, 存储对象方法指针.
template
class CallbackStorage
{
public:
CallbackStorage(T* obj, Method meth) : obj_(obj), meth_(meth) {}
protected:
T* obj_;
Method meth_;
};
// Interface that is exposed to the consumer, that does the actual calling
// of the method.
template
class CallbackRunner
{
public:
typedef Params TupleType;
virtual ~CallbackRunner() {}
virtual void RunWithParams(const Params& params) = 0;
// Convenience functions so callers don't have to deal with Tuples.
inline void Run()
{
RunWithParams(Tuple0());
}
template
inline void Run(const Arg1& a)
{
RunWithParams(Params(a));
}
template
inline void Run(const Arg1& a, const Arg2& b)
{
RunWithParams(Params(a, b));
}
template
inline void Run(const Arg1& a, const Arg2& b, const Arg3& c)
{
RunWithParams(Params(a, b, c));
}
template
inline void Run(const Arg1& a, const Arg2& b, const Arg3& c, const Arg4& d)
{
RunWithParams(Params(a, b, c, d));
}
template
inline void Run(const Arg1& a, const Arg2& b, const Arg3& c,
const Arg4& d, const Arg5& e)
{
RunWithParams(Params(a, b, c, d, e));
}
};
template
class CallbackImpl : public CallbackStorage,
public CallbackRunner
{
public:
CallbackImpl(T* obj, Method meth) : CallbackStorage(obj, meth) {}
virtual void RunWithParams(const Params& params)
{
// use "this->" to force C++ to look inside our templatized base class; see
// Effective C++, 3rd Ed, item 43, p210 for details.
DispatchToMethod(this->obj_, this->meth_, params);
}
};
// 0-arg implementation
struct Callback0
{
typedef CallbackRunner Type;
};
template
typename Callback0::Type* NewCallback(T* object, void (T::*method)())
{
return new CallbackImpl(object, method);
}
// 1-arg implementation
template
struct Callback1
{
typedef CallbackRunner > Type;
};
template
typename Callback1::Type* NewCallback(T* object, void (T::*method)(Arg1))
{
return new CallbackImpl >(object, method);
}
// 2-arg implementation
template
struct Callback2
{
typedef CallbackRunner > Type;
};
template
typename Callback2::Type* NewCallback(
T* object, void (T::*method)(Arg1, Arg2))
{
return new CallbackImpl >(object, method);
}
// 3-arg implementation
template
struct Callback3
{
typedef CallbackRunner > Type;
};
template
typename Callback3::Type* NewCallback(
T* object, void (T::*method)(Arg1, Arg2, Arg3))
{
return new CallbackImpl >(object, method);
}
// 4-arg implementation
template
struct Callback4
{
typedef CallbackRunner > Type;
};
template
typename Callback4::Type* NewCallback(
T* object, void (T::*method)(Arg1, Arg2, Arg3, Arg4))
{
return new CallbackImpl >(object, method);
}
// 5-arg implementation
template
struct Callback5
{
typedef CallbackRunner > Type;
};
template
typename Callback5::Type* NewCallback(
T* object, void (T::*method)(Arg1, Arg2, Arg3, Arg4, Arg5))
{
return new CallbackImpl >(object, method);
}
// An UnboundMethod is a wrapper for a method where the actual object is
// provided at Run dispatch time.
template
class UnboundMethod
{
public:
UnboundMethod(Method m, const Params& p) : m_(m), p_(p)
{
COMPILE_ASSERT((MethodUsesScopedRefptrCorrectly::value),
badunboundmethodparams);
}
void Run(T* obj) const
{
DispatchToMethod(obj, m_, p_);
}
private:
Method m_;
Params p_;
};
// 无参数带返回值版本.
template
struct CallbackWithReturnValue
{
class Type
{
public:
virtual ~Type() {}
virtual ReturnValue Run() = 0;
};
};
template
class CallbackWithReturnValueImpl : public CallbackStorage,
public CallbackWithReturnValue::Type
{
public:
CallbackWithReturnValueImpl(T* obj, Method meth)
: CallbackStorage(obj, meth) {}
virtual ReturnValue Run()
{
return (this->obj_->*(this->meth_))();
}
protected:
virtual ~CallbackWithReturnValueImpl() {}
};
template
typename CallbackWithReturnValue::Type*
NewCallbackWithReturnValue(T* object, ReturnValue (T::*method)())
{
return new CallbackWithReturnValueImpl(object, method);
}
#endif //__callback_h__
chrome自己实现tuple
#ifndef __tuple_h__
#define __tuple_h__
#pragma once
// 元组(Tuple)是一种概念上与std::pair类似的通用模板容器.
// Tuple0到Tuple6对应容器中元素个数. 带有0-6个参数的MakeTuple()函数能够方便
// 的构造对应的元组对象. DispatchToMethod和DispatchToFunction带有函数指针或
// 者对象实例和成员函数指针, 解包元组为参数进行函数调用.
//
// 元组的元素通过值拷贝存储.
//
// 用法示例:
// // These two methods of creating a Tuple are identical.
// Tuple2 tuple_a(1, "wee");
// Tuple2 tuple_b = MakeTuple(1, "wee");
//
// void SomeFunc(int a, const char* b) {}
// DispatchToFunction(&SomeFunc, tuple_a); // SomeFunc(1, "wee")
// DispatchToFunction(
// &SomeFunc, MakeTuple(10, "foo")); // SomeFunc(10, "foo")
//
// struct { void SomeMeth(int a, int b, int c) {} } foo;
// DispatchToMethod(&foo, &Foo::SomeMeth, MakeTuple(1, 2, 3));
// //foo->SomeMeth(1, 2, 3);
// Traits ----------------------------------------------------------------------
//
// A simple traits class for tuple arguments.
//
// ValueType: the bare, nonref version of a type (same as the type for nonrefs).
// RefType: the ref version of a type (same as the type for refs).
// ParamType: what type to pass to functions (refs should not be constified).
template
struct TupleTraits
{
typedef P ValueType;
typedef P& RefType;
typedef const P& ParamType;
};
template
struct TupleTraits
{
typedef P ValueType;
typedef P& RefType;
typedef P& ParamType;
};
template
struct TupleTypes {};
// Tuple -----------------------------------------------------------------------
//
// This set of classes is useful for bundling 0 or more heterogeneous data types
// into a single variable. The advantage of this is that it greatly simplifies
// function objects that need to take an arbitrary number of parameters; see
// RunnableMethod and IPC::MessageWithTuple.
//
// Tuple0 is supplied to act as a 'void' type. It can be used, for example,
// when dispatching to a function that accepts no arguments (see the
// Dispatchers below).
// Tuple1 is rarely useful. One such use is when A is non-const ref that you
// want filled by the dispatchee, and the tuple is merely a container for that
// output (a "tier"). See MakeRefTuple and its usages.
struct Tuple0
{
typedef Tuple0 ValueTuple;
typedef Tuple0 RefTuple;
typedef Tuple0 ParamTuple;
};
template
struct Tuple1
{
public:
typedef A TypeA;
Tuple1() {}
explicit Tuple1(typename TupleTraits::ParamType a) : a(a) {}
A a;
};
template
struct Tuple2
{
public:
typedef A TypeA;
typedef B TypeB;
Tuple2() {}
Tuple2(typename TupleTraits::ParamType a,
typename TupleTraits::ParamType b)
: a(a), b(b) {}
A a;
B b;
};
template
struct Tuple3
{
public:
typedef A TypeA;
typedef B TypeB;
typedef C TypeC;
Tuple3() {}
Tuple3(typename TupleTraits::ParamType a,
typename TupleTraits::ParamType b,
typename TupleTraits::ParamType c)
: a(a), b(b), c(c){}
A a;
B b;
C c;
};
template
struct Tuple4
{
public:
typedef A TypeA;
typedef B TypeB;
typedef C TypeC;
typedef D TypeD;
Tuple4() {}
Tuple4(typename TupleTraits::ParamType a,
typename TupleTraits::ParamType b,
typename TupleTraits::ParamType c,
typename TupleTraits::ParamType d)
: a(a), b(b), c(c), d(d) {}
A a;
B b;
C c;
D d;
};
template
struct Tuple5
{
public:
typedef A TypeA;
typedef B TypeB;
typedef C TypeC;
typedef D TypeD;
typedef E TypeE;
Tuple5() {}
Tuple5(typename TupleTraits::ParamType a,
typename TupleTraits::ParamType b,
typename TupleTraits::ParamType c,
typename TupleTraits::ParamType d,
typename TupleTraits::ParamType e)
: a(a), b(b), c(c), d(d), e(e) {}
A a;
B b;
C c;
D d;
E e;
};
template
struct Tuple6
{
public:
typedef A TypeA;
typedef B TypeB;
typedef C TypeC;
typedef D TypeD;
typedef E TypeE;
typedef F TypeF;
Tuple6() {}
Tuple6(typename TupleTraits::ParamType a,
typename TupleTraits::ParamType b,
typename TupleTraits::ParamType c,
typename TupleTraits::ParamType d,
typename TupleTraits::ParamType e,
typename TupleTraits::ParamType f)
: a(a), b(b), c(c), d(d), e(e), f(f) {}
A a;
B b;
C c;
D d;
E e;
F f;
};
template
struct Tuple7
{
public:
typedef A TypeA;
typedef B TypeB;
typedef C TypeC;
typedef D TypeD;
typedef E TypeE;
typedef F TypeF;
typedef G TypeG;
Tuple7() {}
Tuple7(typename TupleTraits::ParamType a,
typename TupleTraits::ParamType b,
typename TupleTraits::ParamType c,
typename TupleTraits::ParamType d,
typename TupleTraits::ParamType e,
typename TupleTraits::ParamType f,
typename TupleTraits::ParamType g)
: a(a), b(b), c(c), d(d), e(e), f(f), g(g) {}
A a;
B b;
C c;
D d;
E e;
F f;
G g;
};
template
struct Tuple8
{
public:
typedef A TypeA;
typedef B TypeB;
typedef C TypeC;
typedef D TypeD;
typedef E TypeE;
typedef F TypeF;
typedef G TypeG;
typedef H TypeH;
Tuple8() {}
Tuple8(typename TupleTraits::ParamType a,
typename TupleTraits::ParamType b,
typename TupleTraits::ParamType c,
typename TupleTraits::ParamType d,
typename TupleTraits::ParamType e,
typename TupleTraits::ParamType f,
typename TupleTraits::ParamType g,
typename TupleTraits::ParamType h)
: a(a), b(b), c(c), d(d), e(e), f(f), g(g), h(h) {}
A a;
B b;
C c;
D d;
E e;
F f;
G g;
H h;
};
// Tuple types ----------------------------------------------------------------
//
// Allows for selection of ValueTuple/RefTuple/ParamTuple without needing the
// definitions of class types the tuple takes as parameters.
template<>
struct TupleTypes< Tuple0 >
{
typedef Tuple0 ValueTuple;
typedef Tuple0 RefTuple;
typedef Tuple0 ParamTuple;
};
template
struct TupleTypes< Tuple1 >
{
typedef Tuple1::ValueType> ValueTuple;
typedef Tuple1::RefType> RefTuple;
typedef Tuple1::ParamType> ParamTuple;
};
template
struct TupleTypes< Tuple2 >
{
typedef Tuple2::ValueType,
typename TupleTraits::ValueType> ValueTuple;
typedef Tuple2::RefType,
typename TupleTraits::RefType> RefTuple;
typedef Tuple2::ParamType,
typename TupleTraits::ParamType> ParamTuple;
};
template
struct TupleTypes< Tuple3 >
{
typedef Tuple3::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType> ValueTuple;
typedef Tuple3::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType> RefTuple;
typedef Tuple3::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType> ParamTuple;
};
template
struct TupleTypes< Tuple4 >
{
typedef Tuple4::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType> ValueTuple;
typedef Tuple4::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType> RefTuple;
typedef Tuple4::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType> ParamTuple;
};
template
struct TupleTypes< Tuple5 >
{
typedef Tuple5::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType> ValueTuple;
typedef Tuple5::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType> RefTuple;
typedef Tuple5::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType> ParamTuple;
};
template
struct TupleTypes< Tuple6 >
{
typedef Tuple6::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType> ValueTuple;
typedef Tuple6::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType> RefTuple;
typedef Tuple6::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType> ParamTuple;
};
template
struct TupleTypes< Tuple7 >
{
typedef Tuple7::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType> ValueTuple;
typedef Tuple7::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType> RefTuple;
typedef Tuple7::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType> ParamTuple;
};
template
struct TupleTypes< Tuple8 >
{
typedef Tuple8::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType,
typename TupleTraits::ValueType> ValueTuple;
typedef Tuple8::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType,
typename TupleTraits::RefType> RefTuple;
typedef Tuple8::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType,
typename TupleTraits::ParamType> ParamTuple;
};
// Tuple creators -------------------------------------------------------------
//
// Helper functions for constructing tuples while inferring the template
// argument types.
inline Tuple0 MakeTuple()
{
return Tuple0();
}
template
inline Tuple1 MakeTuple(const A& a)
{
return Tuple1(a);
}
template
inline Tuple2 MakeTuple(const A& a, const B& b)
{
return Tuple2(a, b);
}
template
inline Tuple3 MakeTuple(const A& a, const B& b, const C& c)
{
return Tuple3(a, b, c);
}
template
inline Tuple4 MakeTuple(const A& a, const B& b, const C& c,
const D& d)
{
return Tuple4(a, b, c, d);
}
template
inline Tuple5 MakeTuple(const A& a, const B& b, const C& c,
const D& d, const E& e)
{
return Tuple5(a, b, c, d, e);
}
template
inline Tuple6 MakeTuple(const A& a, const B& b, const C& c,
const D& d, const E& e, const F& f)
{
return Tuple6(a, b, c, d, e, f);
}
template
inline Tuple7 MakeTuple(const A& a, const B& b, const C& c,
const D& d, const E& e, const F& f,
const G& g)
{
return Tuple7(a, b, c, d, e, f, g);
}
template
inline Tuple8 MakeTuple(const A& a, const B& b,
const C& c, const D& d,
const E& e, const F& f,
const G& g, const H& h)
{
return Tuple8(a, b, c, d, e, f, g, h);
}
// The following set of helpers make what Boost refers to as "Tiers" - a tuple
// of references.
template
inline Tuple1 MakeRefTuple(A& a)
{
return Tuple1(a);
}
template
inline Tuple2 MakeRefTuple(A& a, B& b)
{
return Tuple2(a, b);
}
template
inline Tuple3 MakeRefTuple(A& a, B& b, C& c)
{
return Tuple3(a, b, c);
}
template
inline Tuple4 MakeRefTuple(A& a, B& b, C& c, D& d)
{
return Tuple4(a, b, c, d);
}
template
inline Tuple5 MakeRefTuple(A& a, B& b, C& c, D& d, E& e)
{
return Tuple5(a, b, c, d, e);
}
template
inline Tuple6 MakeRefTuple(A& a, B& b, C& c, D& d, E& e,
F& f)
{
return Tuple6(a, b, c, d, e, f);
}
template
inline Tuple7 MakeRefTuple(A& a, B& b, C& c, D& d,
E& e, F& f, G& g)
{
return Tuple7(a, b, c, d, e, f, g);
}
template
inline Tuple8 MakeRefTuple(A& a, B& b, C& c,
D& d, E& e, F& f,
G& g, H& h)
{
return Tuple8(a, b, c, d, e, f, g, h);
}
// Dispatchers ----------------------------------------------------------------
//
// Helper functions that call the given method on an object, with the unpacked
// tuple arguments. Notice that they all have the same number of arguments,
// so you need only write:
// DispatchToMethod(object, &Object::method, args);
// This is very useful for templated dispatchers, since they don't need to know
// what type |args| is.
// Non-Static Dispatchers with no out params.
template
inline void DispatchToMethod(ObjT* obj, Method method, const Tuple0& arg)
{
(obj->*method)();
}
template
inline void DispatchToMethod(ObjT* obj, Method method, const A& arg)
{
(obj->*method)(arg);
}
template
inline void DispatchToMethod(ObjT* obj, Method method, const Tuple1& arg)
{
(obj->*method)(arg.a);
}
template
inline void DispatchToMethod(ObjT* obj,
Method method,
const Tuple2& arg)
{
(obj->*method)(arg.a, arg.b);
}
template
inline void DispatchToMethod(ObjT* obj, Method method,
const Tuple3& arg)
{
(obj->*method)(arg.a, arg.b, arg.c);
}
template
inline void DispatchToMethod(ObjT* obj, Method method,
const Tuple4& arg)
{
(obj->*method)(arg.a, arg.b, arg.c, arg.d);
}
template
inline void DispatchToMethod(ObjT* obj, Method method,
const Tuple5& arg)
{
(obj->*method)(arg.a, arg.b, arg.c, arg.d, arg.e);
}
template
inline void DispatchToMethod(ObjT* obj, Method method,
const Tuple6& arg)
{
(obj->*method)(arg.a, arg.b, arg.c, arg.d, arg.e, arg.f);
}
template
inline void DispatchToMethod(ObjT* obj, Method method,
const Tuple7& arg)
{
(obj->*method)(arg.a, arg.b, arg.c, arg.d, arg.e, arg.f, arg.g);
}
// Static Dispatchers with no out params.
template
inline void DispatchToFunction(Function function, const Tuple0& arg)
{
(*function)();
}
template
inline void DispatchToFunction(Function function, const A& arg)
{
(*function)(arg);
}
template
inline void DispatchToFunction(Function function, const Tuple1& arg)
{
(*function)(arg.a);
}
template
inline void DispatchToFunction(Function function, const Tuple2& arg)
{
(*function)(arg.a, arg.b);
}
template
inline void DispatchToFunction(Function function, const Tuple3& arg)
{
(*function)(arg.a, arg.b, arg.c);
}
template
inline void DispatchToFunction(Function function,
const Tuple4& arg)
{
(*function)(arg.a, arg.b, arg.c, arg.d);
}
template
inline void DispatchToFunction(Function function,
const Tuple5& arg)
{
(*function)(arg.a, arg.b, arg.c, arg.d, arg.e);
}
template
inline void DispatchToFunction(Function function,
const Tuple6& arg)
{
(*function)(arg.a, arg.b, arg.c, arg.d, arg.e, arg.f);
}
template
inline void DispatchToFunction(Function function,
const Tuple7& arg)
{
(*function)(arg.a, arg.b, arg.c, arg.d, arg.e, arg.f, arg.g);
}
template
inline void DispatchToFunction(Function function,
const Tuple8& arg)
{
(*function)(arg.a, arg.b, arg.c, arg.d, arg.e, arg.f, arg.g, arg.h);
}
// Dispatchers with 0 out param (as a Tuple0).
template
inline void DispatchToMethod(ObjT* obj,
Method method,
const Tuple0& arg, Tuple0*)
{
(obj->*method)();
}
template
inline void DispatchToMethod(ObjT* obj, Method method, const A& arg, Tuple0*)
{
(obj->*method)(arg);
}
template
inline void DispatchToMethod(ObjT* obj,
Method method,
const Tuple1