Gromacs  2016.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
List of all members | Public Types | Public Member Functions | Protected Member Functions | Static Protected Attributes
gmx::OptionStorageTemplate< T > Class Template Referenceabstract

#include <gromacs/options/optionstoragetemplate.h>

+ Inheritance diagram for gmx::OptionStorageTemplate< T >:
+ Collaboration diagram for gmx::OptionStorageTemplate< T >:

Description

template<typename T>
class gmx::OptionStorageTemplate< T >

Templated base class for constructing option value storage classes.

Template Parameters
TAssignable type that stores a single option value.

Provides an implementation of the clearSet(), valueCount(), and processSet() methods of AbstractOptionStorage, as well as a basic no-action implementation of processAll(). Two new virtual methods are added: processSetValues() and refreshValues(). The default implementation of processSetValues() does nothing, and refreshValues() is used to update secondary storage after values have been added/changed. This leaves typeString(), formatValue(), and convertValue() 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

virtual std::string typeString () const =0
 Returns a short string describing the type of the option.
 
virtual int valueCount () const
 Returns the number of option values added so far. More...
 
virtual std::string formatValue (int i) const
 Returns the i'th value formatted as a string. More...
 
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 OptionInfooptionInfo ()=0
 Returns an option info object corresponding to this option.
 
virtual std::string formatExtraDescription () const
 Formats additional description for the option. More...
 
std::string formatDefaultValueIfSet () const
 Returns the default value if set for the option as a string. 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 std::string &value)
 Adds a new value for the option, converting it from a string. 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 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)
 Initializes the storage from base option settings. More...
 
virtual void clearSet ()
 Removes all values from temporary storage for a set. More...
 
virtual void convertValue (const std::string &value)=0
 Adds a new value, converting it from a string. More...
 
virtual void processSetValues (ValueList *values)
 Processes values for a set after all have been converted. More...
 
virtual void processSet ()
 Performs validation and/or actions once a set of values has been added. More...
 
virtual void processAll ()
 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 clear ()
 Removes all values from the storage. More...
 
void addValue (const T &value)
 Adds a value to a temporary storage. More...
 
void commitValues ()
 Commits values added with addValue(). More...
 
virtual void refreshValues ()
 Updates alternative store locations. 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...
 
ValueListvalues ()
 Provides derived classes access to the current list of values. More...
 
const ValueListvalues () const
 Provides derived classes access to the current list of values.
 
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...
 

Static Protected Attributes

static const int DefaultValueIfSetIndex = -1
 Index used with formatValue() for formatting default value if set.
 

Constructor & Destructor Documentation

template<typename T>
template<class U >
gmx::OptionStorageTemplate< T >::OptionStorageTemplate ( const OptionTemplate< T, U > &  settings,
OptionFlags  staticFlags = OptionFlags() 
)
explicitprotected

Initializes the storage from option settings.

Parameters
[in]settingsOption settings.
[in]staticFlagsOption flags that are always set and specify generic behavior of the option.
Exceptions
APIErrorif invalid settings have been provided.
template<typename T >
gmx::OptionStorageTemplate< T >::OptionStorageTemplate ( const AbstractOption settings)
explicitprotected

Initializes the storage from base option settings.

Parameters
[in]settingsOption settings.
Exceptions
APIErrorif invalid settings have been provided.

This constructor works for cases where there is no matching OptionTemplate (e.g., EnumOption).

Member Function Documentation

template<typename T>
void gmx::OptionStorageTemplate< T >::addValue ( const T &  value)
protected

Adds a value to a temporary storage.

Parameters
[in]valueValue to add. A copy is made.
Exceptions
std::bad_allocif out of memory.
InvalidInputErrorif 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.

void gmx::AbstractOptionStorage::appendValue ( const std::string &  value)
inherited

Adds a new value for the option, converting it from a string.

Parameters
[in]valueString value to convert.
Exceptions
InvalidInputErrorif value cannot be converted, or if there are too many values.

This method should only be called between startSet() and finishSet().

template<typename T>
void gmx::OptionStorageTemplate< T >::clear ( )
inlineprotected

Removes all values from the storage.

Does not throw.

template<typename T >
void gmx::OptionStorageTemplate< T >::clearSet ( )
protectedvirtual

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.

template<typename T >
void gmx::OptionStorageTemplate< T >::commitValues ( )
protected

Commits values added with addValue().

Exceptions
std::bad_allocif 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 refreshValues() and clearSet() if it is successful.

template<typename T>
virtual void gmx::OptionStorageTemplate< T >::convertValue ( const std::string &  value)
protectedpure virtual

Adds a new value, converting it from a string.

Parameters
[in]valueString value to convert.
Exceptions
InvalidInputErrorif 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.

See Also
OptionStorageTemplate::convertValue()

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.

void gmx::AbstractOptionStorage::finish ( )
inherited

Performs validation and/or actions once all values have been added.

Exceptions
InvalidInputErrorif the option is required but not set, or if all valid values together are invalid as a set.

This method should be called after all values have been provided with appendValue().

void gmx::AbstractOptionStorage::finishSet ( )
inherited

Performs validation and/or actions once a set of values has been added.

Exceptions
InvalidInputErrorif too few values have been provided, or if the valid values since previous startSet() are invalid as a set.

If the parameter is specified multiple times, finishSet() should be called after the values for each instance.

