Merge branch 'release-4-6'
[alexxy/gromacs.git] / src / gromacs / options / filenameoption.h
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2009, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \file
32  * \brief
33  * Declares gmx::FileNameOption and gmx::FileNameOptionInfo.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \inpublicapi
37  * \ingroup module_options
38  */
39 #ifndef GMX_OPTIONS_FILENAMEOPTION_H
40 #define GMX_OPTIONS_FILENAMEOPTION_H
41
42 #include <string>
43
44 #include "abstractoption.h"
45 #include "optionfiletype.h"
46
47 namespace gmx
48 {
49
50 class FileNameOptionInfo;
51 class FileNameOptionStorage;
52
53 /*! \brief
54  * Specifies an option that provides file names.
55  *
56  * Public methods in this class do not throw.
57  *
58  * \inpublicapi
59  * \ingroup module_options
60  */
61 class FileNameOption : public OptionTemplate<std::string, FileNameOption>
62 {
63     public:
64         //! OptionInfo subclass corresponding to this option type.
65         typedef FileNameOptionInfo InfoType;
66
67         //! Initializes an option with the given name.
68         explicit FileNameOption(const char *name)
69             : MyBase(name), filetype_(eftUnknown), defaultBasename_(NULL),
70               bRead_(false), bWrite_(false), bLibrary_(false)
71         {
72         }
73
74         /*! \brief
75          * Sets the type of the file this option accepts.
76          *
77          * This attribute must be provided.
78          */
79         MyClass &filetype(OptionFileType type)
80         { filetype_ = type; return me(); }
81         //! Tells that the file provided by this option is used for input only.
82         MyClass &inputFile()
83         { bRead_ = true; bWrite_ = false; return me(); }
84         //! Tells that the file provided by this option is used for output only.
85         MyClass &outputFile()
86         { bRead_ = false; bWrite_ = true; return me(); }
87         /*! \brief
88          * Tells that the file provided by this option is used for input and
89          * output both.
90          */
91         MyClass &inputOutputFile()
92         { bRead_ = bWrite_ = true; return me(); }
93         /*! \brief
94          * Tells that the file will be looked up in library directories in
95          * addition to working directory.
96          *
97          * \todo
98          * Currently, this flag only affects the help output.  Callers must
99          * take care themselves to actually search the file in the library
100          * directories.  It would be nicer to do this searching within the
101          * file name option implementation.
102          */
103         MyClass &libraryFile() { bLibrary_ = true; return me(); }
104         /*! \brief
105          * Sets a default basename for the file option.
106          *
107          * Use this method instead of defaultValue() or defaultValueIfSet() to
108          * set a default value for a file name option.  No extension needs to
109          * be provided; it is automatically added based on filetype().
110          * The behavior is also adjusted based on required(): if the option is
111          * required, the value given to defaultBasename() is treated as for
112          * defaultValue(), otherwise it is treated as for defaultValueIfSet().
113          */
114         MyClass &defaultBasename(const char *basename)
115         { defaultBasename_ = basename; return me(); }
116
117     private:
118         // Use defaultBasename() instead.
119         using MyBase::defaultValue;
120         using MyBase::defaultValueIfSet;
121
122         //! Creates a FileNameOptionStorage object.
123         virtual AbstractOptionStoragePointer createStorage() const;
124
125         OptionFileType          filetype_;
126         const char             *defaultBasename_;
127         bool                    bRead_;
128         bool                    bWrite_;
129         bool                    bLibrary_;
130
131         /*! \brief
132          * Needed to initialize FileNameOptionStorage from this class without
133          * otherwise unnecessary accessors.
134          */
135         friend class FileNameOptionStorage;
136 };
137
138 /*! \brief
139  * Wrapper class for accessing file name option information.
140  *
141  * \inpublicapi
142  * \ingroup module_options
143  */
144 class FileNameOptionInfo : public OptionInfo
145 {
146     public:
147         //! Creates an option info object for the given option.
148         explicit FileNameOptionInfo(FileNameOptionStorage *option);
149
150         //! Whether the option specifies an input file.
151         bool isInputFile() const;
152         //! Whether the option specifies an output file.
153         bool isOutputFile() const;
154         //! Whether the option specifies a file used for both input and output.
155         bool isInputOutputFile() const;
156         /*! \brief
157          * Whether the option specifies a library file.
158          *
159          * \see FileNameOption::libraryFile()
160          */
161         bool isLibraryFile() const;
162
163     private:
164         const FileNameOptionStorage &option() const;
165 };
166
167 } // namespace gmx
168
169 #endif