| 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 |
/// @brief Convenience class to modify and interpret bitflags |
| 21 |
class Bitflags { |
| 22 |
public: |
| 23 |
static void reset(int& flags, int bit = 0) { flags = bit; } |
| 24 |
|
| 25 |
static void set(int& flags, int bit) { flags |= bit; } |
| 26 |
|
| 27 |
static void unset(int& flags, int bit) { flags &= (~bit); } |
| 28 |
|
| 29 |
static void toggle(int& flags, int bit) { flags ^= bit; } |
| 30 |
|
| 31 |
static bool check(int flags, int bits) { return (flags & bits) == bits; } |
| 32 |
|
| 33 |
static bool check_all(int flags, int bits) { return (flags & bits) == bits; } |
| 34 |
|
| 35 |
static bool check_any(int flags, int bits) { return flags & bits; } |
| 36 |
|
| 37 |
static std::string bitstr(int flags) { |
| 38 |
char str[9] = {0}; |
| 39 |
int i; |
| 40 |
for (i = 7; i >= 0; i--) { |
| 41 |
str[i] = (flags & 1) ? '1' : '0'; |
| 42 |
flags >>= 1; |
| 43 |
} |
| 44 |
return std::string(str, 9); |
| 45 |
} |
| 46 |
|
| 47 |
/// @brief Create convenience accessor to modify flags |
| 48 |
/// @note `auto` for return type! BitflagsView is a detail |
| 49 |
static auto view(int& flags); |
| 50 |
|
| 51 |
/// @brief Create convenience accessor to modify flags |
| 52 |
/// @note `auto` for return type! BitflagsView is a detail |
| 53 |
static auto view(const int& flags); |
| 54 |
}; |
| 55 |
|
| 56 |
//---------------------------------------------------------------------------------------------------------------------- |
| 57 |
|
| 58 |
namespace detail { |
| 59 |
|
| 60 |
//---------------------------------------------------------------------------------------------------------------------- |
| 61 |
|
| 62 |
template <typename T> |
| 63 |
class BitflagsView {}; |
| 64 |
|
| 65 |
//---------------------------------------------------------------------------------------------------------------------- |
| 66 |
|
| 67 |
// Template specialication for constant flags. There are no functions to edit the flags |
| 68 |
template <> |
| 69 |
class BitflagsView<const int> { |
| 70 |
const int flags_; |
| 71 |
|
| 72 |
public: |
| 73 |
BitflagsView(const int flags): flags_(flags) {} |
| 74 |
bool check(int bit) const { return Bitflags::check(flags_, bit); } |
| 75 |
bool check_all(int bit) const { return Bitflags::check_all(flags_, bit); } |
| 76 |
bool check_any(int bit) const { return Bitflags::check_any(flags_, bit); } |
| 77 |
}; |
| 78 |
|
| 79 |
//---------------------------------------------------------------------------------------------------------------------- |
| 80 |
|
| 81 |
// Template specialication for nonconst flags. There are functions to edit the flags |
| 82 |
template <> |
| 83 |
class BitflagsView<int> { |
| 84 |
int& flags_; |
| 85 |
|
| 86 |
public: |
| 87 |
BitflagsView(int& flags): flags_(flags) {} |
| 88 |
void reset(int bit = 0) { Bitflags::reset(flags_, bit); } |
| 89 |
void set(int bit) { Bitflags::set(flags_, bit); } |
| 90 |
void unset(int bit) { Bitflags::unset(flags_, bit); } |
| 91 |
void toggle(int bit) { Bitflags::toggle(flags_, bit); } |
| 92 |
bool check(int bit) const { return Bitflags::check(flags_, bit); } |
| 93 |
bool check_all(int bit) const { return Bitflags::check_all(flags_, bit); } |
| 94 |
bool check_any(int bit) const { return Bitflags::check_any(flags_, bit); } |
| 95 |
}; |
| 96 |
|
| 97 |
//---------------------------------------------------------------------------------------------------------------------- |
| 98 |
|
| 99 |
} // namespace detail |
| 100 |
|
| 101 |
//---------------------------------------------------------------------------------------------------------------------- |
| 102 |
|
| 103 |
inline auto Bitflags::view(const int& flags) { |
| 104 |
return detail::BitflagsView<const int>(flags); |
| 105 |
} |
| 106 |
inline auto Bitflags::view(int& flags) { |
| 107 |
return detail::BitflagsView<int>(flags); |
| 108 |
} |
| 109 |
|
| 110 |
//---------------------------------------------------------------------------------------------------------------------- |
| 111 |
|
| 112 |
} // namespace util |
| 113 |
} // namespace atlas |