Back to index

unity  6.0.0
GLibSignal.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003 * Copyright (C) 2011-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: Neil Jagdish patel <neil.patel@canonical.com>
00018 *              Marco Trevisan <marco.trevisan@canonical.com>
00019 */
00020 
00021 #include "GLibSignal.h"
00022 
00023 namespace unity
00024 {
00025 namespace glib
00026 {
00027 
00028 SignalBase::SignalBase()
00029   : object_(0),
00030     connection_id_(0)
00031 {}
00032 
00033 SignalBase::~SignalBase()
00034 {
00035   Disconnect();
00036 }
00037 
00038 void SignalBase::Disconnect()
00039 {
00040   if (connection_id_ && G_IS_OBJECT(object_))
00041     g_signal_handler_disconnect(object_, connection_id_);
00042 
00043   object_ = 0;
00044   connection_id_ = 0;
00045 }
00046 
00047 GObject* SignalBase::object() const
00048 {
00049   return object_;
00050 }
00051 
00052 std::string const& SignalBase::name() const
00053 {
00054   return name_;
00055 }
00056 
00057 SignalManager::SignalManager()
00058 {}
00059 
00060 // Ideally this would be SignalBase& but there is a specific requirment to allow
00061 // only one instance of Signal to control a connection. With the templating, it
00062 // was too messy to try and write a copy constructor/operator that would steal
00063 // from "other" and make the new one the owner. Not only did it create
00064 // opportunity for random bugs, it also made the API bad.
00065 void SignalManager::Add(SignalBase* signal)
00066 {
00067   Add(SignalBase::Ptr(signal));
00068 }
00069 
00070 void SignalManager::Add(SignalBase::Ptr const& signal)
00071 {
00072   connections_.push_back(signal);
00073 }
00074 
00075 // This uses void* to keep in line with the g_signal* functions
00076 // (it allows you to pass in a GObject without casting up).
00077 void SignalManager::Disconnect(void* object, std::string const& signal_name)
00078 {
00079   for (auto it = connections_.begin(); it != connections_.end(); ++it)
00080   {
00081     if ((*it)->object() == object &&
00082         (signal_name.empty() || (*it)->name() == signal_name))
00083     {
00084       (*it)->Disconnect();
00085       connections_.erase(it, it);
00086     }
00087   }
00088 }
00089 
00090 }
00091 }
00092