Gromacs
2025-dev-20241008-cf8b9ef
|
#include <gromacs/options/optionstoragetemplate.h>
Templated base class for constructing option value storage classes.
T | Assignable type that stores a single option value. |
Provides an implementation of the clearSet(), valueCount(), processSet(), and defaultValuesAsStrings() methods of AbstractOptionStorage, as well as a basic no-action implementation of processAll(). Two new virtual methods are added: processSetValues() and formatSingleValue(). This leaves typeString(), convertValue() and formatStringValue() to be implemented in derived classes. processSetValues() and processAll() can also be implemented if necessary.
Implements transaction support for adding values within a set: all calls to addValue() add the value to a temporary storage, processSetValues() operates on this temporary storage, and commitValues() then copies these values to the real storage. commitValues() provides a strong exception safety guarantee for the process (and it only throws if it runs out of memory).
Public Types | |
typedef OptionStorageTemplate< T > | MyBase |
Alias for the template class for use in base classes. | |
typedef std::vector< T > | ValueList |
Type of the container that contains the current values. | |
Public Member Functions | |
std::string | typeString () const override=0 |
Returns a short string describing the type of the option. | |
int | valueCount () const override |
Returns the number of option values added so far. More... | |
std::vector< Any > | defaultValues () const override |
Returns the default value(s) of the option. More... | |
std::vector< std::string > | defaultValuesAsStrings () const override |
Returns the default value(s) of the option as strings. More... | |
Public Member Functions inherited from gmx::AbstractOptionStorage | |
bool | isSet () const |
Returns true if the option has been set. | |
bool | isBoolean () const |
Returns true if the option is a boolean option. More... | |
bool | isHidden () const |
Returns true if the option is a hidden option. | |
bool | isRequired () const |
Returns true if the option is required. | |
bool | isVector () const |
Returns true if the option is vector-valued. | |
const std::string & | name () const |
Returns the name of the option. | |
const std::string & | description () const |
Returns the description of the option set by the calling code. | |
bool | defaultValueIfSetExists () const |
Returns true if defaultValueIfSet() value is specified. | |
int | minValueCount () const |
Returns the minimum number of values required in one set. | |
int | maxValueCount () const |
Returns the maximum allowed number of values in one set (-1 = no limit). | |
virtual OptionInfo & | optionInfo ()=0 |
Returns an option info object corresponding to this option. | |
virtual std::string | formatExtraDescription () const |
Formats additional description for the option. More... | |
virtual std::vector< Any > | normalizeValues (const std::vector< Any > &values) const =0 |
Converts given values to native representation for this option. More... | |
void | startSource () |
Starts adding values from a new source for the option. More... | |
void | startSet () |
Starts adding a new set of values for the option. More... | |
void | appendValue (const Any &value) |
Adds a new value for the option. More... | |
void | finishSet () |
Performs validation and/or actions once a set of values has been added. More... | |
void | finish () |
Performs validation and/or actions once all values have been added. More... | |
Protected Types | |
typedef std::unique_ptr < IOptionValueStore< T > > | StorePointer |
Smart pointer for managing the final storage interface. | |
Protected Member Functions | |
template<class U > | |
OptionStorageTemplate (const OptionTemplate< T, U > &settings, OptionFlags staticFlags=OptionFlags()) | |
Initializes the storage from option settings. More... | |
OptionStorageTemplate (const AbstractOption &settings, StorePointer store) | |
Initializes the storage from base option settings. More... | |
void | clearSet () override |
Removes all values from temporary storage for a set. More... | |
void | convertValue (const Any &value) override=0 |
Adds a new value. More... | |
virtual void | processSetValues (ValueList *values) |
Processes values for a set after all have been converted. More... | |
void | processSet () override |
Performs validation and/or actions once a set of values has been added. More... | |
void | processAll () override |
Performs validation and/or actions once all values have been added. More... | |
virtual std::string | formatSingleValue (const T &value) const =0 |
Formats a single value as a string. More... | |
void | addValue (const T &value) |
Adds a value to a temporary storage. More... | |
void | commitValues () |
Commits values added with addValue(). More... | |
void | setDefaultValue (const T &value) |
Sets the default value for the option. More... | |
void | setDefaultValueIfSet (const T &value) |
Sets the default value if set for the option. More... | |
ArrayRef< T > | values () |
Provides derived classes access to the current list of values. More... | |
ArrayRef< const T > | values () const |
Provides derived classes access to the current list of values. | |
Protected Member Functions inherited from gmx::AbstractOptionStorage | |
AbstractOptionStorage (const AbstractOption &settings, OptionFlags staticFlags) | |
Initializes the storage object from the settings object. More... | |
void | markAsSet () |
Marks the option as set. | |
bool | hasFlag (OptionFlag flag) const |
Returns true if the given flag is set. | |
void | setFlag (OptionFlag flag) |
Sets the given flag. | |
void | clearFlag (OptionFlag flag) |
Clears the given flag. | |
void | setMinValueCount (int count) |
Sets a new minimum number of values required in one set. More... | |
void | setMaxValueCount (int count) |
Sets a new maximum number of values required in one set. More... | |
|
explicitprotected |
Initializes the storage from option settings.
[in] | settings | Option settings. |
[in] | staticFlags | Option flags that are always set and specify generic behavior of the option. |
APIError | if invalid settings have been provided. |
|
protected |
Initializes the storage from base option settings.
[in] | settings | Option settings. |
[in] | store | Final storage location. |
APIError | if invalid settings have been provided. |
This constructor works for cases where there is no matching OptionTemplate (e.g., EnumOption).
|
protected |
Adds a value to a temporary storage.
[in] | value | Value to add. A copy is made. |
std::bad_alloc | if out of memory. |
InvalidInputError | if the maximum value count has been reached. |
Derived classes should call this function from the convertValue() implementation to add converted values to the storage. If the maximum value count has been reached, the value is discarded and an exception is thrown.
If adding values outside convertValue() (e.g., to set a custom default value), derived classes should call clearSet() before adding values (unless in the constructor) and commitValues() once all values are added.
|
overrideprotectedvirtual |
Removes all values from temporary storage for a set.
This function is always called before starting to add values to a set, allowing the storage to clear its internal buffers.
Should not throw.
Implements gmx::AbstractOptionStorage.
|
protected |
Commits values added with addValue().
std::bad_alloc | if out of memory. |
If this function succeeds, values added with addValue() since the previous clearSet() are added to the storage for the option. Only throws in out-of-memory conditions, and provides the strong exception safety guarantee as long as the copy constructor of T
does not throw.
See addValue() for cases where this method should be used in derived classes.
Calls clearSet() if it is successful.
|
overrideprotectedpure virtual |
Adds a new value.
[in] | value | Value to convert. |
InvalidInputError | if value is not valid for this option or if there have been too many values in the set. |
This method may be called multiple times if the underlying option is defined to accept multiple values.
Derived classes should call addValue() after they have converted value
to the storage type. It is allowed to call addValue() more than once, or not at all. OptionsAssigner::appendValue() provides the same exception safety guarantee as this method, so it should be considered whether the implementation can be made strongly exception safe.
Implements gmx::AbstractOptionStorage.
Implemented in gmx::test::anonymous_namespace{abstractoptionstorage.cpp}::MockOptionStorage.
|
overridevirtual |
Returns the default value(s) of the option.
The returned values should all be of the same type, but returning each as a separate any is currently simpler.
Currently, this can only be called before option values have been assigned.
Implements gmx::AbstractOptionStorage.
|
overridevirtual |
Returns the default value(s) of the option as strings.
If there is no default value, but defaultValueIfSet() is set, that is returned instead.
Currently, this can only be called before option values have been assigned.
OptionStorageTemplate implements handling of defaultValueIfSet() cases and composing the vector. Derived classes must implement formatSingleValue() to provide the actual formatting for a value of type T
.
Implements gmx::AbstractOptionStorage.
|
protectedpure virtual |
Formats a single value as a string.
[in] | value | Value to format. |
value
formatted as a string.The derived class must provide this method to format values a strings. Called by defaultValuesAsStrings() to do the actual formatting.
Implemented in gmx::EnumOptionStorage, gmx::StringOptionStorage, gmx::FloatOptionStorage, gmx::DoubleOptionStorage, gmx::Int64OptionStorage, gmx::test::anonymous_namespace{abstractoptionstorage.cpp}::MockOptionStorage, gmx::IntegerOptionStorage, gmx::SelectionOptionStorage, gmx::FileNameOptionStorage, and gmx::BooleanOptionStorage.
|
inlineoverrideprotectedvirtual |
Performs validation and/or actions once all values have been added.
InvalidInputError | if all provided values are not valid as a set. |
This method is always called once.
If the method throws, implementation should take care to leave the option in a consistent, meaningful state. However, currently none of the implementations actually throw in any situation where the option may be left in an inconsistent state.
The implementation in OptionStorageTemplate does nothing.
Implements gmx::AbstractOptionStorage.
|
overrideprotectedvirtual |
Performs validation and/or actions once a set of values has been added.
InvalidInputError | if the values in the set are not valid as a whole. |
This method may be called multiple times if the underlying option can be specified multiple times. This method is not currently called if one of the convertValue() calls throwed.
OptionStorageTemplate implements transaction support for a set of values in this method (see the class description), and provides a more detailed processSetValues() method that can be overridden in subclasses to process the actual values. Derived classes should override that method instead of this one if set value processing is necessary.
Implements gmx::AbstractOptionStorage.
|
inlineprotectedvirtual |
Processes values for a set after all have been converted.
[in,out] | values | Valid values in the set. |
InvalidInputError | if the values do not form a valid set. |
This method is called after all convertValue() calls for a set. values
contains all values that were validly converted by convertValue(). The derived class may alter the values, but should in such a case ensure that a correct number of values is produced. If the derived class throws, all values in values
are discarded.
|
protected |
Sets the default value for the option.
[in] | value | Default value to set. |
std::bad_alloc | if out of memory. |
This method can be used from the derived class constructor to programmatically set a default value.
|
protected |
Sets the default value if set for the option.
[in] | value | Default value to set. |
std::bad_alloc | if out of memory. |
This method can be used from the derived class constructor to programmatically set a default value.
|
inlineoverridevirtual |
Returns the number of option values added so far.
Implements gmx::AbstractOptionStorage.
|
inlineprotected |
Provides derived classes access to the current list of values.
The non-const any should only be used from processAll() in derived classes if necessary.