Support custom default extension for FileNameOption
[alexxy/gromacs.git] / src / gromacs / options / filenameoption.h
index c00e71254d4120e357d140acdd7f55e06c20ecfa..479a35834acb5e878f86fd8d53de7d8a4f075d92 100644 (file)
@@ -44,6 +44,7 @@
 #define GMX_OPTIONS_FILENAMEOPTION_H
 
 #include <string>
+#include <vector>
 
 #include "abstractoption.h"
 #include "optionfiletype.h"
@@ -72,8 +73,9 @@ class FileNameOption : public OptionTemplate<std::string, FileNameOption>
 
         //! Initializes an option with the given name.
         explicit FileNameOption(const char *name)
-            : MyBase(name), filetype_(eftUnknown), legacyType_(-1),
-              defaultBasename_(NULL),
+            : MyBase(name), optionType_(eftUnknown), legacyType_(-1),
+              defaultBasename_(NULL), defaultType_(-1),
+              bLegacyOptionalBehavior_(false),
               bRead_(false), bWrite_(false), bLibrary_(false)
         {
         }
@@ -84,7 +86,7 @@ class FileNameOption : public OptionTemplate<std::string, FileNameOption>
          * Either this attribute or legacyType() must be provided.
          */
         MyClass &filetype(OptionFileType type)
-        { filetype_ = type; return me(); }
+        { optionType_ = type; return me(); }
         /*! \brief
          * Sets the type of the file from an enum in filenm.h.
          *
@@ -93,6 +95,16 @@ class FileNameOption : public OptionTemplate<std::string, FileNameOption>
          */
         MyClass &legacyType(int type)
         { legacyType_ = type; return me(); }
+        /*! \brief
+         * Changes the behavior of optional options to match old t_filenm.
+         *
+         * If this is not set, optional options return an empty string if not
+         * set.  If this is set, a non-empty value is always returned.
+         * In the latter case, whether the option is set only affects the
+         * return value of OptionInfo::isSet() and Options::isSet().
+         */
+        MyClass &legacyOptionalBehavior()
+        { bLegacyOptionalBehavior_ = true; return me(); }
         //! Tells that the file provided by this option is used for input only.
         MyClass &inputFile()
         { bRead_ = true; bWrite_ = false; return me(); }
@@ -127,7 +139,8 @@ class FileNameOption : public OptionTemplate<std::string, FileNameOption>
          *
          * Use this method instead of defaultValue() or defaultValueIfSet() to
          * set a default value for a file name option.  No extension needs to
-         * be provided; it is automatically added based on filetype().
+         * be provided; it is automatically added based on filetype() or
+         * defaultType().
          * The behavior is also adjusted based on required(): if the option is
          * required, the value given to defaultBasename() is treated as for
          * both defaultValue() and defaultValueIfSet(), otherwise it is treated
@@ -136,10 +149,11 @@ class FileNameOption : public OptionTemplate<std::string, FileNameOption>
          * For input files that accept multiple extensions, the extension is
          * completed to the default extension on creation of the option or at
          * time of parsing an option without a value.
-         * The extension may change during Options::finish(), as this is the
-         * time when the default names are checked against the file system to
-         * provide an extension that matches an existing file if that is
-         * possible.
+         *
+         * If FileNameOptionManager is used, the extension may change during
+         * Options::finish(), as this is the time when the default names are
+         * checked against the file system to provide an extension that matches
+         * an existing file if that is possible.
          *
          * If FileNameOptionManager is used, and
          * FileNameOptionManager::addDefaultFileNameOption() is used, and the
@@ -148,6 +162,20 @@ class FileNameOption : public OptionTemplate<std::string, FileNameOption>
          */
         MyClass &defaultBasename(const char *basename)
         { defaultBasename_ = basename; return me(); }
+        /*! \brief
+         * Sets a default type/extension for the file option.
+         *
+         * For options that accept multiple types of files (e.g.,
+         * eftTrajectory), this method sets the default extension used
+         * for completing defaultBasename(), as well as the default extension
+         * used by FileNameOptionManager to complete various file names.
+         *
+         * The value should be one of the enumerated `ef*` values from
+         * filenm.h, and be a valid type for the type specified with
+         * filetype().
+         */
+        MyClass &defaultType(int filetype)
+        { defaultType_ = filetype; return me(); }
 
     private:
         // Use defaultBasename() instead.
@@ -155,11 +183,14 @@ class FileNameOption : public OptionTemplate<std::string, FileNameOption>
         using MyBase::defaultValueIfSet;
 
         //! Creates a FileNameOptionStorage object.
-        virtual AbstractOptionStoragePointer createStorage() const;
+        virtual AbstractOptionStorage *createStorage(
+            const OptionManagerContainer &managers) const;
 
-        OptionFileType          filetype_;
+        OptionFileType          optionType_;
         int                     legacyType_;
         const char             *defaultBasename_;
+        int                     defaultType_;
+        bool                    bLegacyOptionalBehavior_;
         bool                    bRead_;
         bool                    bWrite_;
         bool                    bLibrary_;
@@ -181,26 +212,11 @@ class FileNameOptionInfo : public OptionInfo
 {
     public:
         //! Shorthand for a list of extensions.
-        typedef ConstArrayRef<const char *> ExtensionList;
+        typedef std::vector<const char *> ExtensionList;
 
         //! Creates an option info object for the given option.
         explicit FileNameOptionInfo(FileNameOptionStorage *option);
 
-        /*! \brief
-         * Set manager for handling interaction with other options.
-         *
-         * \param   manager  File name option manager to set.
-         *
-         * This must be called before the values are added.
-         *
-         * Typically it is called through setManagerForFileNameOptions(),
-         * which recursively sets the manager for all file name options in
-         * an Options object.
-         *
-         * Does not throw.
-         */
-        void setManager(FileNameOptionManager *manager);
-
         //! Whether the option specifies an input file.
         bool isInputFile() const;
         //! Whether the option specifies an output file.
@@ -216,11 +232,18 @@ class FileNameOptionInfo : public OptionInfo
 
         //! Whether the option specifies directories.
         bool isDirectoryOption() const;
+        //! Whether the option specifies a generic trajectory file.
+        bool isTrajectoryOption() const;
+        //! Returns the default extension for this option.
+        const char *defaultExtension() const;
         //! Returns the list of extensions this option accepts.
         ExtensionList extensions() const;
+        //! Returns whether \p fileType (from filenm.h) is accepted for this option.
+        bool isValidType(int fileType) const;
+        //! Returns the list of file types this option accepts.
+        ConstArrayRef<int> fileTypes() const;
 
     private:
-        FileNameOptionStorage &option();
         const FileNameOptionStorage &option() const;
 };