Split lines with many copyright years
[alexxy/gromacs.git] / src / gromacs / utility / classhelpers.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
9  *
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.
14  *
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.
19  *
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.
24  *
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.
32  *
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.
35  */
36 /*! \file
37  * \brief
38  * Declares common utility classes and macros.
39  *
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.
43  *
44  * \author Teemu Murtola <teemu.murtola@gmail.com>
45  * \inlibraryapi
46  * \ingroup module_utility
47  */
48 #ifndef GMX_UTILITY_CLASSHELPERS_H
49 #define GMX_UTILITY_CLASSHELPERS_H
50
51 #include <memory>
52
53 namespace gmx
54 {
55
56 #ifdef DOXYGEN
57 /*! \brief
58  * Macro to declare a class non-copyable and non-assignable.
59  *
60  * For consistency, should appear last in the class declaration.
61  *
62  * \ingroup module_utility
63  */
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 /*! \brief
71  * Macro to declare a class non-assignable.
72  *
73  * For consistency, should appear last in the class declaration.
74  *
75  * \ingroup module_utility
76  */
77 #define GMX_DISALLOW_ASSIGN(ClassName) ClassName& operator=(const ClassName&) = delete
78
79 // clang-format off
80 #ifdef DOXYGEN
81 /*! \brief
82  * Macro to declare default constructors
83  *
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.
86  *
87  * \ingroup module_utility
88  */
89 #    define GMX_DEFAULT_CONSTRUCTORS(ClassName)
90 #else
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) */
97 #endif
98 //clang-format on
99
100 /*! \brief
101  * Helper class to manage a pointer to a private implementation class.
102  *
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.
116  *
117  * Move construction and assignment are also disallowed, but can be enabled by
118  * providing explicit move constructor and/or assignment.
119  *
120  * Intended use:
121  * \code
122    // In exampleclass.h
123    class ExampleClass
124    {
125        public:
126            ExampleClass();
127            ~ExampleClass(); // Must be defined, must not be defined inline
128
129            // <...>
130
131        private:
132            class Impl;
133
134            PrivateImplPointer<Impl> impl_;
135    };
136
137    // In exampleclass.cpp
138
139    // <definition of ExampleClass::Impl>
140
141    ExampleClass::ExampleClass()
142        : impl_(new Impl)
143    {
144    }
145
146    ExampleClass::~ExampleClass()
147    {
148    }
149    \endcode
150  *
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).
157  *
158  * \inlibraryapi
159  * \ingroup module_utility
160  */
161 template<class Impl>
162 class PrivateImplPointer
163 {
164 public:
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) {}
169     //! \cond
170     // Explicitly declared to work around MSVC problems.
171     PrivateImplPointer(PrivateImplPointer&& other) noexcept : ptr_(std::move(other.ptr_)) {}
172     PrivateImplPointer& operator=(PrivateImplPointer&& other) noexcept
173     {
174         ptr_ = std::move(other.ptr_);
175         return *this;
176     }
177     //! \endcond
178
179     /*! \brief
180      * Sets a new implementation class and destructs the previous one.
181      *
182      * Needed, e.g., to implement lazily initializable or copy-assignable
183      * classes.
184      */
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_; }
196
197     //! Allows testing whether the implementation is initialized.
198     explicit operator bool() const { return ptr_ != nullptr; }
199
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_; }
204
205 private:
206     std::unique_ptr<Impl> ptr_;
207
208     // Copy construction and assignment disabled by the unique_ptr member.
209 };
210
211 } // namespace gmx
212
213 #endif