SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / gromacs / utility / flags.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,2013,2019,2020, 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 /*! \file
36  * \brief
37  * Declares gmx::FlagsTemplate.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inlibraryapi
41  * \ingroup module_utility
42  */
43 #ifndef GMX_UTILITY_FLAGS_H
44 #define GMX_UTILITY_FLAGS_H
45
46 namespace gmx
47 {
48
49 /*! \brief
50  * Template class for typesafe handling of combination of flags.
51  *
52  * \tparam FlagType An enumerated type that holds the possible single flags. The enum
53  * must be of unsigned underlying type.
54  *
55  * This class is not used publicly, but is present in an installed header
56  * because it is used internally in public template classes.
57  *
58  * Currently, it is not completely transparent, since or'ing together two
59  * \p FlagType flags does not automatically create a FlagsTemplate object.
60  * Also, some operators and more complex operations (like testing for multiple
61  * flags at the same time) are missing, but can be added if the need arises.
62  *
63  * \inlibraryapi
64  * \ingroup module_utility
65  */
66 template<typename FlagType>
67 class FlagsTemplate
68 {
69 public:
70     static_assert(std::is_enum_v<FlagType> && std::is_unsigned_v<std::underlying_type_t<FlagType>>,
71                   "Flags must be an unsigned enum type.");
72     //! Creates a flags object with no flags set.
73     FlagsTemplate() : flags_(0) {}
74     //! Creates a flags object from a single flag.
75     FlagsTemplate(FlagType flag) : flags_(flag) {}
76
77     /*! \brief
78      * Tests if the given flag is set.
79      *
80      * Note that if \p flag has more than a single bit set, then returns
81      * true if any of them is set.
82      */
83     bool test(FlagType flag) const { return (flags_ & flag) != 0; }
84     //! Clears all flags.
85     void clearAll() { flags_ = 0; }
86     //! Sets the given flag.
87     void set(FlagType flag) { flags_ |= flag; }
88     //! Clears the given flag.
89     void clear(FlagType flag) { flags_ &= ~flag; }
90     //! Sets or clears the given flag.
91     void set(FlagType flag, bool bSet)
92     {
93         if (bSet)
94         {
95             set(flag);
96         }
97         else
98         {
99             clear(flag);
100         }
101     }
102
103     //! Combines flags from two flags objects.
104     FlagsTemplate<FlagType> operator|(const FlagsTemplate<FlagType>& other) const
105     {
106         return FlagsTemplate<FlagType>(flags_ | other.flags_);
107     }
108     //! Combines flags from another flag object.
109     FlagsTemplate<FlagType>& operator|=(const FlagsTemplate<FlagType>& other)
110     {
111         flags_ |= other.flags_;
112         return *this;
113     }
114     //! Combined flags from two flags objects.
115     FlagsTemplate<FlagType> operator&(const FlagsTemplate<FlagType>& other) const
116     {
117         return FlagsTemplate<FlagType>(flags_ & other.flags_);
118     }
119     //! Returns an object with all flags flipped.
120     FlagsTemplate<FlagType> operator~() const { return FlagsTemplate<FlagType>(~flags_); }
121
122 private:
123     //! Creates a flags object with the given flags.
124     explicit FlagsTemplate(unsigned long flags) : flags_(flags) {}
125
126     uint64_t flags_;
127 };
128
129 } // namespace gmx
130
131 #endif