Torc  0.1
torcreferencecounted.cpp
Go to the documentation of this file.
1 /* Class TorcReferenceCounted
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2012-18
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 // Qt
24 #include <QObject>
25 
26 // Torc
27 #include "torcreferencecounted.h"
28 
34 
36 {
37  m_eventLoopEnding = Ending;
38 }
39 
41  : m_refCount()
42 {
43  UpRef();
44 }
45 
47 {
48  m_refCount.ref();
49 }
50 
52 {
53  if (!m_refCount.deref())
54  {
55  if (!m_eventLoopEnding)
56  {
57  QObject* object = dynamic_cast<QObject*>(this);
58  if (object)
59  {
60  object->deleteLater();
61  return true;
62  }
63  }
64 
65  delete this;
66  return true;
67  }
68 
69  return false;
70 }
71 
73 {
74  return m_refCount.fetchAndAddOrdered(0) > 1;
75 }
76 
82  : m_refCountedObject(Counter)
83 {
84  if (m_refCountedObject)
85  m_refCountedObject->UpRef();
86 }
87 
89 {
90  if (m_refCountedObject)
91  m_refCountedObject->DownRef();
92  m_refCountedObject = nullptr;
93 }
static void EventLoopEnding(bool Ending)
A reference counting implementation.
TorcReferenceLocker(TorcReferenceCounter *Counter)
virtual bool DownRef(void)