| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212 |
- #pragma once
- /* callback.h
- * This is Rich Hickey's callback library...
- * http://www.tutok.sk/fastgl/callback.html
- *
- * I've just renamed it to callback.h as all my header files use
- * h for extension.
- */
- //**************** callback.hpp **********************
- // Copyright 1994 Rich Hickey
- /* 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. Rich Hickey makes no
- * representations about the suitability of this software for any
- * purpose. It is provided "as is" without express or implied warranty.
- */
- // 06/12/94 Rich Hickey
- // 3rd major revision
- // Now functors are concrete classes, and should be held by value
- // Virtual function mechanism removed
- // Generic makeFunctor() mechanism added for building functors
- // from both stand-alone functions and object/ptr-to-mem-func pairs
- #ifndef CALLBACK_HPP
- #define CALLBACK_HPP
- /*
- To use:
- If you wish to build a component that provides/needs a callback, simply
- specify and hold a CBFunctor of the type corresponding to the args
- you wish to pass and the return value you need. There are 10 Functors
- from which to choose:
- CBFunctor0
- CBFunctor1<P1>
- CBFunctor2<P1,P2>
- CBFunctor3<P1,P2,P3>
- CBFunctor4<P1,P2,P3,P4>
- CBFunctor0wRet<RT>
- CBFunctor1wRet<P1,RT>
- CBFunctor2wRet<P1,P2,RT>
- CBFunctor3wRet<P1,P2,P3,RT>
- CBFunctor4wRet<P1,P2,P3,P4,RT>
- These are parameterized by their args and return value if any. Each has
- a default ctor and an operator() with the corresponding signature.
- They can be treated and used just like ptr-to-functions.
- If you want to be a client of a component that uses callbacks, you
- create a CBFunctor by calling makeFunctor().
- There are three flavors of makeFunctor - you can create a functor from:
- a ptr-to-stand-alone function
- an object and a pointer-to-member function.
- a pointer-to-member function (which will be called on first arg of functor)
- The current iteration of this library requires you pass makeFunctor()
- a dummy first argument of type ptr-to-the-Functor-type-you-want-to-create.
- Simply cast 0 to provide this argument:
- makeFunctor((target-functor*)0,ptr-to-function)
- makeFunctor((target-functor*)0,reference-to-object,ptr-to-member-function)
- makeFunctor((target-functor*)0,ptr-to-member-function)
- Future versions will drop this requirement once member templates are
- available.
- The functor system is 100% type safe. It is also type flexible. You can
- build a functor out of a function that is 'type compatible' with the
- target functor - it need not have an exactly matching signature. By
- type compatible I mean a function with the same number of arguments, of
- types reachable from the functor's argument types by implicit conversion.
- The return type of the function must be implicitly convertible to the
- return type of the functor. A functor with no return can be built from a
- function with a return - the return value is simply ignored.
- (See ethel example below)
- All the correct virtual function behavior is preserved. (see ricky
- example below).
- If you somehow try to create something in violation
- of the type system you will get a compile-time or template-instantiation-
- time error.
- The CBFunctor base class and translator
- classes are artifacts of this implementation. You should not write
- code that relies upon their existence. Nor should you rely on the return
- value of makeFunctor being anything in particular.
- All that is guaranteed is that the Functor classes have a default ctor,
- an operator() with the requested argument types and return type, an
- operator that will allow it to be evaluated in a conditional (with
- 'true' meaning the functor is set, 'false' meaning it is not), and that
- Functors can be constructed from the result of makeFunctor(), given
- you've passed something compatible to makeFunctor().
- /////////////////////// BEGIN Example 1 //////////////////////////
- #include <iostream.h>
- #include "callback.hpp"
- //do5Times() is a function that takes a functor and invokes it 5 times
- void do5Times(const CBFunctor1<int> &doIt)
- {
- for(int i=0;i<5;i++)
- doIt(i);
- }
- //Here are some standalone functions
- void fred(int i){cout << "fred: " << i<<endl;}
- int ethel(long l){cout << "ethel: " << l<<endl;return l;}
- //Here is a class with a virtual function, and a derived class
- class B{
- public:
- virtual void ricky(int i)
- {cout << "B::ricky: " << i<<endl;}
- };
- class D:public B{
- public:
- void ricky(int i)
- {cout << "D::ricky: " << i<<endl;}
- };
- void main()
- {
- //create a typedef of the functor type to simplify dummy argument
- typedef CBFunctor1<int> *FtorType;
- CBFunctor1<int> ftor; //a functor variable
- //make a functor from ptr-to-function
- ftor = makeFunctor((FtorType)0,fred);
- do5Times(ftor);
- //note ethel is not an exact match - ok, is compatible
- ftor = makeFunctor((FtorType)0,ethel);
- do5Times(ftor);
- //create a D object to be a callback target
- D myD;
- //make functor from object and ptr-to-member-func
- ftor = makeFunctor((FtorType)0,myD,&B::ricky);
- do5Times(ftor);
- }
- /////////////////////// END of example 1 //////////////////////////
- /////////////////////// BEGIN Example 2 //////////////////////////
- #include <iostream.h>
- #include "callback.hpp"
- //Button is a component that provides a functor-based
- //callback mechanism, so you can wire it up to whatever you wish
- class Button{
- public:
- //ctor takes a functor and stores it away in a member
- Button(const CBFunctor0 &uponClickDoThis):notify(uponClickDoThis)
- {}
- void click()
- {
- //invoke the functor, thus calling back client
- notify();
- }
- private:
- //note this is a data member with a verb for a name - matches its
- //function-like usage
- CBFunctor0 notify;
- };
- class CDPlayer{
- public:
- void play()
- {cout << "Playing"<<endl;}
- void stop()
- {cout << "Stopped"<<endl;}
- };
- void main()
- {
- CDPlayer myCD;
- Button playButton(makeFunctor((CBFunctor0*)0,myCD,&CDPlayer::play));
- Button stopButton(makeFunctor((CBFunctor0*)0,myCD,&CDPlayer::stop));
- playButton.click(); //calls myCD.play()
- stopButton.click(); //calls myCD.stop()
- }
- /////////////////////// END of example 2 //////////////////////////
- */
- //******************************************************************
- ///////////////////////////////////////////////////////////////////*
- //WARNING - no need to read past this point, lest confusion ensue. *
- //Only the curious need explore further - but remember *
- //about that cat! *
- ///////////////////////////////////////////////////////////////////*
- //******************************************************************
- //////////////////////////////
- // COMPILER BUG WORKAROUNDS:
- // As of version 4.02 Borland has a code generation bug
- // returning the result of a call via a ptr-to-function in a template
- #define BC4_RET_BUG(x) x
- //////////////////////////////
- #include <string.h> //for memcpy
- #include <stddef.h> //for size_t
- //typeless representation of a function and optional object
- class CBFunctorBase {
- public:
- typedef void (CBFunctorBase::* _MemFunc)();
- typedef void (*_Func)();
- CBFunctorBase() :callee(0), func(0) {}
- CBFunctorBase(const void* c, const void* f, size_t sz)
- {
- if (c) //must be callee/memfunc
- {
- callee = (void*)c;
- memcpy(memFunc, f, sz);
- }
- else //must be ptr-to-func
- {
- func = f;
- }
- }
- //for evaluation in conditionals
- operator int()const { return func || callee; }
- class DummyInit {
- };
- ////////////////////////////////////////////////////////////////
- // Note: this code depends on all ptr-to-mem-funcs being same size
- // If that is not the case then make memFunc as large as largest
- ////////////////////////////////////////////////////////////////
- union {
- const void* func;
- char memFunc[sizeof(_MemFunc)];
- };
- void* callee;
- };
- /************************* no arg - no return *******************/
- class CBFunctor0 :protected CBFunctorBase {
- public:
- CBFunctor0(DummyInit* = 0) {}
- void operator()()const
- {
- thunk(*this);
- }
- CBFunctorBase::operator int;
- protected:
- typedef void (*Thunk)(const CBFunctorBase&);
- CBFunctor0(Thunk t, const void* c, const void* f, size_t sz) :
- CBFunctorBase(c, f, sz), thunk(t) {}
- private:
- Thunk thunk;
- };
- template <class Callee, class MemFunc>
- class CBMemberTranslator0 :public CBFunctor0 {
- public:
- CBMemberTranslator0(Callee& c, const MemFunc& m) :
- CBFunctor0(thunk, &c, &m, sizeof(MemFunc)) {}
- static void thunk(const CBFunctorBase& ftor)
- {
- Callee* callee = (Callee*)ftor.callee;
- MemFunc& memFunc(*(MemFunc*)(void*)(ftor.memFunc));
- (callee->*memFunc)();
- }
- };
- template <class Func>
- class CBFunctionTranslator0 :public CBFunctor0 {
- public:
- CBFunctionTranslator0(Func f) :CBFunctor0(thunk, 0, f, 0) {}
- static void thunk(const CBFunctorBase& ftor)
- {
- (Func(ftor.func))();
- }
- };
- template <class Callee, class TRT, class CallType>
- inline CBMemberTranslator0<Callee, TRT(CallType::*)()>
- makeFunctor(CBFunctor0*, Callee& c, TRT(CallType::* const& f)())
- {
- typedef TRT(CallType::* MemFunc)();
- return CBMemberTranslator0<Callee, MemFunc>(c, f);
- }
- template <class Callee, class TRT, class CallType>
- inline CBMemberTranslator0<const Callee, TRT(CallType::*)()const>
- makeFunctor(CBFunctor0*, const Callee& c, TRT(CallType::* const& f)()const)
- {
- typedef TRT(CallType::* MemFunc)()const;
- return CBMemberTranslator0<const Callee, MemFunc>(c, f);
- }
- template <class TRT>
- inline CBFunctionTranslator0<TRT(*)()>
- makeFunctor(CBFunctor0*, TRT(*f)())
- {
- return CBFunctionTranslator0<TRT(*)()>(f);
- }
- /************************* no arg - with return *******************/
- template <class RT>
- class CBFunctor0wRet :protected CBFunctorBase {
- public:
- CBFunctor0wRet(DummyInit* = 0) {}
- RT operator()()const
- {
- return BC4_RET_BUG(thunk(*this));
- }
- CBFunctorBase::operator int;
- protected:
- typedef RT(*Thunk)(const CBFunctorBase&);
- CBFunctor0wRet(Thunk t, const void* c, const void* f, size_t sz) :
- CBFunctorBase(c, f, sz), thunk(t) {}
- private:
- Thunk thunk;
- };
- template <class RT, class Callee, class MemFunc>
- class CBMemberTranslator0wRet :public CBFunctor0wRet<RT> {
- public:
- CBMemberTranslator0wRet(Callee& c, const MemFunc& m) :
- CBFunctor0wRet<RT>(thunk, &c, &m, sizeof(MemFunc)) {}
- static RT thunk(const CBFunctorBase& ftor)
- {
- Callee* callee = (Callee*)ftor.callee;
- MemFunc& memFunc(*(MemFunc*)(void*)(ftor.memFunc));
- return BC4_RET_BUG((callee->*memFunc)());
- }
- };
- template <class RT, class Func>
- class CBFunctionTranslator0wRet :public CBFunctor0wRet<RT> {
- public:
- CBFunctionTranslator0wRet(Func f) :CBFunctor0wRet<RT>(thunk, 0, f, 0) {}
- static RT thunk(const CBFunctorBase& ftor)
- {
- return (Func(ftor.func))();
- }
- };
- template <class RT, class Callee, class TRT, class CallType>
- inline CBMemberTranslator0wRet<RT, Callee, TRT(CallType::*)()>
- makeFunctor(CBFunctor0wRet<RT>*, Callee& c, TRT(CallType::* const& f)())
- {
- typedef TRT(CallType::* MemFunc)();
- return CBMemberTranslator0wRet<RT, Callee, MemFunc>(c, f);
- }
- template <class RT, class Callee, class TRT, class CallType>
- inline CBMemberTranslator0wRet<RT, const Callee, TRT(CallType::*)()const>
- makeFunctor(CBFunctor0wRet<RT>*, const Callee& c, TRT(CallType::* const& f)()const)
- {
- typedef TRT(CallType::* MemFunc)()const;
- return CBMemberTranslator0wRet<RT, const Callee, MemFunc>(c, f);
- }
- template <class RT, class TRT>
- inline CBFunctionTranslator0wRet<RT, TRT(*)()>
- makeFunctor(CBFunctor0wRet<RT>*, TRT(*f)())
- {
- return CBFunctionTranslator0wRet<RT, TRT(*)()>(f);
- }
- /************************* one arg - no return *******************/
- template <class P1>
- class CBFunctor1 :protected CBFunctorBase {
- public:
- CBFunctor1(DummyInit* = 0) {}
- void operator()(P1 p1)const
- {
- thunk(*this, p1);
- }
- CBFunctorBase::operator int;
- //for STL
- typedef P1 argument_type;
- typedef void result_type;
- protected:
- typedef void (*Thunk)(const CBFunctorBase&, P1);
- CBFunctor1(Thunk t, const void* c, const void* f, size_t sz) :
- CBFunctorBase(c, f, sz), thunk(t) {}
- private:
- Thunk thunk;
- };
- template <class P1, class Callee, class MemFunc>
- class CBMemberTranslator1 :public CBFunctor1<P1> {
- public:
- CBMemberTranslator1(Callee& c, const MemFunc& m) :
- CBFunctor1<P1>(thunk, &c, &m, sizeof(MemFunc)) {}
- static void thunk(const CBFunctorBase& ftor, P1 p1)
- {
- Callee* callee = (Callee*)ftor.callee;
- MemFunc& memFunc(*(MemFunc*)(void*)(ftor.memFunc));
- (callee->*memFunc)(p1);
- }
- };
- template <class P1, class Func>
- class CBFunctionTranslator1 :public CBFunctor1<P1> {
- public:
- CBFunctionTranslator1(Func f) :CBFunctor1<P1>(thunk, 0, f, 0) {}
- static void thunk(const CBFunctorBase& ftor, P1 p1)
- {
- (Func(ftor.func))(p1);
- }
- };
- template <class P1, class Callee, class TRT, class CallType, class TP1>
- inline CBMemberTranslator1<P1, Callee, TRT(CallType::*)(TP1)>
- makeFunctor(CBFunctor1<P1>*, Callee& c, TRT(CallType::* const& f)(TP1))
- {
- typedef TRT(CallType::* MemFunc)(TP1);
- return CBMemberTranslator1<P1, Callee, MemFunc>(c, f);
- }
- template <class P1, class Callee, class TRT, class CallType, class TP1>
- inline CBMemberTranslator1<P1, const Callee, TRT(CallType::*)(TP1)const>
- makeFunctor(CBFunctor1<P1>*, const Callee& c, TRT(CallType::* const& f)(TP1)const)
- {
- typedef TRT(CallType::* MemFunc)(TP1)const;
- return CBMemberTranslator1<P1, const Callee, MemFunc>(c, f);
- }
- template <class P1, class TRT, class TP1>
- inline CBFunctionTranslator1<P1, TRT(*)(TP1)>
- makeFunctor(CBFunctor1<P1>*, TRT(*f)(TP1))
- {
- return CBFunctionTranslator1<P1, TRT(*)(TP1)>(f);
- }
- template <class P1, class MemFunc>
- class CBMemberOf1stArgTranslator1 :public CBFunctor1<P1> {
- public:
- CBMemberOf1stArgTranslator1(const MemFunc& m) :
- CBFunctor1<P1>(thunk, (void*)1, &m, sizeof(MemFunc)) {}
- static void thunk(const CBFunctorBase& ftor, P1 p1)
- {
- MemFunc& memFunc(*(MemFunc*)(void*)(ftor.memFunc));
- (p1.*memFunc)();
- }
- };
- template <class P1, class TRT, class CallType>
- inline CBMemberOf1stArgTranslator1<P1, TRT(CallType::*)()>
- makeFunctor(CBFunctor1<P1>*, TRT(CallType::* const& f)())
- {
- typedef TRT(CallType::* MemFunc)();
- return CBMemberOf1stArgTranslator1<P1, MemFunc>(f);
- }
- template <class P1, class TRT, class CallType>
- inline CBMemberOf1stArgTranslator1<P1, TRT(CallType::*)()const>
- makeFunctor(CBFunctor1<P1>*, TRT(CallType::* const& f)()const)
- {
- typedef TRT(CallType::* MemFunc)()const;
- return CBMemberOf1stArgTranslator1<P1, MemFunc>(f);
- }
- /************************* one arg - with return *******************/
- template <class P1, class RT>
- class CBFunctor1wRet :protected CBFunctorBase {
- public:
- CBFunctor1wRet(DummyInit* = 0) {}
- RT operator()(P1 p1)const
- {
- return BC4_RET_BUG(thunk(*this, p1));
- }
- CBFunctorBase::operator int;
- //for STL
- typedef P1 argument_type;
- typedef RT result_type;
- protected:
- typedef RT(*Thunk)(const CBFunctorBase&, P1);
- CBFunctor1wRet(Thunk t, const void* c, const void* f, size_t sz) :
- CBFunctorBase(c, f, sz), thunk(t) {}
- private:
- Thunk thunk;
- };
- template <class P1, class RT, class Callee, class MemFunc>
- class CBMemberTranslator1wRet :public CBFunctor1wRet<P1, RT> {
- public:
- CBMemberTranslator1wRet(Callee& c, const MemFunc& m) :
- CBFunctor1wRet<P1, RT>(thunk, &c, &m, sizeof(MemFunc)) {}
- static RT thunk(const CBFunctorBase& ftor, P1 p1)
- {
- Callee* callee = (Callee*)ftor.callee;
- MemFunc& memFunc(*(MemFunc*)(void*)(ftor.memFunc));
- return BC4_RET_BUG((callee->*memFunc)(p1));
- }
- };
- template <class P1, class RT, class Func>
- class CBFunctionTranslator1wRet :public CBFunctor1wRet<P1, RT> {
- public:
- // CBFunctionTranslator1wRet(Func f):CBFunctor1wRet<P1,RT>(thunk,0,f,0){}
- // EBR
- CBFunctionTranslator1wRet(Func f) :CBFunctor1wRet<P1, RT>(thunk, 0, (void*)f, 0) {}
- static RT thunk(const CBFunctorBase& ftor, P1 p1)
- {
- return (Func(ftor.func))(p1);
- }
- };
- template <class P1, class RT,
- class Callee, class TRT, class CallType, class TP1>
- inline CBMemberTranslator1wRet<P1, RT, Callee, TRT(CallType::*)(TP1)>
- makeFunctor(CBFunctor1wRet<P1, RT>*, Callee& c, TRT(CallType::* const& f)(TP1))
- {
- typedef TRT(CallType::* MemFunc)(TP1);
- return CBMemberTranslator1wRet<P1, RT, Callee, MemFunc>(c, f);
- }
- template <class P1, class RT,
- class Callee, class TRT, class CallType, class TP1>
- inline CBMemberTranslator1wRet<P1, RT,
- const Callee, TRT(CallType::*)(TP1)const>
- makeFunctor(CBFunctor1wRet<P1, RT>*,
- const Callee& c, TRT(CallType::* const& f)(TP1)const)
- {
- typedef TRT(CallType::* MemFunc)(TP1)const;
- return CBMemberTranslator1wRet<P1, RT, const Callee, MemFunc>(c, f);
- }
- template <class P1, class RT, class TRT, class TP1>
- inline CBFunctionTranslator1wRet<P1, RT, TRT(*)(TP1)>
- makeFunctor(CBFunctor1wRet<P1, RT>*, TRT(*f)(TP1))
- {
- return CBFunctionTranslator1wRet<P1, RT, TRT(*)(TP1)>(f);
- }
- template <class P1, class RT, class MemFunc>
- class CBMemberOf1stArgTranslator1wRet :public CBFunctor1wRet<P1, RT> {
- public:
- CBMemberOf1stArgTranslator1wRet(const MemFunc& m) :
- CBFunctor1wRet<P1, RT>(thunk, (void*)1, &m, sizeof(MemFunc)) {}
- static RT thunk(const CBFunctorBase& ftor, P1 p1)
- {
- MemFunc& memFunc(*(MemFunc*)(void*)(ftor.memFunc));
- return BC4_RET_BUG((p1.*memFunc)());
- }
- };
- template <class P1, class RT, class TRT, class CallType>
- inline CBMemberOf1stArgTranslator1wRet<P1, RT, TRT(CallType::*)()>
- makeFunctor(CBFunctor1wRet<P1, RT>*, TRT(CallType::* const& f)())
- {
- typedef TRT(CallType::* MemFunc)();
- return CBMemberOf1stArgTranslator1wRet<P1, RT, MemFunc>(f);
- }
- template <class P1, class RT, class TRT, class CallType>
- inline CBMemberOf1stArgTranslator1wRet<P1, RT, TRT(CallType::*)()const>
- makeFunctor(CBFunctor1wRet<P1, RT>*, TRT(CallType::* const& f)()const)
- {
- typedef TRT(CallType::* MemFunc)()const;
- return CBMemberOf1stArgTranslator1wRet<P1, RT, MemFunc>(f);
- }
- /************************* two args - no return *******************/
- template <class P1, class P2>
- class CBFunctor2 :protected CBFunctorBase {
- public:
- CBFunctor2(DummyInit* = 0) {}
- void operator()(P1 p1, P2 p2)const
- {
- thunk(*this, p1, p2);
- }
- CBFunctorBase::operator int;
- //for STL
- typedef P1 first_argument_type;
- typedef P2 second_argument_type;
- typedef void result_type;
- protected:
- typedef void (*Thunk)(const CBFunctorBase&, P1, P2);
- CBFunctor2(Thunk t, const void* c, const void* f, size_t sz) :
- CBFunctorBase(c, f, sz), thunk(t) {}
- private:
- Thunk thunk;
- };
- template <class P1, class P2, class Callee, class MemFunc>
- class CBMemberTranslator2 :public CBFunctor2<P1, P2> {
- public:
- CBMemberTranslator2(Callee& c, const MemFunc& m) :
- CBFunctor2<P1, P2>(thunk, &c, &m, sizeof(MemFunc)) {}
- static void thunk(const CBFunctorBase& ftor, P1 p1, P2 p2)
- {
- Callee* callee = (Callee*)ftor.callee;
- MemFunc& memFunc(*(MemFunc*)(void*)(ftor.memFunc));
- (callee->*memFunc)(p1, p2);
- }
- };
- template <class P1, class P2, class Func>
- class CBFunctionTranslator2 :public CBFunctor2<P1, P2> {
- public:
- CBFunctionTranslator2(Func f) :CBFunctor2<P1, P2>(thunk, 0, f, 0) {}
- static void thunk(const CBFunctorBase& ftor, P1 p1, P2 p2)
- {
- (Func(ftor.func))(p1, p2);
- }
- };
- template <class P1, class P2, class Callee,
- class TRT, class CallType, class TP1, class TP2>
- inline CBMemberTranslator2<P1, P2, Callee, TRT(CallType::*)(TP1, TP2)>
- makeFunctor(CBFunctor2<P1, P2>*, Callee& c, TRT(CallType::* const& f)(TP1, TP2))
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2);
- return CBMemberTranslator2<P1, P2, Callee, MemFunc>(c, f);
- }
- template <class P1, class P2, class Callee,
- class TRT, class CallType, class TP1, class TP2>
- inline CBMemberTranslator2<P1, P2, const Callee,
- TRT(CallType::*)(TP1, TP2)const>
- makeFunctor(CBFunctor2<P1, P2>*, const Callee& c,
- TRT(CallType::* const& f)(TP1, TP2)const)
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2)const;
- return CBMemberTranslator2<P1, P2, const Callee, MemFunc>(c, f);
- }
- template <class P1, class P2, class TRT, class TP1, class TP2>
- inline CBFunctionTranslator2<P1, P2, TRT(*)(TP1, TP2)>
- makeFunctor(CBFunctor2<P1, P2>*, TRT(*f)(TP1, TP2))
- {
- return CBFunctionTranslator2<P1, P2, TRT(*)(TP1, TP2)>(f);
- }
- template <class P1, class P2, class MemFunc>
- class CBMemberOf1stArgTranslator2 :public CBFunctor2<P1, P2> {
- public:
- CBMemberOf1stArgTranslator2(const MemFunc& m) :
- CBFunctor2<P1, P2>(thunk, (void*)1, &m, sizeof(MemFunc)) {}
- static void thunk(const CBFunctorBase& ftor, P1 p1, P2 p2)
- {
- MemFunc& memFunc(*(MemFunc*)(void*)(ftor.memFunc));
- (p1.*memFunc)(p2);
- }
- };
- template <class P1, class P2, class TRT, class CallType, class TP1>
- inline CBMemberOf1stArgTranslator2<P1, P2, TRT(CallType::*)(TP1)>
- makeFunctor(CBFunctor2<P1, P2>*, TRT(CallType::* const& f)(TP1))
- {
- typedef TRT(CallType::* MemFunc)(TP1);
- return CBMemberOf1stArgTranslator2<P1, P2, MemFunc>(f);
- }
- template <class P1, class P2, class TRT, class CallType, class TP1>
- inline CBMemberOf1stArgTranslator2<P1, P2, TRT(CallType::*)(TP1)const>
- makeFunctor(CBFunctor2<P1, P2>*, TRT(CallType::* const& f)(TP1)const)
- {
- typedef TRT(CallType::* MemFunc)(TP1)const;
- return CBMemberOf1stArgTranslator2<P1, P2, MemFunc>(f);
- }
- /************************* two args - with return *******************/
- template <class P1, class P2, class RT>
- class CBFunctor2wRet :protected CBFunctorBase {
- public:
- CBFunctor2wRet(DummyInit* = 0) {}
- RT operator()(P1 p1, P2 p2)const
- {
- return BC4_RET_BUG(thunk(*this, p1, p2));
- }
- CBFunctorBase::operator int;
- //for STL
- typedef P1 first_argument_type;
- typedef P2 second_argument_type;
- typedef RT result_type;
- protected:
- typedef RT(*Thunk)(const CBFunctorBase&, P1, P2);
- CBFunctor2wRet(Thunk t, const void* c, const void* f, size_t sz) :
- CBFunctorBase(c, f, sz), thunk(t) {}
- private:
- Thunk thunk;
- };
- template <class P1, class P2, class RT, class Callee, class MemFunc>
- class CBMemberTranslator2wRet :public CBFunctor2wRet<P1, P2, RT> {
- public:
- CBMemberTranslator2wRet(Callee& c, const MemFunc& m) :
- CBFunctor2wRet<P1, P2, RT>(thunk, &c, &m, sizeof(MemFunc)) {}
- static RT thunk(const CBFunctorBase& ftor, P1 p1, P2 p2)
- {
- Callee* callee = (Callee*)ftor.callee;
- MemFunc& memFunc(*(MemFunc*)(void*)(ftor.memFunc));
- return BC4_RET_BUG((callee->*memFunc)(p1, p2));
- }
- };
- template <class P1, class P2, class RT, class Func>
- class CBFunctionTranslator2wRet :public CBFunctor2wRet<P1, P2, RT> {
- public:
- // CBFunctionTranslator2wRet(Func f):CBFunctor2wRet<P1,P2,RT>(thunk,0,f,0){}
- // EBR
- CBFunctionTranslator2wRet(Func f) :CBFunctor2wRet<P1, P2, RT>(thunk, 0, (void*)f, 0) {}
- static RT thunk(const CBFunctorBase& ftor, P1 p1, P2 p2)
- {
- return (Func(ftor.func))(p1, p2);
- }
- };
- template <class P1, class P2, class RT, class Callee,
- class TRT, class CallType, class TP1, class TP2>
- inline CBMemberTranslator2wRet<P1, P2, RT, Callee,
- TRT(CallType::*)(TP1, TP2)>
- makeFunctor(CBFunctor2wRet<P1, P2, RT>*, Callee& c, TRT(CallType::* const& f)(TP1, TP2))
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2);
- return CBMemberTranslator2wRet<P1, P2, RT, Callee, MemFunc>(c, f);
- }
- template <class P1, class P2, class RT, class Callee,
- class TRT, class CallType, class TP1, class TP2>
- inline CBMemberTranslator2wRet<P1, P2, RT, const Callee,
- TRT(CallType::*)(TP1, TP2)const>
- makeFunctor(CBFunctor2wRet<P1, P2, RT>*, const Callee& c,
- TRT(CallType::* const& f)(TP1, TP2)const)
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2)const;
- return CBMemberTranslator2wRet<P1, P2, RT, const Callee, MemFunc>(c, f);
- }
- template <class P1, class P2, class RT, class TRT, class TP1, class TP2>
- inline CBFunctionTranslator2wRet<P1, P2, RT, TRT(*)(TP1, TP2)>
- makeFunctor(CBFunctor2wRet<P1, P2, RT>*, TRT(*f)(TP1, TP2))
- {
- return CBFunctionTranslator2wRet<P1, P2, RT, TRT(*)(TP1, TP2)>(f);
- }
- template <class P1, class P2, class RT, class MemFunc>
- class CBMemberOf1stArgTranslator2wRet :public CBFunctor2wRet<P1, P2, RT> {
- public:
- CBMemberOf1stArgTranslator2wRet(const MemFunc& m) :
- CBFunctor2wRet<P1, P2, RT>(thunk, (void*)1, &m, sizeof(MemFunc)) {}
- static RT thunk(const CBFunctorBase& ftor, P1 p1, P2 p2)
- {
- MemFunc& memFunc(*(MemFunc*)(void*)(ftor.memFunc));
- return BC4_RET_BUG((p1.*memFunc)(p2));
- }
- };
- template <class P1, class P2, class RT, class TRT, class CallType, class TP1>
- inline CBMemberOf1stArgTranslator2wRet<P1, P2, RT, TRT(CallType::*)(TP1)>
- makeFunctor(CBFunctor2wRet<P1, P2, RT>*, TRT(CallType::* const& f)(TP1))
- {
- typedef TRT(CallType::* MemFunc)(TP1);
- return CBMemberOf1stArgTranslator2wRet<P1, P2, RT, MemFunc>(f);
- }
- template <class P1, class P2, class RT, class TRT, class CallType, class TP1>
- inline CBMemberOf1stArgTranslator2wRet<P1, P2, RT, TRT(CallType::*)(TP1)const>
- makeFunctor(CBFunctor2wRet<P1, P2, RT>*, TRT(CallType::* const& f)(TP1)const)
- {
- typedef TRT(CallType::* MemFunc)(TP1)const;
- return CBMemberOf1stArgTranslator2wRet<P1, P2, RT, MemFunc>(f);
- }
- /************************* three args - no return *******************/
- template <class P1, class P2, class P3>
- class CBFunctor3 :protected CBFunctorBase {
- public:
- CBFunctor3(DummyInit* = 0) {}
- void operator()(P1 p1, P2 p2, P3 p3)const
- {
- thunk(*this, p1, p2, p3);
- }
- CBFunctorBase::operator int;
- protected:
- typedef void (*Thunk)(const CBFunctorBase&, P1, P2, P3);
- CBFunctor3(Thunk t, const void* c, const void* f, size_t sz) :
- CBFunctorBase(c, f, sz), thunk(t) {}
- private:
- Thunk thunk;
- };
- template <class P1, class P2, class P3, class Callee, class MemFunc>
- class CBMemberTranslator3 :public CBFunctor3<P1, P2, P3> {
- public:
- CBMemberTranslator3(Callee& c, const MemFunc& m) :
- CBFunctor3<P1, P2, P3>(thunk, &c, &m, sizeof(MemFunc)) {}
- static void thunk(const CBFunctorBase& ftor, P1 p1, P2 p2, P3 p3)
- {
- Callee* callee = (Callee*)ftor.callee;
- MemFunc& memFunc(*(MemFunc*)(void*)(ftor.memFunc));
- (callee->*memFunc)(p1, p2, p3);
- }
- };
- template <class P1, class P2, class P3, class Func>
- class CBFunctionTranslator3 :public CBFunctor3<P1, P2, P3> {
- public:
- CBFunctionTranslator3(Func f) :CBFunctor3<P1, P2, P3>(thunk, 0, f, 0) {}
- static void thunk(const CBFunctorBase& ftor, P1 p1, P2 p2, P3 p3)
- {
- (Func(ftor.func))(p1, p2, p3);
- }
- };
- template <class P1, class P2, class P3, class Callee,
- class TRT, class CallType, class TP1, class TP2, class TP3>
- inline CBMemberTranslator3<P1, P2, P3, Callee,
- TRT(CallType::*)(TP1, TP2, TP3)>
- makeFunctor(CBFunctor3<P1, P2, P3>*, Callee& c,
- TRT(CallType::* const& f)(TP1, TP2, TP3))
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2, TP3);
- return CBMemberTranslator3<P1, P2, P3, Callee, MemFunc>(c, f);
- }
- template <class P1, class P2, class P3, class Callee,
- class TRT, class CallType, class TP1, class TP2, class TP3>
- inline CBMemberTranslator3<P1, P2, P3, const Callee,
- TRT(CallType::*)(TP1, TP2, TP3)const>
- makeFunctor(CBFunctor3<P1, P2, P3>*, const Callee& c,
- TRT(CallType::* const& f)(TP1, TP2, TP3)const)
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2, TP3)const;
- return CBMemberTranslator3<P1, P2, P3, const Callee, MemFunc>(c, f);
- }
- template <class P1, class P2, class P3,
- class TRT, class TP1, class TP2, class TP3>
- inline CBFunctionTranslator3<P1, P2, P3, TRT(*)(TP1, TP2, TP3)>
- makeFunctor(CBFunctor3<P1, P2, P3>*, TRT(*f)(TP1, TP2, TP3))
- {
- return CBFunctionTranslator3<P1, P2, P3, TRT(*)(TP1, TP2, TP3)>(f);
- }
- template <class P1, class P2, class P3, class MemFunc>
- class CBMemberOf1stArgTranslator3 :public CBFunctor3<P1, P2, P3> {
- public:
- CBMemberOf1stArgTranslator3(const MemFunc& m) :
- CBFunctor3<P1, P2, P3>(thunk, (void*)1, &m, sizeof(MemFunc)) {}
- static void thunk(const CBFunctorBase& ftor, P1 p1, P2 p2, P3 p3)
- {
- MemFunc& memFunc(*(MemFunc*)(void*)(ftor.memFunc));
- (p1.*memFunc)(p2, p3);
- }
- };
- template <class P1, class P2, class P3, class TRT, class CallType,
- class TP1, class TP2>
- inline CBMemberOf1stArgTranslator3<P1, P2, P3, TRT(CallType::*)(TP1, TP2)>
- makeFunctor(CBFunctor3<P1, P2, P3>*, TRT(CallType::* const& f)(TP1, TP2))
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2);
- return CBMemberOf1stArgTranslator3<P1, P2, P3, MemFunc>(f);
- }
- template <class P1, class P2, class P3, class TRT, class CallType,
- class TP1, class TP2>
- inline CBMemberOf1stArgTranslator3<P1, P2, P3, TRT(CallType::*)(TP1, TP2)const>
- makeFunctor(CBFunctor3<P1, P2, P3>*, TRT(CallType::* const& f)(TP1, TP2)const)
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2)const;
- return CBMemberOf1stArgTranslator3<P1, P2, P3, MemFunc>(f);
- }
- /************************* three args - with return *******************/
- template <class P1, class P2, class P3, class RT>
- class CBFunctor3wRet :protected CBFunctorBase {
- public:
- CBFunctor3wRet(DummyInit* = 0) {}
- RT operator()(P1 p1, P2 p2, P3 p3)const
- {
- return BC4_RET_BUG(thunk(*this, p1, p2, p3));
- }
- CBFunctorBase::operator int;
- protected:
- typedef RT(*Thunk)(const CBFunctorBase&, P1, P2, P3);
- CBFunctor3wRet(Thunk t, const void* c, const void* f, size_t sz) :
- CBFunctorBase(c, f, sz), thunk(t) {}
- private:
- Thunk thunk;
- };
- template <class P1, class P2, class P3,
- class RT, class Callee, class MemFunc>
- class CBMemberTranslator3wRet :public CBFunctor3wRet<P1, P2, P3, RT> {
- public:
- CBMemberTranslator3wRet(Callee& c, const MemFunc& m) :
- CBFunctor3wRet<P1, P2, P3, RT>(thunk, &c, &m, sizeof(MemFunc)) {}
- static RT thunk(const CBFunctorBase& ftor, P1 p1, P2 p2, P3 p3)
- {
- Callee* callee = (Callee*)ftor.callee;
- MemFunc& memFunc(*(MemFunc*)(void*)(ftor.memFunc));
- return BC4_RET_BUG((callee->*memFunc)(p1, p2, p3));
- }
- };
- template <class P1, class P2, class P3, class RT, class Func>
- class CBFunctionTranslator3wRet :public CBFunctor3wRet<P1, P2, P3, RT> {
- public:
- CBFunctionTranslator3wRet(Func f) :
- CBFunctor3wRet<P1, P2, P3, RT>(thunk, 0, f, 0) {}
- static RT thunk(const CBFunctorBase& ftor, P1 p1, P2 p2, P3 p3)
- {
- return (Func(ftor.func))(p1, p2, p3);
- }
- };
- template <class P1, class P2, class P3, class RT, class Callee,
- class TRT, class CallType, class TP1, class TP2, class TP3>
- inline CBMemberTranslator3wRet<P1, P2, P3, RT, Callee,
- TRT(CallType::*)(TP1, TP2, TP3)>
- makeFunctor(CBFunctor3wRet<P1, P2, P3, RT>*, Callee& c,
- TRT(CallType::* const& f)(TP1, TP2, TP3))
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2, TP3);
- return CBMemberTranslator3wRet<P1, P2, P3, RT, Callee, MemFunc>(c, f);
- }
- template <class P1, class P2, class P3, class RT, class Callee,
- class TRT, class CallType, class TP1, class TP2, class TP3>
- inline CBMemberTranslator3wRet<P1, P2, P3, RT, const Callee,
- TRT(CallType::*)(TP1, TP2, TP3)const>
- makeFunctor(CBFunctor3wRet<P1, P2, P3, RT>*, const Callee& c,
- TRT(CallType::* const& f)(TP1, TP2, TP3)const)
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2, TP3)const;
- return CBMemberTranslator3wRet<P1, P2, P3, RT, const Callee, MemFunc>(c, f);
- }
- template <class P1, class P2, class P3, class RT,
- class TRT, class TP1, class TP2, class TP3>
- inline CBFunctionTranslator3wRet<P1, P2, P3, RT, TRT(*)(TP1, TP2, TP3)>
- makeFunctor(CBFunctor3wRet<P1, P2, P3, RT>*, TRT(*f)(TP1, TP2, TP3))
- {
- return CBFunctionTranslator3wRet<P1, P2, P3, RT, TRT(*)(TP1, TP2, TP3)>(f);
- }
- template <class P1, class P2, class P3, class RT, class MemFunc>
- class CBMemberOf1stArgTranslator3wRet :public CBFunctor3wRet<P1, P2, P3, RT> {
- public:
- CBMemberOf1stArgTranslator3wRet(const MemFunc& m) :
- CBFunctor3wRet<P1, P2, P3, RT>(thunk, (void*)1, &m, sizeof(MemFunc)) {}
- static RT thunk(const CBFunctorBase& ftor, P1 p1, P2 p2, P3 p3)
- {
- MemFunc& memFunc(*(MemFunc*)(void*)(ftor.memFunc));
- return BC4_RET_BUG((p1.*memFunc)(p2, p3));
- }
- };
- template <class P1, class P2, class P3, class RT, class TRT, class CallType,
- class TP1, class TP2>
- inline CBMemberOf1stArgTranslator3wRet<P1, P2, P3, RT, TRT(CallType::*)(TP1, TP2)>
- makeFunctor(CBFunctor3wRet<P1, P2, P3, RT>*, TRT(CallType::* const& f)(TP1, TP2))
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2);
- return CBMemberOf1stArgTranslator3wRet<P1, P2, P3, RT, MemFunc>(f);
- }
- template <class P1, class P2, class P3, class RT, class TRT, class CallType,
- class TP1, class TP2>
- inline CBMemberOf1stArgTranslator3wRet<P1, P2, P3, RT,
- TRT(CallType::*)(TP1, TP2)const>
- makeFunctor(CBFunctor3wRet<P1, P2, P3, RT>*,
- TRT(CallType::* const& f)(TP1, TP2)const)
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2)const;
- return CBMemberOf1stArgTranslator3wRet<P1, P2, P3, RT, MemFunc>(f);
- }
- /************************* four args - no return *******************/
- template <class P1, class P2, class P3, class P4>
- class CBFunctor4 :protected CBFunctorBase {
- public:
- CBFunctor4(DummyInit* = 0) {}
- void operator()(P1 p1, P2 p2, P3 p3, P4 p4)const
- {
- thunk(*this, p1, p2, p3, p4);
- }
- CBFunctorBase::operator int;
- protected:
- typedef void (*Thunk)(const CBFunctorBase&, P1, P2, P3, P4);
- CBFunctor4(Thunk t, const void* c, const void* f, size_t sz) :
- CBFunctorBase(c, f, sz), thunk(t) {}
- private:
- Thunk thunk;
- };
- template <class P1, class P2, class P3, class P4,
- class Callee, class MemFunc>
- class CBMemberTranslator4 :public CBFunctor4<P1, P2, P3, P4> {
- public:
- CBMemberTranslator4(Callee& c, const MemFunc& m) :
- CBFunctor4<P1, P2, P3, P4>(thunk, &c, &m, sizeof(MemFunc)) {}
- static void thunk(const CBFunctorBase& ftor, P1 p1, P2 p2, P3 p3, P4 p4)
- {
- Callee* callee = (Callee*)ftor.callee;
- MemFunc& memFunc(*(MemFunc*)(void*)(ftor.memFunc));
- (callee->*memFunc)(p1, p2, p3, p4);
- }
- };
- template <class P1, class P2, class P3, class P4, class Func>
- class CBFunctionTranslator4 :public CBFunctor4<P1, P2, P3, P4> {
- public:
- // CBFunctionTranslator4(Func f):CBFunctor4<P1,P2,P3,P4>(thunk,0,f,0){}
- //EBR
- CBFunctionTranslator4(Func f) :CBFunctor4<P1, P2, P3, P4>(thunk, 0, (void*)f, 0) {}
- static void thunk(const CBFunctorBase& ftor, P1 p1, P2 p2, P3 p3, P4 p4)
- {
- (Func(ftor.func))(p1, p2, p3, p4);
- }
- };
- template <class P1, class P2, class P3, class P4, class Callee,
- class TRT, class CallType, class TP1, class TP2, class TP3, class TP4>
- inline CBMemberTranslator4<P1, P2, P3, P4, Callee,
- TRT(CallType::*)(TP1, TP2, TP3, TP4)>
- makeFunctor(CBFunctor4<P1, P2, P3, P4>*, Callee& c,
- TRT(CallType::* const& f)(TP1, TP2, TP3, TP4))
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2, TP3, TP4);
- return CBMemberTranslator4<P1, P2, P3, P4, Callee, MemFunc>(c, f);
- }
- template <class P1, class P2, class P3, class P4, class Callee,
- class TRT, class CallType, class TP1, class TP2, class TP3, class TP4>
- inline CBMemberTranslator4<P1, P2, P3, P4, const Callee,
- TRT(CallType::*)(TP1, TP2, TP3, TP4)const>
- makeFunctor(CBFunctor4<P1, P2, P3, P4>*, const Callee& c,
- TRT(CallType::* const& f)(TP1, TP2, TP3, TP4)const)
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2, TP3, TP4)const;
- return CBMemberTranslator4<P1, P2, P3, P4, const Callee, MemFunc>(c, f);
- }
- template <class P1, class P2, class P3, class P4,
- class TRT, class TP1, class TP2, class TP3, class TP4>
- inline CBFunctionTranslator4<P1, P2, P3, P4, TRT(*)(TP1, TP2, TP3, TP4)>
- makeFunctor(CBFunctor4<P1, P2, P3, P4>*, TRT(*f)(TP1, TP2, TP3, TP4))
- {
- return CBFunctionTranslator4<P1, P2, P3, P4, TRT(*)(TP1, TP2, TP3, TP4)>(f);
- }
- template <class P1, class P2, class P3, class P4, class MemFunc>
- class CBMemberOf1stArgTranslator4 :public CBFunctor4<P1, P2, P3, P4> {
- public:
- CBMemberOf1stArgTranslator4(const MemFunc& m) :
- CBFunctor4<P1, P2, P3, P4>(thunk, (void*)1, &m, sizeof(MemFunc)) {}
- static void thunk(const CBFunctorBase& ftor, P1 p1, P2 p2, P3 p3, P4 p4)
- {
- MemFunc& memFunc(*(MemFunc*)(void*)(ftor.memFunc));
- (p1.*memFunc)(p2, p3, p4);
- }
- };
- template <class P1, class P2, class P3, class P4, class TRT, class CallType,
- class TP1, class TP2, class TP3>
- inline CBMemberOf1stArgTranslator4<P1, P2, P3, P4, TRT(CallType::*)(TP1, TP2, TP3)>
- makeFunctor(CBFunctor4<P1, P2, P3, P4>*, TRT(CallType::* const& f)(TP1, TP2, TP3))
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2, TP3);
- return CBMemberOf1stArgTranslator4<P1, P2, P3, P4, MemFunc>(f);
- }
- template <class P1, class P2, class P3, class P4, class TRT, class CallType,
- class TP1, class TP2, class TP3>
- inline CBMemberOf1stArgTranslator4<P1, P2, P3, P4,
- TRT(CallType::*)(TP1, TP2, TP3)const>
- makeFunctor(CBFunctor4<P1, P2, P3, P4>*,
- TRT(CallType::* const& f)(TP1, TP2, TP3)const)
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2, TP3)const;
- return CBMemberOf1stArgTranslator4<P1, P2, P3, P4, MemFunc>(f);
- }
- /************************* four args - with return *******************/
- template <class P1, class P2, class P3, class P4, class RT>
- class CBFunctor4wRet :protected CBFunctorBase {
- public:
- CBFunctor4wRet(DummyInit* = 0) {}
- RT operator()(P1 p1, P2 p2, P3 p3, P4 p4)const
- {
- return BC4_RET_BUG(thunk(*this, p1, p2, p3, p4));
- }
- CBFunctorBase::operator int;
- protected:
- typedef RT(*Thunk)(const CBFunctorBase&, P1, P2, P3, P4);
- CBFunctor4wRet(Thunk t, const void* c, const void* f, size_t sz) :
- CBFunctorBase(c, f, sz), thunk(t) {}
- private:
- Thunk thunk;
- };
- template <class P1, class P2, class P3, class P4, class RT,
- class Callee, class MemFunc>
- class CBMemberTranslator4wRet :public CBFunctor4wRet<P1, P2, P3, P4, RT> {
- public:
- CBMemberTranslator4wRet(Callee& c, const MemFunc& m) :
- CBFunctor4wRet<P1, P2, P3, P4, RT>(thunk, &c, &m, sizeof(MemFunc)) {}
- static RT thunk(const CBFunctorBase& ftor, P1 p1, P2 p2, P3 p3, P4 p4)
- {
- Callee* callee = (Callee*)ftor.callee;
- MemFunc& memFunc(*(MemFunc*)(void*)(ftor.memFunc));
- return BC4_RET_BUG((callee->*memFunc)(p1, p2, p3, p4));
- }
- };
- template <class P1, class P2, class P3, class P4, class RT, class Func>
- class CBFunctionTranslator4wRet :public CBFunctor4wRet<P1, P2, P3, P4, RT> {
- public:
- CBFunctionTranslator4wRet(Func f) :
- CBFunctor4wRet<P1, P2, P3, P4, RT>(thunk, 0, f, 0) {}
- static RT thunk(const CBFunctorBase& ftor, P1 p1, P2 p2, P3 p3, P4 p4)
- {
- return (Func(ftor.func))(p1, p2, p3, p4);
- }
- };
- template <class P1, class P2, class P3, class P4, class RT, class Callee,
- class TRT, class CallType, class TP1, class TP2, class TP3, class TP4>
- inline CBMemberTranslator4wRet<P1, P2, P3, P4, RT, Callee,
- TRT(CallType::*)(TP1, TP2, TP3, TP4)>
- makeFunctor(CBFunctor4wRet<P1, P2, P3, P4, RT>*, Callee& c,
- TRT(CallType::* const& f)(TP1, TP2, TP3, TP4))
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2, TP3, TP4);
- return CBMemberTranslator4wRet<P1, P2, P3, P4, RT, Callee, MemFunc>(c, f);
- }
- template <class P1, class P2, class P3, class P4, class RT, class Callee,
- class TRT, class CallType, class TP1, class TP2, class TP3, class TP4>
- inline CBMemberTranslator4wRet<P1, P2, P3, P4, RT, const Callee,
- TRT(CallType::*)(TP1, TP2, TP3, TP4)const>
- makeFunctor(CBFunctor4wRet<P1, P2, P3, P4, RT>*, const Callee& c,
- TRT(CallType::* const& f)(TP1, TP2, TP3, TP4)const)
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2, TP3, TP4)const;
- return CBMemberTranslator4wRet<P1, P2, P3, P4, RT, const Callee, MemFunc>(c, f);
- }
- template <class P1, class P2, class P3, class P4, class RT,
- class TRT, class TP1, class TP2, class TP3, class TP4>
- inline CBFunctionTranslator4wRet<P1, P2, P3, P4, RT, TRT(*)(TP1, TP2, TP3, TP4)>
- makeFunctor(CBFunctor4wRet<P1, P2, P3, P4, RT>*, TRT(*f)(TP1, TP2, TP3, TP4))
- {
- return CBFunctionTranslator4wRet
- <P1, P2, P3, P4, RT, TRT(*)(TP1, TP2, TP3, TP4)>(f);
- }
- template <class P1, class P2, class P3, class P4, class RT, class MemFunc>
- class CBMemberOf1stArgTranslator4wRet :public CBFunctor4wRet<P1, P2, P3, P4, RT> {
- public:
- CBMemberOf1stArgTranslator4wRet(const MemFunc& m) :
- CBFunctor4wRet<P1, P2, P3, P4, RT>(thunk, (void*)1, &m, sizeof(MemFunc)) {}
- static RT thunk(const CBFunctorBase& ftor, P1 p1, P2 p2, P3 p3, P4 p4)
- {
- MemFunc& memFunc(*(MemFunc*)(void*)(ftor.memFunc));
- return BC4_RET_BUG((p1.*memFunc)(p2, p3, p4));
- }
- };
- template <class P1, class P2, class P3, class P4, class RT, class TRT,
- class CallType, class TP1, class TP2, class TP3>
- inline CBMemberOf1stArgTranslator4wRet<P1, P2, P3, P4, RT,
- TRT(CallType::*)(TP1, TP2, TP3)>
- makeFunctor(CBFunctor4wRet<P1, P2, P3, P4, RT>*,
- TRT(CallType::* const& f)(TP1, TP2, TP3))
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2, TP3);
- return CBMemberOf1stArgTranslator4wRet<P1, P2, P3, P4, RT, MemFunc>(f);
- }
- template <class P1, class P2, class P3, class P4, class RT, class TRT,
- class CallType, class TP1, class TP2, class TP3>
- inline CBMemberOf1stArgTranslator4wRet<P1, P2, P3, P4, RT,
- TRT(CallType::*)(TP1, TP2, TP3)const>
- makeFunctor(CBFunctor4wRet<P1, P2, P3, P4, RT>*,
- TRT(CallType::* const& f)(TP1, TP2, TP3)const)
- {
- typedef TRT(CallType::* MemFunc)(TP1, TP2, TP3)const;
- return CBMemberOf1stArgTranslator4wRet<P1, P2, P3, P4, RT, MemFunc>(f);
- }
- #endif //CALLBACK_HPP
|