Reformat existing LGPL copyright notices.
[alexxy/gromacs.git] / src / testutils / cmdlinetest.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013, 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 /*! \libinternal \file
36  * \brief
37  * Declares gmx::test::CommandLine.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inlibraryapi
41  * \ingroup module_testutils
42  */
43 #ifndef GMX_TESTUTILS_CMDLINETEST_H
44 #define GMX_TESTUTILS_CMDLINETEST_H
45
46 #include <string>
47
48 #include "gromacs/utility/common.h"
49
50 namespace gmx
51 {
52 namespace test
53 {
54
55 /*! \libinternal \brief
56  * Helper class for tests that check command-line handling.
57  *
58  * This class helps in writing tests for command-line handling.
59  * The create() method takes an array of const char pointers, specifying the
60  * command-line arguments, each as one array element.
61  * The argc() and argv() methods can then be used to obtain the argc and argv
62  * (non-const char pointers) arrays for passing into methods that expect these.
63  *
64  * Note that although the interface allows passing the argc and argv pointers
65  * to methods that modify them (typically as \p f(&argc(), argv())), currently
66  * the CommandLine object is not in a consistent state internally if the
67  * parameters are actually modified.  Reading the command line is possible
68  * afterwards, but modification is not.
69  *
70  * All constructors and methods that modify this class may throw an
71  * std::bad_alloc.  Const methods and accessors do not throw.
72  *
73  * \inlibraryapi
74  * \ingroup module_testutils
75  */
76 class CommandLine
77 {
78     public:
79         /*! \brief
80          * Initializes a command-line object from a const C array.
81          *
82          * \param[in] cmdline  Array of command-line arguments.
83          * \tparam    count    Deduced number of elements in \p cmdline.
84          *
85          * \p cmdline should include the binary name as the first element if
86          * that is desired in the output.
87          *
88          * This is not a constructor, because template constructors are not
89          * possible with a private implementation class.
90          */
91         template <size_t count> static
92         CommandLine create(const char *const (&cmdline)[count])
93         {
94             return CommandLine(cmdline, count);
95         }
96
97         //! Initializes an empty command-line object.
98         CommandLine();
99         /*! \brief
100          * Initializes a command-line object.
101          *
102          * \param[in] cmdline  Array of command-line arguments.
103          * \param[in] count    Number of elements in \p cmdline.
104          *
105          * \p cmdline should include the binary name as the first element if
106          * that is desired in the output.
107          */
108         CommandLine(const char *const cmdline[], size_t count);
109         //! Creates a deep copy of a command-line object.
110         CommandLine(const CommandLine &other);
111         ~CommandLine();
112
113         /*! \brief
114          * Append an argument to the command line.
115          *
116          * \param[in] arg  Argument to append.
117          *
118          * Strong exception safety.
119          */
120         void append(const char *arg);
121         //! Convenience overload taking a std::string.
122         void append(const std::string &arg) { append(arg.c_str()); }
123
124         //! Returns argc for passing into C-style command-line handling.
125         int &argc();
126         //! Returns argv for passing into C-style command-line handling.
127         char **argv();
128         //! Returns argc for passing into C-style command-line handling.
129         int argc() const;
130         //! Returns argv for passing into C-style command-line handling.
131         const char *const *argv() const;
132         //! Returns a single argument.
133         const char *arg(int i) const;
134
135         //! Returns the command line formatted as a single string.
136         std::string toString() const;
137
138     private:
139         class Impl;
140
141         PrivateImplPointer<Impl> impl_;
142 };
143
144 } // namespace test
145 } // namespace gmx
146
147 #endif