A
alan
Hello all, I'd like to know if there is a nice method of defining a
functor creator which accepts an N-ary function and returns a functor
based on that function.
For example I have a function:
template<class X, class Y, class Z>
X function(Y, Z);
Passing it to the functor creator will give a functor equivalent to:
template<class X, class Y, class Z>
cell<X> functor(cell<Y>&, cell<Z>&);
cell<X> is an already-defined class I created which defers computation
- basically cell<X> + cell<X> will return a cell<X> which, when its
get_value() member function is called, will return the sum of the
source cells.
So far this is how I would implement it (unchecked):
template<class R, class A1, class A2>
class encell2 {
typedef R funtype(A1, A2);
funtype* funptr;
public:
encell2<R, A1, A2> (funtype& f): funptr(&f) {}
cell<R> operator()(const cell<A1> a1, const cell<A2> a2) const {
return cell<R>(
/*helper class to perform unpacking of data for us*/
new cell_function2<R, A1, A2>(funptr, a1, a2)
);
}
}
template<class R, class A1, class A2>
class cell_function2: public cell_internal{
/*not exactly how its done but this is a reasonable
approximation*/
boost::shared_ptr< cell_body<A1> > a1_;
boost::shared_ptr< cell_body<A2> > a2_;
typedef R funtype(A1, A2);
funtype *funptr;
public:
cell_function2<R, A1, A2>(const funtype* f, const cell<A1>&a1,
const cell<A2>& a2)
: funtype(f), a1_(a1.value_), a2_(a2.value_){ }
/*virtual function inherited from cell_internal*/
R get_value(){
return funptr(a1_->get_value(), a2_->get_value());
}
}
However, the solution above requires that I also add the arity to the
name. And it won't work with other functors (though I'm sure that's
possible, after all that's what the various variants of bind do).
I really liked the syntax of boost::function, where you just declare
the function's type in the template. However I got dizzy while trying
to hack through its code.
functor creator which accepts an N-ary function and returns a functor
based on that function.
For example I have a function:
template<class X, class Y, class Z>
X function(Y, Z);
Passing it to the functor creator will give a functor equivalent to:
template<class X, class Y, class Z>
cell<X> functor(cell<Y>&, cell<Z>&);
cell<X> is an already-defined class I created which defers computation
- basically cell<X> + cell<X> will return a cell<X> which, when its
get_value() member function is called, will return the sum of the
source cells.
So far this is how I would implement it (unchecked):
template<class R, class A1, class A2>
class encell2 {
typedef R funtype(A1, A2);
funtype* funptr;
public:
encell2<R, A1, A2> (funtype& f): funptr(&f) {}
cell<R> operator()(const cell<A1> a1, const cell<A2> a2) const {
return cell<R>(
/*helper class to perform unpacking of data for us*/
new cell_function2<R, A1, A2>(funptr, a1, a2)
);
}
}
template<class R, class A1, class A2>
class cell_function2: public cell_internal{
/*not exactly how its done but this is a reasonable
approximation*/
boost::shared_ptr< cell_body<A1> > a1_;
boost::shared_ptr< cell_body<A2> > a2_;
typedef R funtype(A1, A2);
funtype *funptr;
public:
cell_function2<R, A1, A2>(const funtype* f, const cell<A1>&a1,
const cell<A2>& a2)
: funtype(f), a1_(a1.value_), a2_(a2.value_){ }
/*virtual function inherited from cell_internal*/
R get_value(){
return funptr(a1_->get_value(), a2_->get_value());
}
}
However, the solution above requires that I also add the arity to the
name. And it won't work with other functors (though I'm sure that's
possible, after all that's what the various variants of bind do).
I really liked the syntax of boost::function, where you just declare
the function's type in the template. However I got dizzy while trying
to hack through its code.