Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help writing a functor binder

34 views
Skip to first unread message

Brendon

unread,
May 16, 2012, 8:29:01 PM5/16/12
to
Hi all, I am a bit stuck on how to achieve something I want to do and
was wondering if anyone can point me to somewhere where I can find a
pattern that may help me implement this.

I started implementing something and kept running into the problem of
needing to specialize for two things at once:
1) void* return type (indicating to ignore return value of functor)
and
2) void* input type (indicating don't pass any params to the functor)

Basically I want to "bind" a functor to an optional return storage and
an optional input argument to produce a new functor that has usage
like: void f(void)

One thing is I do not want to use any external libraries except STL to
implement this.

I am trying hard to reduce the scope of this problem and formulate it
in a isolated way. So sorry if the example I give below seems strange
it is not exactly how it is fitting into a larger piece of software.

Thanks in advance for any help you might be able to give.

Here is an example interface:

template <typename ResultT, typename InputT, typename FunctorT>
class CallFunctor
{
// result:
// pointer to storage where the functor result is to be stored. Not
required to be exact same type as return of functor just assignable
// If this is a void* then the result of the functor is to be
ignored
//
// input:
// pointer to storage where input parameter to the functor can be
obtained from. Not required to be exact same type as arg of functor
just assignable
// If this is a void* then the functor is to be called with no input
arguments
//
// functor:
// a unary or nullary functor that may or may-not return anything
CallFunctor(ResultT* result, InputT* input, FunctorT functor)
{
... do stuff ...
}

// Will call functor with optional input arg and optionally assign
result of functor to result
void operator()()
{
... do stuff ...
}
}

// Simple function wrapper to allow automatic type deduction
template <typename ResultT, typename InputT, typename FunctorT>
CallFunctor<ResultT, InputT, FunctorT> GenerateFunctor(ResultT*
result, InputT* input, FunctorT functor)
{
return CallFunctor(result, input, functor);
}

// This could be used like:


void VoidFuncVoid();
void VoidFuncInt(int i);
int IntFuncVoid();
int IntFuncFloat(float f);


void* NOTHING = NULL;
int i = 1;

// Call functor not looking at the return value and not passing any
arguments
GenerateFunctor(NOTHING, NOTHING, &VoidFuncVoid)();

// Call functor not looking at return value passing an integer as the
only argument
GenerateFunctor(NOTHING, &i, &VoidFuncInt)();

// Call functor assigning return value to i and passing no arguments
GenerateFunctor(&i, NOTHING, &IntFuncVoid)();

// Call functor assigning return value to i and passing i as the only
argument (implicitly casted to float)
GenerateFunctor(&i, &i, &IntFuncFloat)();


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Brendon

unread,
May 17, 2012, 2:31:24 PM5/17/12
to
It turns out after looking at the problem more, it is simple to solve
with partial template specialization. I didn't think it would work in
this case but it does.
0 new messages