Huggle  build:^490^dce1e5c
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
collectable.hpp
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 
12 #ifndef COLLECTABLE_H
13 #define COLLECTABLE_H
14 
15 #include <QMutex>
16 #include <QList>
17 #include <QString>
18 #include <QStringList>
19 #include "gc.hpp"
20 #include "exception.hpp"
21 
22 namespace Huggle
23 {
24  //! Base for all items that are supposed to be collected by garbage collector
25 
26  //! In order for any item to be maintained by garbage collector it must be inherited from
27  //! this class.
28  //! Every object by default when created is unmanaged, which means, that garbage collector doesn't
29  //! care about it. In order to change it to managed object, you have to register at least one consumer.
30  //! Once you do that, the class must not be explicitly deleted using delete, if you do that
31  //! unrecoverable exception will be thrown. The class which is managed (you can verify that by calling Collectable::IsManaged)
32  //! can be only deleted by garbage collector when no consumers are using it. Basically every
33  //! object that has 0 consumers, will be deleted.
35  {
36  public:
37  Collectable();
38  virtual ~Collectable();
39  /*!
40  * \brief IsManaged Managed class is deleted by GC and must not be deleted by hand
41  * \return whether the class is managed
42  */
43  bool IsManaged();
44  //! Use this if you are not sure if you can delete this object in this moment
45  virtual bool SafeDelete();
46  //! Whether the object is locked (other threads can't register nor unregister consumers
47  //! neither it is possible to delete this object by any other thread)
48  bool IsLocked();
49  /*!
50  * \brief Lock this object so that other threads can't change consumers or modify its properties
51  */
52  void Lock();
53  /*!
54  * \brief Unlock this object for deletion by other threads
55  */
56  void Unlock();
57  /*!
58  * \brief Registers a consumer
59  *
60  * This function will store a string which prevent the object from being removed
61  * by GC, by calling this function you change type to managed
62  * \param consumer String that lock the object
63  */
64  void RegisterConsumer(const int consumer);
65  /*!
66  * \brief This function will remove a string which prevent the object from being removed
67  * \param consumer Unique string that unlock the object
68  */
69  void UnregisterConsumer(const int consumer);
70  /*!
71  * \brief Registers a consumer
72  *
73  * This function will store a string which prevent the object from being removed
74  * by QueryGC, by calling this function you change the query type to managed
75  * \param consumer String that lock the object
76  */
77  void RegisterConsumer(const QString consumer);
78  /*!
79  * \brief This function will remove a string which prevent the object from being removed
80  * \param consumer Unique string that unlock the object
81  */
82  void UnregisterConsumer(const QString consumer);
83  /*!
84  * \brief DebugHgc
85  * \return debug info
86  */
87  QString DebugHgc();
88  /*!
89  * \brief CollectableID
90  * \return
91  */
92  unsigned long CollectableID();
93  private:
94  static QString ConsumerIdToString(const int id);
95  static QMutex WideLock;
96  static unsigned long LastCID;
97  unsigned long CID;
98  //! Internal variable that contains a cache whether object is managed
99  bool Managed;
100  void SetManaged();
101  QStringList Consumers;
102  QList<int> iConsumers;
103  QMutex *QL;
104  bool Locked;
105  };
106 }
107 #endif // COLLECTABLE_H
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
Base for all items that are supposed to be collected by garbage collector.
Definition: collectable.hpp:34
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
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.