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
|
/*
SPDX-FileCopyrightText: 2007 Hamish Rodda <rodda@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "micommandqueue.h"
#include "mi.h"
#include "micommand.h"
#include "debuglog.h"
using namespace KDevMI::MI;
CommandQueue::CommandQueue() = default;
CommandQueue::~CommandQueue() = default;
void CommandQueue::enqueue(std::unique_ptr<MICommand> command)
{
++m_tokenCounter;
if (m_tokenCounter == 0)
m_tokenCounter = 1;
command->setToken(m_tokenCounter);
// take the time when this command was added to the command queue
command->markAsEnqueued();
if (command->flags() & (CmdImmediately | CmdInterrupt))
++m_immediatelyCounter;
m_commandList.push_back(std::move(command));
rationalizeQueue(m_commandList.back().get());
dumpQueue();
}
void CommandQueue::dumpQueue() const
{
qCDebug(DEBUGGERCOMMON) << "Pending commands" << m_commandList.size();
unsigned commandNum = 0;
for (const auto& command : m_commandList) {
qCDebug(DEBUGGERCOMMON) << "Command" << commandNum << command->initialString();
++commandNum;
}
}
void CommandQueue::rationalizeQueue(MICommand* command)
{
if ((command->type() >= ExecAbort && command->type() <= ExecUntil) &&
command->type() != ExecArguments ) {
// Changing execution location, abort any variable updates and stack list updates
auto predicate = [this](const auto& command) {
const auto type = command->type();
const auto isVariableUpdate
= (type >= VarEvaluateExpression && type <= VarListChildren) || type == VarUpdate;
const auto isStackListUpdate = type >= StackListArguments && type <= StackListLocals;
if (isVariableUpdate || isStackListUpdate) {
if (command->flags() & (CmdImmediately | CmdInterrupt))
--m_immediatelyCounter;
return true;
}
return false;
};
m_commandList.erase(std::remove_if(m_commandList.begin(), m_commandList.end(), predicate), m_commandList.end());
}
}
void CommandQueue::clear()
{
m_commandList.clear();
m_immediatelyCounter = 0;
}
int CommandQueue::count() const
{
return m_commandList.size();
}
bool CommandQueue::isEmpty() const
{
return m_commandList.empty();
}
bool CommandQueue::haveImmediateCommand() const
{
return m_immediatelyCounter > 0;
}
std::unique_ptr<MICommand> CommandQueue::nextCommand()
{
if (m_commandList.empty())
return {};
auto command = std::exchange(m_commandList.front(), {});
m_commandList.pop_front();
if (command->flags() & (CmdImmediately | CmdInterrupt))
--m_immediatelyCounter;
return command;
}
|