File: Instruments.cpp

package info (click to toggle)
megacmd 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 32,592 kB
  • sloc: cpp: 326,437; ansic: 34,524; python: 4,630; java: 3,965; sh: 2,869; objc: 2,459; makefile: 197; xml: 113
file content (199 lines) | stat: -rw-r--r-- 5,221 bytes parent folder | download | duplicates (2)
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
194
195
196
197
198
199
/**
 * (c) 2013 by Mega Limited, Auckland, New Zealand
 *
 * This file is part of MEGAcmd.
 *
 * MEGAcmd is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright Simplified (2-clause) BSD License.
 *
 * You should have received a copy of the license along with this
 * program.
 */

#include "Instruments.h"

TestInstruments& TestInstruments::Instance()
{
    static TestInstruments instance;
    return instance;
}

void TestInstruments::clearAll()
{
    std::lock_guard<std::recursive_mutex> lock(mMutex);
    mFlags.clear();
    clearEvents();
    mProbes.clear();
}

bool TestInstruments::flag(Flag flag)
{
    std::lock_guard<std::recursive_mutex> lock(mMutex);
    return mFlags.find(flag) != mFlags.end();
}

void TestInstruments::setFlag(Flag flag)
{
    std::lock_guard<std::recursive_mutex> lock(mMutex);
    mFlags.insert(flag);
}

void TestInstruments::resetFlag(Flag flag)
{
    std::lock_guard<std::recursive_mutex> lock(mMutex);
    mFlags.erase(flag);
}

void TestInstruments::clearFlags()
{
    std::lock_guard<std::recursive_mutex> lock(mMutex);
    mFlags.clear();
}

void TestInstruments::throwIfFlagAndReset(TestInstruments::Flag flag)
{
    std::lock_guard<std::recursive_mutex> lock(mMutex);
    if (mFlags.find(flag) != mFlags.end())
    {
        mFlags.erase(flag);
        throw InstrumentsException();
    }
}

void TestInstruments::onEventOnce(Event event, EventCallback&& handler)
{
    std::lock_guard<std::recursive_mutex> lock(mMutex);
    mSingleEventHandlers.insert_or_assign(event, std::move(handler));
}

void TestInstruments::onEventOnce(Event event, EventCallback& handler)
{
    std::lock_guard<std::recursive_mutex> lock(mMutex);
    mSingleEventHandlers.insert_or_assign(event, handler);
}

void TestInstruments::clearEvent(Event event)
{
    std::lock_guard<std::recursive_mutex> lock(mMutex);
    mSingleEventHandlers.erase(event);
    mEventMultiHandlers.erase(event);
}

void TestInstruments::clearEvent(MegaCmdEvent event)
{
    std::lock_guard<std::recursive_mutex> lock(mMutex);
    mMegaCmdSingleEventHandlers.erase(event);
    mMegaCmdEventMultiHandlers.erase(event);
}

void TestInstruments::fireEvent(Event event)
{
    std::vector<EventCallback> handlersToExecute;
    {
        std::lock_guard<std::recursive_mutex> lock(mMutex);
        auto handlerIt = mSingleEventHandlers.find(event);
        if (handlerIt != mSingleEventHandlers.end())
        {
            handlersToExecute.push_back(std::move(handlerIt->second));
            mSingleEventHandlers.erase(handlerIt);
        }

        auto range = mEventMultiHandlers.equal_range(event);
        for (auto handlerIt = range.first; handlerIt != range.second; handlerIt++)
        {
            handlerIt->second();
        }
    }
    for (auto &handler : handlersToExecute)
    {
        handler();
    }
}

void TestInstruments::fireEvent(MegaCmdEvent event)
{
    std::vector<EventCallback> handlersToExecute;
    {
        std::lock_guard<std::recursive_mutex> lock(mMutex);
        auto handlerIt = mMegaCmdSingleEventHandlers.find(event);
        if (handlerIt != mMegaCmdSingleEventHandlers.end())
        {
            handlersToExecute.push_back(std::move(handlerIt->second));
            mMegaCmdSingleEventHandlers.erase(handlerIt);
        }

        auto range = mMegaCmdEventMultiHandlers.equal_range(event);
        for (auto handlerIt = range.first; handlerIt != range.second; handlerIt++)
        {
            handlerIt->second();
        }
    }
    for (auto &handler : handlersToExecute)
    {
        handler();
    }
}

void TestInstruments::clearEvents()
{
    std::lock_guard<std::recursive_mutex> lock(mMutex);
    mSingleEventHandlers.clear();
    mMegaCmdSingleEventHandlers.clear();
    mEventMultiHandlers.clear();
    mMegaCmdEventMultiHandlers.clear();
}

std::optional<TestInstruments::TestValue_t> TestInstruments::testValue(TestValue key)
{
    std::lock_guard<std::recursive_mutex> lock(mMutex);
    auto it = mTestValues.find(key);
    if (it != mTestValues.end())
    {
        return it->second;
    }
    return std::nullopt;
}

void TestInstruments::setTestValue(TestValue key, TestValue_t value)
{
    std::lock_guard<std::recursive_mutex> lock(mMutex);
    mTestValues.insert_or_assign(key, value);
}

void TestInstruments::resetTestValue(TestValue key)
{
    std::lock_guard<std::recursive_mutex> lock(mMutex);
    mTestValues.erase(key);
}

void TestInstruments::increaseTestValue(TestValue key)
{
    std::lock_guard<std::recursive_mutex> lock(mMutex);
    auto it = mTestValues.find(key);
    if (it == mTestValues.end())
    {
        return;
    }
    mTestValues.insert_or_assign(key, ++std::get<uint64_t>(it->second));
}

void TestInstruments::clearTestValues()
{
    std::lock_guard<std::recursive_mutex> lock(mMutex);
    mTestValues.clear();
}

bool TestInstruments::probe(const std::string &str)
{
    std::lock_guard<std::recursive_mutex> lock(mMutex);
    return mProbes.find(str) != mProbes.end();
}

void TestInstruments::clearProbes()
{
    std::lock_guard<std::recursive_mutex> lock(mMutex);
    mProbes.clear();
}