Huggle  build:^490^dce1e5c
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
collectable.cpp
1 //This program is free software: you can redistribute it and/or modify
2 //it under the terms of the GNU General Public License as published by
3 //the Free Software Foundation, either version 3 of the License, or
4 //(at your option) any later version.
5 
6 //This program is distributed in the hope that it will be useful,
7 //but WITHOUT ANY WARRANTY; without even the implied warranty of
8 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 //GNU General Public License for more details.
10 
11 #include "collectable.hpp"
12 
13 using namespace Huggle;
14 
15 unsigned long Collectable::LastCID = 0;
16 QMutex Collectable::WideLock(QMutex::Recursive);
17 
18 Collectable::Collectable()
19 {
20  WideLock.lock();
21  this->CID = Collectable::LastCID;
22  Collectable::LastCID++;
23  WideLock.unlock();
24  this->Locked = false;
25  this->Managed = false;
26  this->QL = new QMutex(QMutex::Recursive);
27 }
28 
29 Collectable::~Collectable()
30 {
31  if (this->IsManaged())
32  {
33  throw new Exception("Request to delete managed entity");
34  }
35  delete this->QL;
36 }
37 
39 {
40  if (this->Consumers.count() == 0 && this->iConsumers.count() == 0)
41  {
42  GC::Lock.lock();
43  if (GC::list.contains(this))
44  {
45  GC::list.removeAll(this);
46  }
47  GC::Lock.unlock();
48  this->Managed = false;
49  delete this;
50  return true;
51  }
52 
53  this->SetManaged();
54  return false;
55 }
56 
57 void Collectable::RegisterConsumer(const int consumer)
58 {
59  this->Lock();
60  if (!this->iConsumers.contains(consumer))
61  {
62  this->iConsumers.append(consumer);
63  }
64  this->SetManaged();
65  this->Unlock();
66 }
67 
68 void Collectable::UnregisterConsumer(const int consumer)
69 {
70  this->Lock();
71  this->iConsumers.removeAll(consumer);
72  this->SetManaged();
73  this->Unlock();
74 }
75 
76 void Collectable::RegisterConsumer(const QString consumer)
77 {
78  this->Lock();
79  this->Consumers.append(consumer);
80  this->Consumers.removeDuplicates();
81  this->SetManaged();
82  this->Unlock();
83 }
84 
85 void Collectable::UnregisterConsumer(const QString consumer)
86 {
87  this->Lock();
88  this->Consumers.removeAll(consumer);
89  this->SetManaged();
90  this->Unlock();
91 }
92 
94 {
95  return this->CID;
96 }
97 
98 QString Collectable::ConsumerIdToString(const int id)
99 {
100  switch (id)
101  {
102  case HUGGLECONSUMER_WIKIEDIT:
103  return "WikiEdit";
104  case HUGGLECONSUMER_PROVIDERIRC:
105  return "ProviderIRC";
106  case HUGGLECONSUMER_QUEUE:
107  return "Queue";
108  case HUGGLECONSUMER_CORE_POSTPROCESS:
109  return "Core::Postprocess";
110  case HUGGLECONSUMER_DELETEFORM:
111  return "Delete Form";
112  case HUGGLECONSUMER_PROCESSLIST:
113  return "ProcessList";
114  case HUGGLECONSUMER_HUGGLETOOL:
115  return "HuggleTool";
116  case HUGGLECONSUMER_EDITQUERY:
117  return "EditQuery";
118  case HUGGLECONSUMER_REVERTQUERY:
119  return "RevertQuery";
120  case HUGGLECONSUMER_MAINFORM:
121  return "Main Form";
122  case HUGGLECONSUMER_LOGINFORM:
123  return "Login Form";
124  }
125  return "Unknown consumer: " + QString::number(id);
126 }
127 
128 void Collectable::SetManaged()
129 {
130  this->Managed = true;
131  if (!GC::list.contains(this))
132  {
133  GC::list.append(this);
134  }
135 }
136 
138 {
139  QString result = "";
140  if (this->iConsumers.count() > 0 || this->Consumers.count() > 0)
141  {
142  result += ("GC: Listing all dependencies for " + QString::number(this->CollectableID())) + "\n";
143  int Item=0;
144  while (Item < this->Consumers.count())
145  {
146  result +=("GC: " + QString::number(this->CollectableID()) + " " + this->Consumers.at(Item)) + "\n";
147  Item++;
148  }
149  Item=0;
150  while (Item < this->iConsumers.count())
151  {
152  result +=("GC: " + QString::number(this->CollectableID()) + " " + ConsumerIdToString( this->iConsumers.at(Item)) ) + "\n";
153  Item++;
154  }
155  } else
156  {
157  result += "No consumers found: " + QString::number(this->CollectableID());
158  }
159  return result;
160 }
161 
163 {
164  return Locked;
165 }
166 
168 {
169  this->QL->lock();
170  Locked = true;
171 }
172 
174 {
175  this->QL->unlock();
176  Locked = false;
177 }
178 
180 {
181  if (this->Managed)
182  {
183  return true;
184  }
185  return ((this->Consumers.count() > 0) || (this->iConsumers.count() > 0));
186 }
bool IsManaged()
IsManaged Managed class is deleted by GC and must not be deleted by hand.
virtual bool SafeDelete()
Use this if you are not sure if you can delete this object in this moment.
Definition: collectable.cpp:38
void UnregisterConsumer(const int consumer)
This function will remove a string which prevent the object from being removed.
Definition: collectable.cpp:68
void RegisterConsumer(const int consumer)
Registers a consumer.
Definition: collectable.cpp:57
QString DebugHgc()
DebugHgc.
void Unlock()
Unlock this object for deletion by other threads.
unsigned long CollectableID()
CollectableID.
Definition: collectable.cpp:93
Every exception raised by huggle is defined by this class.
Definition: exception.hpp:20
static QMutex Lock
QMutex that is used to lock the GC::list object.
Definition: gc.hpp:47
bool Managed
Internal variable that contains a cache whether object is managed.
Definition: collectable.hpp:99
void Lock()
Lock this object so that other threads can&#39;t change consumers or modify its properties.
static QList< Collectable * > list
List of all managed queries that qgc keeps track of.
Definition: gc.hpp:42