PCL
Flags.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.1.19
6 // ----------------------------------------------------------------------------
7 // pcl/Flags.h - Released 2019-11-07T10:59:34Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2019 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (http://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_Flags_h
53 #define __PCL_Flags_h
54 
56 
57 #include <pcl/Defs.h>
58 
59 #ifdef __PCL_BUILDING_PIXINSIGHT_APPLICATION
60 namespace pi
61 {
62 class GlobalSettings;
63 }
64 #endif
65 
66 namespace pcl
67 {
68 
69 // ----------------------------------------------------------------------------
70 
71 template <bool> struct FlagType {};
72 template <> struct FlagType<true> { typedef unsigned type; };
73 template <> struct FlagType<false> { typedef int type; };
74 
75 // ----------------------------------------------------------------------------
76 
83 template <typename E>
84 class PCL_CLASS Flags
85 {
86 public:
87 
88  static_assert( sizeof( E ) <= sizeof( int ),
89  "Invalid sizeof( Flags::enum_type ): Must not be larger than sizeof( int )" );
90 
94  typedef E enum_type;
95 
99  typedef typename FlagType<std::is_unsigned<enum_type>::value>::type
101 
105  Flags() = default;
106 
110  constexpr
111  Flags( enum_type e ) : m_flags( int( e ) )
112  {
113  }
114 
118  constexpr
119  Flags( flag_type m ) : m_flags( int( m ) )
120  {
121  }
122 
126  Flags( const Flags& ) = default;
127 
131  Flags& operator =( const Flags& ) = default;
132 
137  Flags& operator =( enum_type e )
138  {
139  m_flags = e;
140  return *this;
141  }
142 
148  {
149  m_flags &= f.m_flags;
150  return *this;
151  }
152 
158  Flags& operator &=( enum_type e )
159  {
160  m_flags &= unsigned( e );
161  return *this;
162  }
163 
168  Flags& operator &=( unsigned m )
169  {
170  m_flags &= m;
171  return *this;
172  }
173 
179  Flags& operator |=( Flags f )
180  {
181  m_flags |= f.m_flags;
182  return *this;
183  }
184 
190  Flags& operator |=( enum_type e )
191  {
192  m_flags |= unsigned( e );
193  return *this;
194  }
195 
201  Flags& operator |=( unsigned m )
202  {
203  m_flags |= m;
204  return *this;
205  }
206 
212  Flags& operator ^=( Flags f )
213  {
214  m_flags ^= f.m_flags;
215  return *this;
216  }
217 
223  Flags& operator ^=( enum_type e )
224  {
225  m_flags ^= unsigned( e );
226  return *this;
227  }
228 
234  Flags& operator ^=( unsigned m )
235  {
236  m_flags ^= m;
237  return *this;
238  }
239 
243  constexpr bool IsFlagSet( enum_type e ) const
244  {
245  return (m_flags & e) != 0;
246  }
247 
251  void SetFlag( enum_type e, bool on = true )
252  {
253  if ( on )
254  m_flags |= e;
255  else
256  m_flags &= ~e;
257  }
258 
263  void ClearFlag( enum_type e )
264  {
265  SetFlag( e, false );
266  }
267 
271  void Clear()
272  {
273  m_flags = 0;
274  }
275 
280  Flags& operator <<( enum_type e )
281  {
282  SetFlag( e );
283  return *this;
284  }
285 
289  constexpr bool operator !() const
290  {
291  return m_flags == 0;
292  }
293 
297  constexpr operator flag_type() const
298  {
299  return m_flags;
300  }
301 
306  constexpr Flags operator ~() const
307  {
308  return Flags( enum_type( ~m_flags ) );
309  }
310 
314  constexpr Flags operator &( Flags f ) const
315  {
316  return Flags( enum_type( m_flags & f.m_flags ) );
317  }
318 
322  constexpr Flags operator &( enum_type e ) const
323  {
324  return Flags( enum_type( m_flags & flag_type( e ) ) );
325  }
326 
330  constexpr Flags operator &( unsigned m ) const
331  {
332  return Flags( enum_type( m_flags & m ) );
333  }
334 
338  constexpr Flags operator |( Flags f ) const
339  {
340  return Flags( enum_type( m_flags | f.m_flags ) );
341  }
342 
346  constexpr Flags operator |( enum_type e ) const
347  {
348  return Flags( enum_type( m_flags | flag_type( e ) ) );
349  }
350 
354  constexpr Flags operator |( unsigned m ) const
355  {
356  return Flags( enum_type( m_flags | m ) );
357  }
358 
362  constexpr Flags operator ^( Flags f ) const
363  {
364  return Flags( enum_type( m_flags ^ f.m_flags ) );
365  }
366 
370  constexpr Flags operator ^( enum_type e ) const
371  {
372  return Flags( enum_type( m_flags ^ flag_type( e ) ) );
373  }
374 
378  constexpr Flags operator ^( unsigned m ) const
379  {
380  return Flags( enum_type( m_flags ^ m ) );
381  }
382 
383 private:
384 
385  flag_type m_flags = 0;
386 
387 #ifndef __PCL_NO_FLAGS_SETTINGS_IO
388  friend class Settings;
389 #endif
390 
391 #ifndef __PCL_NO_FLAGS_FILE_IO
392  friend class File;
393 #endif
394 
395 #ifdef __PCL_BUILDING_PIXINSIGHT_APPLICATION
396  friend class pi::GlobalSettings;
397 #endif
398 };
399 
400 // ----------------------------------------------------------------------------
401 
402 } // pcl
403 
404 #endif // __PCL_Flags_h
405 
406 // ----------------------------------------------------------------------------
407 // EOF pcl/Flags.h - Released 2019-11-07T10:59:34Z
Flags & operator&=(enum_type e)
Definition: Flags.h:158
GenericImage< P > operator^(const GenericImage< P > &image, T scalar)
Definition: Image.h:16199
void SetFlag(enum_type e, bool on=true)
Definition: Flags.h:251
PCL root namespace.
Definition: AbstractImage.h:76
A type-safe collection of enumerated flags.
Definition: Flags.h:84
void Clear()
Definition: Flags.h:271
constexpr Flags(enum_type e)
Definition: Flags.h:111
Array< T, A > & operator<<(Array< T, A > &x, const V &v)
Definition: Array.h:2103
constexpr Flags operator&(Flags f) const
Definition: Flags.h:314
constexpr Flags operator&(enum_type e) const
Definition: Flags.h:322
void ClearFlag(enum_type e)
Definition: Flags.h:263
constexpr Flags operator&(unsigned m) const
Definition: Flags.h:330
Flags & operator&=(unsigned m)
Definition: Flags.h:168
FlagType< std::is_unsigned< enum_type >::value >::type flag_type
Definition: Flags.h:100
constexpr bool IsFlagSet(enum_type e) const
Definition: Flags.h:243
constexpr Flags(flag_type m)
Definition: Flags.h:119
Flags & operator&=(Flags f)
Definition: Flags.h:147
Persistent module settings.
Definition: Settings.h:105
A platform-independent interface to the local file system.
Definition: File.h:496