Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsModel/ApplicationState.cpp (C++)

Code before:
                                                            \
        _throttler();                                       \
    }
    MTSM_APPLICATION_STATE_FIELDS(MTSM_APPLICATION_STATE_GEN)
#undef MTSM_APPLICATION_STATE_GEN

    // Method Description:
    // - See GH#11119. Removes all of the data in this ApplicationState object
    //   and resets it to the defaults. This will delete the state file! That's
    //   the sure-fire way to make sure the data doesn't come back. If we leave
    //   it untouched, then when we go to write the file back out, we'll first
    //   re-read it's contents and try to overlay our new state. However,
    //   nullopts won't remove keys from the JSON, so we'll end up with the
    //   original state in the file.
    // Arguments:
    // - <none>
    // Return Value:
    // - <none>
    void ApplicationState::Reset() noexcept
    try
    {
        LOG_LAST_ERROR_IF(!DeleteFile(_path.c_str()));

#define MTSM_APPLICATION_STATE_GEN(type, name, key, ...) \
    {                                                    \
        auto state = _state.lock();                      \
        state->name = std::nullopt;                      \
        state->name##Changed = true;                     \
    }                                                    \
    _throttler();

        MTSM_APPLICATION_STATE_FIELDS(MTSM_APPLICATION_STATE_GEN)
#undef MTSM_APPLICATION_STATE_GEN
    }
    CATCH_LOG()

    Json::Value ApplicationState::_getRoot(const locked_hfile& file) const noexcept
    {
        Json::Value root;
        try
        {
            const auto data = ReadUTF8FileLocked(file);
            if (data.empty())
            {
                return root;
            }

            std::string errs;
            std::unique_ptr<Json::CharReader> reader{ Json::CharReaderBuilder::CharReaderBuilder().newCharReader() };

            if (!reader->parse(data.data(), data.data() + data.size(), &root, &errs))

Review comment (refactor):
we may like to refactor this to do the whole thing under one lock over `_state`; @lhecker?

Code after:
                                                            \
        _throttler();                                       \
    }
    MTSM_APPLICATION_STATE_FIELDS(MTSM_APPLICATION_STATE_GEN)
#undef MTSM_APPLICATION_STATE_GEN

    // Method Description:
    // - See GH#11119. Removes all of the data in this ApplicationState object
    //   and resets it to the defaults. This will delete the state file! That's
    //   the sure-fire way to make sure the data doesn't come back. If we leave
    //   it untouched, then when we go to write the file back out, we'll first
    //   re-read it's contents and try to overlay our new state. However,
    //   nullopts won't remove keys from the JSON, so we'll end up with the
    //   original state in the file.
    // Arguments:
    // - <none>
    // Return Value:
    // - <none>
    void ApplicationState::Reset() noexcept
    try
    {
        LOG_LAST_ERROR_IF(!DeleteFile(_path.c_str()));
        *_state.lock() = {};
    }
    CATCH_LOG()

    Json::Value ApplicationState::_getRoot(const locked_hfile& file) const noexcept
    {
        Json::Value root;
        try
        {
            const auto data = ReadUTF8FileLocked(file);
            if (data.empty())
            {
                return root;
            }

            std::string errs;
            std::unique_ptr<Json::CharReader> reader{ Json::CharReaderBuilder::CharReaderBuilder().newCharReader() };

            if (!reader->parse(data.data(), data.data() + data.size(), &root, &errs))
            {
                throw winrt::hresult_error(WEB_E_INVALID_JSON_STRING, winrt::to_hstring(errs));
            }
        }
        CATCH_LOG()

        return root;
    }

    // Deserializes the state.json at _path into this ApplicationState.