From a6eea3cd08ec2e4af98a3abac85e945522a998f5 Mon Sep 17 00:00:00 2001 From: Vasily Date: Fri, 24 Jan 2020 13:33:53 +0100 Subject: [PATCH] Add `save/loadAllObjects` to object settings API Looping through the vectors was done for almost all vectors individually. Because there are some weird off by one issues that could crop up it's easier to just have a function for it. --- src/settings/settings_object.h | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/settings/settings_object.h b/src/settings/settings_object.h index eaed5d7..b6be012 100644 --- a/src/settings/settings_object.h +++ b/src/settings/settings_object.h @@ -91,6 +91,54 @@ void loadObject( ISettingsObject& obj ); */ void saveNumberedObject( const ISettingsObject& obj, const int slot ); +/*! + \brief Saves \a vec of \c ISettingsObject. + \param vec Vector of \c ISettingsObject derived objects. + + Saves the objects starting from \c slot 1 to \c slot \c{1 + vec.size()}. +*/ +template void saveAllObjects( const std::vector vec ) +{ + static_assert( + std::is_base_of::value, + "Only objects that inherit from ISettingsObject can be saved." ); + + auto i = 1; + for ( auto& p : vec ) + { + saveNumberedObject( p, i ); + ++i; + } +} + +/*! + \brief Loads \a vec of \c ISettingsObject. + \param vec Vector of \c ISettingsObject derived objects. + + Loads the objects starting from \c slot 1 to the last contiguous object. +*/ +template void loadAllObjects( std::vector& vec ) +{ + static_assert( + std::is_base_of::value, + "Only objects that inherit from ISettingsObject can be loaded." ); + + vec.clear(); + + // We'll need to know which name the object is saved under. + // We could check vec.at(0), but it's not guaranteed to exist. + auto o = T{}; + auto profileCount = getAmountOfSavedObjects( o ); + for ( int profileNumber = 1; profileNumber < profileCount; ++profileNumber ) + { + vec.emplace_back(); + + const auto index = static_cast( profileNumber - 1 ); + + loadNumberedObject( vec.at( index ), profileNumber ); + } +} + /*! \brief Loads \a obj from permanent storage in a numbered \a slot. \param obj Object to load.