Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / selection / selectionoption.cpp
index 86bdf19a2c787ffbdfab2f1f2ee3d55bedb0dfa9..f278723cc104fdc28ff49f818d4231a5cb29a201 100644 (file)
@@ -1,49 +1,53 @@
 /*
+ * This file is part of the GROMACS molecular simulation package.
  *
- *                This source code is part of
+ * Copyright (c) 2010,2011,2012,2013,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.
  */
 /*! \internal \file
  * \brief
  * Implements classes in selectionoption.h and selectionoptionstorage.h.
  *
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
  * \ingroup module_selection
  */
-#include "selectionfileoption.h"
-#include "selectionfileoptioninfo.h"
+#include "gmxpre.h"
+
 #include "selectionoption.h"
-#include "selectionoptioninfo.h"
 
 #include <string>
 
-#include "gromacs/options/optionsvisitor.h"
+#include "gromacs/options/optionmanagercontainer.h"
 #include "gromacs/selection/selection.h"
+#include "gromacs/selection/selectionfileoption.h"
 #include "gromacs/selection/selectionoptionmanager.h"
 #include "gromacs/utility/exceptions.h"
 #include "gromacs/utility/gmxassert.h"
@@ -59,28 +63,18 @@ namespace gmx
  * SelectionOptionStorage
  */
 
-SelectionOptionStorage::SelectionOptionStorage(const SelectionOption &settings)
-    : MyBase(settings, OptionFlags() | efNoDefaultValue | efDontCheckMinimumCount),
-      info_(this), manager_(NULL), selectionFlags_(settings.selectionFlags_)
+SelectionOptionStorage::SelectionOptionStorage(const SelectionOption  &settings,
+                                               SelectionOptionManager *manager)
+    : MyBase(settings, OptionFlags() | efOption_NoDefaultValue
+             | efOption_DontCheckMinimumCount),
+      info_(this), manager_(*manager), defaultText_(settings.defaultText_),
+      selectionFlags_(settings.selectionFlags_)
 {
-    GMX_RELEASE_ASSERT(!hasFlag(efMulti),
+    GMX_RELEASE_ASSERT(manager != NULL,
+                       "SelectionOptionManager must be added before SelectionOption");
+    GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
                        "allowMultiple() is not supported for selection options");
-    if (settings.infoPtr_ != NULL)
-    {
-        *settings.infoPtr_ = &info_;
-    }
-}
-
-
-void SelectionOptionStorage::setManager(SelectionOptionManager *manager)
-{
-    GMX_RELEASE_ASSERT(manager_ == NULL || manager_ == manager,
-                       "Manager cannot be changed once set");
-    if (manager_ == NULL)
-    {
-        manager->registerOption(this);
-        manager_ = manager;
-    }
+    manager_.registerOption(this);
 }
 
 
