2 * This file is part of the GROMACS molecular simulation package.
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.
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.
37 * Declares common utility classes and macros.
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.
43 * \author Teemu Murtola <teemu.murtola@gmail.com>
45 * \ingroup module_utility
47 #ifndef GMX_UTILITY_COMMON_H
48 #define GMX_UTILITY_COMMON_H
50 #include <boost/scoped_ptr.hpp>
58 * Helper for ignoring values in macros.
60 * \ingroup module_utility
63 void inline ignoreValueHelper(const T &)
67 } // namespace internal
70 * Macro to declare a class non-copyable and non-assignable.
72 * For consistency, should appear last in the class declaration.
74 * \ingroup module_utility
76 #define GMX_DISALLOW_COPY_AND_ASSIGN(ClassName) \
78 ClassName &operator=(const ClassName &); \
79 ClassName(const ClassName &)
81 * Macro to declare a class non-assignable.
83 * For consistency, should appear last in the class declaration.
85 * \ingroup module_utility
87 #define GMX_DISALLOW_ASSIGN(ClassName) \
89 ClassName &operator=(const ClassName &)
91 * Macro to explicitly ignore a return value of a call.
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
101 * \ingroup module_utility
103 #define GMX_IGNORE_RETURN_VALUE(call) \
104 ::gmx::internal::ignoreValueHelper(call)
106 * Macro to explicitly ignore an unused value.
108 * \ingroup module_utility
110 #define GMX_UNUSED_VALUE(value) (void)value
113 * Helper class to manage a pointer to a private implementation class.
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.
136 ~ExampleClass(); // Must not be defined inline
143 PrivateImplPointer<Impl> impl_;
146 // In exampleclass.cpp
148 // <definition of ExampleClass::Impl>
150 ExampleClass::ExampleClass()
155 ExampleClass::~ExampleClass()
161 * \ingroup module_utility
163 template <class Impl>
164 class PrivateImplPointer
167 //! Initialize with the given implementation class.
168 explicit PrivateImplPointer(Impl *ptr) : ptr_(ptr) {}
169 ~PrivateImplPointer() {}
172 * Sets a new implementation class and destructs the previous one.
174 * Needed, e.g., to implement assignable classes.
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_; }
191 boost::scoped_ptr<Impl> ptr_;
193 // Copy and assign disabled by the scoped_ptr member.