* \author Teemu Murtola <teemu.murtola@gmail.com>
* \ingroup module_options
*/
+#include "gmxpre.h"
+
#include "basicoptions.h"
-#include "basicoptionstorage.h"
#include <cerrno>
#include <cstdio>
#include "gromacs/utility/exceptions.h"
#include "gromacs/utility/stringutil.h"
+#include "basicoptionstorage.h"
+
namespace
{
GMX_THROW(APIError("Conflicting default values"));
}
}
- // If there is no default value, match is still -1.
- if (enumIndexStore_ != NULL)
- {
- *enumIndexStore_ = match;
- }
}
if (settings.defaultEnumIndex_ >= 0)
{
addValue(allowed_[settings.defaultEnumIndex_]);
commitValues();
}
+ // Somewhat subtly, this does not update the stored enum index if the
+ // caller has not provided store() or storeVector(), because values()
+ // will be empty in such a case. This leads to (desired) behavior of
+ // preserving the existing value in the enum index store variable in such
+ // cases.
+ refreshEnumIndexStore();
}
std::string StringOptionStorage::formatExtraDescription() const
void StringOptionStorage::refreshValues()
{
MyBase::refreshValues();
+ refreshEnumIndexStore();
+}
+
+void StringOptionStorage::refreshEnumIndexStore()
+{
if (enumIndexStore_ != NULL)
{
for (size_t i = 0; i < values().size(); ++i)
{
- ValueList::const_iterator match =
- std::find(allowed_.begin(), allowed_.end(), values()[i]);
- GMX_ASSERT(match != allowed_.end(),
- "Enum value not found (internal error)");
- enumIndexStore_[i] = static_cast<int>(match - allowed_.begin());
+ if (values()[i].empty())
+ {
+ enumIndexStore_[i] = -1;
+ }
+ else
+ {
+ ValueList::const_iterator match =
+ std::find(allowed_.begin(), allowed_.end(), values()[i]);
+ GMX_ASSERT(match != allowed_.end(),
+ "Enum value not found (internal error)");
+ enumIndexStore_[i] = static_cast<int>(match - allowed_.begin());
+ }
}
}
}