-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmymanager.h
193 lines (154 loc) · 5.39 KB
/
mymanager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifndef MYMANAGER_H
#define MYMANAGER_H
#include "constants.h"
#include "nntpproxy.h"
#include <QList>
#include <QMutex>
#include <QMutexLocker>
#include <QTextStream>
/*!
* \brief "Thread Safe" wrapper of a QList of pointers
*/
template<typename Data> class MyManager
{
public:
explicit MyManager(const char * aDataType); //!< \param name of the data type
MyManager(const MyManager &) = delete;
MyManager(const MyManager &&) = delete;
MyManager & operator=(const MyManager &) = delete;
MyManager & operator=(const MyManager &&) = delete;
virtual ~MyManager(); //!< will delete all the elements of iList
/*!
* \brief erase data from the list (blocking or non blocking, Thread_Safe by default)
* \param aData : data to remove
* \param useMutex : shall we protect (lock) the list
* \return
*/
bool erase(Data *aData, bool useMutex = true);
inline ushort size() const; //!< return the size of the list Thread_Safe
inline ushort size_noLock() const; //!< return the size of the list (non blocking)
void dump() const; //!< Dump manager in the log file
void dump(QTextStream &aStream); //!< Dump manager on text stream
protected:
inline QString getSizeStr_noLock() const; //!< return a string of the size (log purposes)
/*!
* \brief find data in the list (blocking or non blocking, Thread_Safe by default)
* \param aId : data id (Data type should have a getId() method)
* \param useMutex : shall we protect (lock) the list
* \return
*/
Data * find(ushort aId, bool useMutex = true) const;
/*!
* \brief find data in the list (blocking or non blocking, Thread_Safe by default)
* \param aData : data to find (dereference pointer and use operator== on objects)
* \param useMutex : shall we protect (lock) the list
* \return
*/
Data * find(Data *aData, bool useMutex = true) const;
inline void _log(const char * aMessage) const; //!< log function for char *
inline void _log(const QString & aMessage) const; //!< log function for QString
protected:
QList<Data *> iList; //!< list of pointers
mutable QMutex *mMutex; //!< Mutex to be thread safe
const QString iDataName; //!< string name of the data type
const QString iLogPrefix; //!< log prefix iDataName<Manager>
};
//////////////////////
/// inlines functions
template<typename Data> ushort MyManager<Data>::size() const{
QMutexLocker lock(mMutex);
return iList.size();
}
template<typename Data> ushort MyManager<Data>::size_noLock() const{
return iList.size();
}
template<typename Data> void MyManager<Data>::_log(const QString & aMessage) const{
NntpProxy::log(iLogPrefix, aMessage);
}
template<typename Data> void MyManager<Data>::_log(const char * aMessage) const{
NntpProxy::log(iLogPrefix, aMessage);
}
template<typename Data> QString MyManager<Data>::getSizeStr_noLock() const{
return QString("Current list size: ").append(QString::number(iList.size()));
}
////////////////////
// Normal functions
template<typename Data> MyManager<Data>::MyManager(const char * aDataType):
iList(), mMutex(new QMutex()), iDataName(aDataType),
iLogPrefix(QString("[").append(iDataName).append("Manager] "))
{
#ifdef LOG_CONSTRUCTORS
_log("Constructor");
#endif
}
template<typename Data> MyManager<Data>::~MyManager(){
mMutex->lock();
#ifdef LOG_CONSTRUCTORS
QString str("Destructor, ");
str += getSizeStr_noLock();
_log(str);
#endif
for (int i=0; i<iList.size(); ++i)
delete iList[i];
iList.clear();
mMutex->unlock();
delete mMutex;
}
template<typename Data> bool MyManager<Data>::erase(Data *aData, bool useMutex){
if (useMutex)
mMutex->lock();
bool out = iList.removeOne(aData);
QTextStream &is = NntpProxy::acquireLog(iLogPrefix);
is << "erase " << iDataName << " with id: " << aData->getId()
<< ", result: " << out
<< ", " << getSizeStr_noLock();
NntpProxy::releaseLog();
if (useMutex)
mMutex->unlock();
return out;
}
template<typename Data> Data * MyManager<Data>::find(ushort aId, bool useMutex) const{
Data *out = Q_NULLPTR;
if (useMutex)
mMutex->lock();
for (int i=0; i<iList.size(); ++i){
if (iList[i]->getId() == aId){
out = iList[i];
break;
}
}
if (useMutex)
mMutex->unlock();
return out;
}
template<typename Data> Data * MyManager<Data>::find(Data *aData, bool useMutex) const{
Data *out = Q_NULLPTR;
if (useMutex)
mMutex->lock();
for (int i=0; i<iList.size(); ++i){
if (*aData == *(iList[i])){
out = iList[i];
break;
}
}
if (useMutex)
mMutex->unlock();
return out;
}
template<typename Data> void MyManager<Data>::dump() const{
QMutexLocker lock(mMutex);
QTextStream &ostream = NntpProxy::acquireLog(iLogPrefix);
ostream << "Dump, " << getSizeStr_noLock() << "\n";
for (int i=0; i<iList.size(); ++i){
ostream << "\t- " << *(iList[i]) << "\n";
}
NntpProxy::releaseLog();
}
template<typename Data> void MyManager<Data>::dump(QTextStream &aStream){
QMutexLocker lock(mMutex);
aStream << iLogPrefix << "Dump, " << getSizeStr_noLock() << "\n";
for (int i=0; i<iList.size(); ++i){
aStream << "\t- " << *(iList[i]) << "\n";
}
}
#endif // MYMANAGER_H