@@ -92,7 +86,7 @@ std::string SelectionOptionStorage::formatSingleValue(const Selection &value) co
 
 void SelectionOptionStorage::addSelections(
         const SelectionList &selections,
-        bool bFullValue)
+        bool                 bFullValue)
 {
     if (bFullValue && selections.size() < static_cast<size_t>(minValueCount()))
     {
@@ -107,54 +101,50 @@ void SelectionOptionStorage::addSelections(
     {
         // TODO: Having this check in the parser would make interactive input
         // behave better.
-        if (selectionFlags_.test(efOnlyStatic) && i->isDynamic())
+        if (selectionFlags_.test(efSelection_OnlyStatic) && i->isDynamic())
         {
             GMX_THROW(InvalidInputError("Dynamic selections not supported"));
         }
-        addValue(*i);
+        // Create a copy to allow modifications.
+        Selection sel(*i);
+        sel.data().setFlags(selectionFlags_);
+        addValue(sel);
     }
     if (bFullValue)
     {
         commitValues();
-        setFlag(efSet);
+        markAsSet();
     }
 }
 
 
 void SelectionOptionStorage::convertValue(const std::string &value)
 {
-    GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
-
-    manager_->convertOptionValue(this, value);
+    manager_.convertOptionValue(this, value, false);
 }
 
 void SelectionOptionStorage::processSetValues(ValueList *values)
 {
-    GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
-
     if (values->size() == 0)
     {
-        manager_->requestOptionDelayedParsing(this);
+        manager_.requestOptionDelayedParsing(this);
     }
     else if (values->size() < static_cast<size_t>(minValueCount()))
     {
         GMX_THROW(InvalidInputError("Too few (valid) values provided"));
     }
-    ValueList::iterator i;
-    for (i = values->begin(); i != values->end(); ++i)
-    {
-        i->data().setFlags(selectionFlags_);
-    }
 }
 
 void SelectionOptionStorage::processAll()
 {
+    if (!isSet() && !defaultText_.empty())
+    {
+        manager_.convertOptionValue(this, defaultText_, true);
+    }
     if (isRequired() && !isSet())
     {
-        GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
-
-        manager_->requestOptionDelayedParsing(this);
-        setFlag(efSet);
+        manager_.requestOptionDelayedParsing(this);
+        markAsSet();
     }
 }
 
@@ -165,7 +155,7 @@ void SelectionOptionStorage::setAllowedValueCount(int count)
     errors.startContext("In option '" + name() + "'");
     if (count >= 0)
     {
-        // Should not throw because efDontCheckMinimumCount is set
+        // Should not throw because efOption_DontCheckMinimumCount is set.
         setMinValueCount(count);
         if (valueCount() > 0 && valueCount() < count)
         {
@@ -192,7 +182,7 @@ void SelectionOptionStorage::setSelectionFlag(SelectionFlag flag, bool bSet)
     ValueList::iterator i;
     for (i = values().begin(); i != values().end(); ++i)
     {
-        if (flag == efOnlyStatic && bSet && i->isDynamic())
+        if (flag == efSelection_OnlyStatic && bSet && i->isDynamic())
         {
             MessageStringCollector errors;
             errors.startContext("In option '" + name() + "'");
@@ -228,11 +218,6 @@ const SelectionOptionStorage &SelectionOptionInfo::option() const
     return static_cast<const SelectionOptionStorage &>(OptionInfo::option());
 }
 
-void SelectionOptionInfo::setManager(SelectionOptionManager *manager)
-{
-    option().setManager(manager);
-}
-
 void SelectionOptionInfo::setValueCount(int count)
 {
     option().setAllowedValueCount(count);
@@ -240,32 +225,27 @@ void SelectionOptionInfo::setValueCount(int count)
 
 void SelectionOptionInfo::setEvaluateVelocities(bool bEnabled)
 {
-    option().setSelectionFlag(efEvaluateVelocities, bEnabled);
+    option().setSelectionFlag(efSelection_EvaluateVelocities, bEnabled);
 }
 
 void SelectionOptionInfo::setEvaluateForces(bool bEnabled)
 {
-    option().setSelectionFlag(efEvaluateForces, bEnabled);
+    option().setSelectionFlag(efSelection_EvaluateForces, bEnabled);
 }
 
 void SelectionOptionInfo::setOnlyAtoms(bool bEnabled)
 {
-    option().setSelectionFlag(efOnlyAtoms, bEnabled);
+    option().setSelectionFlag(efSelection_OnlyAtoms, bEnabled);
 }
 
 void SelectionOptionInfo::setOnlyStatic(bool bEnabled)
 {
-    option().setSelectionFlag(efOnlyStatic, bEnabled);
+    option().setSelectionFlag(efSelection_OnlyStatic, bEnabled);
 }
 
 void SelectionOptionInfo::setDynamicMask(bool bEnabled)
 {
-    option().setSelectionFlag(efDynamicMask, bEnabled);
-}
-
-void SelectionOptionInfo::setDynamicOnlyWhole(bool bEnabled)
-{
-    option().setSelectionFlag(efDynamicOnlyWhole, bEnabled);
+    option().setSelectionFlag(efSelection_DynamicMask, bEnabled);
 }
 
 
@@ -273,9 +253,11 @@ void SelectionOptionInfo::setDynamicOnlyWhole(bool bEnabled)
  * SelectionOption
  */
 
-AbstractOptionStoragePointer SelectionOption::createStorage() const
+AbstractOptionStorage *
+SelectionOption::createStorage(const OptionManagerContainer &managers) const
 {
-    return AbstractOptionStoragePointer(new SelectionOptionStorage(*this));
+    return new SelectionOptionStorage(
+            *this, managers.get<SelectionOptionManager>());
 }
 
 
@@ -283,10 +265,14 @@ AbstractOptionStoragePointer SelectionOption::createStorage() const
  * SelectionFileOptionStorage
  */
 
-SelectionFileOptionStorage::SelectionFileOptionStorage(const SelectionFileOption &settings)
-    : AbstractOptionStorage(settings, OptionFlags() | efMulti | efDontCheckMinimumCount),
-      info_(this), manager_(NULL), bValueParsed_(false)
+SelectionFileOptionStorage::SelectionFileOptionStorage(
+        const SelectionFileOption &settings, SelectionOptionManager *manager)
+    : AbstractOptionStorage(settings, OptionFlags() | efOption_MultipleTimes
+                            | efOption_DontCheckMinimumCount),
+      info_(this), manager_(*manager), bValueParsed_(false)
 {
+    GMX_RELEASE_ASSERT(manager != NULL,
+                       "SelectionOptionManager must be added before SelectionFileOption");
 }
 
 void SelectionFileOptionStorage::clearSet()
@@ -296,15 +282,13 @@ void SelectionFileOptionStorage::clearSet()
 
 void SelectionFileOptionStorage::convertValue(const std::string &value)
 {
-    GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
-
     if (bValueParsed_)
     {
         GMX_THROW(InvalidInputError("More than one file name provided"));
     }
     bValueParsed_ = true;
     // TODO: Should we throw an InvalidInputError if the file does not exist?
-    manager_->parseRequestedFromFile(value);
+    manager_.parseRequestedFromFile(value);
 }
 
 void SelectionFileOptionStorage::processSet()
@@ -325,21 +309,6 @@ SelectionFileOptionInfo::SelectionFileOptionInfo(SelectionFileOptionStorage *opt
 {
 }
 
-SelectionFileOptionStorage &SelectionFileOptionInfo::option()
-{
-    return static_cast<SelectionFileOptionStorage &>(OptionInfo::option());
-}
-
-const SelectionFileOptionStorage &SelectionFileOptionInfo::option() const
-{
-    return static_cast<const SelectionFileOptionStorage &>(OptionInfo::option());
-}
-
-void SelectionFileOptionInfo::setManager(SelectionOptionManager *manager)
-{
-    option().setManager(manager);
-}
-
 
 /********************************************************************
  * SelectionFileOption
@@ -351,66 +320,11 @@ SelectionFileOption::SelectionFileOption(const char *name)
     setDescription("Provide selections from files");
 }
 
-AbstractOptionStoragePointer SelectionFileOption::createStorage() const
-{
-    return AbstractOptionStoragePointer(new SelectionFileOptionStorage(*this));
-}
-
-
-/********************************************************************
- * Global functions
- */
-
-namespace
-{
-
-/*! \internal \brief
- * Visitor that sets the manager for each selection option.
- *
- * \ingroup module_selection
- */
-class SelectionOptionManagerSetter : public OptionsModifyingVisitor
-{
-    public:
-        //! Construct a visitor that sets given manager.
-        explicit SelectionOptionManagerSetter(SelectionOptionManager *manager)
-            : manager_(manager)
-        {
-        }
-
-        void visitSubSection(Options *section)
-        {
-            OptionsModifyingIterator iterator(section);
-            iterator.acceptSubSections(this);
-            iterator.acceptOptions(this);
-        }
-
-        void visitOption(OptionInfo *option)
-        {
-            SelectionOptionInfo *selOption
-                = option->toType<SelectionOptionInfo>();
-            if (selOption != NULL)
-            {
-                selOption->setManager(manager_);
-            }
-            SelectionFileOptionInfo *selFileOption
-                = option->toType<SelectionFileOptionInfo>();
-            if (selFileOption != NULL)
-            {
-                selFileOption->setManager(manager_);
-            }
-        }
-
-    private:
-        SelectionOptionManager *manager_;
-};
-
-} // namespace
-
-void setManagerForSelectionOptions(Options *options,
-                                   SelectionOptionManager *manager)
+AbstractOptionStorage *
+SelectionFileOption::createStorage(const OptionManagerContainer &managers) const
 {
-    SelectionOptionManagerSetter(manager).visitSubSection(options);
+    return new SelectionFileOptionStorage(
+            *this, managers.get<SelectionOptionManager>());
 }
 
 } // namespace gmx