PCL
Flags.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/Flags.h - Released 2024-06-18T15:48:54Z
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-2024 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 (https://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> { using type = unsigned; };
73 template <> struct FlagType<false> { using type = int; };
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  using enum_type = E;
95 
99  using flag_type = typename FlagType<std::is_unsigned<enum_type>::value>::type;
100 
104  Flags() = default;
105 
109  constexpr
111  : m_flags( int( e ) )
112  {
113  }
114 
118  constexpr
120  : m_flags( int( m ) )
121  {
122  }
123 
127  Flags( const Flags& ) = default;
128 
132  Flags& operator =( const Flags& ) = default;
133 
138  Flags& operator =( enum_type e )
139  {
140  m_flags = e;
141  return *this;
142  }
143 
148  Flags& operator &=( Flags f )
149  {
150  m_flags &= f.m_flags;
151  return *this;
152  }
153 
159  Flags& operator &=( enum_type e )
160  {
161  m_flags &= unsigned( e );
162  return *this;
163  }
164 
169  Flags& operator &=( unsigned m )
170  {
171  m_flags &= m;
172  return *this;
173  }
174 
180  Flags& operator |=( Flags f )
181  {
182  m_flags |= f.m_flags;
183  return *this;
184  }
185 
191  Flags& operator |=( enum_type e )
192  {
193  m_flags |= unsigned( e );
194  return *this;
195  }
196 
202  Flags& operator |=( unsigned m )
203  {
204  m_flags |= m;
205  return *this;
206  }
207 
213  Flags& operator ^=( Flags f )
214  {
215  m_flags ^= f.m_flags;
216  return *this;
217  }
218 
224  Flags& operator ^=( enum_type e )
225  {
226  m_flags ^= unsigned( e );
227  return *this;
228  }
229 
235  Flags& operator ^=( unsigned m )
236  {
237  m_flags ^= m;
238  return *this;
239  }
240 
244  constexpr bool IsFlagSet( enum_type e ) const
245  {
246  return (m_flags & e) != flag_type( 0 );
247  }
248 
252  void SetFlag( enum_type e, bool on = true )
253  {
254  if ( on )
255  m_flags |= e;
256  else
257  m_flags &= ~e;
258  }
259 
265  {
266  SetFlag( e, false );
267  }
268 
272  void Clear()
273  {
274  m_flags = flag_type( 0 );
275  }
276 
282  {
283  SetFlag( e );
284  return *this;
285  }
286 
290  constexpr bool operator !() const
291  {
292  return m_flags == flag_type( 0 );
293  }
294 
298  constexpr operator flag_type() const
299  {
300  return m_flags;
301  }
302 
307  constexpr flag_type AsInteger() const
308  {
309  return m_flags;
310  }
311 
316  constexpr Flags operator ~() const
317  {
318  return Flags( enum_type( ~m_flags ) );
319  }
320 
324  constexpr Flags operator &( Flags f ) const
325  {
326  return Flags( enum_type( m_flags & f.m_flags ) );
327  }
328 
332  constexpr Flags operator &( enum_type e ) const
333  {
334  return Flags( enum_type( m_flags & flag_type( e ) ) );
335  }
336 
340  constexpr Flags operator &( unsigned m ) const
341  {
342  return Flags( enum_type( m_flags & m ) );
343  }
344 
348  constexpr Flags operator |( Flags f ) const
349  {
350  return Flags( enum_type( m_flags | f.m_flags ) );
351  }
352 
356  constexpr Flags operator |( enum_type e ) const
357  {
358  return Flags( enum_type( m_flags | flag_type( e ) ) );
359  }
360 
364  constexpr Flags operator |( unsigned m ) const
365  {
366  return Flags( enum_type( m_flags | m ) );
367  }
368 
372  constexpr Flags operator ^( Flags f ) const
373  {
374  return Flags( enum_type( m_flags ^ f.m_flags ) );
375  }
376 
380  constexpr Flags operator ^( enum_type e ) const
381  {
382  return Flags( enum_type( m_flags ^ flag_type( e ) ) );
383  }
384 
388  constexpr Flags operator ^( unsigned m ) const
389  {
390  return Flags( enum_type( m_flags ^ m ) );
391  }
392 
393 private:
394 
395  flag_type m_flags = flag_type( 0 );
396 
397 #ifndef __PCL_NO_FLAGS_SETTINGS_IO
398  friend class Settings;
399 #endif
400 
401 #ifndef __PCL_NO_FLAGS_FILE_IO
402  friend class File;
403 #endif
404 
405 #ifdef __PCL_BUILDING_PIXINSIGHT_APPLICATION
406  friend class pi::GlobalSettings;
407 #endif
408 };
409 
410 // ----------------------------------------------------------------------------
411 
412 } // pcl
413 
414 #endif // __PCL_Flags_h
415 
416 // ----------------------------------------------------------------------------
417 // EOF pcl/Flags.h - Released 2024-06-18T15:48:54Z
A platform-independent interface to the local file system.
Definition: File.h:344
A type-safe collection of enumerated flags.
Definition: Flags.h:85
void Clear()
Definition: Flags.h:272
Flags(const Flags &)=default
constexpr Flags(flag_type m)
Definition: Flags.h:119
constexpr Flags(enum_type e)
Definition: Flags.h:110
constexpr bool IsFlagSet(enum_type e) const
Definition: Flags.h:244
void ClearFlag(enum_type e)
Definition: Flags.h:264
constexpr flag_type AsInteger() const
Definition: Flags.h:307
E enum_type
Definition: Flags.h:94
Flags()=default
void SetFlag(enum_type e, bool on=true)
Definition: Flags.h:252
typename FlagType< std::is_unsigned< enum_type >::value >::type flag_type
Definition: Flags.h:99
Persistent module settings.
Definition: Settings.h:106
Array< T, A > & operator<<(Array< T, A > &x, const V &v)
Definition: Array.h:2295
GenericImage< P > operator^(const GenericImage< P > &image, T scalar)
Definition: Image.h:17571
PCL root namespace.
Definition: AbstractImage.h:77