1 /**
2 * UGENE - Integrated Bioinformatics Tools.
3 * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4 * http://ugene.net
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 #include "Counter.h"
23
24 namespace U2 {
25
26 /** A helper class that deletes all onHeap counters when destroyed. */
27 class GCounterList {
28 public:
~GCounterList()29 ~GCounterList() {
30 for (int i = 0; i < list.size(); i++) {
31 GCounter *counter = list[i];
32 if (counter->isOnHeap) {
33 list[i] = nullptr;
34 delete counter;
35 }
36 }
37 }
38 QList<GCounter *> list;
39 };
40
getGlobalCounterList()41 static QList<GCounter *> &getGlobalCounterList() {
42 // Thread safe initialization of the global counters list.
43 static GCounterList counterList;
44 return counterList.list;
45 }
46
GCounter(const QString & name,const QString & suffix,qint64 value,double scale,bool isReportable,bool isOnHeap)47 GCounter::GCounter(const QString &name, const QString &suffix, qint64 value, double scale, bool isReportable, bool isOnHeap)
48 : name(name), suffix(suffix), value(value), scale(scale), isReportable(isReportable), isOnHeap(isOnHeap) {
49 getGlobalCounterList() << this;
50 }
51
~GCounter()52 GCounter::~GCounter() {
53 getGlobalCounterList().removeOne(this);
54 };
55
findCounter(const QString & name,const QString & suffix)56 GCounter *GCounter::findCounter(const QString &name, const QString &suffix) {
57 for (GCounter *counter : qAsConst(getGlobalCounterList())) {
58 if (name == counter->name && suffix == counter->suffix) {
59 return counter;
60 }
61 }
62 return nullptr;
63 }
64
getAllCounters()65 QList<GCounter *> GCounter::getAllCounters() {
66 return getGlobalCounterList();
67 }
68
increment(const QString & name,const QString & suffix)69 void GCounter::increment(const QString &name, const QString &suffix) {
70 GCounter *counter = GCounter::findCounter(name, suffix);
71 if (counter == nullptr) {
72 counter = new GCounter(name, suffix, 0, 1, true, true);
73 }
74 counter->value++;
75 }
76
77 } // namespace U2
78