By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,495 Members | 1,220 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,495 IT Pros & Developers. It's quick & easy.

How to do "events"

P: n/a
Hi group,

I would like to simulate something that can be quite similar to
"Events".
I would like that a member function of a class can be "setted" as an
Event and that this method can be called after something special.
Consider the following example :
I'm setting a function with a pointor function, then in a while-true
block, I ask for an input, if input is "abc", i'm firing an event
(that prints "abc").

#include <iostream>
#include <string>

using namespace std;

typedef void (*fPtrEvent)();

class Event
{
private:
static fPtrEvent fEvent;
public:
Event() {};
static void Set(fPtrEvent f)
{
fEvent = f;
}
void WhileTrue()
{
while(1)
{
string s;
cin >s;
if (s == "abc" && fEvent != 0)
fEvent();
if (s == "q")
break;
}
}
};

fPtrEvent Event::fEvent = 0;

class Test
{
public:
static void DoPrint()
{
cout << "print abc" << endl;
}
};

int main (void)
{
Event *c = new Event();
c->Set(&Test::DoPrint);
c->WhileTrue();
delete c;
return 0;
}

What I would like from you is :
- to tell me if it's a good way to achieve what I want.
- if there are better way to do that.
- tell me how I can avoid to use static methods (because that means
can only access to static attributes)
- everything you want :)

Thanks for your help,

S.

Apr 12 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 12 Apr, 15:12, timor.su...@gmail.com wrote:
Hi group,

I would like to simulate something that can be quite similar to
"Events".
I would like that a member function of a class can be "setted" as an
Event and that this method can be called after something special.
Consider the following example :
I'm setting a function with a pointor function, then in a while-true
block, I ask for an input, if input is "abc", i'm firing an event
(that prints "abc").

#include <iostream>
#include <string>

using namespace std;

typedef void (*fPtrEvent)();

class Event
{
private:
static fPtrEvent fEvent;
public:
Event() {};
static void Set(fPtrEvent f)
{
fEvent = f;
}
void WhileTrue()
{
while(1)
{
string s;
cin >s;
if (s == "abc" && fEvent != 0)
fEvent();
if (s == "q")
break;
}
}

};

fPtrEvent Event::fEvent = 0;

class Test
{
public:
static void DoPrint()
{
cout << "print abc" << endl;
}

};

int main (void)
{
Event *c = new Event();
c->Set(&Test::DoPrint);
c->WhileTrue();
delete c;
return 0;

}

What I would like from you is :
- to tell me if it's a good way to achieve what I want.
- if there are better way to do that.
- tell me how I can avoid to use static methods (because that means
can only access to static attributes)
- everything you want :)
Google for delegates, I think boos::funtion can be of use.

--
Erik Wikström

Apr 12 '07 #2

P: n/a
On Apr 12, 9:55 am, "Erik Wikström" <eri...@student.chalmers.se>
wrote:
On 12 Apr, 15:12, timor.su...@gmail.com wrote:
Hi group,
I would like to simulate something that can be quite similar to
"Events".
I would like that a member function of a class can be "setted" as an
Event and that this method can be called after something special.
Consider the following example :
I'm setting a function with a pointor function, then in a while-true
block, I ask for an input, if input is "abc", i'm firing an event
(that prints "abc").
#include <iostream>
#include <string>
using namespace std;
typedef void (*fPtrEvent)();
class Event
{
private:
static fPtrEvent fEvent;
public:
Event() {};
static void Set(fPtrEvent f)
{
fEvent = f;
}
void WhileTrue()
{
while(1)
{
string s;
cin >s;
if (s == "abc" && fEvent != 0)
fEvent();
if (s == "q")
break;
}
}
};
fPtrEvent Event::fEvent = 0;
class Test
{
public:
static void DoPrint()
{
cout << "print abc" << endl;
}
};
int main (void)
{
Event *c = new Event();
c->Set(&Test::DoPrint);
c->WhileTrue();
delete c;
return 0;
}
What I would like from you is :
- to tell me if it's a good way to achieve what I want.
- if there are better way to do that.
- tell me how I can avoid to use static methods (because that means
can only access to static attributes)
- everything you want :)

Google for delegates, I think boos::funtion can be of use.
Or better, std::tr1::bind (aka boost::bind). Then you needn't use
static functions.

