2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 * Declares gmx::FlagsTemplate.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_utility
43 #ifndef GMX_UTILITY_FLAGS_H
44 #define GMX_UTILITY_FLAGS_H
50 * Template class for typesafe handling of combination of flags.
52 * \tparam FlagType An enumerated type that holds the possible single flags. The enum
53 * must be of unsigned underlying type.
55 * This class is not used publicly, but is present in an installed header
56 * because it is used internally in public template classes.
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.
64 * \ingroup module_utility
66 template<typename FlagType
>
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
) {}
78 * Tests if the given flag is set.
80 * Note that if \p flag has more than a single bit set, then returns
81 * true if any of them is set.
83 bool test(FlagType flag
) const { return (flags_
& flag
) != 0; }
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
)
103 //! Combines flags from two flags objects.
104 FlagsTemplate
<FlagType
> operator|(const FlagsTemplate
<FlagType
>& other
) const
106 return FlagsTemplate
<FlagType
>(flags_
| other
.flags_
);
108 //! Combines flags from another flag object.
109 FlagsTemplate
<FlagType
>& operator|=(const FlagsTemplate
<FlagType
>& other
)
111 flags_
|= other
.flags_
;
114 //! Combined flags from two flags objects.
115 FlagsTemplate
<FlagType
> operator&(const FlagsTemplate
<FlagType
>& other
) const
117 return FlagsTemplate
<FlagType
>(flags_
& other
.flags_
);
119 //! Returns an object with all flags flipped.
120 FlagsTemplate
<FlagType
> operator~() const { return FlagsTemplate
<FlagType
>(~flags_
); }
123 //! Creates a flags object with the given flags.
124 explicit FlagsTemplate(unsigned long flags
) : flags_(flags
) {}