atlas
Bitflags.h
1 /*
2  * (C) Copyright 2013 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation
8  * nor does it submit to any jurisdiction.
9  */
10 
11 #pragma once
12 
13 #include <string>
14 
15 namespace atlas {
16 namespace util {
17 
18 //----------------------------------------------------------------------------------------------------------------------
19 
20 // Forward declaration of BitflagsView, defined further down in this file
21 namespace detail {
22 template <typename T>
23 class BitflagsView {};
24 } // namespace detail
25 
26 //----------------------------------------------------------------------------------------------------------------------
27 
29 class Bitflags {
30 public:
31  static void reset( int& flags, int bit = 0 ) { flags = bit; }
32 
33  static void set( int& flags, int bit ) { flags |= bit; }
34 
35  static void unset( int& flags, int bit ) { flags &= ( ~bit ); }
36 
37  static void toggle( int& flags, int bit ) { flags ^= bit; }
38 
39  static bool check( int flags, int bits ) { return ( flags & bits ) == bits; }
40 
41  static bool check_all( int flags, int bits ) { return ( flags & bits ) == bits; }
42 
43  static bool check_any( int flags, int bits ) { return flags & bits; }
44 
45  static std::string bitstr( int flags ) {
46  char str[9] = {0};
47  int i;
48  for ( i = 7; i >= 0; i-- ) {
49  str[i] = ( flags & 1 ) ? '1' : '0';
50  flags >>= 1;
51  }
52  return std::string( str, 9 );
53  }
54 
57  static detail::BitflagsView<int> view( int& flags );
58 
61  static detail::BitflagsView<const int> view( const int& flags );
62 };
63 
64 //----------------------------------------------------------------------------------------------------------------------
65 
66 namespace detail {
67 
68 //----------------------------------------------------------------------------------------------------------------------
69 
70 // Template specialication for constant flags. There are no functions to edit the flags
71 template <>
72 class BitflagsView<const int> {
73  const int flags_;
74 
75 public:
76  BitflagsView( const int flags ) : flags_( flags ) {}
77  bool check( int bit ) const { return Bitflags::check( flags_, bit ); }
78  bool check_all( int bit ) const { return Bitflags::check_all( flags_, bit ); }
79  bool check_any( int bit ) const { return Bitflags::check_any( flags_, bit ); }
80 };
81 
82 //----------------------------------------------------------------------------------------------------------------------
83 
84 // Template specialication for nonconst flags. There are functions to edit the flags
85 template <>
86 class BitflagsView<int> {
87  int& flags_;
88 
89 public:
90  BitflagsView( int& flags ) : flags_( flags ) {}
91  void reset( int bit = 0 ) { Bitflags::reset( flags_, bit ); }
92  void set( int bit ) { Bitflags::set( flags_, bit ); }
93  void unset( int bit ) { Bitflags::unset( flags_, bit ); }
94  void toggle( int bit ) { Bitflags::toggle( flags_, bit ); }
95  bool check( int bit ) const { return Bitflags::check( flags_, bit ); }
96  bool check_all( int bit ) const { return Bitflags::check_all( flags_, bit ); }
97  bool check_any( int bit ) const { return Bitflags::check_any( flags_, bit ); }
98 };
99 
100 //----------------------------------------------------------------------------------------------------------------------
101 
102 } // namespace detail
103 
104 //----------------------------------------------------------------------------------------------------------------------
105 
107  return detail::BitflagsView<const int>( flags );
108 }
110  return detail::BitflagsView<int>( flags );
111 }
112 
113 //----------------------------------------------------------------------------------------------------------------------
114 
115 } // namespace util
116 } // namespace atlas
Definition: Bitflags.h:23
Contains all atlas classes and methods.
Definition: atlas-grids.cc:33
Convenience class to modify and interpret bitflags.
Definition: Bitflags.h:29
static detail::BitflagsView< int > view(int &flags)
Create convenience accessor to modify flags.
Definition: Bitflags.h:109