• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • kasten
  • controllers
  • view
  • structures
  • script
safereference.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Okteta Kasten Framework, made within the KDE community.
3  *
4  * Copyright 2013 Alex Richardson <alex.richardson@gmx.de>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) version 3, or any
10  * later version accepted by the membership of KDE e.V. (or its
11  * successor approved by the membership of KDE e.V.), which shall
12  * act as a proxy defined in Section 6 of version 3 of the license.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21  */
22 #ifndef SAFEREFERENCE_H
23 #define SAFEREFERENCE_H
24 
25 #include <QMultiHash>
26 #include <QMetaType>
27 #include <QDebug>
28 
29 class DataInformation;
30 class SafeReference;
31 
32 class SafeReferenceHolder {
33  Q_DISABLE_COPY(SafeReferenceHolder)
34  SafeReferenceHolder();
35  ~SafeReferenceHolder();
36 public:
38  void invalidateAll(DataInformation* data);
39  void safeReferenceDestroyed(SafeReference* ref);
40  void registerSafeReference(SafeReference* ref, DataInformation* data);
41  static SafeReferenceHolder instance;
42 private:
43  typedef QMultiHash<DataInformation*, SafeReference*> Container;
44  int safeRefDestroyCnt;
45  int safeRefRegisterCnt;
46  Container mRefs;
47 };
48 
52 class SafeReference
53 {
54 public:
55  SafeReference();
56  explicit SafeReference(DataInformation* data);
57  SafeReference(const SafeReference& other);
58  ~SafeReference();
59  inline DataInformation* data() const;
60 private:
61  friend class SafeReferenceHolder;
62  inline void invalidate();
63 private:
64  DataInformation* mData;
65 };
66 
67 Q_DECLARE_METATYPE(SafeReference)
68 
69 inline SafeReference::SafeReference() : mData(0)
70 {
71  qDebug() << "default constructed safe reference";
72 }
73 
74 inline SafeReference::SafeReference(DataInformation* data) : mData(data)
75 {
76  SafeReferenceHolder::instance.registerSafeReference(this, mData);
77 }
78 
79 inline SafeReference::SafeReference(const SafeReference& other) : mData(other.mData)
80 {
81  SafeReferenceHolder::instance.registerSafeReference(this, mData);
82 }
83 
84 inline SafeReference::~SafeReference()
85 {
86  SafeReferenceHolder::instance.safeReferenceDestroyed(this);
87 }
88 
89 inline DataInformation* SafeReference::data() const
90 {
91  return mData;
92 }
93 
94 inline void SafeReference::invalidate()
95 {
96  mData = 0;
97 }
98 
99 inline void SafeReferenceHolder::safeReferenceDestroyed(SafeReference* ref)
100 {
101  if (!ref->data())
102  return; //has been invalidated -> was already removed
103  int removed = mRefs.remove(ref->data(), ref);
104  //qDebug() << "safe ref destroyed " << ref;
105  if (removed <= 0)
106  qDebug() << "safe refenrece could not be removed:" << ref;
107  else
108  safeRefDestroyCnt += removed;
109 }
110 
111 inline void SafeReferenceHolder::registerSafeReference(SafeReference* ref, DataInformation* data)
112 {
113  if (Q_LIKELY(data))
114  {
115  mRefs.insert(data, ref);
116  //qDebug() << "registered safe ref" << ref << data;
117  safeRefRegisterCnt++;
118  }
119  else {
120  qWarning() << "invalid ref copied";
121  }
122 }
123 
124 #endif // SAFEREFERENCE_H
SafeReferenceHolder::safeReferenceDestroyed
void safeReferenceDestroyed(SafeReference *ref)
Definition: safereference.h:99
DataInformation
Interface that must be implemented by all datatypes.
Definition: datainformation.h:67
SafeReference::data
DataInformation * data() const
Definition: safereference.h:89
SafeReference
A class wrapping a DataInformation* As soon as that DataInformation is destroyed all references to it...
Definition: safereference.h:52
SafeReferenceHolder::registerSafeReference
void registerSafeReference(SafeReference *ref, DataInformation *data)
Definition: safereference.h:111
SafeReference::~SafeReference
~SafeReference()
Definition: safereference.h:84
SafeReference::SafeReference
SafeReference()
Definition: safereference.h:69
SafeReferenceHolder
Definition: safereference.h:32
SafeReferenceHolder::instance
static SafeReferenceHolder instance
Definition: safereference.h:41
SafeReferenceHolder::invalidateAll
void invalidateAll(DataInformation *data)
sets all refereces to this object to null
Definition: safereference.cpp:30
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:09 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal