File indexing completed on 2024-05-19 04:49:29

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Maximilian Kossick <maximilian.kossick@googlemail.com>            *
0003  * Copyright (c) 2007 Ian Monroe <ian@monroe.nu>                                        *
0004  * Copyright (c) 2008 Mark Kretschmann <kretschmann@kde.org>                            *
0005  * Copyright (c) 2013 Matěj Laitl <matej@laitl.cz>                                      *
0006  *                                                                                      *
0007  * This program is free software; you can redistribute it and/or modify it under        *
0008  * the terms of the GNU General Public License as published by the Free Software        *
0009  * Foundation; either version 2 of the License, or (at your option) any later           *
0010  * version.                                                                             *
0011  *                                                                                      *
0012  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0013  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0014  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0015  *                                                                                      *
0016  * You should have received a copy of the GNU General Public License along with         *
0017  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0018  ***************************************************************************************/
0019 
0020 #include "Base.h"
0021 
0022 #include "core/meta/Observer.h"
0023 
0024 #include <QDebug>
0025 
0026 using namespace Meta;
0027 
0028 Base::Base()
0029     : m_observersLock( QReadWriteLock::Recursive )
0030 {
0031 }
0032 
0033 Base::~Base()
0034 {
0035     // we need to notify all observers that we're deleted to avoid stale pointers
0036     foreach( Observer *observer, m_observers )
0037     {
0038         observer->destroyedNotify( this );
0039     }
0040 }
0041 
0042 void
0043 Base::subscribe( Observer *observer )
0044 {
0045     if( observer )
0046     {
0047         QWriteLocker locker( &m_observersLock );
0048         m_observers.insert( observer );
0049     }
0050 }
0051 
0052 void
0053 Base::unsubscribe( Observer *observer )
0054 {
0055     QWriteLocker locker( &m_observersLock );
0056     m_observers.remove( observer );
0057 }
0058 
0059 QDebug
0060 operator<<( QDebug dbg, const Base &base )
0061 {
0062     dbg.nospace() << "Meta::Base(" << base.name() << " at " << &base << ")";
0063     return dbg.space();
0064 }