2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013, 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.
54 * This class is not used publicly, but is present in an installed header
55 * because it is used internally in public template classes.
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.
63 * \ingroup module_utility
65 template <typename FlagType
>
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
) {}
75 * Tests if the given flag is set.
77 * Note that if \p flag has more than a single bit set, then returns
78 * true if any of them is set.
80 bool test(FlagType flag
) const { return (flags_
& flag
) != 0; }
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
)
100 //! Combines flags from two flags objects.
101 FlagsTemplate
<FlagType
>
102 operator|(const FlagsTemplate
<FlagType
> &other
) const
104 return FlagsTemplate
<FlagType
>(flags_
| other
.flags_
);
106 //! Combines flags from another flag object.
107 FlagsTemplate
<FlagType
> &
108 operator|=(const FlagsTemplate
<FlagType
> &other
)
110 flags_
|= other
.flags_
;
113 //! Combined flags from two flags objects.
114 FlagsTemplate
<FlagType
>
115 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
122 return FlagsTemplate
<FlagType
>(~flags_
);
126 //! Creates a flags object with the given flags.
127 explicit FlagsTemplate(unsigned long flags
) : flags_(flags
) {}
129 unsigned long flags_
;