Back to index

unity  6.0.0
GLibSource.h
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003 * Copyright (C) 2012 Canonical Ltd
00004 *
00005 * This program is free software: you can redistribute it and/or modify
00006 * it under the terms of the GNU General Public License version 3 as
00007 * published by the Free Software Foundation.
00008 *
00009 * This program is distributed in the hope that it will be useful,
00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 * GNU General Public License for more details.
00013 *
00014 * You should have received a copy of the GNU General Public License
00015 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 *
00017 * Authored by: Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
00018 */
00019 
00020 #ifndef UNITY_GLIB_SOURCE_H
00021 #define UNITY_GLIB_SOURCE_H
00022 
00023 #include <boost/utility.hpp>
00024 #include <sigc++/sigc++.h>
00025 #include <glib.h>
00026 #include <memory>
00027 #include <map>
00028 
00029 namespace unity
00030 {
00031 namespace glib
00032 {
00033 
00053 class Source : public boost::noncopyable
00054 {
00055 public:
00056   typedef std::shared_ptr<Source> Ptr;
00057   typedef std::unique_ptr<Source> UniquePtr;
00058   typedef sigc::slot<bool> SourceCallback;
00059 
00064   enum Priority
00065   {
00066     HIGH = G_PRIORITY_HIGH,                 // -100
00067     DEFAULT = G_PRIORITY_DEFAULT,           // 0
00068     HIGH_IDLE = G_PRIORITY_HIGH_IDLE,       // 100
00069     DEFAULT_IDLE = G_PRIORITY_DEFAULT_IDLE, // 200
00070     LOW = G_PRIORITY_LOW                    // 300
00071   };
00072 
00073   virtual ~Source();
00074   unsigned int Id() const;
00075 
00080   bool Run(SourceCallback callback);
00081   bool IsRunning() const;
00082 
00087   void Remove();
00088 
00089   void SetPriority(Priority prio);
00090   Priority GetPriority() const;
00091 
00097   sigc::signal<void, unsigned int> removed;
00098 
00099 protected:
00100   Source();
00101 
00102   GSource* source_;
00103 
00104 private:
00105   struct CallBackData
00106   {
00107     CallBackData(Source* src)
00108       : self(src)
00109     {}
00110 
00111     Source* self;
00112   };
00113 
00114   static gboolean Callback(gpointer data);
00115   static void DestroyCallback(gpointer data);
00116 
00117   unsigned int source_id_;
00118   CallBackData* callback_data_;
00119   SourceCallback callback_;
00120 };
00121 
00122 
00132 class Timeout : public Source
00133 {
00134 public:
00135   Timeout(unsigned int milliseconds, Priority prio = Priority::DEFAULT);
00136   Timeout(unsigned int milliseconds, SourceCallback cb, Priority prio = Priority::DEFAULT);
00137 
00138 private:
00139   void Init(unsigned int milliseconds, Priority prio);
00140 };
00141 
00142 
00152 class TimeoutSeconds : public Source
00153 {
00154 public:
00155   TimeoutSeconds(unsigned int seconds, Priority prio = Priority::DEFAULT);
00156   TimeoutSeconds(unsigned int seconds, SourceCallback cb, Priority prio = Priority::DEFAULT);
00157 
00158 private:
00159   void Init(unsigned int seconds, Priority prio);
00160 };
00161 
00162 
00172 class Idle : public Source
00173 {
00174 public:
00175   Idle(Priority prio = Priority::DEFAULT_IDLE);
00176   Idle(SourceCallback cb, Priority prio = Priority::DEFAULT_IDLE);
00177 
00178 private:
00179   void Init(Priority prio);
00180 };
00181 
00182 
00191 class SourceManager : public boost::noncopyable
00192 {
00193 public:
00194   SourceManager();
00195   ~SourceManager();
00196 
00205   bool Add(Source* source, std::string const& nick = "");
00206   bool Add(Source::Ptr const& source, std::string const& nick = "");
00207 
00208   bool Remove(std::string const& nick);
00209   bool Remove(unsigned int id);
00210 
00211   Source::Ptr GetSource(std::string const& nick) const;
00212   Source::Ptr GetSource(unsigned int id) const;
00213 
00214 protected: // For testing purposes
00215   typedef std::map<std::string, Source::Ptr> SourcesMap;
00216   SourcesMap sources_;
00217 
00218 private:
00219   void OnSourceRemoved(unsigned int id);
00220   void RemoveItem(SourcesMap::iterator it);
00221 };
00222 
00223 } // glib namespace
00224 } // unity namespace
00225 
00226 
00227 /* This code is needed to make the lambda functions with a return value to work
00228  * with the sigc::slot. We need that here to use lambdas as SourceCallback.
00229  * This can safely removed once libsigc++ will include it.
00230  *
00231  * Thanks to Chow Loong Jin <hyperair@gmail.com> for this code, see:
00232  * http://mail.gnome.org/archives/libsigc-list/2012-January/msg00000.html */
00233 
00234 #if __cplusplus >= 201100L || defined (__GXX_EXPERIMENTAL_CXX0X__)
00235 #include <type_traits>
00236 
00237 namespace sigc
00238 {
00239   template <typename Functor>
00240   struct functor_trait<Functor, false>
00241   {
00242     typedef decltype (::sigc::mem_fun(std::declval<Functor&>(), &Functor::operator())) _intermediate;
00243     typedef typename _intermediate::result_type result_type;
00244     typedef Functor functor_type;
00245   };
00246 }
00247 #endif
00248 
00249 #endif