From 8e8d7e93d5d235b5233baee5d5e44978e0348a53 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 5 Sep 2016 23:26:36 +0100 Subject: [PATCH] Tidy up legacy-compatibility code in PATX/PLYX handlers. Use lambdas instead of manual emulation. Move an inner struct back into its owner function. --- src/settings.cpp | 56 +++++++++++------------------------------------- 1 file changed, 13 insertions(+), 43 deletions(-) diff --git a/src/settings.cpp b/src/settings.cpp index 18fbab98c6..39fba94cc6 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -2247,18 +2247,6 @@ static void SaveSettings(const SettingDesc *sd, void *object) /** Sorted list of PATX settings, generated by MakeSettingsPatxList */ static std::vector _sorted_patx_settings; -/** - * Internal structure used in LoadSettingsPatx() - * placed outside for legacy compiler compatibility - * this makes me miss lambdas :/ - */ -struct StringSorter { - bool operator()(const SettingDesc *a, const SettingDesc *b) - { - return strcmp(a->patx_name, b->patx_name) < 0; - } -}; - /** * Prepare a sorted list of settings to be potentially be loaded out of the PATX chunk * This is to enable efficient lookup of settings by name @@ -2277,28 +2265,11 @@ static void MakeSettingsPatxList(const SettingDesc *sd) _sorted_patx_settings.push_back(desc); } - std::sort(_sorted_patx_settings.begin(), _sorted_patx_settings.end(), StringSorter()); + std::sort(_sorted_patx_settings.begin(), _sorted_patx_settings.end(), [](const SettingDesc *a, const SettingDesc *b) { + return strcmp(a->patx_name, b->patx_name) < 0; + }); } -/** - * Internal structure used in LoadSettingsPatx() - * placed outside for legacy compiler compatibility - * this is effectively a reference capture lambda - */ -struct StringSearcher { - bool &m_exact_match; - - StringSearcher(bool &exact_match) - : m_exact_match(exact_match) { } - - bool operator()(const SettingDesc *a, const char *b) - { - int result = strcmp(a->patx_name, b); - if (result == 0) m_exact_match = true; - return result < 0; - } -}; - /** * Internal structure used in LoadSettingsPatx() and LoadSettingsPlyx() */ @@ -2354,9 +2325,13 @@ static void LoadSettingsPatx(const SettingDesc *sd, void *object) // flags are not in use yet, reserve for future expansion if (current_setting.flags != 0) SlErrorCorruptFmt("PATX chunk: unknown setting header flags: 0x%X", current_setting.flags); - // now try to find corresponding setting, this would be much easier with C++11 support... + // now try to find corresponding setting bool exact_match = false; - std::vector::iterator iter = std::lower_bound(_sorted_patx_settings.begin(), _sorted_patx_settings.end(), current_setting.name, StringSearcher(exact_match)); + auto iter = std::lower_bound(_sorted_patx_settings.begin(), _sorted_patx_settings.end(), current_setting.name, [&](const SettingDesc *a, const char *b) { + int result = strcmp(a->patx_name, b); + if (result == 0) exact_match = true; + return result < 0; + }); if (exact_match) { assert(iter != _sorted_patx_settings.end()); @@ -2376,15 +2351,6 @@ static void LoadSettingsPatx(const SettingDesc *sd, void *object) } } -/** - * Internal structure used in SaveSettingsPatx() - * placed outside for legacy compiler compatibility - */ -struct SettingToAdd { - const SettingDesc *setting; - uint32 setting_length; -}; - /** * Save handler for settings which go in the PATX chunk * @param sd SettingDesc struct containing all information @@ -2395,6 +2361,10 @@ static void SaveSettingsPatx(const SettingDesc *sd, void *object) { SettingsExtSave current_setting; + struct SettingToAdd { + const SettingDesc *setting; + uint32 setting_length; + }; std::vector settings_to_add; size_t length = 8;