terminal/src/types/IInputEvent.cpp
pi1024e 7621994b46
Make loop in IInputEvent more consistent and modern (#4959)
In IInputEvent, there are two for loops. One is a range based loop operating on "records", and the other is a classic for-loop doing the same. For consistency, the for-loop was changed into the more modern variation via a compiler refactoring, which has the exact same behavior as the other for-loop in the main function.

Yes, of course this was tested manually and with the unit tests.

# Validation Steps
Unit testing passed. In addition, for the manual validation tests, I compared the output for sample values between the two loops ensuring the same results.
2020-03-17 10:52:33 -07:00

69 lines
2 KiB
C++

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "inc/IInputEvent.hpp"
#include <unordered_map>
std::unique_ptr<IInputEvent> IInputEvent::Create(const INPUT_RECORD& record)
{
switch (record.EventType)
{
case KEY_EVENT:
return std::make_unique<KeyEvent>(record.Event.KeyEvent);
case MOUSE_EVENT:
return std::make_unique<MouseEvent>(record.Event.MouseEvent);
case WINDOW_BUFFER_SIZE_EVENT:
return std::make_unique<WindowBufferSizeEvent>(record.Event.WindowBufferSizeEvent);
case MENU_EVENT:
return std::make_unique<MenuEvent>(record.Event.MenuEvent);
case FOCUS_EVENT:
return std::make_unique<FocusEvent>(record.Event.FocusEvent);
default:
THROW_HR(E_INVALIDARG);
}
}
std::deque<std::unique_ptr<IInputEvent>> IInputEvent::Create(gsl::span<const INPUT_RECORD> records)
{
std::deque<std::unique_ptr<IInputEvent>> outEvents;
for (const auto& record : records)
{
outEvents.push_back(Create(record));
}
return outEvents;
}
// Routine Description:
// - Converts std::deque<INPUT_RECORD> to std::deque<std::unique_ptr<IInputEvent>>
// Arguments:
// - inRecords - records to convert
// Return Value:
// - std::deque of IInputEvents on success. Will throw exception on failure.
std::deque<std::unique_ptr<IInputEvent>> IInputEvent::Create(const std::deque<INPUT_RECORD>& records)
{
std::deque<std::unique_ptr<IInputEvent>> outEvents;
for (const auto& record : records)
{
std::unique_ptr<IInputEvent> event = Create(record);
outEvents.push_back(std::move(event));
}
return outEvents;
}
std::vector<INPUT_RECORD> IInputEvent::ToInputRecords(const std::deque<std::unique_ptr<IInputEvent>>& events)
{
std::vector<INPUT_RECORD> records;
records.reserve(events.size());
for (auto& evt : events)
{
records.push_back(evt->ToInputRecord());
}
return records;
}