Cheers! --M

Apr 12 '07 #3

P: n/a
On 12 Apr, 14:12, timor.su...@gmail.com wrote:
Hi group,

I would like to simulate something that can be quite similar to
"Events".
I would like that a member function of a class can be "setted" as an
Event and that this method can be called after something special.
Consider the following example :
I'm setting a function with a pointor function, then in a while-true
block, I ask for an input, if input is "abc", i'm firing an event
(that prints "abc").

#include <iostream>
#include <string>
..>
using namespace std;

typedef void (*fPtrEvent)();

class Event
{
private:
static fPtrEvent fEvent;
public:
Event() {};
static void Set(fPtrEvent f)
{
fEvent = f;
}
void WhileTrue()
{
while(1)
{
string s;
cin >s;
if (s == "abc" && fEvent != 0)
fEvent();
if (s == "q")
break;
}
}

};

fPtrEvent Event::fEvent = 0;

class Test
{
public:
static void DoPrint()
{
cout << "print abc" << endl;
}

};

int main (void)
{
Event *c = new Event();
c->Set(&Test::DoPrint);
c->WhileTrue();
delete c;
return 0;

}
for more general solution check boost::signals library
it is a type safe callback library
you can also check battle tested libsigc++
which does the same think in almost the
same way as boost::signals
#include <iostream>
#include <string>

#include <boost/signals.hpp>
#include <boost/bind.hpp>

(you spend most of your time designing
your program, typing five extra characters
should not be such a chore. Injecting whole
of std into your namespace is never a good
idea)
//using namespace std;

class Event
{
::boost::signal<void()sig_;
public:

template<class Fun>
::boost::signals::connection connect(Fun fun)
{
return sig_.connect(fun);
}

void WhileTrue()
{
std::string s;
do
{
std::cin >s;
if (s == "abc")
// call all connected
sig_();

}while(s != "q");
}
};

class Test
{
public:

void DoPrint()
{
std::cout << "Test\n";
}
};

void Test1()
{
std::cout << "Test1\n";
}

int main()
{
Event c;
Test t;
// connect non static member function
c.connect(boost::bind(&Test::DoPrint, &t));
// connect non member function
c.connect(&Test1);
c.WhileTrue();
return 0;
}

Apr 13 '07 #4

P: n/a
Hi,

thanks for your answer,
that looks like very good for me ...
only one thing is missing ... boost :)

I will configure it now

Best regards,

S.
On 13 avr, 13:24, "dasjotre" <dasjo...@googlemail.comwrote:
On 12 Apr, 14:12, timor.su...@gmail.com wrote:


Hi group,
I would like to simulate something that can be quite similar to
"Events".
I would like that a member function of a class can be "setted" as an
Eventand that this method can be called after something special.
Consider the following example :
I'm setting a function with a pointor function, then in a while-true
block, I ask for an input, if input is "abc", i'm firing anevent
(that prints "abc").
#include <iostream>
#include <string>
.>
using namespace std;
typedef void (*fPtrEvent)();
classEvent
{
private:
static fPtrEvent fEvent;
public:
Event() {};
static void Set(fPtrEvent f)
{
fEvent = f;
}
void WhileTrue()
{
while(1)
{
string s;
cin >s;
if (s == "abc" && fEvent != 0)
fEvent();
if (s == "q")
break;
}
}
};
fPtrEventEvent::fEvent = 0;
class Test
{
public:
static void DoPrint()
{
cout << "print abc" << endl;
}
};
int main (void)
{
Event*c = newEvent();
c->Set(&Test::DoPrint);
c->WhileTrue();
delete c;
return 0;
}

for more general solution check boost::signals library
it is a type safe callback library
you can also check battle tested libsigc++
which does the same think in almost the
same way as boost::signals
#include <iostream>
#include <string>

#include <boost/signals.hpp>
#include <boost/bind.hpp>

(you spend most of your time designing
your program, typing five extra characters
should not be such a chore. Injecting whole
of std into your namespace is never a good
idea)
//using namespace std;

