Make ConstArrayRef alias to ArrayRef<const T>
[gromacs.git] / src / gromacs / options / filenameoption.h
blobc245326e6147d65e86d2ea24261c2b9fe69be3dc
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2017, 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.
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.
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.
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.
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.
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.
35 /*! \file
36 * \brief
37 * Declares gmx::FileNameOption and gmx::FileNameOptionInfo.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \inpublicapi
41 * \ingroup module_options
43 #ifndef GMX_OPTIONS_FILENAMEOPTION_H
44 #define GMX_OPTIONS_FILENAMEOPTION_H
46 #include <string>
47 #include <vector>
49 #include "gromacs/options/abstractoption.h"
50 #include "gromacs/options/optionfiletype.h"
52 namespace gmx
55 template <typename T> class ArrayRef;
56 class FileNameOptionInfo;
57 class FileNameOptionManager;
58 class FileNameOptionStorage;
60 /*! \brief
61 * Specifies an option that provides file names.
63 * Public methods in this class do not throw.
65 * \inpublicapi
66 * \ingroup module_options
68 class FileNameOption : public OptionTemplate<std::string, FileNameOption>
70 public:
71 //! OptionInfo subclass corresponding to this option type.
72 typedef FileNameOptionInfo InfoType;
74 //! Initializes an option with the given name.
75 explicit FileNameOption(const char *name)
76 : MyBase(name), optionType_(eftUnknown), legacyType_(-1),
77 defaultBasename_(nullptr), defaultType_(-1),
78 bLegacyOptionalBehavior_(false),
79 bRead_(false), bWrite_(false), bLibrary_(false),
80 bAllowMissing_(false)
84 /*! \brief
85 * Sets the type of the file this option accepts.
87 * Either this attribute or legacyType() must be provided.
89 MyClass &filetype(OptionFileType type)
90 { optionType_ = type; return me(); }
91 /*! \brief
92 * Sets the type of the file from an enum in filetypes.h.
94 * New code should prefer filetype(), extending the enumeration if
95 * necessary.
97 MyClass &legacyType(int type)
98 { legacyType_ = type; return me(); }
99 /*! \brief
100 * Changes the behavior of optional options to match old t_filenm.
102 * If this is not set, optional options return an empty string if not
103 * set. If this is set, a non-empty value is always returned.
104 * In the latter case, whether the option is set only affects the
105 * return value of OptionInfo::isSet() and Options::isSet().
107 MyClass &legacyOptionalBehavior()
108 { bLegacyOptionalBehavior_ = true; return me(); }
109 //! Tells that the file provided by this option is used for input only.
110 MyClass &inputFile()
111 { bRead_ = true; bWrite_ = false; return me(); }
112 //! Tells that the file provided by this option is used for output only.
113 MyClass &outputFile()
114 { bRead_ = false; bWrite_ = true; return me(); }
115 /*! \brief
116 * Tells that the file provided by this option is used for input and
117 * output both.
119 MyClass &inputOutputFile()
120 { bRead_ = bWrite_ = true; return me(); }
121 /*! \brief
122 * Sets the read/write usage for this file from boolean flags.
124 MyClass &readWriteFlags(bool bRead, bool bWrite)
125 { bRead_ = bRead; bWrite_ = bWrite; return me(); }
126 /*! \brief
127 * Tells that the file will be looked up in library directories in
128 * addition to working directory.
130 * \todo
131 * Currently, this flag only affects the help output. Callers must
132 * take care themselves to actually search the file in the library
133 * directories. It would be nicer to do this searching within the
134 * file name option implementation.
136 MyClass &libraryFile(bool bLibrary = true)
137 { bLibrary_ = bLibrary; return me(); }
138 /*! \brief
139 * Tells that missing file names explicitly provided by the user are
140 * valid for this input option.
142 * If this method is not called, an error will be raised if the user
143 * explicitly provides a file name that does not name an existing file,
144 * or if the default value does not resolve to a valid file name for a
145 * required option that the user has not set.
147 * This method only has effect with input files, and only if a
148 * FileNameOptionManager is being used.
150 MyClass &allowMissing(bool bAllow = true)
151 { bAllowMissing_ = bAllow; return me(); }
152 /*! \brief
153 * Sets a default basename for the file option.
155 * Use this method instead of defaultValue() or defaultValueIfSet() to
156 * set a default value for a file name option. No extension needs to
157 * be provided; it is automatically added based on filetype() or
158 * defaultType().
159 * The behavior is also adjusted based on required(): if the option is
160 * required, the value given to defaultBasename() is treated as for
161 * both defaultValue() and defaultValueIfSet(), otherwise it is treated
162 * as for defaultValueIfSet().
164 * For input files that accept multiple extensions, the extension is
165 * completed to the default extension on creation of the option or at
166 * time of parsing an option without a value.
168 * If FileNameOptionManager is used, the extension may change during
169 * Options::finish(), as this is the time when the default names are
170 * checked against the file system to provide an extension that matches
171 * an existing file if that is possible.
173 * If FileNameOptionManager is used, and
174 * FileNameOptionManager::addDefaultFileNameOption() is used, and the
175 * user provides a global default file name using that option, then the
176 * global default takes precedence over defaultBasename().
178 MyClass &defaultBasename(const char *basename)
179 { defaultBasename_ = basename; return me(); }
180 /*! \brief
181 * Sets a default type/extension for the file option.
183 * For options that accept multiple types of files (e.g.,
184 * eftTrajectory), this method sets the default extension used
185 * for completing defaultBasename(), as well as the default extension
186 * used by FileNameOptionManager to complete various file names.
188 * The value should be one of the enumerated `ef*` values from
189 * filetypes.h, and be a valid type for the type specified with
190 * filetype().
192 MyClass &defaultType(int filetype)
193 { defaultType_ = filetype; return me(); }
195 private:
196 // Use defaultBasename() instead.
197 using MyBase::defaultValue;
198 using MyBase::defaultValueIfSet;
200 //! Creates a FileNameOptionStorage object.
201 virtual AbstractOptionStorage *createStorage(
202 const OptionManagerContainer &managers) const;
204 OptionFileType optionType_;
205 int legacyType_;
206 const char *defaultBasename_;
207 int defaultType_;
208 bool bLegacyOptionalBehavior_;
209 bool bRead_;
210 bool bWrite_;
211 bool bLibrary_;
212 bool bAllowMissing_;
214 /*! \brief
215 * Needed to initialize FileNameOptionStorage from this class without
216 * otherwise unnecessary accessors.
218 friend class FileNameOptionStorage;
221 /*! \brief
222 * Wrapper class for accessing file name option information.
224 * \inpublicapi
225 * \ingroup module_options
227 class FileNameOptionInfo : public OptionInfo
229 public:
230 //! Shorthand for a list of extensions.
231 typedef std::vector<const char *> ExtensionList;
233 //! Creates an option info object for the given option.
234 explicit FileNameOptionInfo(FileNameOptionStorage *option);
236 //! Whether the option specifies an input file.
237 bool isInputFile() const;
238 //! Whether the option specifies an output file.
239 bool isOutputFile() const;
240 //! Whether the option specifies a file used for both input and output.
241 bool isInputOutputFile() const;
242 /*! \brief
243 * Whether the option specifies a library file.
245 * \see FileNameOption::libraryFile()
247 bool isLibraryFile() const;
248 //! Whether the (input) option allows missing files to be provided.
249 bool allowMissing() const;
251 //! Whether the option specifies directories.
252 bool isDirectoryOption() const;
253 //! Whether the option specifies a generic trajectory file.
254 bool isTrajectoryOption() const;
255 //! Returns the default extension for this option.
256 const char *defaultExtension() const;
257 //! Returns the list of extensions this option accepts.
258 ExtensionList extensions() const;
259 //! Returns whether \p fileType (from filetypes.h) is accepted for this option.
260 bool isValidType(int fileType) const;
261 //! Returns the list of file types this option accepts.
262 ArrayRef<const int> fileTypes() const;
264 private:
265 const FileNameOptionStorage &option() const;
268 } // namespace gmx
270 #endif