[gtkmm] Dispatcher for an arbitrary MainContext: a humble proposal



Dear members of this mailing list!

After searching through the glibmm headers, I am pretty sure, that
it is possible to create two different mainloops in two different
threads. The Dispatcher class would be a natural candidate for the
communication between such mainloops. The only problem is, that
the Dispatcher class attaches to the default MainContext in the
DispatchNotifier class (I do hope, that I am not completely mistaken -
If yes: sorry):

 conn_io_handler_ = Glib::signal_io().connect(
      SigC::slot_class(*this, &DispatchNotifier::pipe_io_handler),
      fd_receiver_, Glib::IO_IN);

Since it is possible to create several MainContexts and several MainLoops,
would it not be a nice addition to be able to attach the Dispatcher
to any MainContext available? Like doing the following thing:

Glib::Dispatcher signal_;
signal_ = new Glib::Dispatcher(context);


I admit, that I am not a C++ mastercoder and I admit my partial knowledge
regarding gtkmm, but I offer the following changes/additions to
dispatcher.h and dispatcher.cc as a basis for discussion.

I have not tested the changes, since the application I am writing has not
progressed this far, but the additions do compile and the dispatcher test
programm still works fine. The additions are simple extentions of the
present code that accept the context as an argument.

Thanks alot!

	Stephan

------------------------------

diff for dispatcher.h (against gtkmm-1.3.23):

25a26
> #include <glibmm/main.h>
68a70
>   Dispatcher(const Glib::RefPtr<MainContext>& context);


------------------------------

diff for dispatcher.cc (against gtkmm-1.3.23):

97a98
>   static DispatchNotifier* reference_instance(const
Glib::RefPtr<MainContext>& context);
106a108
>   DispatchNotifier(const Glib::RefPtr<MainContext>& context);
141a144,156
> DispatchNotifier::DispatchNotifier(const Glib::RefPtr<MainContext>&
context)
> :
>   ref_count_    (0),
>   fd_receiver_  (-1),
>   fd_sender_    (-1)
> {
>   create_pipe();
>
>   const Glib::RefPtr<Glib::IOSource> io_source =
Glib::IOSource::create(fd_receiver_, Glib::IO_IN);
>   io_source->connect(SigC::slot_class(*this,
&DispatchNotifier::pipe_io_handler));
>   io_source->attach(context);
> }
>
186a202,217
> DispatchNotifier* DispatchNotifier::reference_instance(const
Glib::RefPtr<MainContext>& context)
> {
>   DispatchNotifier* instance = thread_specific_instance_.get();
>
>   if(!instance)
>   {
>     instance = new DispatchNotifier(context);
>     thread_specific_instance_.set(instance);
>   }
>
>   ++instance->ref_count_; // initially 0
>
>   return instance;
> }
>
> // static
284a316,321
> Dispatcher::Dispatcher(const Glib::RefPtr<MainContext>& context)
> :
>   signal_   (),
>   notifier_ (DispatchNotifier::reference_instance(context))
> {}
>
306d342
<
~








[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]