Merge branch release-2016 into release-2018
[gromacs.git] / src / gromacs / utility / classhelpers.h
blob3714313fe6cd5dea3dc9172ea09df8c7fc4d531c
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, 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.
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.
35 /*! \file
36 * \brief
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>
44 * \inlibraryapi
45 * \ingroup module_utility
47 #ifndef GMX_UTILITY_CLASSHELPERS_H
48 #define GMX_UTILITY_CLASSHELPERS_H
50 #include <memory>
52 namespace gmx
55 #ifdef DOXYGEN
56 /*! \brief
57 * Macro to declare a class non-copyable and non-assignable.
59 * For consistency, should appear last in the class declaration.
61 * \ingroup module_utility
63 #define GMX_DISALLOW_COPY_AND_ASSIGN(ClassName)
64 #else
65 #define GMX_DISALLOW_COPY_AND_ASSIGN(ClassName) \
66 ClassName &operator=(const ClassName &) = delete; \
67 ClassName(const ClassName &) = delete
68 #endif
69 /*! \brief
70 * Macro to declare a class non-assignable.
72 * For consistency, should appear last in the class declaration.
74 * \ingroup module_utility
76 #define GMX_DISALLOW_ASSIGN(ClassName) \
77 ClassName &operator=(const ClassName &) = delete
79 #ifdef DOXYGEN
80 /*! \brief
81 * Macro to declare default constructors
83 * Intended for copyable interfaces or bases classes which require to create custom
84 * destructor (e.g. protected or virtual) but need the default constructors.
86 * \ingroup module_utility
88 #define GMX_DEFAULT_CONSTRUCTORS(ClassName)
89 #else
90 #define GMX_DEFAULT_CONSTRUCTORS(ClassName) \
91 ClassName() = default; \
92 ClassName &operator=(const ClassName &) = default; \
93 ClassName(const ClassName &) = default; \
94 ClassName &operator=(ClassName &&) = default; \
95 ClassName(ClassName &&) = default
96 #endif
98 /*! \brief
99 * Helper class to manage a pointer to a private implementation class.
101 * This helper provides the following benefits (all but the last could also be
102 * achieved with std::unique_ptr):
103 * - Automatic memory management: the implementation pointer is freed in
104 * the destructor automatically. If the destructor is not declared or is
105 * defined inline in the header file, a compilation error occurs instead
106 * of a memory leak or undefined behavior.
107 * - Exception safety in constructors: the implementation pointer is freed
108 * correctly even if the constructor of the containing class throws after
109 * the implementation class is constructed.
110 * - Copy and/or assignment is automatically disallowed if explicit copy
111 * constructor and/or assignment operator is not provided.
112 * - Compiler helps to manage const-correctness: in const methods, it is not
113 * possible to change the implementation class.
115 * Move construction and assignment are also disallowed, but can be enabled by
116 * providing explicit move constructor and/or assignment.
118 * Intended use:
119 * \code
120 // In exampleclass.h
121 class ExampleClass
123 public:
124 ExampleClass();
125 ~ExampleClass(); // Must not be defined inline
127 // <...>
129 private:
130 class Impl;
132 PrivateImplPointer<Impl> impl_;
135 // In exampleclass.cpp
137 // <definition of ExampleClass::Impl>
139 ExampleClass::ExampleClass()
140 : impl_(new Impl)
144 ExampleClass::~ExampleClass()
147 \endcode
149 * \inlibraryapi
150 * \ingroup module_utility
152 template <class Impl>
153 class PrivateImplPointer
155 public:
156 //! Allow implicit initialization from nullptr to support comparison.
157 PrivateImplPointer(std::nullptr_t) : ptr_(nullptr) {}
158 //! Initialize with the given implementation class.
159 explicit PrivateImplPointer(Impl *ptr) : ptr_(ptr) {}
160 //! \cond
161 // Explicitly declared to work around MSVC problems.
162 PrivateImplPointer(PrivateImplPointer &&other) : ptr_(std::move(other.ptr_)) {}
163 PrivateImplPointer &operator=(PrivateImplPointer &&other)
165 ptr_ = std::move(other.ptr_);
166 return *this;
168 //! \endcond
170 /*! \brief
171 * Sets a new implementation class and destructs the previous one.
173 * Needed, e.g., to implement lazily initializable or copy-assignable
174 * 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 implementation class as with a raw pointer.
184 const Impl *operator->() const { return ptr_.get(); }
185 //! Access the implementation class as with a raw pointer.
186 const Impl &operator*() const { return *ptr_; }
188 //! Allows testing whether the implementation is initialized.
189 explicit operator bool() const { return ptr_ != nullptr; }
191 //! Tests for equality (mainly useful against nullptr).
192 bool operator==(const PrivateImplPointer &other) const { return ptr_ == other.ptr_; }
193 //! Tests for inequality (mainly useful against nullptr).
194 bool operator!=(const PrivateImplPointer &other) const { return ptr_ != other.ptr_; }
196 private:
197 std::unique_ptr<Impl> ptr_;
199 // Copy construction and assignment disabled by the unique_ptr member.
202 } // namespace gmx
204 #endif