Code beautification with uncrustify
[alexxy/gromacs.git] / src / gromacs / utility / flags.h
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2009, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \file
32  * \brief
33  * Declares gmx::FlagsTemplate.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \inlibraryapi
37  * \ingroup module_utility
38  */
39 #ifndef GMX_UTILITY_FLAGS_H
40 #define GMX_UTILITY_FLAGS_H
41
42 namespace gmx
43 {
44
45 /*! \brief
46  * Template class for typesafe handling of combination of flags.
47  *
48  * \tparam T An enumerated type that holds the possible single flags.
49  *
50  * This class is not used publicly, but is present in an installed header
51  * because it is used internally in public template classes.
52  *
53  * \inlibraryapi
54  * \ingroup module_utility
55  */
56 template <typename T>
57 class FlagsTemplate
58 {
59     public:
60         //! Creates a flags object with no flags set.
61         FlagsTemplate() : flags_(0) {}
62         //! Creates a flags object from a single flag.
63         FlagsTemplate(T flag) : flags_(flag) {}
64
65         //! Returns true if the given flag is set.
66         bool test(T flag) const { return flags_ & flag; }
67         //! Clears all flags.
68         void clearAll() { flags_ = 0; }
69         //! Sets the given flag.
70         void set(T flag) { flags_ |= flag; }
71         //! Clears the given flag.
72         void clear(T flag) { flags_ &= ~flag; }
73         //! Sets or clears the given flag.
74         void set(T flag, bool bSet)
75         {
76             if (bSet)
77             {
78                 set(flag);
79             }
80             else
81             {
82                 clear(flag);
83             }
84         }
85
86         //! Combines flags from two flags objects.
87         FlagsTemplate<T> operator|(const FlagsTemplate<T> &other) const
88         {
89             return FlagsTemplate<T>(flags_ | other.flags_);
90         }
91         //! Combines flags from another flag object.
92         FlagsTemplate<T> &operator|=(const FlagsTemplate<T> &other)
93         {
94             flags_ |= other.flags_;
95             return *this;
96         }
97         //! Combined flags from two flags objects.
98         FlagsTemplate<T> operator&(const FlagsTemplate<T> &other) const
99         {
100             return FlagsTemplate<T>(flags_ & other.flags_);
101         }
102         //! Returns an object with all flags flipped.
103         FlagsTemplate<T> operator~() const
104         {
105             return FlagsTemplate<T>(~flags_);
106         }
107
108     private:
109         //! Creates a flags object with the given flags.
110         explicit FlagsTemplate(unsigned long flags) : flags_(flags) {}
111
112         unsigned long           flags_;
113 };
114
115 } // namespace gmx
116
117 #endif