Reformat existing LGPL copyright notices.
[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, 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>
102         operator|(const FlagsTemplate<FlagType> &other) const
103         {
104             return FlagsTemplate<FlagType>(flags_ | other.flags_);
105         }
106         //! Combines flags from another flag object.
107         FlagsTemplate<FlagType> &
108         operator|=(const FlagsTemplate<FlagType> &other)
109         {
110             flags_ |= other.flags_;
111             return *this;
112         }
113         //! Combined flags from two flags objects.
114         FlagsTemplate<FlagType>
115         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
121         {
122             return FlagsTemplate<FlagType>(~flags_);
123         }
124
125     private:
126         //! Creates a flags object with the given flags.
127         explicit FlagsTemplate(unsigned long flags) : flags_(flags) {}
128
129         unsigned long           flags_;
130 };
131
132 } // namespace gmx
133
134 #endif