Repository: microsoft/terminal
File: src/cascadia/Remoting/Monarch.cpp (C++)

Code before:
                                  TraceLoggingBoolean(args.OnCurrentDesktop(), "OnCurrentDesktop", "true iff the window needs to be on the current virtual desktop"),
                                  TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
                                  TraceLoggingKeyword(TIL_KEYWORD_TRACE));
            }
        }
        catch (...)
        {
            LOG_CAUGHT_EXCEPTION();
            TraceLoggingWrite(g_hRemotingProvider,
                              "Monarch_SummonWindow_Failed",
                              TraceLoggingWideString(searchedForName.c_str(), "searchedForName", "The name of the window we tried to summon"),
                              TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
                              TraceLoggingKeyword(TIL_KEYWORD_TRACE));
        }
    }

    // Method Description:
    // - This method creates a map of peasant IDs to peasant names
    //   while removing dead peasants.
    // Arguments:
    // - <none>
    // Return Value:
    // - A map of peasant IDs to their names.
    Windows::Foundation::Collections::IVectorView<winrt::Microsoft::Terminal::Remoting::PeasantInfo> Monarch::GetAllPeasantInfo()
    {
        auto names = winrt::single_threaded_vector<winrt::Microsoft::Terminal::Remoting::PeasantInfo>();

        std::vector<uint64_t> peasantsToErase{};
        for (const auto& [id, p] : _peasants)
        {
            try
            {
                names.Append({ id, p.WindowName(), p.ActiveTabTitle() });
            }
            catch (...)
            {
                LOG_CAUGHT_EXCEPTION();
                peasantsToErase.push_back(id);
            }
        }

        // Remove the dead peasants we came across while iterating.
        for (const auto& id : peasantsToErase)
        {
            _peasants.erase(id);
            _clearOldMruEntries(id);
        }

        return names.GetView();
    }


Review comment (suggestion):
I wish we could move to something like this instead:
```cpp
std::vector<winrt::Microsoft::Terminal::Remoting::PeasantInfo> names;

// names.emplace_back(...);

return winrt::single_threaded_vector(std::move(names)).GetView();
```

Code after:
        const auto onError = [&](const auto& id) {
            TraceLoggingWrite(g_hRemotingProvider,
                              "Monarch_identifyWindows_Failed",
                              TraceLoggingInt64(id, "peasantID", "The ID of the peasant which we could not identify"),
                              TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
                              TraceLoggingKeyword(TIL_KEYWORD_TRACE));
        };

        _forEachPeasant(func, onError);

        return winrt::single_threaded_vector<PeasantInfo>(std::move(names)).GetView();
    }

    bool Monarch::DoesQuakeWindowExist()
    {
        bool result = false;
        const auto func = [&](const auto& /*id*/, const auto& p) {
            if (p.WindowName() == QuakeWindowName)
            {
                result = true;
            }
            // continue if we didn't get a positive result
            return !result;
        };

        const auto onError = [&](const auto& id) {
            TraceLoggingWrite(g_hRemotingProvider,
                              "Monarch_DoesQuakeWindowExist_Failed",
                              TraceLoggingInt64(id, "peasantID", "The ID of the peasant which we could not ask for its name"),
                              TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
                              TraceLoggingKeyword(TIL_KEYWORD_TRACE));
        };

        _forEachPeasant(func, onError);
        return result;
    }

    void Monarch::SummonAllWindows()
    {
        const auto func = [&](const auto& /*id*/, const auto& p) {
            SummonWindowBehavior args{};
            args.ToggleVisibility(false);
            p.Summon(args);
        };

        const auto onError = [&](const auto& id) {
            TraceLoggingWrite(g_hRemotingProvider,
                              "Monarch_SummonAll_Failed",
                              TraceLoggingInt64(id, "peasantID", "The ID of the peasant which we could not summon"),
                              TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
                              TraceLoggingKeyword(TIL_KEYWORD_TRACE));