Use proper doxygen tags in modular simulator
[gromacs.git] / src / gromacs / utility / classhelpers.h
blobae4458fdda49848a5b947f1c72deb09c66cd63cd
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
5 * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
36 /*! \file
37 * \brief
38 * Declares common utility classes and macros.
40 * This header contains helpers used to implement classes in the library.
41 * It is installed, because the helpers are used in installed headers, but
42 * typically users of the library should not need to be aware of these helpers.
44 * \author Teemu Murtola <teemu.murtola@gmail.com>
45 * \inlibraryapi
46 * \ingroup module_utility
48 #ifndef GMX_UTILITY_CLASSHELPERS_H
49 #define GMX_UTILITY_CLASSHELPERS_H
51 #include <memory>
53 namespace gmx
56 #ifdef DOXYGEN
57 /*! \brief
58 * Macro to declare a class non-copyable and non-assignable.
60 * For consistency, should appear last in the class declaration.
62 * \ingroup module_utility
64 # define GMX_DISALLOW_COPY_AND_ASSIGN(ClassName)
65 #else
66 # define GMX_DISALLOW_COPY_AND_ASSIGN(ClassName) \
67 ClassName& operator=(const ClassName&) = delete; \
68 ClassName(const ClassName&) = delete
69 #endif
70 #ifdef DOXYGEN
71 /*! \brief
72 * Macro to declare a class non-copyable, non-movable, non-copy-assignable and
73 * non-move-assignable.
75 * For consistency, should appear last in the class declaration.
77 * \ingroup module_utility
79 # define GMX_DISALLOW_COPY_MOVE_AND_ASSIGN(ClassName)
80 #else
81 # define GMX_DISALLOW_COPY_MOVE_AND_ASSIGN(ClassName) \
82 ClassName& operator=(const ClassName&) = delete; \
83 ClassName(const ClassName&) = delete; \
84 ClassName& operator=(ClassName&&) = delete; /* NOLINT(misc-macro-parentheses,bugprone-macro-parentheses) */ \
85 ClassName(ClassName&&) = delete /* NOLINT(misc-macro-parentheses,bugprone-macro-parentheses) */
86 #endif
87 /*! \brief
88 * Macro to declare a class non-assignable.
90 * For consistency, should appear last in the class declaration.
92 * \ingroup module_utility
94 #define GMX_DISALLOW_ASSIGN(ClassName) ClassName& operator=(const ClassName&) = delete
96 // clang-format off
97 #ifdef DOXYGEN
98 /*! \brief
99 * Macro to declare default constructors
101 * Intended for copyable interfaces or bases classes which require to create custom
102 * destructor (e.g. protected or virtual) but need the default constructors.
104 * \ingroup module_utility
106 # define GMX_DEFAULT_CONSTRUCTORS(ClassName)
107 #else
108 # define GMX_DEFAULT_CONSTRUCTORS(ClassName) \
109 ClassName() = default; \
110 ClassName& operator=(const ClassName&) = default; /* NOLINT(misc-macro-parentheses,bugprone-macro-parentheses) */ \
111 ClassName(const ClassName&) = default; \
112 ClassName& operator=(ClassName&&) = default; /* NOLINT(misc-macro-parentheses,bugprone-macro-parentheses) */ \
113 ClassName(ClassName&&) = default /* NOLINT(misc-macro-parentheses,bugprone-macro-parentheses) */
114 #endif
115 //clang-format on
117 /*! \brief
118 * Helper class to manage a pointer to a private implementation class.
120 * This helper provides the following benefits (all but the last could also be
121 * achieved with std::unique_ptr):
122 * - Automatic memory management: the implementation pointer is freed in
123 * the destructor automatically. If the destructor is not declared or is
124 * defined inline in the header file, a compilation error occurs instead
125 * of a memory leak or undefined behavior.
126 * - Exception safety in constructors: the implementation pointer is freed
127 * correctly even if the constructor of the containing class throws after
128 * the implementation class is constructed.
129 * - Copy and/or assignment is automatically disallowed if explicit copy
130 * constructor and/or assignment operator is not provided.
131 * - Compiler helps to manage const-correctness: in const methods, it is not
132 * possible to change the implementation class.
134 * Move construction and assignment are also disallowed, but can be enabled by
135 * providing explicit move constructor and/or assignment.
137 * Intended use:
138 * \code
139 // In exampleclass.h
140 class ExampleClass
142 public:
143 ExampleClass();
144 ~ExampleClass(); // Must be defined, must not be defined inline
146 // <...>
148 private:
149 class Impl;
151 PrivateImplPointer<Impl> impl_;
154 // In exampleclass.cpp
156 // <definition of ExampleClass::Impl>
158 ExampleClass::ExampleClass()
159 : impl_(new Impl)
163 ExampleClass::~ExampleClass()
166 \endcode
168 * Note that ExampleClass::~ExampleClass cannot be declared inline (or
169 * generated by the compiler) because the implementation of impl_
170 * requires that ExampleClass::Impl be known in size, whereas it has
171 * only been forward declared. Only the translation unit where
172 * ExampleClass::Impl is declared can define the destructor for
173 * ExampleClass (which may be defaulted).
175 * \inlibraryapi
176 * \ingroup module_utility
178 template<class Impl>
179 class PrivateImplPointer
181 public:
182 //! Allow implicit initialization from nullptr to support comparison.
183 PrivateImplPointer(std::nullptr_t) : ptr_(nullptr) {}
184 //! Initialize with the given implementation class.
185 explicit PrivateImplPointer(Impl* ptr) : ptr_(ptr) {}
186 //! \cond
187 // Explicitly declared to work around MSVC problems.
188 PrivateImplPointer(PrivateImplPointer&& other) noexcept : ptr_(std::move(other.ptr_)) {}
189 PrivateImplPointer& operator=(PrivateImplPointer&& other) noexcept
191 ptr_ = std::move(other.ptr_);
192 return *this;
194 //! \endcond
196 /*! \brief
197 * Sets a new implementation class and destructs the previous one.
199 * Needed, e.g., to implement lazily initializable or copy-assignable
200 * classes.
202 void reset(Impl* ptr) { ptr_.reset(ptr); }
203 //! Access the raw pointer.
204 Impl* get() { return ptr_.get(); }
205 //! Access the implementation class as with a raw pointer.
206 Impl* operator->() { return ptr_.get(); }
207 //! Access the implementation class as with a raw pointer.
208 Impl& operator*() { return *ptr_; }
209 //! Access the implementation class as with a raw pointer.
210 const Impl* operator->() const { return ptr_.get(); }
211 //! Access the implementation class as with a raw pointer.
212 const Impl& operator*() const { return *ptr_; }
214 //! Allows testing whether the implementation is initialized.
215 explicit operator bool() const { return ptr_ != nullptr; }
217 //! Tests for equality (mainly useful against nullptr).
218 bool operator==(const PrivateImplPointer& other) const { return ptr_ == other.ptr_; }
219 //! Tests for inequality (mainly useful against nullptr).
220 bool operator!=(const PrivateImplPointer& other) const { return ptr_ != other.ptr_; }
222 private:
223 std::unique_ptr<Impl> ptr_;
225 // Copy construction and assignment disabled by the unique_ptr member.
228 } // namespace gmx
230 #endif