Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Flags.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef Flags_INCLUDE_ONCE
33 #define Flags_INCLUDE_ONCE
34 
35 namespace vl
36 {
38  template <typename T_FlagEnum>
39  class Flags
40  {
41  // friends:
42  template <typename T> friend Flags<T> operator|(T flag1, T flag2);
43  template <typename T> friend Flags<T> operator&(T flag1, T flag2);
44 
45  public:
46  typedef T_FlagEnum flag_type;
47  typedef unsigned int flag_storage;
48 
49  public:
50  Flags(): mFlags(0) { }
51 
52  flag_storage flags() const { return mFlags; }
53 
54  void reset() { mFlags = 0; }
55 
56  // --- using Flags ---
57 
58  Flags operator|(const Flags& flag) const
59  {
60  Flags other = *this;
61  other.mFlags |= flag.mFlags;
62  return other;
63  }
64 
65  Flags operator&(const Flags& flag) const
66  {
67  Flags other = *this;
68  other.mFlags &= flag.mFlags;
69  return other;
70  }
71 
72  Flags operator^(const Flags& flag) const
73  {
74  Flags other = *this;
75  other.mFlags ^= flag.mFlags;
76  return other;
77  }
78 
79  Flags operator-(const Flags& flag) const
80  {
81  Flags other = *this;
82  other.mFlags &= ~flag.mFlags;
83  return other;
84  }
85 
86  // --- using T_FlagEnum ---
87 
88  Flags& set(T_FlagEnum flag)
89  {
90  mFlags |= (flag_storage)flag;
91  return *this;
92  }
93 
94  Flags& unset(T_FlagEnum flag)
95  {
96  mFlags &= ~(flag_storage)flag;
97  return *this;
98  }
99 
100  Flags& operator=(T_FlagEnum flag)
101  {
102  mFlags = flag;
103  return this;
104  }
105 
106  Flags operator|(T_FlagEnum flag) const
107  {
108  Flags other = *this;
109  other.mFlags |= (flag_storage)flag;
110  return other;
111  }
112 
113  Flags operator&(T_FlagEnum flag) const
114  {
115  Flags other = *this;
116  other.mFlags &= (flag_storage)flag;
117  return other;
118  }
119 
120  Flags operator^(T_FlagEnum flag) const
121  {
122  Flags other = *this;
123  other.mFlags ^= (flag_storage)flag;
124  return other;
125  }
126 
127  Flags operator-(T_FlagEnum flag) const
128  {
129  Flags other = *this;
130  other.unset(flag);
131  return other;
132  }
133 
134  operator bool() const
135  {
136  return mFlags != 0;
137  }
138 
139  private:
140  flag_storage mFlags;
141  };
142 
143  template <typename T>
144  Flags<T> operator|(T flag1, T flag2)
145  {
146  Flags<T> flags;
147  flags.mFlags = (typename Flags<T>::flag_storage)flag1 | (typename Flags<T>::flag_storage)flag2;
148  return flags;
149  }
150 
151  template <typename T>
152  Flags<T> operator&(T flag1, T flag2)
153  {
154  Flags<T> flags;
155  flags.mFlags = (typename Flags<T>::flag_storage)flag1 & (typename Flags<T>::flag_storage)flag2;
156  return flags;
157  }
158 }
159 
160 #define VL_DECLARE_FLAGS(EnumType, FlagTypeName) \
161  template<EnumType> Flags<EnumType> operator|(EnumType flag1, EnumType flag2); \
162  template<EnumType> Flags<EnumType> operator&(EnumType flag1, EnumType flag2); \
163  typedef Flags<EnumType> FlagTypeName;
164 
165 /***
166 
167 usage:
168 
169 enum MyFlagEnum
170 {
171  Flag1 = 0x1,
172  Flag2 = 0x2,
173  Flag3 = 0x4,
174 };
175 
176 VL_DECLARE_FLAGS(MyFlagEnum, MyFlags)
177 
178 ...
179 
180 MyFlags f = Flag3 | Flag1;
181 
182 ***/
183 
184 #endif
Simple class to manage flags in a type safe manner.
Definition: Flags.hpp:39
void reset()
Definition: Flags.hpp:54
Flags & operator=(T_FlagEnum flag)
Definition: Flags.hpp:100
flag_storage flags() const
Definition: Flags.hpp:52
Flags operator|(const Flags &flag) const
Definition: Flags.hpp:58
Flags operator^(const Flags &flag) const
Definition: Flags.hpp:72
Flags operator|(T_FlagEnum flag) const
Definition: Flags.hpp:106
friend Flags< T > operator|(T flag1, T flag2)
Definition: Flags.hpp:144
Visualization Library main namespace.
Flags operator-(const Flags &flag) const
Definition: Flags.hpp:79
Flags operator-(T_FlagEnum flag) const
Definition: Flags.hpp:127
Flags()
Definition: Flags.hpp:50
Flags operator^(T_FlagEnum flag) const
Definition: Flags.hpp:120
unsigned int flag_storage
Definition: Flags.hpp:47
Flags & unset(T_FlagEnum flag)
Definition: Flags.hpp:94
friend Flags< T > operator &(T flag1, T flag2)
Definition: Flags.hpp:152
T_FlagEnum flag_type
Definition: Flags.hpp:46