473,409 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Creating a functor creator

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<Xfunctor(cell<Y>&, cell<Z>&);

cell<Xis an already-defined class I created which defers computation
- basically cell<X+ cell<Xwill return a cell<Xwhich, 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<Roperator()(const cell<A1a1, const cell<A2a2) 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.
Nov 18 '07 #1
3 2191
alan wrote:
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.
http://groups.google.com/group/comp....4c97e168e627c4

I'd like to recommend you the implementation from
Thiago R. Adams
But now I can't access this page
Nov 18 '07 #2
On Nov 18, 11:06 pm, Barry <dhb2...@gmail.comwrote:
alan wrote:
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.

http://groups.google.com/group/comp....msg/c84c97e168...

I'd like to recommend you the implementation from
Thiago R. Adams
But now I can't access this page
I can. Thanks.

In any case, it appears very similar to the code I'm seeing in the
boost::function library - there's a bunch of macros for creating the
variable number of arguments, and several inclusions of the same file
with different numbers, even the comma-macro thing. Although it does
seem easier to read than the boost::function code. Hmm. Is
preprocessor hackery really the best solution?

I suppose I'll need to copy that preprocessor framework as well.
Nov 18 '07 #3
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<Xfunctor(cell<Y>&, cell<Z>&);
On Nov 18, 11:30 pm, alan <almkg...@gmail.comwrote:
On Nov 18, 11:06 pm, Barry <dhb2...@gmail.comwrote:alan wrote:
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.
http://groups.google.com/group/comp....msg/c84c97e168...
I'd like to recommend you the implementation from
Thiago R. Adams
But now I can't access this page

I can. Thanks.

In any case, it appears very similar to the code I'm seeing in the
boost::function library - there's a bunch of macros for creating the
variable number of arguments, and several inclusions of the same file
with different numbers, even the comma-macro thing. Although it does
seem easier to read than the boost::function code. Hmm. Is
preprocessor hackery really the best solution?

I suppose I'll need to copy that preprocessor framework as well.
No wait a minute, there's something I completely and totally forgot.

What the functor creator should return should be a polymorphic
functor. That is, it should return something equivalent to:
template<class X, class Y, class Z>
cell<Xfunctor(cell<Y>, cell<Z>){...}

template<class X, class Y, class Z>
cell<Xfunctor(Y y, cell<Zz) {return functor(cell<Y>(y), z);}

template<class X, class Y, class Z>
cell<Xfunctor(cell<Yy, Z z) {return functor(y, cell<Z>(z));}

template<class X, class Y, class Z>
cell<Xfunctor(Y y, Z z) {return functor(cell<Y>(y), cell<Z>(z));}
Dang. I can just barely imagine how to do it similar to
boost::function, but I can't imagine how this is to be done... I'll
need 2^N expansions. Hmm hmm hmm. This will really exercise the
preprocessor...
Nov 18 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function<...
3
by: CoolPint | last post by:
I have implemented a generic priority queue below and tested it works fine, but I have one small problem I cannot understand. I have type parameter F which determines the priority so that users can...
8
by: daniel.w.gelder | last post by:
Hello, I have been trying to write a functor template for a week now and I'm just having tons of trouble because I don't understand an issue that I guess is pretty basic to this task. ...
4
by: daniel.w.gelder | last post by:
I wrote a template class that takes a function prototype and lets you store and call a C-level function, like this: inline string SampleFunction(int, bool) {..} functor<string (int, bool)>...
0
by: Roshan | last post by:
Hi, I am trying to programatically add a FileSystemAccessRule for CREATOR OWNER to the filesystemsecurity obj of a folder whose creator and owner is a user account say 'SomeUser'. The rule gets...
12
by: aaragon | last post by:
Hi everyone, I'm trying to provide some external functionality to a class through a functor object defined by the user. The concept is as follows: template <class Functor> class ClassA {...
2
by: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1...
5
by: Fei Liu | last post by:
Hello, I have a situation where I need to design a library for multi-thread application, each thread does some work in a client supplied std::ptr_fun(free_function) or a functor. Now since it's...
2
by: aaragon | last post by:
Hi guys, Is there a way to return a functor from a recursive call that takes different paths? Let's say that I have a tree structure like: root | first child ---- nextSibling ----nextSibling...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.