add boost/TR1 like usage of Functor, with a test program from boost 1.33

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@320 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
syntheticpp 2005-10-24 15:40:22 +00:00
parent b73fe0bbcf
commit da979a7e0e
4 changed files with 1295 additions and 14 deletions

337
include/loki/Function.h Executable file
View file

@ -0,0 +1,337 @@
////////////////////////////////////////////////////////////////////////////////
// The Loki Library
// Copyright (c) 2005 Peter Kümmel
// Permission to use, copy, modify, distribute and sell this software for any
// purpose is hereby granted without fee, provided that the above copyright
// notice appear in all copies and that both that copyright notice and this
// permission notice appear in supporting documentation.
// The author makes no representations about the
// suitability of this software for any purpose. It is provided "as is"
// without express or implied warranty.
////////////////////////////////////////////////////////////////////////////////
#define LOKI_ENABLE_FUNCTION
#include <loki/Functor.h>
#include <loki/Sequence.h>
namespace Loki
{
template<class R>
struct Function;
////////////////////////////////////////////////////////////////////////////////
// Function allows a boost/TR1 like usage of Functor.
//
// e.g. Functor<int,LOKI_TYPELIST_2(int,int)> becomes Function<int(int,int)>
////////////////////////////////////////////////////////////////////////////////
template<class R>
struct Function<R()> : public Functor<R>
{
typedef Functor<R> FBase;
Function() : FBase() {}
Function(const Function& func) : FBase()
{
if( !func.empty())
FBase::operator=(func);
}
// emptiness
template<class R2>
Function(Function<R2()> func) : FBase()
{
if(!func.empty())
FBase::operator=(func);
}
// clear by '= 0'
Function(const int i) : FBase()
{
if(i==0)
FBase::clear();
else
throw std::runtime_error("Loki::Function(const int i): i!=0");
}
template<class Func>
Function(Func func) : FBase(func) {}
template<class Host, class Func>
Function(const Host& host, const Func& func) : FBase(host,func) {}
};
////////////////////////////////////////////////////////////////////////////////
// macros for the repetitions
////////////////////////////////////////////////////////////////////////////////
#define LOKI_FUNCTION_BODY \
\
Function() : FBase() {} \
\
Function(const Function& func) : FBase() \
{ \
if( !func.empty()) \
FBase::operator=(func); \
} \
\
Function(const int i) : FBase() \
{ \
if(i==0) \
FBase::clear(); \
else \
throw std::runtime_error( \
"Loki::Function(const int i): i!=0"); \
} \
\
template<class Func> \
Function(Func func) : FBase(func) {} \
\
template<class Host, class Func> \
Function(const Host& host, const Func& func): FBase(host,func) {}
#define LOKI_FUNCTION_R2_CTOR_BODY \
\
: FBase() \
{ \
if(!func.empty()) \
FBase::operator=(func); \
}
////////////////////////////////////////////////////////////////////////////////
// repetitions
////////////////////////////////////////////////////////////////////////////////
template<class R,class P01>
struct Function<R(P01)>
: public Loki::Functor<R,typename Seq<P01>::Type>
{
typedef Functor<R,typename Seq<P01>::Type> FBase;
template<class R2,class Q01>
Function(Function<R2(Q01)> func)
LOKI_FUNCTION_R2_CTOR_BODY
LOKI_FUNCTION_BODY
};
template<class R,class P01,class P02>
struct Function<R(P01,P02)>
: public Functor<R,typename Seq<P01,P02>::Type>
{
typedef Functor<R,typename Seq<P01,P02>::Type> FBase;
template<class R2,class Q01, class Q02>
Function(Function<R2(Q01,Q02)> func)
LOKI_FUNCTION_R2_CTOR_BODY
LOKI_FUNCTION_BODY
};
template<class R,class P01,class P02, class P03>
struct Function<R(P01,P02,P03)>
: public Functor<R,typename Seq<P01,P02,P03>::Type>
{
typedef Functor<R,typename Seq<P01,P02,P03>::Type> FBase;
template<class R2,class Q01, class Q02,class Q03>
Function(Function<R2(Q01,Q02,Q03)> func)
LOKI_FUNCTION_R2_CTOR_BODY
LOKI_FUNCTION_BODY
};
template<class R,class P01,class P02, class P03,class P04>
struct Function<R(P01,P02,P03,P04)>
: public Functor<R,typename Seq<P01,P02,P03,P04>::Type>
{
typedef Functor<R,typename Seq<P01,P02,P03,P04>::Type> FBase;
template<class R2,class Q01,class Q02, class Q03,class Q04>
Function(Function<R2(Q01,Q02,Q03,Q04)> func)
LOKI_FUNCTION_R2_CTOR_BODY
LOKI_FUNCTION_BODY
};
template<class R,class P01,class P02, class P03,class P04,class P05>
struct Function<R(P01,P02,P03,P04,P05)>
: public Functor<R,typename Seq<P01,P02,P03,P04,P05>::Type>
{
typedef Functor<R,typename Seq<P01,P02,P03,P04,P05>::Type> FBase;
template<class R2,class Q01,class Q02, class Q03,class Q04,class Q05>
Function(Function<R2(Q01,Q02,Q03,Q04,Q05)> func)
LOKI_FUNCTION_R2_CTOR_BODY
LOKI_FUNCTION_BODY
};
template<class R, class P01,class P02, class P03,class P04,class P05,
class P06>
struct Function<R(P01,P02,P03,P04,P05,P06)>
: public Functor<R,typename Seq<P01,P02,P03,P04,P05,P06>::Type>
{
typedef Functor<R,typename Seq<P01,P02,P03,P04,P05,P06>::Type> FBase;
template<class R2, class Q01,class Q02, class Q03,class Q04,class Q05,
class Q06>
Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06)> func)
LOKI_FUNCTION_R2_CTOR_BODY
LOKI_FUNCTION_BODY
};
template<class R, class P01,class P02, class P03,class P04,class P05,
class P06,class P07>
struct Function<R(P01,P02,P03,P04,P05,P06,P07)>
: public Functor<R,typename Seq<P01,P02,P03,P04,P05,P06,P07>::Type>
{
typedef Functor<R,typename Seq<P01,P02,P03,P04,P05,P06,P07>::Type> FBase;
template<class R2, class Q01,class Q02, class Q03,class Q04,class Q05,
class Q06,class Q07>
Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06,Q07)> func)
LOKI_FUNCTION_R2_CTOR_BODY
LOKI_FUNCTION_BODY
};
template<class R, class P01,class P02, class P03,class P04,class P05,
class P06,class P07, class P08>
struct Function<R(P01,P02,P03,P04,P05,P06,P07,P08)>
: public Functor<R,typename Seq<P01,P02,P03,P04,P05,P06,P07,P08>::Type>
{
typedef Functor<R,typename Seq<P01,P02,P03,P04,P05,P06,P07,P08>::Type> FBase;
template<class R2, class Q01,class Q02, class Q03,class Q04,class Q05,
class Q06,class Q07, class Q08>
Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06,Q07,Q08)> func)
LOKI_FUNCTION_R2_CTOR_BODY
LOKI_FUNCTION_BODY
};
template<class R, class P01,class P02, class P03,class P04,class P05,
class P06,class P07, class P08,class P09>
struct Function<R(P01,P02,P03,P04,P05,P06,P07,P08,P09)>
: public Functor<R,typename Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09>::Type>
{
typedef Functor<R,typename Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09 >::Type> FBase;
template<class R2, class Q01,class Q02, class Q03,class Q04,class Q05,
class Q06,class Q07, class Q08,class Q09>
Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06,Q07,Q08,Q09)> func)
LOKI_FUNCTION_R2_CTOR_BODY
LOKI_FUNCTION_BODY
};
template<class R, class P01,class P02, class P03,class P04,class P05,
class P06,class P07, class P08,class P09,class P10>
struct Function<R(P01,P02,P03,P04,P05,P06,P07,P08,P09,P10)>
: public Functor<R,typename Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10>::Type>
{
typedef Functor<R,typename Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10>::Type> FBase;
template<class R2, class Q01,class Q02, class Q03,class Q04,class Q05,
class Q06,class Q07, class Q08,class Q09,class Q10>
Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06,Q07,Q08,Q09,Q10)> func)
LOKI_FUNCTION_R2_CTOR_BODY
LOKI_FUNCTION_BODY
};
template<class R, class P01,class P02, class P03,class P04,class P05,
class P06,class P07, class P08,class P09,class P10,
class P11>
struct Function<R(P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11)>
: public Functor<R,typename Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11>::Type>
{
typedef Functor<R,typename Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11>::Type>FBase;
template<class R2, class Q01,class Q02, class Q03,class Q04,class Q05,
class Q06,class Q07, class Q08,class Q09,class Q10,
class Q11>
Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06,Q07,Q08,Q09,Q10,Q11)> func)
LOKI_FUNCTION_R2_CTOR_BODY
LOKI_FUNCTION_BODY
};
template<class R, class P01,class P02, class P03,class P04,class P05,
class P06,class P07, class P08,class P09,class P10,
class P11,class P12>
struct Function<R(P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12)>
: public Functor<R,typename Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12>::Type>
{
typedef Functor<R,typename Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12>::Type> FBase;
template<class R2, class Q01,class Q02, class Q03,class Q04,class Q05,
class Q06,class Q07, class Q08,class Q09,class Q10,
class Q11,class Q12>
Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06,Q07,Q08,Q09,Q10,Q11)> func)
LOKI_FUNCTION_R2_CTOR_BODY
LOKI_FUNCTION_BODY
};
template<class R, class P01,class P02, class P03,class P04,class P05,
class P06,class P07, class P08,class P09,class P10,
class P11,class P12, class P13>
struct Function<R(P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13)>
: public Functor<R,typename Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13>::Type>
{
typedef Functor<R,typename Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13>::Type> FBase;
template<class R2, class Q01,class Q02, class Q03,class Q04,class Q05,
class Q06,class Q07, class Q08,class Q09,class Q10,
class Q11,class Q12, class Q13>
Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06,Q07,Q08,Q09,Q10,Q11,Q12,Q13)> func)
LOKI_FUNCTION_R2_CTOR_BODY
LOKI_FUNCTION_BODY
};
template<class R, class P01,class P02, class P03,class P04,class P05,
class P06,class P07, class P08,class P09,class P10,
class P11,class P12, class P13,class P14>
struct Function<R(P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13,P14)>
: public Functor<R,typename Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13,P14>::Type>
{
typedef Functor<R,typename Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13,P14>::Type> FBase;
template<class R2, class Q01,class Q02, class Q03,class Q04,class Q05,
class Q06,class Q07, class Q08,class Q09,class Q10,
class Q11,class Q12, class Q13,class Q14>
Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06,Q07,Q08,Q09,Q10,Q11,Q12,Q13,Q14)> func)
LOKI_FUNCTION_R2_CTOR_BODY
LOKI_FUNCTION_BODY
};
template<class R, class P01,class P02, class P03,class P04,class P05,
class P06,class P07, class P08,class P09,class P10,
class P11,class P12, class P13,class P14,class P15>
struct Function<R(P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13,P14,P15)>
: public Functor<R,typename Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13,P14,P15>::Type>
{
typedef Functor<R,typename Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13,P14,P15>::Type> FBase;
template<class R2, class Q01,class Q02, class Q03,class Q04,class Q05,
class Q06,class Q07, class Q08,class Q09,class Q10,
class Q11,class Q12, class Q13,class Q14,class Q15>
Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06,Q07,Q08,Q09,Q10,Q11,Q12,Q13,Q14,Q15)> func)
LOKI_FUNCTION_R2_CTOR_BODY
LOKI_FUNCTION_BODY
};
}// namespace Loki