std::string gmx::AbstractOptionStorage::formatDefaultValueIfSet ( ) const
inlineinherited

Returns the default value if set for the option as a string.

See Also
OptionTemplate::defaultValueIfSet()
virtual std::string gmx::AbstractOptionStorage::formatExtraDescription ( ) const
inlinevirtualinherited

Formats additional description for the option.

If this method returns a non-empty string, it is appended to the plain description when printing help texts. The default implementation returns an empty string.

template<typename T>
virtual std::string gmx::OptionStorageTemplate< T >::formatSingleValue ( const T &  value) const
protectedpure virtual

Formats a single value as a string.

Parameters
[in]valueValue to format.
Returns
value formatted as a string.

The derived class must provide this method to format values a strings. Called by formatValue() to do the actual formatting.

template<typename T >
std::string gmx::OptionStorageTemplate< T >::formatValue ( int  i) const
virtual

Returns the i'th value formatted as a string.

If i is DefaultValueIfSetIndex, should format the default value if set (see OptionTemplate::defaultValueIfSet()).

OptionStorageTemplate implements handling of DefaultValueIfSetIndex in this method, as well as checking that i is a valid index. Derived classes must implement formatSingleValue() to provide the actual formatting for a value of type T.

Implements gmx::AbstractOptionStorage.

bool gmx::AbstractOptionStorage::isBoolean ( ) const
inherited

Returns true if the option is a boolean option.

This is used to optionally support an alternative syntax where an option provided with no value sets the value to true and an option prefixed with "no" clears the value.

template<typename T>
virtual void gmx::OptionStorageTemplate< T >::processAll ( )
inlineprotectedvirtual

Performs validation and/or actions once all values have been added.

Exceptions
InvalidInputErrorif 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.

template<typename T >
void gmx::OptionStorageTemplate< T >::processSet ( )
protectedvirtual

Performs validation and/or actions once a set of values has been added.

Exceptions
InvalidInputErrorif 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.

Todo:
Improve the call semantics.
See Also
OptionStorageTemplate::processSetValues()

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.

template<typename T>
virtual void gmx::OptionStorageTemplate< T >::processSetValues ( ValueList values)
inlineprotectedvirtual

Processes values for a set after all have been converted.

Parameters
[in,out]valuesValid values in the set.
Exceptions
InvalidInputErrorif 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.

template<typename T >
void gmx::OptionStorageTemplate< T >::refreshValues ( )
protectedvirtual

Updates alternative store locations.

Derived classes should override this method if they implement alternative store locations, and copy/translate values from the values() vector to these alternative storages. They should also call the base class implementation as part of their implementation.

Should be called in derived classes if values are modified directly through the values() method, e.g., in processAll(). Does not need to be called if commitValues() is used.

Does not throw, and derived classes should not change that.

template<typename T>
void gmx::OptionStorageTemplate< T >::setDefaultValue ( const T &  value)
protected

Sets the default value for the option.

Parameters
[in]valueDefault value to set.
Exceptions
std::bad_allocif out of memory.

This method can be used from the derived class constructor to programmatically set a default value.

template<typename T>
void gmx::OptionStorageTemplate< T >::setDefaultValueIfSet ( const T &  value)
protected

Sets the default value if set for the option.

Parameters
[in]valueDefault value to set.
Exceptions
std::bad_allocif out of memory.

This method can be used from the derived class constructor to programmatically set a default value.

void gmx::AbstractOptionStorage::setMaxValueCount ( int  count)
protectedinherited

Sets a new maximum number of values required in one set.

Parameters
[in]countNew maximum number of values (must be > 0, or -1 for no limit).
Exceptions
InvalidInputErrorif already provided values violate the limit.

If values have already been provided, it is checked that there are not too many.

Cannot be called for options with efOption_MultipleTimes set, because it is impossible to check the requirement after the values have been set. If attempted, will assert.

void gmx::AbstractOptionStorage::setMinValueCount ( int  count)
protectedinherited

Sets a new minimum number of values required in one set.

Parameters
[in]countNew minimum number of values (must be > 0).
Exceptions
InvalidInputErrorif already provided values violate the limit.

If values have already been provided, it is checked that there are enough.

Cannot be called for options with efOption_MultipleTimes set, because it is impossible to check the requirement after the values have been set. If attempted, will assert.

void gmx::AbstractOptionStorage::startSet ( )
inherited

Starts adding a new set of values for the option.

Exceptions
InvalidInputErrorif option is specified multiple times, but is not specified to accept it.

If the parameter is specified multiple times, startSet() should be called before the values for each instance.

Strong exception safety guarantee.

void gmx::AbstractOptionStorage::startSource ( )
inherited

Starts adding values from a new source for the option.

This marks the vurrent value of the option as a default value, causing next call to startSet() to clear it. This allows values from the new source to overwrite old values.

This method does not throw.

template<typename T>
virtual int gmx::OptionStorageTemplate< T >::valueCount ( ) const
inlinevirtual

Returns the number of option values added so far.

Implements gmx::AbstractOptionStorage.

template<typename T>
ValueList& gmx::OptionStorageTemplate< T >::values ( )
inlineprotected

Provides derived classes access to the current list of values.

The non-const variant should only be used from processAll() in derived classes if necessary, and refreshValues() should be called if any changes are made.


The documentation for this class was generated from the following files: