/*
+ * This file is part of the GROMACS molecular simulation package.
*
- * This source code is part of
+ * Copyright (c) 2010,2011,2012,2014, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
*
- * G R O M A C S
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
*
- * GROningen MAchine for Chemical Simulations
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
*
- * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
- * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2009, The GROMACS development team,
- * check out http://www.gromacs.org for more information.
-
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
- * If you want to redistribute modifications, please consider that
- * scientific software is very special. Version control is crucial -
- * bugs must be traceable. We will be happy to consider code for
- * inclusion in the official distribution, but derived work must not
- * be called official GROMACS. Details are found in the README & COPYING
- * files - if they are missing, get the official version at www.gromacs.org.
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
*
* To help us fund GROMACS development, we humbly ask that you cite
- * the papers on the package - you can find them in the top README file.
- *
- * For more info, check our website at http://www.gromacs.org
+ * the research papers on the package. Check out http://www.gromacs.org.
*/
/*! \libinternal \file
* \brief
* Declares gmx::OptionsVisitor interface and supporting classes.
*
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
* \inlibraryapi
* \ingroup module_options
*/
#ifndef GMX_OPTIONS_OPTIONSVISITOR_H
#define GMX_OPTIONS_OPTIONSVISITOR_H
+#include <cstddef>
+
#include <string>
+#include "gromacs/options/abstractoption.h"
+#include "gromacs/utility/common.h"
+
namespace gmx
{
-class AbstractOptionStorage;
class Options;
/*! \libinternal \brief
- * Wrapper class for accessing option information.
+ * Pure interface for visiting options in a Options object.
*
- * This class isolates the details of the internal option implementation
- * from option visitors.
+ * \see OptionsIterator
*
+ * \inlibraryapi
+ * \ingroup module_options
+ */
+class OptionsVisitor
+{
+ public:
+ virtual ~OptionsVisitor() {}
+
+ /*! \brief
+ * Called for each subsection in Options.
+ */
+ virtual void visitSubSection(const Options §ion) = 0;
+ /*! \brief
+ * Called for each option in Options.
+ */
+ virtual void visitOption(const OptionInfo &option) = 0;
+};
+
+/*! \libinternal \brief
+ * Abstract base class for visiting options of a particular type.
+ *
+ * \see OptionsIterator
* \see OptionsVisitor
*
* \inlibraryapi
* \ingroup module_options
*/
-class OptionInfo
+template <class InfoType>
+class OptionsTypeVisitor : public OptionsVisitor
{
public:
+ virtual ~OptionsTypeVisitor() {}
+
+ virtual void visitSubSection(const Options §ion) = 0;
/*! \brief
- * Wraps a given option object.
+ * Called for each option of type \p InfoType.
*/
- OptionInfo(const AbstractOptionStorage &option);
-
- //! Returns true if the option is a boolean option.
- bool isBoolean() const;
- //! Returns true if the option is a file name option.
- bool isFile() const;
- //! Returns true if the option is a hidden option.
- bool isHidden() const;
- //! Returns the name of the option.
- const std::string &name() const;
- //! Returns the description of the option.
- const std::string &description() const;
- //! Returns the type of the option as a string.
- const char *type() const;
- //! Returns the number of values given for the option.
- int valueCount() const;
- //! Returns the i'th value of the option as a string.
- std::string formatValue(int i) const;
- //! Returns all the values of the option as a single string.
- std::string formatValues() const;
+ virtual void visitOptionType(const InfoType &option) = 0;
private:
- //! The wrapped option.
- const AbstractOptionStorage &_option;
+ virtual void visitOption(const OptionInfo &option)
+ {
+ const InfoType *subtype = option.toType<InfoType>();
+ if (subtype != NULL)
+ {
+ visitOptionType(*subtype);
+ }
+ }
+};
+
+/*! \libinternal \brief
+ * Decorator class for visiting options in a Options object.
+ *
+ * This class provides an interface for looping through subsections and
+ * options in a Options object.
+ *
+ * Typical use (loop over all options, iteratively descending into
+ * subsections):
+ * \code
+ class Visitor : public gmx::OptionsVisitor
+ {
+ public:
+ void visitSubSection(const Options §ion)
+ {
+ OptionsIterator iterator(section);
+ iterator.acceptSubSections(this);
+ iterator.acceptOptions(this);
+ }
+
+ void visitOption(const OptionInfo &option)
+ {
+ // Do something.
+ }
+ }
+
+ Visitor().visitSubSection(options);
+ * \endcode
+ *
+ * \inlibraryapi
+ * \ingroup module_options
+ */
+class OptionsIterator
+{
+ public:
+ /*! \brief
+ * Creates an object for visiting options in a Options object.
+ */
+ explicit OptionsIterator(const Options &options);
- // Disallow copy and assign.
- OptionInfo(const OptionInfo &);
- void operator =(const OptionInfo &);
+ /*! \brief
+ * Visits each subsection in the wrapped Options object.
+ */
+ void acceptSubSections(OptionsVisitor *visitor) const;
+ /*! \brief
+ * Visits each option in the wrapped Options object.
+ */
+ void acceptOptions(OptionsVisitor *visitor) const;
+
+ private:
+ //! The wrapped Options object.
+ const Options &options_;
- // This is a workaround for a buggy gcc 4.2, which seems to
- // require access to all constructors:
- friend class OptionsIterator;
+ GMX_DISALLOW_COPY_AND_ASSIGN(OptionsIterator);
};
/*! \libinternal \brief
- * Pure interface for visiting options in a Options object.
+ * Pure interface for visiting options in a Options object, allowing
+ * modifications.
*
- * \see OptionsIterator
+ * \see OptionsModifyingIterator
*
* \inlibraryapi
* \ingroup module_options
*/
-class OptionsVisitor
+class OptionsModifyingVisitor
{
public:
- virtual ~OptionsVisitor() {}
+ virtual ~OptionsModifyingVisitor() {}
/*! \brief
* Called for each subsection in Options.
*/
- virtual void visitSubSection(const Options §ion) = 0;
+ virtual void visitSubSection(Options *section) = 0;
/*! \brief
* Called for each option in Options.
*/
- virtual void visitOption(const OptionInfo &option) = 0;
+ virtual void visitOption(OptionInfo *option) = 0;
};
/*! \libinternal \brief
- * Decorator class for visiting options in a Options object.
+ * Abstract base class for visiting options of a particular type, allowing
+ * modifications.
*
- * This class provides an interface for looping through subsections and
- * options in a Options object.
+ * \see OptionsModifyingIterator
+ * \see OptionsModifyingVisitor
*
- * Typical use (loop over all options, iteratively descending into
- * subsections):
- * \code
-class Visitor : public gmx::options::OptionsVisitor
+ * \inlibraryapi
+ * \ingroup module_options
+ */
+template <class InfoType>
+class OptionsModifyingTypeVisitor : public OptionsModifyingVisitor
{
public:
- void visitSubSection(const Options §ion)
- {
- OptionsIterator iterator(section);
- iterator.acceptSubSections(this);
- iterator.acceptOptions(this);
- }
+ virtual ~OptionsModifyingTypeVisitor() {}
+
+ virtual void visitSubSection(Options *section) = 0;
+ /*! \brief
+ * Called for each option of type \p InfoType.
+ */
+ virtual void visitOptionType(InfoType *option) = 0;
- void visitOption(const OptionInfo &option)
+ private:
+ virtual void visitOption(OptionInfo *option)
{
- // Do something.
+ InfoType *subtype = option->toType<InfoType>();
+ if (subtype != NULL)
+ {
+ visitOptionType(subtype);
+ }
}
-}
+};
-Visitor().visitSubSection(options);
- * \endcode
+/*! \libinternal \brief
+ * Decorator class for visiting options in a Options object, allowing changes.
+ *
+ * This class works exactly like OptionsIterator, except that it uses
+ * OptionsModifyingVisitor interface, which allows modifying the options.
+ *
+ * \see OptionsIterator
*
* \inlibraryapi
* \ingroup module_options
*/
-class OptionsIterator
+class OptionsModifyingIterator
{
public:
/*! \brief
* Creates an object for visiting options in a Options object.
*/
- OptionsIterator(const Options &options);
+ explicit OptionsModifyingIterator(Options *options);
/*! \brief
* Visits each subsection in the wrapped Options object.
*/
- void acceptSubSections(OptionsVisitor *visitor) const;
+ void acceptSubSections(OptionsModifyingVisitor *visitor) const;
/*! \brief
* Visits each option in the wrapped Options object.
*/
- void acceptOptions(OptionsVisitor *visitor) const;
+ void acceptOptions(OptionsModifyingVisitor *visitor) const;
private:
//! The wrapped Options object.
- const Options &_options;
+ Options &options_;
- // Disallow copy and assign.
- OptionsIterator(const OptionsIterator &);
- void operator =(const OptionsIterator &);
+ GMX_DISALLOW_COPY_AND_ASSIGN(OptionsModifyingIterator);
};
} // namespace gmx