View file

@ -714,11 +714,31 @@ namespace Loki
PointerToMemFn pMemFn_;
};
////////////////////////////////////////////////////////////////////////////////
// TR1 exception
//////////////////////////////////////////////////////////////////////////////////
#ifdef LOKI_ENABLE_FUNCTION
class bad_function_call : public std::runtime_error
{
public:
bad_function_call() : std::runtime_error("bad_function_call in Loki::Functor")
{}
};
#define LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL if(empty()) throw bad_function_call();
#else
#define LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
#endif
////////////////////////////////////////////////////////////////////////////////
// class template Functor
// A generalized functor implementation with value semantics
////////////////////////////////////////////////////////////////////////////////
template <typename R, class TList = NullType,
template<class> class ThreadingModel = LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL>
class Functor
@ -781,53 +801,103 @@ namespace Loki
copy.spImpl_.reset(p);
return *this;
}
#ifdef LOKI_ENABLE_FUNCTION
bool empty() const
{
return spImpl_.get() == 0;
}
void clear()
{
spImpl_.reset(0);
}
#endif
ResultType operator()() const
{ return (*spImpl_)(); }
{
LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
return (*spImpl_)();
}
ResultType operator()(Parm1 p1) const
{ return (*spImpl_)(p1); }
{
LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
return (*spImpl_)(p1);
}
ResultType operator()(Parm1 p1, Parm2 p2) const
{ return (*spImpl_)(p1, p2); }
{
LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
return (*spImpl_)(p1, p2);
}
ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3) const
{ return (*spImpl_)(p1, p2, p3); }
{
LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
return (*spImpl_)(p1, p2, p3);
}
ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4) const
{ return (*spImpl_)(p1, p2, p3, p4); }
{
LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
return (*spImpl_)(p1, p2, p3, p4);
}
ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5) const
{ return (*spImpl_)(p1, p2, p3, p4, p5); }
{
LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
return (*spImpl_)(p1, p2, p3, p4, p5);
}
ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
Parm6 p6) const
{ return (*spImpl_)(p1, p2, p3, p4, p5, p6); }
{
LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
return (*spImpl_)(p1, p2, p3, p4, p5, p6);
}
ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
Parm6 p6, Parm7 p7) const
{ return (*spImpl_)(p1, p2, p3, p4, p5, p6, p7); }
{
LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
return (*spImpl_)(p1, p2, p3, p4, p5, p6, p7);
}
ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
Parm6 p6, Parm7 p7, Parm8 p8) const
{ return (*spImpl_)(p1, p2, p3, p4, p5, p6, p7, p8); }
{
LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
return (*spImpl_)(p1, p2, p3, p4, p5, p6, p7, p8);
}
ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9) const
{ return (*spImpl_)(p1, p2, p3, p4, p5, p6, p7, p8, p9); }
{
LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
return (*spImpl_)(p1, p2, p3, p4, p5, p6, p7, p8, p9);
}
ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10) const
{ return (*spImpl_)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); }
{
LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
return (*spImpl_)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10);
}
ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11) const
{ return (*spImpl_)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11); }
{
LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
return (*spImpl_)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11);
}
ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11,
Parm12 p12) const
{
LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
return (*spImpl_)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11,
p12);
}
@ -836,6 +906,7 @@ namespace Loki
Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11,
Parm12 p12, Parm13 p13) const
{
LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
return (*spImpl_)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11,
p12, p13);
}
@ -844,6 +915,7 @@ namespace Loki
Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11,
Parm12 p12, Parm13 p13, Parm14 p14) const
{
LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
return (*spImpl_)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11,
p12, p13, p14);
}
@ -852,6 +924,7 @@ namespace Loki
Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11,
Parm12 p12, Parm13 p13, Parm14 p14, Parm15 p15) const
{
LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
return (*spImpl_)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11,
p12, p13, p14, p15);
}