473,503 Members | 7,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loki-like functors with boost::function?

Hi,

in a project I am using callbacks which are called like
ordinary functions but in fact are Loki::Functor's
encapsulating calls to non-static member functions on instances
of different classes.

The approach using Loki::Functor looks like this:

| class X {
| public:
| void foo(int) { }
| };
|
| X x;
|
| typedef Loki::Functor<void, TYPELIST_1(int)> Functor1;
| Functor1 f1(&x, &X::foo);
|
| f1(42); // x disappears, f can be called like a function

The nice thing about it is you do not need to template the
actual type of the object on which you want to call the
member function.

Using boost::function1 and boost::bind you can come close to
this:

| template <class C, typename R, typename A1> class MyFunction
| : public boost::function1 <R, A1> {
| public:
| MyFunction(C *c, R (C::*f)(A1)) {
| boost::function1 <R, A1> t = boost::bind(f, c, _1);
| swap(t);
| }
| };
|
| template<class C>struct A {
| typedef MyFunction<C, void, int> f2(x, &X::foo) Functor2;
| };
|
| A::Functor2<X> f2(&x, &X::foo);

This does not only look more complicated, it is also less
flexible. While with Loki::Functor I can use my function object
on any member function of any class provided the signature
fits, my Functor2 template will only work with class X.

I would like to avoid this dependency since if using the
current boost-based approach I would have to make a large number
of classes using this functor class templates, too.

On the other hand I would like to reduce the number of
third-party libraries my project depends on, and from the
Loki package I am currently only using the Functor class, so
that I would actually prefer to replace its functionality by
something else.

Any idea?

Cheers,

Martin

--
Ruft man einen Hund, dann kommt er. Ruft man eine Katze, dann nimmt
sie das zur Kenntnis, und kommt gelegentlich darauf zurueck.
-=-=- -=-=-=-=- --=-=-
Martin Dietze -=-=- http://www.the-little-red-haired-girl.org
Mar 30 '06 #1
2 4001
Martin Herbert Dietze wrote:
Hi,

in a project I am using callbacks which are called like
ordinary functions but in fact are Loki::Functor's
encapsulating calls to non-static member functions on instances
of different classes.

The approach using Loki::Functor looks like this:
class X {
public:
void foo(int) { }
};

X x;

typedef Loki::Functor<void, TYPELIST_1(int)> Functor1;
Functor1 f1(&x, &X::foo);

f1(42); // x disappears, f can be called like a function


The nice thing about it is you do not need to template the
actual type of the object on which you want to call the
member function.

Using boost::function1 and boost::bind you can come close to
this:


Why not just:

typdef boost::function1<void,int> Functor1;

Functor1 f1( boost::bind( &x::for, &x ) );

f1(42);

Jeff
Mar 30 '06 #2
Jeff Flinn <NO****@nowhere.com> wrote:
Why not just:

typdef boost::function1<void,int> Functor1;

Functor1 f1( boost::bind( &x::for, &x ) );

f1(42);


This would add boost-specific code to dozens of modules
throughout the project. I would like to encapsulate this in a
class which does that magic for the user. However I have not
got around explicitly having to specify the class X as a
template argument.

Cheers,

Martin

--
There's only one serious question. And that is: Who knows how to make love stay?
Answer me that and I will tell you whether or not to kill yourself. Answer me
that and I will ease your mind about the beginning and the end of time. Answer
me that and I will reveal to you the purpose of the moon. -- Tom Robbins
-=-=- -=-=-=-=- --=-=-
Martin Dietze -=-=- http://www.the-little-red-haired-girl.org
Mar 30 '06 #3

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

Similar topics

7
2676
by: Senthilraja | last post by:
I have the following program using templates. Someone please let me know the syntax to be used for defining the member functions push, pop etc. as non-inline functions. #include <iostream>...
4
1823
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
3
2220
by: Krivenok Dmitry | last post by:
I writing simple class CmdLine: ..... ..... class CmdLine { ..... ..... public: /// Constructor CmdLine(int argc, char** argv);
13
4503
by: Fei Liu | last post by:
Hi Group, I've got a problem I couldn't find a good solution. I am working with scientific data files in netCDF format. One of the properties of netCDF data is that the actual type of data is only...
3
4579
by: Krivenok Dmitry | last post by:
Hello All! This is example of code: ////////////////////////////////////////////////////////////////////////////////////////////////// #ifndef A_H_ #define A_H_ #include <Loki/Singleton.h>...
12
1991
by: Belebele | last post by:
Suppose that a class A depends on class B because an object of class B is passed into A's constructor. See below: class B { ... }; class A { public: A(B const& b); ... };
4
1712
by: ma740988 | last post by:
Trying to build on something I picked up from a Scott Meyers. Consider: class BaseMenuItem { public: virtual void invoke() const = 0; }; template<class Class, class MemFuncPtr, class...
11
11946
by: Guch Wu | last post by:
I want to pass a type name to a function. Then in this function, create a instance of this type according to the type name. Can I do this? void test(typename X) { X x; do sth with x;
16
3858
by: Manuel | last post by:
hi, In the past I made the question "how to implement a simple class forname". I made this finally and it compiled well. but now when i execute the program, it crash with a sigsegv. The code...
3
2296
by: aaragon | last post by:
Hello everyone, I've been trying to work with the visitor design pattern, and it works fine except for the following. Let's suppose that we have a fixed hierarchy of classes (many of them)...
0
7093
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
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
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
4690
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...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.