File: operation.cpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (189 lines) | stat: -rw-r--r-- 4,357 bytes parent folder | download
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
#include "operation.hpp"

#include <algorithm>
#include <exception>
#include <vector>

#include <QTimer>

#include <components/debug/debuglog.hpp>

#include <apps/opencs/model/doc/messages.hpp>

#include "../world/universalid.hpp"

#include "stage.hpp"

namespace CSMDoc
{
    namespace
    {
        std::string_view operationToString(State value)
        {
            switch (value)
            {
                case State_Saving:
                    return "Saving";
                case State_Merging:
                    return "Merging";
                case State_Verifying:
                    return "Verifying";
                case State_Searching:
                    return "Searching";
                case State_Loading:
                    return "Loading";
                default:
                    break;
            }
            return "Unknown";
        }
    }
}

void CSMDoc::Operation::prepareStages()
{
    mCurrentStage = mStages.begin();
    mCurrentStep = 0;
    mCurrentStepTotal = 0;
    mTotalSteps = 0;
    mError = false;

    for (std::vector<std::pair<Stage*, int>>::iterator iter(mStages.begin()); iter != mStages.end(); ++iter)
    {
        iter->second = iter->first->setup();
        mTotalSteps += iter->second;
    }
}

CSMDoc::Operation::Operation(State type, bool ordered, bool finalAlways)
    : mType(type)
    , mStages(std::vector<std::pair<Stage*, int>>())
    , mCurrentStage(mStages.begin())
    , mCurrentStep(0)
    , mCurrentStepTotal(0)
    , mTotalSteps(0)
    , mOrdered(ordered)
    , mFinalAlways(finalAlways)
    , mError(false)
    , mConnected(false)
    , mPrepared(false)
    , mDefaultSeverity(Message::Severity_Error)
{
    mTimer = new QTimer(this);
}

CSMDoc::Operation::~Operation()
{
    for (std::vector<std::pair<Stage*, int>>::iterator iter(mStages.begin()); iter != mStages.end(); ++iter)
        delete iter->first;
}

void CSMDoc::Operation::run()
{
    mTimer->stop();

    if (!mConnected)
    {
        connect(mTimer, &QTimer::timeout, this, &Operation::executeStage);
        mConnected = true;
    }

    mPrepared = false;
    mStart = std::chrono::steady_clock::now();

    mTimer->start(0);
}

void CSMDoc::Operation::appendStage(Stage* stage)
{
    mStages.emplace_back(stage, 0);
}

void CSMDoc::Operation::setDefaultSeverity(Message::Severity severity)
{
    mDefaultSeverity = severity;
}

bool CSMDoc::Operation::hasError() const
{
    return mError;
}

void CSMDoc::Operation::abort()
{
    if (!mTimer->isActive())
        return;

    mError = true;

    if (mFinalAlways)
    {
        if (mStages.begin() != mStages.end() && mCurrentStage != --mStages.end())
        {
            mCurrentStep = 0;
            mCurrentStage = --mStages.end();
        }
    }
    else
        mCurrentStage = mStages.end();
}

void CSMDoc::Operation::executeStage()
{
    if (!mPrepared)
    {
        prepareStages();
        mPrepared = true;
    }

    Messages messages(mDefaultSeverity);

    while (mCurrentStage != mStages.end())
    {
        if (mCurrentStep >= mCurrentStage->second)
        {
            mCurrentStep = 0;
            ++mCurrentStage;
        }
        else
        {
            try
            {
                mCurrentStage->first->perform(mCurrentStep++, messages);
            }
            catch (const std::exception& e)
            {
                emit reportMessage(
                    Message(CSMWorld::UniversalId(), e.what(), "", Message::Severity_SeriousError), mType);
                abort();
            }

            ++mCurrentStepTotal;
            break;
        }
    }

    emit progress(mCurrentStepTotal, mTotalSteps ? mTotalSteps : 1, mType);

    for (Messages::Iterator iter(messages.begin()); iter != messages.end(); ++iter)
        emit reportMessage(*iter, mType);

    if (mCurrentStage == mStages.end())
    {
        if (mStart.has_value())
        {
            const auto duration = std::chrono::steady_clock::now() - *mStart;
            Log(Debug::Verbose) << operationToString(mType) << " operation is completed in "
                                << std::chrono::duration_cast<std::chrono::duration<double>>(duration).count() << 's';
            mStart.reset();
        }

        operationDone();
    }
}

void CSMDoc::Operation::operationDone()
{
    mTimer->stop();
    emit done(mType, mError);
}