classEvent
{
::boost::signal<void()sig_;
public:

template<class Fun>
::boost::signals::connection connect(Fun fun)
{
return sig_.connect(fun);
}

void WhileTrue()
{
std::string s;
do
{
std::cin >s;
if (s == "abc")
// call all connected
sig_();

}while(s != "q");
}

};

class Test
{
public:

void DoPrint()
{
std::cout << "Test\n";
}

};

void Test1()
{
std::cout << "Test1\n";

}

int main()
{
Eventc;
Test t;
// connect non static member function
c.connect(boost::bind(&Test::DoPrint, &t));
// connect non member function
c.connect(&Test1);
c.WhileTrue();
return 0;

}- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -

Apr 13 '07 #5

P: n/a
On Apr 13, 7:24 am, "dasjotre" <dasjo...@googlemail.comwrote:
>
for more general solution check boost::signals library
it is a type safe callback library
you can also check battle tested libsigc++
Right, but note that unlike boost::function and boost::bind,
boost::signals is not part of std::tr1 (some employers eschew Boost or
at least non-TR1 Boost). Which of these solutions is best will depend
on the OP's needs, I suppose.
(you spend most of your time designing
your program, typing five extra characters
should not be such a chore. Injecting whole
of std into your namespace is never a good
idea)
//using namespace std;
_Never_? I disagree just because of the absoluteness and universality
of your advice. Moreover, gurus Sutter and Alexandrescu say in _C++
Coding Standards_, item 59 (italics in original): "You can and should
use namespace using declarations and directives liberally _in your
implementation files after #include directives_ and feel good about
it. Despite repeated assertions to the contrary, namespace using
declarations and directives are not evil and they do not defeat the
purposes of namespaces. Rather, they are what make namespaces usable."

Cheers! --M

Apr 13 '07 #6

P: n/a
On 13 Apr, 14:26, "mlimber" <mlim...@gmail.comwrote:
_Never_? I disagree just because of the absoluteness and universality
of your advice. Moreover, gurus Sutter and Alexandrescu say in _C++
Coding Standards_, item 59 (italics in original): "You can and should
use namespace using declarations and directives liberally _in your
implementation files after #include directives_ and feel good about
it. Despite repeated assertions to the contrary, namespace using
declarations and directives are not evil and they do not defeat the
purposes of namespaces. Rather, they are what make namespaces usable."
I accept that 'never' is too strong a word. But in
the case of std:: I don't think that any program,
other than most simple ones, should, or in fact
need to, use 'using' directive with std, in a
top level namespace. I consider it just lazy and
potentially problematic. I believe FAQ agrees.

I find using declarations (used sparingly) and
namespace aliases very useful in some cases.

Apr 16 '07 #7

P: n/a
On Apr 16, 5:51 am, "dasjotre" <dasjo...@googlemail.comwrote:
On 13 Apr, 14:26, "mlimber" <mlim...@gmail.comwrote:
_Never_? I disagree just because of the absoluteness and universality
of your advice. Moreover, gurus Sutter and Alexandrescu say in _C++
Coding Standards_, item 59 (italics in original): "You can and should
use namespace using declarations and directives liberally _in your
implementation files after #include directives_ and feel good about
it. Despite repeated assertions to the contrary, namespace using
declarations and directives are not evil and they do not defeat the
purposes of namespaces. Rather, they are what make namespaces usable."

I accept that 'never' is too strong a word. But in
the case of std:: I don't think that any program,
other than most simple ones, should, or in fact
need to, use 'using' directive with std, in a
top level namespace. I consider it just lazy and
potentially problematic. I believe FAQ agrees.

I find using declarations (used sparingly) and
namespace aliases very useful in some cases.
To each his/her own. Just know that there are diverse opinions on the
matter from equally respected authorities, so more latitude should be
shown here than, say, with using gotos liberally.

Cheers! --M

Apr 16 '07 #8

P: n/a
On 16 Apr, 16:37, "mlimber" <mlim...@gmail.comwrote:
>
To each his/her own. Just know that there are diverse opinions on the
matter from equally respected authorities, so more latitude should be
shown here than, say, with using gotos liberally.

Advice taken and accepted, but still don't feel corrected :)

I will however buy and read the book. I had a lot of
fun reading 'modern c++ design' and I'm sorry
that Mr. Sutter stopped contributing to his gow
page.

Des.

Apr 17 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.