connection_handler.h

Go to the documentation of this file.
00001 #ifndef _SIGX_CONNECTION_HANDLER_HPP_
00002 #define _SIGX_CONNECTION_HANDLER_HPP_
00003 
00004 /*
00005  * Copyright 2005 Klaus Triendl
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Library General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Library General Public
00018  * License along with this library; if not, write to the Free 
00019  * Software Foundation, 51 Franklin Street, Fifth Floor, 
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #include <tr1/memory>   // std::tr1::shared_ptr
00024 #include <map>
00025 #include <sigc++/signal.h>
00026 #include <glib.h>   // message macros
00027 #include <glibmm/thread.h> // Glib::StaticPrivate
00028 #include <glibmm/main.h>
00029 #include <sigxconfig.h>
00030 #include <sigx/fwddecl.h>
00031 #include <sigx/noninstantiatable.h>
00032 #include <sigx/signal_traits.h>
00033 #include <sigx/signal_source_base.h>
00034 #include <sigx/connection_wrapper.h>
00035 
00036 
00037 namespace sigx
00038 {
00039 
00044 class SIGX_API connection_handler: noninstantiatable
00045 {
00046 public:
00054     static void destroy(const sigc_connection_ptr* handle);
00055 
00060     static void store(
00061         const std::tr1::shared_ptr<sigc_connection_ptr>& _A_refconn, 
00062         const sigc::connection& c);
00063 
00064 
00065 protected:
00074     struct connections_container_wrapper
00075     {
00080         typedef std::map<const sigc_connection_ptr* const /*handle*/, std::tr1::shared_ptr<sigc_connection_ptr> > container_type;
00081 
00082         container_type m_connections;
00083         ~connections_container_wrapper();
00084     };
00085 
00086     static Glib::StaticPrivate<connections_container_wrapper> thread_specific_connections;
00087 };
00088 
00089 
00090 template<typename T_signal, internal::signal_group I_oneof>
00091 class typed_connection_handler;
00092 
00093 template<typename T_signal>
00094 class typed_connection_handler<T_signal, internal::SIGGROUP_SIGC>: noninstantiatable
00095 {
00096 public:
00097     typedef T_signal signal_type;
00098     typedef typename signal_type::slot_type slot_type;
00099 
00104     static void connect(const std::tr1::shared_ptr<sigc_connection_ptr>& _A_refconnptr, const std::tr1::shared_ptr<signal_source_base>& psigsource, const slot_type& _A_slot)
00105     {
00106         // must have a valid signal source
00107         g_return_if_fail(psigsource.get());
00108 
00109         // get the signal from the signal source ...
00110         typedef signal_type (*fp_sig_getter)(signal_source_ptr);
00111         const fp_sig_getter getsig = 
00112             reinterpret_cast<fp_sig_getter>(psigsource->getter());
00113 
00114         // ... and connect the slot
00115         const sigc::connection& c = getsig(psigsource.get()).connect(_A_slot);
00116         // ... store the resulting connection in the container of the thread's connections
00117         connection_handler::store(_A_refconnptr, c);
00118     }
00119 };
00120 
00123 template<typename T_signal>
00124 class typed_connection_handler<T_signal, internal::SIGGROUP_GLIB_PROXY>: noninstantiatable
00125 {
00126 public:
00127     typedef T_signal signal_type;
00128     typedef typename signal_type::SlotType slot_type;
00129     typedef typename signal_type::VoidSlotType void_slot_type;
00130 
00133     static void connect(const std::tr1::shared_ptr<sigc_connection_ptr>& _A_refconnptr, const std::tr1::shared_ptr<signal_source_base>& psigsource, const slot_type& _A_slot, bool after)
00134     {
00135         // must have a valid signal source
00136         g_return_if_fail(psigsource.get());
00137 
00138         // get the signal from the signal source ...
00139         typedef signal_type (*fp_sig_getter)(signal_source_ptr);
00140         const fp_sig_getter getsig = 
00141             reinterpret_cast<fp_sig_getter>(psigsource->getter());
00142 
00143         // ... and connect the slot
00144         const sigc::connection& c = getsig(psigsource.get()).connect(_A_slot, after);
00145         // ... store the resulting connection in the container of the thread's connections
00146         connection_handler::store(_A_refconnptr, c);
00147     }
00148 
00151     static void connect_notify(const std::tr1::shared_ptr<sigc_connection_ptr>& _A_refconnptr, const std::tr1::shared_ptr<signal_source_base>& psigsource, const void_slot_type& _A_slot, bool after)
00152     {
00153         // must have a valid signal source
00154         g_return_if_fail(psigsource.get());
00155 
00156         // get the signal from the signal source ...
00157         typedef signal_type (*fp_sig_getter)(signal_source_ptr);
00158         const fp_sig_getter getsig = 
00159             reinterpret_cast<fp_sig_getter>(psigsource->getter());
00160 
00161         // ... and connect the slot
00162         const sigc::connection& c = getsig(psigsource.get()).connect_notify(_A_slot, after);
00163         // ... store the resulting connection in the container of the thread's connections
00164         connection_handler::store(_A_refconnptr, c);
00165     }
00166 };
00167 
00170 template<>
00171 class SIGX_API typed_connection_handler<Glib::SignalIdle, internal::SIGGROUP_IRRELEVANT>: noninstantiatable
00172 {
00173 public:
00174     typedef Glib::SignalIdle signal_type;
00175     typedef sigc::slot<bool> slot_type;
00176 
00179     static void connect(const std::tr1::shared_ptr<sigc_connection_ptr>& _A_refconnptr, const std::tr1::shared_ptr<signal_source_base>& psigsource, const slot_type& _A_slot, int priority);
00180 };
00181 
00182 
00185 template<>
00186 class SIGX_API typed_connection_handler<Glib::SignalTimeout, internal::SIGGROUP_IRRELEVANT>: noninstantiatable
00187 {
00188 public:
00189     typedef Glib::SignalTimeout signal_type;
00190     typedef sigc::slot<bool> slot_type;
00191 
00194     static void connect(const std::tr1::shared_ptr<sigc_connection_ptr>& _A_refconnptr, const std::tr1::shared_ptr<signal_source_base>& psigsource, const slot_type& _A_slot, unsigned int interval, int priority);
00195 };
00196 
00197 
00200 template<>
00201 class SIGX_API typed_connection_handler<Glib::SignalIO, internal::SIGGROUP_IRRELEVANT>: noninstantiatable
00202 {
00203 public:
00204     typedef Glib::SignalIO signal_type;
00205     typedef sigc::slot<bool, Glib::IOCondition> slot_type;
00206 
00209     static void connect(const std::tr1::shared_ptr<sigc_connection_ptr>& _A_refconnptr, const std::tr1::shared_ptr<signal_source_base>& psigsource, const slot_type& _A_slot, int fd, Glib::IOCondition condition, int priority);
00210 };
00211 
00212 
00215 template<>
00216 class SIGX_API typed_connection_handler<Glib::SignalChildWatch, internal::SIGGROUP_IRRELEVANT>: noninstantiatable
00217 {
00218 public:
00219     typedef Glib::SignalChildWatch signal_type;
00220     typedef sigc::slot<void, GPid, int> slot_type;
00221 
00224     static void connect(const std::tr1::shared_ptr<sigc_connection_ptr>& _A_refconnptr, const std::tr1::shared_ptr<signal_source_base>& psigsource, const slot_type& _A_slot, GPid pid, int priority);
00225 };
00226 
00227 
00228 } // namespace sigx
00229 
00230 
00231 #endif // end file guard

Generated on Sun May 17 15:47:33 2009 for sigx++ by  doxygen 1.5.9