Merge branch 'release-4-6'
[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, 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 class FileNameOptionInfo;
55 class FileNameOptionStorage;
56
57 /*! \brief
58  * Specifies an option that provides file names.
59  *
60  * Public methods in this class do not throw.
61  *
62  * \inpublicapi
63  * \ingroup module_options
64  */
65 class FileNameOption : public OptionTemplate<std::string, FileNameOption>
66 {
67     public:
68         //! OptionInfo subclass corresponding to this option type.
69         typedef FileNameOptionInfo InfoType;
70
71         //! Initializes an option with the given name.
72         explicit FileNameOption(const char *name)
73             : MyBase(name), filetype_(eftUnknown), defaultBasename_(NULL),
74               bRead_(false), bWrite_(false), bLibrary_(false)
75         {
76         }
77
78         /*! \brief
79          * Sets the type of the file this option accepts.
80          *
81          * This attribute must be provided.
82          */
83         MyClass &filetype(OptionFileType type)
84         { filetype_ = type; return me(); }
85         //! Tells that the file provided by this option is used for input only.
86         MyClass &inputFile()
87         { bRead_ = true; bWrite_ = false; return me(); }
88         //! Tells that the file provided by this option is used for output only.
89         MyClass &outputFile()
90         { bRead_ = false; bWrite_ = true; return me(); }
91         /*! \brief
92          * Tells that the file provided by this option is used for input and
93          * output both.
94          */
95         MyClass &inputOutputFile()
96         { bRead_ = bWrite_ = true; return me(); }
97         /*! \brief
98          * Tells that the file will be looked up in library directories in
99          * addition to working directory.
100          *
101          * \todo
102          * Currently, this flag only affects the help output.  Callers must
103          * take care themselves to actually search the file in the library
104          * directories.  It would be nicer to do this searching within the
105          * file name option implementation.
106          */
107         MyClass &libraryFile() { bLibrary_ = true; return me(); }
108         /*! \brief
109          * Sets a default basename for the file option.
110          *
111          * Use this method instead of defaultValue() or defaultValueIfSet() to
112          * set a default value for a file name option.  No extension needs to
113          * be provided; it is automatically added based on filetype().
114          * The behavior is also adjusted based on required(): if the option is
115          * required, the value given to defaultBasename() is treated as for
116          * defaultValue(), otherwise it is treated as for defaultValueIfSet().
117          */
118         MyClass &defaultBasename(const char *basename)
119         { defaultBasename_ = basename; return me(); }
120
121     private:
122         // Use defaultBasename() instead.
123         using MyBase::defaultValue;
124         using MyBase::defaultValueIfSet;
125
126         //! Creates a FileNameOptionStorage object.
127         virtual AbstractOptionStoragePointer createStorage() const;
128
129         OptionFileType          filetype_;
130         const char             *defaultBasename_;
131         bool                    bRead_;
132         bool                    bWrite_;
133         bool                    bLibrary_;
134
135         /*! \brief
136          * Needed to initialize FileNameOptionStorage from this class without
137          * otherwise unnecessary accessors.
138          */
139         friend class FileNameOptionStorage;
140 };
141
142 /*! \brief
143  * Wrapper class for accessing file name option information.
144  *
145  * \inpublicapi
146  * \ingroup module_options
147  */
148 class FileNameOptionInfo : public OptionInfo
149 {
150     public:
151         //! Creates an option info object for the given option.
152         explicit FileNameOptionInfo(FileNameOptionStorage *option);
153
154         //! Whether the option specifies an input file.
155         bool isInputFile() const;
156         //! Whether the option specifies an output file.
157         bool isOutputFile() const;
158         //! Whether the option specifies a file used for both input and output.
159         bool isInputOutputFile() const;
160         /*! \brief
161          * Whether the option specifies a library file.
162          *
163          * \see FileNameOption::libraryFile()
164          */
165         bool isLibraryFile() const;
166
167     private:
168         const FileNameOptionStorage &option() const;
169 };
170
171 } // namespace gmx
172
173 #endif