473,748 Members | 9,416 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<v oid, 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::function 1 and boost::bind you can come close to
this:

| template <class C, typename R, typename A1> class MyFunction
| : public boost::function 1 <R, A1> {
| public:
| MyFunction(C *c, R (C::*f)(A1)) {
| boost::function 1 <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 4024
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<v oid, 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::function 1 and boost::bind you can come close to
this:


Why not just:

typdef boost::function 1<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::function 1<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
2696
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> using namespace std; template<class T, int size = 50> class Stack {
4
1844
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 elaborate to make sure my questions are clear enough. Let's say I need to write a function whose logic is same for all types (T) except in the case of T * (including const T *). Furtheremore , the function needs to be written differently for...
3
2238
by: Krivenok Dmitry | last post by:
I writing simple class CmdLine: ..... ..... class CmdLine { ..... ..... public: /// Constructor CmdLine(int argc, char** argv);
13
4531
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 known at run time. Therefore a lot of template based trick isn't too useful. Considering datafile float x(3) 3.5, 2.5, 8.9 double y(3) 2.7, -2.3, 1.2 int z(3) 5, 2, 3
3
4596
by: Krivenok Dmitry | last post by:
Hello All! This is example of code: ////////////////////////////////////////////////////////////////////////////////////////////////// #ifndef A_H_ #define A_H_ #include <Loki/Singleton.h> #include <Loki/SmartPtr.h> #include <iostream>
12
2028
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
1733
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 Param>
11
11969
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
3897
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 is this: FACTORY_H
3
2309
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) which I cannot modify. I decided to use the visitor design pattern depending on the actual type of the classes because those classes already support the loki visitor. #include <Loki/Visitor.h>
0
8823
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9238
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6793
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2206
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.