Overview
Features
Download
Documentation
Community
Add-Ons & Services

Poco::Event examples & features

Please post support and help requests here.

Poco::Event examples & features

Postby pjwhite » 01 Feb 2013, 23:44

Hi,

I am looking at using the Poco::Event modules in my application as a signal/slot mechanism. In the documentation there is mention of using it with the following features:


1) - auto disconnect on delete of class slot is in ( extend the slot class from Poco::Expire? )
2) - run the handler in a separate thread ( haven't seen which class would do this? )

Also, a new feature:

3) I would LOVE to be able to pass more than one arg, so was thinking of adding more template implementations ( ie. one for 2 and 3 args, anything higher and I would think a class/struct would be acceptable ).

I would be keen on developing the multiple-arg feature if it's possible in the current architecture, but are there any reasonable examples that have more complicated use cases ( as #1 & #2 above ). The test-suites are very basic. If not, and someone explains how to do the above things, I would be happy to develop a bigger sample that includes 1 & 2, ( and 3 when i'd have it finished ).

Thanks!
pjwhite
 
Posts: 11
Joined: 30 Aug 2012, 04:45

Re: Poco::Event examples & features

Postby alex » 02 Feb 2013, 15:54

If I understand your requirements properly, except for multiple arguments, delegates should do what you want.
pjwhite wrote:1) - auto disconnect on delete of class slot is in ( extend the slot class from Poco::Expire? )

See Ping example:
Code: Select all
void initialize(Application& self) {
   _icmpClient.pingBegin += Delegate<Ping, ICMPEventArgs>(this, &Ping::onBegin);
   // ...
}

void uninitialize() {
   _icmpClient.pingBegin -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onBegin);
      // ...
}

While looking through samples, I noticed there's a simpler syntax to add/remove delegates, see EchoServer example:
Code: Select all
EchoServiceHandler(StreamSocket& socket, SocketReactor& reactor):
   // ...
{
   // ...
   _fifoOut.readable += delegate(this, &EchoServiceHandler::onFIFOOutReadable);
   _fifoIn.writable += delegate(this, &EchoServiceHandler::onFIFOInWritable);
}
   
~EchoServiceHandler()
{
   //...
   _fifoOut.readable -= delegate(this, &EchoServiceHandler::onFIFOOutReadable);
   _fifoIn.writable -= delegate(this, &EchoServiceHandler::onFIFOInWritable);
}

pjwhite wrote:2) - run the handler in a separate thread ( haven't seen which class would do this? )

See AbstractEvent::notifyAsync()

pjwhite wrote:3) I would LOVE to be able to pass more than one arg, so was thinking of adding more template implementations ( ie. one for 2 and 3 args, anything higher and I would think a class/struct would be acceptable ).

I'm not sure explicitly adding more arguments would be the best approach. I did not think about it at all but C++11 variadic templates come in mind first.
alex
 
Posts: 1101
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Poco::Event examples & features

Postby pjwhite » 05 Feb 2013, 20:59

alex wrote:If I understand your requirements properly, except for multiple arguments, delegates should do what you want.
pjwhite wrote:1) - auto disconnect on delete of class slot is in ( extend the slot class from Poco::Expire? )

See Ping example:
Code: Select all
void initialize(Application& self) {
   _icmpClient.pingBegin += Delegate<Ping, ICMPEventArgs>(this, &Ping::onBegin);
   // ...
}

void uninitialize() {
   _icmpClient.pingBegin -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onBegin);
      // ...
}

While looking through samples, I noticed there's a simpler syntax to add/remove delegates, see EchoServer example:
Code: Select all
EchoServiceHandler(StreamSocket& socket, SocketReactor& reactor):
   // ...
{
   // ...
   _fifoOut.readable += delegate(this, &EchoServiceHandler::onFIFOOutReadable);
   _fifoIn.writable += delegate(this, &EchoServiceHandler::onFIFOInWritable);
}
   
~EchoServiceHandler()
{
   //...
   _fifoOut.readable -= delegate(this, &EchoServiceHandler::onFIFOOutReadable);
   _fifoIn.writable -= delegate(this, &EchoServiceHandler::onFIFOInWritable);
}


This isn't what I meant by auto-disconnect. I figure that the Ping class could implement a Poco::EventHandler interface, which would actually disconnect (-=) for you in it's destructor. This would essentially remove all delegates with the specific pointer.
alex wrote:
pjwhite wrote:2) - run the handler in a separate thread ( haven't seen which class would do this? )

See AbstractEvent::notifyAsync()

Thanks, This is what I am looking for, but it's hard to tell what threads run. Is it a newly spun up thread for each delegate, a thread pool, or a single thread for each event?
alex wrote:
pjwhite wrote:3) I would LOVE to be able to pass more than one arg, so was thinking of adding more template implementations ( ie. one for 2 and 3 args, anything higher and I would think a class/struct would be acceptable ).

I'm not sure explicitly adding more arguments would be the best approach. I did not think about it at all but C++11 variadic templates come in mind first.

I mean something like boost::signal2 where the BasicEvent could be defined like BasicEvent<float, int, std::string> s; This would be very nice for many events.


I guess what I am looking for is closer to boost::signals2 but with option asynchronous signal passing... Hmmm
pjwhite
 
Posts: 11
Joined: 30 Aug 2012, 04:45

Re: Poco::Event examples & features

Postby alex » 07 Feb 2013, 04:19

pjwhite wrote:
alex wrote:
pjwhite wrote:2) - run the handler in a separate thread ( haven't seen which class would do this? )

See AbstractEvent::notifyAsync()

Thanks, This is what I am looking for, but it's hard to tell what threads run. Is it a newly spun up thread for each delegate, a thread pool, or a single thread for each event?

It is ActiveMethod, i.e. a thread from default thread pool.

pjwhite wrote:I mean something like boost::signal2 where the BasicEvent could be defined like BasicEvent<float, int, std::string> s; This would be very nice for many events.

Variadic templates should do. The only problem I see is breaking backward compatibility because template arguments should be reordered:
Code: Select all
template <class TArgs, class TMutex = FastMutex>
class BasicEvent
alex
 
Posts: 1101
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Poco::Event examples & features

Postby pjwhite » 08 Feb 2013, 19:54

OK, Variadic Templates sounds promising. I haven't used variadic-templates before ( nor have I done anything this complicated with templates ). Do you have any hints on where to start?

If I wanted to do this would I have to change every TArgs with TArgs... in AbstractEvent? Would this work for the NotifyAsyncParams value of TArgs too?
pjwhite
 
Posts: 11
Joined: 30 Aug 2012, 04:45

Re: Poco::Event examples & features

Postby alex » 12 Feb 2013, 06:31

Yes, variadic args would obviously propagate ... I dont see us breaking code in such drastic way so it may be better to create a whole new hierarchy and experiment there. To start, here's an hour of mind-bending by Alexandrescu.
alex
 
Posts: 1101
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Poco::Event examples & features

Postby alex » 19 Feb 2013, 17:44

FWIW, there's a void event contribution from Arturo Castro I merged recently; It's loosely related to this but perhaps you can learn something from it.
alex
 
Posts: 1101
Joined: 11 Jul 2006, 16:27
Location: United_States


Return to Support

Who is online

Users browsing this forum: No registered users and 5 guests