ug4
flags.h
Go to the documentation of this file.
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 
39 
62 template <class TEnum, class TStorageType = unsigned int, TStorageType defaultValue = 0>
63 class Flag{
64  public:
65  Flag() : m_value(defaultValue) {}
66  Flag(TStorageType flag) : m_value(flag) {}
67  Flag(const Flag& flag) : m_value(flag.m_value) {}
68 
69  bool contains(TStorageType flag) const {return (m_value & flag) == flag;}
70  bool contains(const Flag& flag) const {return (m_value & flag.m_value) == flag.m_value;}
71 
72  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  Flag& set(TStorageType flag) {m_value = flag; return *this;}
76  Flag& add(TStorageType flag) {m_value |= flag; return *this;}
77  Flag& remove(TStorageType flag) {m_value &= (~flag); return *this;}
78 
79  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  Flag operator= (const Flag& flag) {m_value = flag.m_value; return *this;}
84  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  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
Helps maintaining, activating and deactivating a set of flags from an enum.
Definition: flags.h:63
Flag()
Definition: flags.h:65
Flag operator=(const Flag &flag)
Definition: flags.h:83
TStorageType operator()() const
Definition: flags.h:86
Flag operator|=(const Flag &flag)
Definition: flags.h:82
Flag & set(TStorageType flag)
Definition: flags.h:75
Flag(TStorageType flag)
Definition: flags.h:66
TStorageType m_value
Definition: flags.h:96
bool partially_contains(const Flag &flag) const
Definition: flags.h:73
Flag operator&(const Flag &flag) const
Definition: flags.h:79
Flag operator&=(const Flag &flag)
Definition: flags.h:80
Flag operator|(const Flag &flag) const
Definition: flags.h:81
bool operator!=(const Flag &flag) const
Definition: flags.h:92
Flag & add(TStorageType flag)
Definition: flags.h:76
TStorageType get() const
Definition: flags.h:87
bool operator==(const Flag &flag) const
Definition: flags.h:89
Flag & remove(TStorageType flag)
Definition: flags.h:77
bool contains(TStorageType flag) const
Definition: flags.h:69
bool contains(const Flag &flag) const
Definition: flags.h:70
bool partially_contains(TStorageType flag) const
Definition: flags.h:72
Flag(const Flag &flag)
Definition: flags.h:67
the ug namespace