gromacs cpp: clean up -Wunused-parameter warnings
[alexxy/gromacs.git] / src / gromacs / utility / common.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  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6  * others, as listed in the AUTHORS file in the top-level source
7  * 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 common utility classes and macros.
38  *
39  * This header contains helpers used to implement classes in the library.
40  * It is installed, because the helpers are used in installed headers, but
41  * typically users of the library should not need to be aware of these helpers.
42  *
43  * \author Teemu Murtola <teemu.murtola@gmail.com>
44  * \inlibraryapi
45  * \ingroup module_utility
46  */
47 #ifndef GMX_UTILITY_COMMON_H
48 #define GMX_UTILITY_COMMON_H
49
50 #include <boost/scoped_ptr.hpp>
51
52 namespace gmx
53 {
54 namespace internal
55 {
56 /*! \cond internal */
57 /*! \internal \brief
58  * Helper for ignoring values in macros.
59  *
60  * \ingroup module_utility
61  */
62 template <typename T>
63 void inline ignoreValueHelper(const T &)
64 {
65 }
66 //! \endcond
67 }   // namespace internal
68
69 /*! \brief
70  * Macro to declare a class non-copyable and non-assignable.
71  *
72  * For consistency, should appear last in the class declaration.
73  *
74  * \ingroup module_utility
75  */
76 #define GMX_DISALLOW_COPY_AND_ASSIGN(ClassName) \
77     private: \
78         ClassName &operator=(const ClassName &); \
79         ClassName(const ClassName &)
80 /*! \brief
81  * Macro to declare a class non-assignable.
82  *
83  * For consistency, should appear last in the class declaration.
84  *
85  * \ingroup module_utility
86  */
87 #define GMX_DISALLOW_ASSIGN(ClassName) \
88     private: \
89         ClassName &operator=(const ClassName &)
90 /*! \brief
91  * Macro to explicitly ignore a return value of a call.
92  *
93  * Mainly meant for ignoring values of functions declared with
94  * `__attribute__((warn_unused_return))`.  Makes it easy to find those places if
95  * they need to be fixed, and document the intent in cases where the return
96  * value really can be ignored.  It also makes it easy to adapt the approach so
97  * that they don't produce warnings.  A cast to void doesn't remove the warning
98  * in gcc, while adding a dummy variable can cause warnings about an unused
99  * variable.
100  *
101  * \ingroup module_utility
102  */
103 #define GMX_IGNORE_RETURN_VALUE(call) \
104         ::gmx::internal::ignoreValueHelper(call)
105 /*! \brief
106  * Macro to explicitly ignore an unused value.
107  *
108  * \ingroup module_utility
109  */
110 #define GMX_UNUSED_VALUE(value) (void)value
111
112 /*! \brief
113  * Helper class to manage a pointer to a private implementation class.
114  *
115  * This helper provides the following benefits (all but the last could also be
116  * achieved with boost::scoped_ptr):
117  *  - Automatic memory management: the implementation pointer is freed in
118  *    the destructor automatically.  If the destructor is not declared or is
119  *    defined inline in the header file, a compilation error occurs instead
120  *    of a memory leak or undefined behavior.
121  *  - Exception safety in constructors: the implementation pointer is freed
122  *    correctly even if the constructor of the containing class throws after
123  *    the implementation class is constructed.
124  *  - Copy and/or assignment is automatically disallowed if explicit copy
125  *    constructor and/or assignment operator is not provided.
126  *  - Compiler helps to manage const-correctness: in const methods, it is not
127  *    possible to change the implementation class.
128  *
129  * Intended use:
130  * \code
131    // In exampleclass.h
132    class ExampleClass
133    {
134        public:
135            ExampleClass();
136            ~ExampleClass(); // Must not be defined inline
137
138            // <...>
139
140        private:
141            class Impl;
142
143            PrivateImplPointer<Impl> impl_;
144    };
145
146    // In exampleclass.cpp
147
148    // <definition of ExampleClass::Impl>
149
150    ExampleClass::ExampleClass()
151        : impl_(new Impl)
152    {
153    }
154
155    ExampleClass::~ExampleClass()
156    {
157    }
158    \endcode
159  *
160  * \inlibraryapi
161  * \ingroup module_utility
162  */
163 template <class Impl>
164 class PrivateImplPointer
165 {
166     public:
167         //! Initialize with the given implementation class.
168         explicit PrivateImplPointer(Impl *ptr) : ptr_(ptr) {}
169         ~PrivateImplPointer() {}
170
171         /*! \brief
172          * Sets a new implementation class and destructs the previous one.
173          *
174          * Needed, e.g., to implement assignable classes.
175          */
176         void reset(Impl *ptr) { ptr_.reset(ptr); }
177         //! Access the raw pointer.
178         Impl *get() { return ptr_.get(); }
179         //! Access the implementation class as with a raw pointer.
180         Impl *operator->() { return ptr_.get(); }
181         //! Access the implementation class as with a raw pointer.
182         Impl &operator*() { return *ptr_; }
183         //! Access the raw pointer.
184         const Impl *get() const { return ptr_.get(); }
185         //! Access the implementation class as with a raw pointer.
186         const Impl *operator->() const { return ptr_.get(); }
187         //! Access the implementation class as with a raw pointer.
188         const Impl &operator*() const { return *ptr_; }
189
190     private:
191         boost::scoped_ptr<Impl> ptr_;
192
193         // Copy and assign disabled by the scoped_ptr member.
194 };
195
196 } // namespace gmx
197
198 #endif