Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / options / filenameoption.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \file
36  * \brief
37  * Declares gmx::FileNameOption and gmx::FileNameOptionInfo.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inpublicapi
41  * \ingroup module_options
42  */
43 #ifndef GMX_OPTIONS_FILENAMEOPTION_H
44 #define GMX_OPTIONS_FILENAMEOPTION_H
45
46 #include <string>
47
48 #include "abstractoption.h"
49 #include "optionfiletype.h"
50
51 namespace gmx
52 {
53
54 template <typename T> class ConstArrayRef;
55 class FileNameOptionInfo;
56 class FileNameOptionManager;
57 class FileNameOptionStorage;
58
59 /*! \brief
60  * Specifies an option that provides file names.
61  *
62  * Public methods in this class do not throw.
63  *
64  * \inpublicapi
65  * \ingroup module_options
66  */
67 class FileNameOption : public OptionTemplate<std::string, FileNameOption>
68 {
69     public:
70         //! OptionInfo subclass corresponding to this option type.
71         typedef FileNameOptionInfo InfoType;
72
73         //! Initializes an option with the given name.
74         explicit FileNameOption(const char *name)
75             : MyBase(name), filetype_(eftUnknown), legacyType_(-1),
76               defaultBasename_(NULL),
77               bRead_(false), bWrite_(false), bLibrary_(false)
78         {
79         }
80
81         /*! \brief
82          * Sets the type of the file this option accepts.
83          *
84          * Either this attribute or legacyType() must be provided.
85          */
86         MyClass &filetype(OptionFileType type)
87         { filetype_ = type; return me(); }
88         /*! \brief
89          * Sets the type of the file from an enum in filenm.h.
90          *
91          * New code should prefer filetype(), extending the enumeration if
92          * necessary.
93          */
94         MyClass &legacyType(int type)
95         { legacyType_ = type; return me(); }
96         //! Tells that the file provided by this option is used for input only.
97         MyClass &inputFile()
98         { bRead_ = true; bWrite_ = false; return me(); }
99         //! Tells that the file provided by this option is used for output only.
100         MyClass &outputFile()
101         { bRead_ = false; bWrite_ = true; return me(); }
102         /*! \brief
103          * Tells that the file provided by this option is used for input and
104          * output both.
105          */
106         MyClass &inputOutputFile()
107         { bRead_ = bWrite_ = true; return me(); }
108         /*! \brief
109          * Sets the read/write usage for this file from boolean flags.
110          */
111         MyClass &readWriteFlags(bool bRead, bool bWrite)
112         { bRead_ = bRead; bWrite_ = bWrite; return me(); }
113         /*! \brief
114          * Tells that the file will be looked up in library directories in
115          * addition to working directory.
116          *
117          * \todo
118          * Currently, this flag only affects the help output.  Callers must
119          * take care themselves to actually search the file in the library
120          * directories.  It would be nicer to do this searching within the
121          * file name option implementation.
122          */
123         MyClass &libraryFile(bool bLibrary = true)
124         { bLibrary_ = bLibrary; return me(); }
125         /*! \brief
126          * Sets a default basename for the file option.
127          *
128          * Use this method instead of defaultValue() or defaultValueIfSet() to
129          * set a default value for a file name option.  No extension needs to
130          * be provided; it is automatically added based on filetype().
131          * The behavior is also adjusted based on required(): if the option is
132          * required, the value given to defaultBasename() is treated as for
133          * both defaultValue() and defaultValueIfSet(), otherwise it is treated
134          * as for defaultValueIfSet().
135          *
136          * For input files that accept multiple extensions, the extension is
137          * completed to the default extension on creation of the option or at
138          * time of parsing an option without a value.
139          * The extension may change during Options::finish(), as this is the
140          * time when the default names are checked against the file system to
141          * provide an extension that matches an existing file if that is
142          * possible.
143          *
144          * If FileNameOptionManager is used, and
145          * FileNameOptionManager::addDefaultFileNameOption() is used, and the
146          * user provides a global default file name using that option, then the
147          * global default takes precedence over defaultBasename().
148          */
149         MyClass &defaultBasename(const char *basename)
150         { defaultBasename_ = basename; return me(); }
151
152     private:
153         // Use defaultBasename() instead.
154         using MyBase::defaultValue;
155         using MyBase::defaultValueIfSet;
156
157         //! Creates a FileNameOptionStorage object.
158         virtual AbstractOptionStoragePointer createStorage() const;
159
160         OptionFileType          filetype_;
161         int                     legacyType_;
162         const char             *defaultBasename_;
163         bool                    bRead_;
164         bool                    bWrite_;
165         bool                    bLibrary_;
166
167         /*! \brief
168          * Needed to initialize FileNameOptionStorage from this class without
169          * otherwise unnecessary accessors.
170          */
171         friend class FileNameOptionStorage;
172 };
173
174 /*! \brief
175  * Wrapper class for accessing file name option information.
176  *
177  * \inpublicapi
178  * \ingroup module_options
179  */
180 class FileNameOptionInfo : public OptionInfo
181 {
182     public:
183         //! Shorthand for a list of extensions.
184         typedef ConstArrayRef<const char *> ExtensionList;
185
186         //! Creates an option info object for the given option.
187         explicit FileNameOptionInfo(FileNameOptionStorage *option);
188
189         /*! \brief
190          * Set manager for handling interaction with other options.
191          *
192          * \param   manager  File name option manager to set.
193          *
194          * This must be called before the values are added.
195          *
196          * Typically it is called through setManagerForFileNameOptions(),
197          * which recursively sets the manager for all file name options in
198          * an Options object.
199          *
200          * Does not throw.
201          */
202         void setManager(FileNameOptionManager *manager);
203
204         //! Whether the option specifies an input file.
205         bool isInputFile() const;
206         //! Whether the option specifies an output file.
207         bool isOutputFile() const;
208         //! Whether the option specifies a file used for both input and output.
209         bool isInputOutputFile() const;
210         /*! \brief
211          * Whether the option specifies a library file.
212          *
213          * \see FileNameOption::libraryFile()
214          */
215         bool isLibraryFile() const;
216
217         //! Whether the option specifies directories.
218         bool isDirectoryOption() const;
219         //! Returns the list of extensions this option accepts.
220         ExtensionList extensions() const;
221
222     private:
223         FileNameOptionStorage &option();
224         const FileNameOptionStorage &option() const;
225 };
226
227 } // namespace gmx
228
229 #endif