QtGStreamer  0.10.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
objectstore.cpp
00001 /*
00002     Copyright (C) 2010  Collabora Multimedia.
00003       @author Mauricio Piacentini <mauricio.piacentini@collabora.co.uk>
00004 
00005     This library is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU Lesser General Public License as published
00007     by the Free Software Foundation; either version 2.1 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 */
00018 
00019 #include <QtCore/QMutex>
00020 #include <QtCore/QHash>
00021 #include <QtCore/QAtomicInt>
00022 
00023 #include "objectstore_p.h"
00024 
00025 namespace {
00026     class GlobalStore
00027     {
00028     public:
00029         QMutex mutex;
00030         QHash<const void *, QAtomicInt> refCount;
00031     };
00032 }
00033 
00034 Q_GLOBAL_STATIC(GlobalStore, globalStore)
00035 
00036 namespace QGst {
00037 namespace Private {
00038 
00039 bool ObjectStore::put(const void * ptr)
00040 {
00041     bool mustAddStrongRef = false;
00042     GlobalStore *const gs = globalStore();
00043     if (!gs) return mustAddStrongRef;
00044 
00045     QMutexLocker lock(&gs->mutex);
00046     if (!gs->refCount.contains(ptr)) {
00047         gs->refCount.insert(ptr, QAtomicInt(0));
00048         mustAddStrongRef = true;
00049     }
00050     (gs->refCount[ptr]).ref();
00051 
00052     return mustAddStrongRef;
00053 }
00054 
00055 bool ObjectStore::take(const void * ptr)
00056 {
00057     bool mustSubtractStrongRef = false;
00058     GlobalStore *const gs = globalStore();
00059     if (!gs) return mustSubtractStrongRef;
00060 
00061     QMutexLocker lock(&gs->mutex);
00062 
00063     //Make sure there are no extra unrefs()
00064     Q_ASSERT(gs->refCount.contains(ptr));
00065 
00066     if (!gs->refCount.contains(ptr)) {
00067         return false;
00068     }
00069 
00070     //Decrease our bindings (weak) reference count
00071     (gs->refCount[ptr]).deref();
00072 
00073     if (!gs->refCount[ptr]) {
00074         //refCount is 0
00075         gs->refCount.remove(ptr);
00076         mustSubtractStrongRef = true;
00077     }
00078     return mustSubtractStrongRef;
00079 }
00080 
00081 bool ObjectStore::isEmpty()
00082 {
00083     GlobalStore *const gs = globalStore();
00084     if (!gs) return true;
00085 
00086     QMutexLocker lock(&gs->mutex);
00087 
00088     if (gs->refCount.count()>0) {
00089         return false;
00090     }
00091 
00092     return true;
00093 }
00094 
00095 }
00096 } //namespace QGst