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.
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>
46 * \ingroup module_utility
48 #ifndef GMX_UTILITY_CLASSHELPERS_H
49 #define GMX_UTILITY_CLASSHELPERS_H
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)
66 # define GMX_DISALLOW_COPY_AND_ASSIGN(ClassName) \
67 ClassName& operator=(const ClassName&) = delete; \
68 ClassName(const ClassName&) = delete
71 * Macro to declare a class non-assignable.
73 * For consistency, should appear last in the class declaration.
75 * \ingroup module_utility
77 #define GMX_DISALLOW_ASSIGN(ClassName) ClassName& operator=(const ClassName&) = delete
82 * Macro to declare default constructors
84 * Intended for copyable interfaces or bases classes which require to create custom
85 * destructor (e.g. protected or virtual) but need the default constructors.
87 * \ingroup module_utility
89 # define GMX_DEFAULT_CONSTRUCTORS(ClassName)
91 # define GMX_DEFAULT_CONSTRUCTORS(ClassName) \
92 ClassName() = default; \
93 ClassName& operator=(const ClassName&) = default; /* NOLINT(misc-macro-parentheses,bugprone-macro-parentheses) */ \
94 ClassName(const ClassName&) = default; \
95 ClassName& operator=(ClassName&&) = default; /* NOLINT(misc-macro-parentheses,bugprone-macro-parentheses) */ \
96 ClassName(ClassName&&) = default /* NOLINT(misc-macro-parentheses,bugprone-macro-parentheses) */
101 * Helper class to manage a pointer to a private implementation class.
103 * This helper provides the following benefits (all but the last could also be
104 * achieved with std::unique_ptr):
105 * - Automatic memory management: the implementation pointer is freed in
106 * the destructor automatically. If the destructor is not declared or is
107 * defined inline in the header file, a compilation error occurs instead
108 * of a memory leak or undefined behavior.
109 * - Exception safety in constructors: the implementation pointer is freed
110 * correctly even if the constructor of the containing class throws after
111 * the implementation class is constructed.
112 * - Copy and/or assignment is automatically disallowed if explicit copy
113 * constructor and/or assignment operator is not provided.
114 * - Compiler helps to manage const-correctness: in const methods, it is not
115 * possible to change the implementation class.
117 * Move construction and assignment are also disallowed, but can be enabled by
118 * providing explicit move constructor and/or assignment.
127 ~ExampleClass(); // Must be defined, must not be defined inline
134 PrivateImplPointer<Impl> impl_;
137 // In exampleclass.cpp
139 // <definition of ExampleClass::Impl>
141 ExampleClass::ExampleClass()
146 ExampleClass::~ExampleClass()
151 * Note that ExampleClass::~ExampleClass cannot be declared inline (or
152 * generated by the compiler) because the implementation of impl_
153 * requires that ExampleClass::Impl be known in size, whereas it has
154 * only been forward declared. Only the translation unit where
155 * ExampleClass::Impl is declared can define the destructor for
156 * ExampleClass (which may be defaulted).
159 * \ingroup module_utility
162 class PrivateImplPointer
165 //! Allow implicit initialization from nullptr to support comparison.
166 PrivateImplPointer(std::nullptr_t) : ptr_(nullptr) {}
167 //! Initialize with the given implementation class.
168 explicit PrivateImplPointer(Impl* ptr) : ptr_(ptr) {}
170 // Explicitly declared to work around MSVC problems.
171 PrivateImplPointer(PrivateImplPointer&& other) noexcept : ptr_(std::move(other.ptr_)) {}
172 PrivateImplPointer& operator=(PrivateImplPointer&& other) noexcept
174 ptr_ = std::move(other.ptr_);
180 * Sets a new implementation class and destructs the previous one.
182 * Needed, e.g., to implement lazily initializable or copy-assignable
185 void reset(Impl* ptr) { ptr_.reset(ptr); }
186 //! Access the raw pointer.
187 Impl* get() { return ptr_.get(); }
188 //! Access the implementation class as with a raw pointer.
189 Impl* operator->() { return ptr_.get(); }
190 //! Access the implementation class as with a raw pointer.
191 Impl& operator*() { return *ptr_; }
192 //! Access the implementation class as with a raw pointer.
193 const Impl* operator->() const { return ptr_.get(); }
194 //! Access the implementation class as with a raw pointer.
195 const Impl& operator*() const { return *ptr_; }
197 //! Allows testing whether the implementation is initialized.
198 explicit operator bool() const { return ptr_ != nullptr; }
200 //! Tests for equality (mainly useful against nullptr).
201 bool operator==(const PrivateImplPointer& other) const { return ptr_ == other.ptr_; }
202 //! Tests for inequality (mainly useful against nullptr).
203 bool operator!=(const PrivateImplPointer& other) const { return ptr_ != other.ptr_; }
206 std::unique_ptr<Impl> ptr_;
208 // Copy construction and assignment disabled by the unique_ptr member.