Apply clang-format to source tree
[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, 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.
53  *
54  * This class is not used publicly, but is present in an installed header
55  * because it is used internally in public template classes.
56  *
57  * Currently, it is not completely transparent, since or'ing together two
58  * \p FlagType flags does not automatically create a FlagsTemplate object.
59  * Also, some operators and more complex operations (like testing for multiple
60  * flags at the same time) are missing, but can be added if the need arises.
61  *
62  * \inlibraryapi
63  * \ingroup module_utility
64  */
65 template<typename FlagType>
66 class FlagsTemplate
67 {
68 public:
69     //! Creates a flags object with no flags set.
70     FlagsTemplate() : flags_(0) {}
71     //! Creates a flags object from a single flag.
72     FlagsTemplate(FlagType flag) : flags_(flag) {}
73
74     /*! \brief
75      * Tests if the given flag is set.
76      *
77      * Note that if \p flag has more than a single bit set, then returns
78      * true if any of them is set.
79      */
80     bool test(FlagType flag) const { return (flags_ & flag) != 0; }
81     //! Clears all flags.
82     void clearAll() { flags_ = 0; }
83     //! Sets the given flag.
84     void set(FlagType flag) { flags_ |= flag; }
85     //! Clears the given flag.
86     void clear(FlagType flag) { flags_ &= ~flag; }
87     //! Sets or clears the given flag.
88     void set(FlagType flag, bool bSet)
89     {
90         if (bSet)
91         {
92             set(flag);
93         }
94         else
95         {
96             clear(flag);
97         }
98     }
99
100     //! Combines flags from two flags objects.
101     FlagsTemplate<FlagType> operator|(const FlagsTemplate<FlagType>& other) const
102     {
103         return FlagsTemplate<FlagType>(flags_ | other.flags_);
104     }
105     //! Combines flags from another flag object.
106     FlagsTemplate<FlagType>& operator|=(const FlagsTemplate<FlagType>& other)
107     {
108         flags_ |= other.flags_;
109         return *this;
110     }
111     //! Combined flags from two flags objects.
112     FlagsTemplate<FlagType> operator&(const FlagsTemplate<FlagType>& other) const
113     {
114         return FlagsTemplate<FlagType>(flags_ & other.flags_);
115     }
116     //! Returns an object with all flags flipped.
117     FlagsTemplate<FlagType> operator~() const { return FlagsTemplate<FlagType>(~flags_); }
118
119 private:
120     //! Creates a flags object with the given flags.
121     explicit FlagsTemplate(unsigned long flags) : flags_(flags) {}
122
123     unsigned long flags_;
124 };
125
126 } // namespace gmx
127
128 #endif