Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / mdspan / accessor_policy.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,2019, 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  * This file is a modified version of original work of Sandia Corporation.
37  * In the spirit of the original code, this particular file can be distributed
38  * on the terms of Sandia Corporation.
39  */
40 /*
41  *                         Kokkos v. 2.0
42  *               Copyright (2014) Sandia Corporation
43  *
44  * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
45  * the U.S. Government retains certain rights in this software.
46  *
47  * Kokkos is licensed under 3-clause BSD terms of use:
48  *
49  * Redistribution and use in source and binary forms, with or without
50  * modification, are permitted provided that the following conditions are
51  * met:
52  *
53  * 1. Redistributions of source code must retain the above copyright
54  * notice, this list of conditions and the following disclaimer.
55  *
56  * 2. Redistributions in binary form must reproduce the above copyright
57  * notice, this list of conditions and the following disclaimer in the
58  * documentation and/or other materials provided with the distribution.
59  *
60  * 3. Neither the name of the Corporation nor the names of the
61  * contributors may be used to endorse or promote products derived from
62  * this software without specific prior written permission.
63  *
64  * THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
65  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
67  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
68  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
69  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
70  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
71  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
72  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
73  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
74  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
75  *
76  * Questions? Contact Christian R. Trott (crtrott@sandia.gov)
77  */
78 /*! \libinternal \file
79  * \brief Declares accessor policies for mdspan.
80  *
81  * Implement ways how to convert a linear offset from a pointer to memory access.
82  * \author David Hollman <dshollm@sandia.gov>
83  * \author Christian Blau <cblau@gwdg.de>
84  * \libinternal
85  * \ingroup mdspan
86  */
87 #ifndef MDSPAN_ACCESSOR_POLICY_H
88 #define MDSPAN_ACCESSOR_POLICY_H
89
90 #include <cstddef>
91
92 namespace gmx
93 {
94
95 /*! \libinternal \brief The most basic memory access model for mdspan.
96  * \tparam ElementType the type held in memory to be accessed
97  */
98 template<class ElementType>
99 class accessor_basic
100 {
101 public:
102     //! Type of element to be accessed.
103     using element_type = ElementType;
104     //! Pointer to element to be accessed.
105     using pointer = ElementType*;
106     //! How to determine a memory offset, provided by self accessor.
107     using offset_policy = accessor_basic;
108     //! Type of references.
109     using reference = ElementType&;
110
111     /*! \brief Shift a pointer by an offset.
112      * \param[in] p Pointer to reference memory location.
113      * \param[in] i offset from memory location.
114      * \returns pointer to offset memory location.
115      */
116     constexpr typename offset_policy::pointer offset(pointer p, ptrdiff_t i) const noexcept
117     {
118         return typename offset_policy::pointer(p + i);
119     }
120
121     /*! \brief Access element from an offset to given pointer.
122      * \param[in] p Pointer to reference memory location.
123      * \param[in] i offset from memory location.
124      * \returns reference to element stored at offset from memory location.
125      */
126     constexpr reference access(pointer p, ptrdiff_t i) const noexcept { return p[i]; }
127
128     /*! \brief Decay pointer to pointer to ElementType.
129      * NOTE This function does nothing, because it is the trivial implementation of an accessor.
130      * \returns input pointer as pointer to ElementType
131      */
132     constexpr ElementType* decay(pointer p) const noexcept { return p; }
133 };
134
135 } // namespace gmx
136 #endif /* end of include guard: MDSPAN_ACCESSOR_POLICY_H */