403918ce34aba114bb97a23bb45a731ecab11c79
[alexxy/gromacs.git] / src / gromacs / compat / pointers.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018, 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35
36 /*! \libinternal \file
37  * \brief Provides ported functions/classes from gsl/pointers
38  *
39  * Adapted from the Guidelines Support Library v2.0.0. at
40  * https://github.com/Microsoft/GSL
41  *
42  * \todo Replace comments referring to c++14-constexpr with constexpr
43  * when we require C++14.
44  *
45  * \author Mark Abraham <mark.j.abraham@gmail.com>
46  * \ingroup module_compat
47  * \inlibraryapi
48  */
49  #ifndef GMX_COMPAT_POINTERS_H
50  #define GMX_COMPAT_POINTERS_H
51
52 #include <type_traits>
53 #include <utility>
54
55 #include "gromacs/utility/gmxassert.h"
56
57 namespace gmx
58 {
59 namespace compat
60 {
61
62 //! Contract-assurance macros that work like a simple version of the GSL ones
63 //! \{
64 #if !defined(__INTEL_COMPILER) || !(__INTEL_COMPILER == 1800 && __INTEL_COMPILER_UPDATE == 0)
65 #define Expects(cond) GMX_ASSERT((cond), "Precondition violation")
66 #define Ensures(cond) GMX_ASSERT((cond), "Postcondition violation")
67 #else
68 // icc 18.0.0 in a RelWithAssert build has an ICE, even if we directly
69 // embed the contents of GMX_ASSERT, so it seems the lambda in
70 // GMX_ASSERT is too complex for it in this use case.
71 #define Expects(cond)
72 #define Ensures(cond)
73 #endif
74 //! \}
75
76 /*! \libinternal
77  * \brief Restricts a pointer or smart pointer to only hold non-null values.
78  *
79  * Has zero size overhead over T.
80  *
81  * If T is a pointer (i.e. T == U*) then
82  * - allow construction from U*
83  * - disallow construction from nullptr_t
84  * - disallow default construction
85  * - ensure construction from null U* fails (only in debug builds)
86  * - allow implicit conversion to U*
87  *
88  * \todo Eliminate this when we require a version of C++ that supports
89  * std::not_null.
90  */
91 template <class T>
92 class not_null
93 {
94     public:
95         static_assert(std::is_assignable<T &, std::nullptr_t>::value, "T cannot be assigned nullptr.");
96
97         //! Move constructor. Asserts in debug mode if \c is nullptr.
98         template <typename U, typename = typename std::enable_if<std::is_convertible<U, T>::value>::type >
99         /*c++14-constexpr*/ explicit not_null(U &&u) : ptr_(std::forward<U>(u))
100         {
101             Expects(ptr_ != nullptr);
102         }
103
104         //! Simple constructor. Asserts in debug mode if \c u is nullptr.
105         template <typename = typename std::enable_if<!std::is_same<std::nullptr_t, T>::value>::type >
106         /*c++14-constexpr*/ explicit not_null(T u) : ptr_(u)
107         {
108             Expects(ptr_ != nullptr);
109         }
110
111         //! Copy constructor.
112         template <typename U, typename = typename std::enable_if<std::is_convertible<U, T>::value>::type >
113         constexpr not_null(const not_null<U> &other) : not_null(other.get())
114         {
115         }
116
117         //! Default constructors and assignment.
118         //! \{
119         not_null(not_null &&other) noexcept        = default;
120         not_null(const not_null &other)            = default;
121         not_null &operator=(const not_null &other) = default;
122         //! \}
123
124         //! Getters
125         //! \{
126         /*c++14-constexpr*/ T get() const
127         {
128             Ensures(ptr_ != nullptr);
129             return ptr_;
130         }
131
132         constexpr operator T() const { return get(); }
133         constexpr T operator->() const { return get(); }
134         //! \}
135
136         //! Deleted to prevent compilation when someone attempts to assign a null pointer constant.
137         //! \{
138         not_null(std::nullptr_t)            = delete;
139         not_null &operator=(std::nullptr_t) = delete;
140         //! \}
141
142         //! Deleted unwanted operators because pointers only point to single objects.
143         //! \{
144         not_null &operator++()                = delete;
145         not_null &operator--()                = delete;
146         not_null operator++(int)              = delete;
147         not_null operator--(int)              = delete;
148         not_null &operator+=(std::ptrdiff_t)  = delete;
149         not_null &operator-=(std::ptrdiff_t)  = delete;
150         void operator[](std::ptrdiff_t) const = delete;
151         //! \}
152
153     private:
154         T ptr_;
155 };
156
157 //! Convenience function for making not_null pointers.
158 template <class T>
159 not_null<T> make_not_null(T &&t)
160 {
161     return not_null < typename std::remove_cv < typename std::remove_reference<T>::type>::type >{
162                std::forward<T>(t)
163     };
164 }
165
166 //! Operators to compare not_null pointers.
167 //! \{
168 template <class T, class U>
169 auto operator==(const not_null<T> &lhs, const not_null<U> &rhs)->decltype(lhs.get() == rhs.get())
170 {
171     return lhs.get() == rhs.get();
172 }
173
174 template <class T, class U>
175 auto operator!=(const not_null<T> &lhs, const not_null<U> &rhs)->decltype(lhs.get() != rhs.get())
176 {
177     return lhs.get() != rhs.get();
178 }
179
180 template <class T, class U>
181 auto operator<(const not_null<T> &lhs, const not_null<U> &rhs)->decltype(lhs.get() < rhs.get())
182 {
183     return lhs.get() < rhs.get();
184 }
185
186 template <class T, class U>
187 auto operator<=(const not_null<T> &lhs, const not_null<U> &rhs)->decltype(lhs.get() <= rhs.get())
188 {
189     return lhs.get() <= rhs.get();
190 }
191
192 template <class T, class U>
193 auto operator>(const not_null<T> &lhs, const not_null<U> &rhs)->decltype(lhs.get() > rhs.get())
194 {
195     return lhs.get() > rhs.get();
196 }
197
198 template <class T, class U>
199 auto operator>=(const not_null<T> &lhs, const not_null<U> &rhs)->decltype(lhs.get() >= rhs.get())
200 {
201     return lhs.get() >= rhs.get();
202 }
203 //! \}
204
205 //! Deleted unwanted arithmetic operators.
206 //! \{
207 template <class T, class U>
208 std::ptrdiff_t operator-(const not_null<T> &, const not_null<U> &) = delete;
209 template <class T>
210 not_null<T> operator-(const not_null<T> &, std::ptrdiff_t) = delete;
211 template <class T>
212 not_null<T> operator+(const not_null<T> &, std::ptrdiff_t) = delete;
213 template <class T>
214 not_null<T> operator+(std::ptrdiff_t, const not_null<T> &) = delete;
215 //! \}
216
217 } // namespace compat
218 } // namespace gmx
219
220 #endif