Line data Source code
1 : /*
2 : * Copyright (c) 2013-2015: G-CSC, Goethe University Frankfurt
3 : * Author: Sebastian Reiter
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : #ifndef __H__UG__flags__
34 : #define __H__UG__flags__
35 :
36 : namespace ug{
37 :
38 : /// Helps maintaining, activating and deactivating a set of flags from an enum.
39 : /** Given an enum
40 : * \code
41 : * enum SomeEnum{
42 : * E1 = 0,
43 : * E2 = 1,
44 : * E3 = 1 << 1,
45 : * E4 = 1 << 2
46 : * };
47 : *
48 : * \endcode
49 : * One can use the Flag class as follows:
50 : *
51 : * \code
52 : * typedef Flag<SomeEnum> SomeFlag;
53 : * ...
54 : * SomeFlag f1(E2), f2(E4);
55 : * SomeFlag f3(f1 | f2);
56 : * if(f3.contains(E2))
57 : * f3.remove(E2);
58 : * ...
59 : * \endcode
60 : *
61 : */
62 : template <class TEnum, class TStorageType = unsigned int, TStorageType defaultValue = 0>
63 : class Flag{
64 : public:
65 0 : Flag() : m_value(defaultValue) {}
66 0 : Flag(TStorageType flag) : m_value(flag) {}
67 0 : Flag(const Flag& flag) : m_value(flag.m_value) {}
68 :
69 0 : bool contains(TStorageType flag) const {return (m_value & flag) == flag;}
70 0 : bool contains(const Flag& flag) const {return (m_value & flag.m_value) == flag.m_value;}
71 :
72 0 : bool partially_contains(TStorageType flag) const {return (m_value & flag) != 0;}
73 : bool partially_contains(const Flag& flag) const {return (m_value & flag.m_value) != 0;}
74 :
75 0 : Flag& set(TStorageType flag) {m_value = flag; return *this;}
76 0 : Flag& add(TStorageType flag) {m_value |= flag; return *this;}
77 0 : Flag& remove(TStorageType flag) {m_value &= (~flag); return *this;}
78 :
79 0 : Flag operator& (const Flag& flag) const {return Flag(m_value & flag.m_value);}
80 : Flag operator&= (const Flag& flag) {m_value &= flag.m_value; return *this;}
81 : Flag operator| (const Flag& flag) const {return Flag(m_value | flag.m_value);}
82 : Flag operator|= (const Flag& flag) {m_value |= flag.m_value; return *this;}
83 0 : Flag operator= (const Flag& flag) {m_value = flag.m_value; return *this;}
84 0 : Flag operator= (TStorageType val) {m_value = val; return *this;}
85 :
86 : TStorageType operator()() const {return m_value;}
87 : TStorageType get() const {return m_value;}
88 :
89 : bool operator== (const Flag& flag) const {return m_value == flag.m_value;}
90 0 : bool operator== (TStorageType val) const {return m_value == val;}
91 :
92 : bool operator!= (const Flag& flag) const {return m_value != flag.m_value;}
93 : bool operator!= (TStorageType val) const {return m_value != val;}
94 :
95 : private:
96 : TStorageType m_value;
97 : };
98 :
99 : }// end of namespace
100 :
101 : #endif
|