Device interface library for multiple platforms including Mbed.
Dependents: DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#
Maxim Interface is a library framework focused on providing flexible and expressive hardware interfaces. Both communication interfaces such as I2C and 1-Wire and device interfaces such as DS18B20 are supported. Modern C++ concepts are used extensively while keeping compatibility with C++98/C++03 and requiring no external dependencies. The embedded-friendly design does not depend on exceptions or RTTI.
The full version of the project is hosted on GitLab: https://gitlab.com/iabenz/MaximInterface
MaximInterfaceCore/FlagSet.hpp@12:7eb41621ba22, 2020-05-29 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Fri May 29 16:19:22 2020 -0500
- Revision:
- 12:7eb41621ba22
- Parent:
- 11:3f3bf6bf5e6c
Updated to version 2.2.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IanBenzMaxim | 7:9cd16581b578 | 1 | /******************************************************************************* |
IanBenzMaxim | 8:5ea891c7d1a1 | 2 | * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved. |
IanBenzMaxim | 7:9cd16581b578 | 3 | * |
IanBenzMaxim | 7:9cd16581b578 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
IanBenzMaxim | 7:9cd16581b578 | 5 | * copy of this software and associated documentation files (the "Software"), |
IanBenzMaxim | 7:9cd16581b578 | 6 | * to deal in the Software without restriction, including without limitation |
IanBenzMaxim | 7:9cd16581b578 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
IanBenzMaxim | 7:9cd16581b578 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
IanBenzMaxim | 7:9cd16581b578 | 9 | * Software is furnished to do so, subject to the following conditions: |
IanBenzMaxim | 7:9cd16581b578 | 10 | * |
IanBenzMaxim | 7:9cd16581b578 | 11 | * The above copyright notice and this permission notice shall be included |
IanBenzMaxim | 7:9cd16581b578 | 12 | * in all copies or substantial portions of the Software. |
IanBenzMaxim | 7:9cd16581b578 | 13 | * |
IanBenzMaxim | 7:9cd16581b578 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
IanBenzMaxim | 7:9cd16581b578 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
IanBenzMaxim | 7:9cd16581b578 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
IanBenzMaxim | 7:9cd16581b578 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
IanBenzMaxim | 7:9cd16581b578 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
IanBenzMaxim | 7:9cd16581b578 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
IanBenzMaxim | 7:9cd16581b578 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
IanBenzMaxim | 7:9cd16581b578 | 21 | * |
IanBenzMaxim | 7:9cd16581b578 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
IanBenzMaxim | 7:9cd16581b578 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
IanBenzMaxim | 7:9cd16581b578 | 24 | * Products, Inc. Branding Policy. |
IanBenzMaxim | 7:9cd16581b578 | 25 | * |
IanBenzMaxim | 7:9cd16581b578 | 26 | * The mere transfer of this software does not imply any licenses |
IanBenzMaxim | 7:9cd16581b578 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
IanBenzMaxim | 7:9cd16581b578 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
IanBenzMaxim | 7:9cd16581b578 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
IanBenzMaxim | 7:9cd16581b578 | 30 | * ownership rights. |
IanBenzMaxim | 7:9cd16581b578 | 31 | *******************************************************************************/ |
IanBenzMaxim | 7:9cd16581b578 | 32 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 33 | #ifndef MaximInterfaceCore_FlagSet_hpp |
IanBenzMaxim | 8:5ea891c7d1a1 | 34 | #define MaximInterfaceCore_FlagSet_hpp |
IanBenzMaxim | 7:9cd16581b578 | 35 | |
IanBenzMaxim | 7:9cd16581b578 | 36 | #include <stddef.h> |
IanBenzMaxim | 7:9cd16581b578 | 37 | #include <bitset> |
IanBenzMaxim | 7:9cd16581b578 | 38 | |
IanBenzMaxim | 7:9cd16581b578 | 39 | namespace MaximInterfaceCore { |
IanBenzMaxim | 7:9cd16581b578 | 40 | |
IanBenzMaxim | 7:9cd16581b578 | 41 | /// @brief |
IanBenzMaxim | 7:9cd16581b578 | 42 | /// Provides functionality similar to std::bitset except using a bit flag, |
IanBenzMaxim | 7:9cd16581b578 | 43 | /// typically of an enum type, as the indexer. |
IanBenzMaxim | 7:9cd16581b578 | 44 | template <typename Flag, size_t flagBits> class FlagSet { |
IanBenzMaxim | 7:9cd16581b578 | 45 | public: |
IanBenzMaxim | 7:9cd16581b578 | 46 | class reference { |
IanBenzMaxim | 7:9cd16581b578 | 47 | public: |
IanBenzMaxim | 7:9cd16581b578 | 48 | reference(FlagSet & flagSet, Flag flag) : flagSet(&flagSet), flag(flag) {} |
IanBenzMaxim | 7:9cd16581b578 | 49 | |
IanBenzMaxim | 7:9cd16581b578 | 50 | reference & operator=(bool x) { |
IanBenzMaxim | 7:9cd16581b578 | 51 | flagSet->set(flag, x); |
IanBenzMaxim | 7:9cd16581b578 | 52 | return *this; |
IanBenzMaxim | 7:9cd16581b578 | 53 | } |
IanBenzMaxim | 7:9cd16581b578 | 54 | |
IanBenzMaxim | 7:9cd16581b578 | 55 | reference & operator=(const reference & x) { |
IanBenzMaxim | 7:9cd16581b578 | 56 | return operator=(static_cast<bool>(x)); |
IanBenzMaxim | 7:9cd16581b578 | 57 | } |
IanBenzMaxim | 7:9cd16581b578 | 58 | |
IanBenzMaxim | 7:9cd16581b578 | 59 | operator bool() const { return flagSet->test(flag); } |
IanBenzMaxim | 7:9cd16581b578 | 60 | |
IanBenzMaxim | 11:3f3bf6bf5e6c | 61 | bool operator~() const { return !(*this); } |
IanBenzMaxim | 7:9cd16581b578 | 62 | |
IanBenzMaxim | 7:9cd16581b578 | 63 | reference & flip() { |
IanBenzMaxim | 11:3f3bf6bf5e6c | 64 | flagSet->flip(flag); |
IanBenzMaxim | 7:9cd16581b578 | 65 | return *this; |
IanBenzMaxim | 7:9cd16581b578 | 66 | } |
IanBenzMaxim | 7:9cd16581b578 | 67 | |
IanBenzMaxim | 7:9cd16581b578 | 68 | private: |
IanBenzMaxim | 7:9cd16581b578 | 69 | FlagSet * flagSet; |
IanBenzMaxim | 7:9cd16581b578 | 70 | Flag flag; |
IanBenzMaxim | 7:9cd16581b578 | 71 | }; |
IanBenzMaxim | 7:9cd16581b578 | 72 | |
IanBenzMaxim | 7:9cd16581b578 | 73 | FlagSet() : bits() {} |
IanBenzMaxim | 7:9cd16581b578 | 74 | |
IanBenzMaxim | 7:9cd16581b578 | 75 | FlagSet(unsigned long val) : bits(val) {} |
IanBenzMaxim | 7:9cd16581b578 | 76 | |
IanBenzMaxim | 7:9cd16581b578 | 77 | template <typename CharT, typename Traits, typename Alloc> |
IanBenzMaxim | 7:9cd16581b578 | 78 | explicit FlagSet( |
IanBenzMaxim | 7:9cd16581b578 | 79 | const std::basic_string<CharT, Traits, Alloc> & str, |
IanBenzMaxim | 7:9cd16581b578 | 80 | typename std::basic_string<CharT, Traits, Alloc>::size_type pos = 0, |
IanBenzMaxim | 7:9cd16581b578 | 81 | typename std::basic_string<CharT, Traits, Alloc>::size_type n = |
IanBenzMaxim | 7:9cd16581b578 | 82 | std::basic_string<CharT, Traits, Alloc>::npos) |
IanBenzMaxim | 7:9cd16581b578 | 83 | : bits(str, pos, n) {} |
IanBenzMaxim | 7:9cd16581b578 | 84 | |
IanBenzMaxim | 7:9cd16581b578 | 85 | bool operator==(const FlagSet & rhs) const { return bits == rhs.bits; } |
IanBenzMaxim | 7:9cd16581b578 | 86 | |
IanBenzMaxim | 7:9cd16581b578 | 87 | bool operator!=(const FlagSet & rhs) const { return bits != rhs.bits; } |
IanBenzMaxim | 7:9cd16581b578 | 88 | |
IanBenzMaxim | 7:9cd16581b578 | 89 | /// @name Element access |
IanBenzMaxim | 7:9cd16581b578 | 90 | /// @{ |
IanBenzMaxim | 7:9cd16581b578 | 91 | |
IanBenzMaxim | 7:9cd16581b578 | 92 | bool operator[](Flag flag) const { return test(flag); } |
IanBenzMaxim | 7:9cd16581b578 | 93 | |
IanBenzMaxim | 7:9cd16581b578 | 94 | reference operator[](Flag flag) { return reference(*this, flag); } |
IanBenzMaxim | 7:9cd16581b578 | 95 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 96 | bool test(Flag flag) const { |
IanBenzMaxim | 8:5ea891c7d1a1 | 97 | const unsigned long rawFlag = static_cast<unsigned long>(flag); |
IanBenzMaxim | 8:5ea891c7d1a1 | 98 | return (bits.to_ulong() & rawFlag) == rawFlag; |
IanBenzMaxim | 8:5ea891c7d1a1 | 99 | } |
IanBenzMaxim | 7:9cd16581b578 | 100 | |
IanBenzMaxim | 7:9cd16581b578 | 101 | bool any() const { return bits.any(); } |
IanBenzMaxim | 7:9cd16581b578 | 102 | |
IanBenzMaxim | 7:9cd16581b578 | 103 | bool none() const { return bits.none(); } |
IanBenzMaxim | 7:9cd16581b578 | 104 | |
IanBenzMaxim | 7:9cd16581b578 | 105 | size_t count() const { return bits.count(); } |
IanBenzMaxim | 7:9cd16581b578 | 106 | |
IanBenzMaxim | 7:9cd16581b578 | 107 | /// @} |
IanBenzMaxim | 7:9cd16581b578 | 108 | |
IanBenzMaxim | 7:9cd16581b578 | 109 | /// @name Capacity |
IanBenzMaxim | 7:9cd16581b578 | 110 | /// @{ |
IanBenzMaxim | 7:9cd16581b578 | 111 | |
IanBenzMaxim | 7:9cd16581b578 | 112 | size_t size() const { return bits.size(); } |
IanBenzMaxim | 7:9cd16581b578 | 113 | |
IanBenzMaxim | 7:9cd16581b578 | 114 | /// @} |
IanBenzMaxim | 7:9cd16581b578 | 115 | |
IanBenzMaxim | 7:9cd16581b578 | 116 | /// @name Modifiers |
IanBenzMaxim | 7:9cd16581b578 | 117 | /// @{ |
IanBenzMaxim | 7:9cd16581b578 | 118 | |
IanBenzMaxim | 7:9cd16581b578 | 119 | FlagSet & operator&=(const FlagSet & other) { |
IanBenzMaxim | 7:9cd16581b578 | 120 | bits &= other.bits; |
IanBenzMaxim | 7:9cd16581b578 | 121 | return *this; |
IanBenzMaxim | 7:9cd16581b578 | 122 | } |
IanBenzMaxim | 7:9cd16581b578 | 123 | |
IanBenzMaxim | 7:9cd16581b578 | 124 | FlagSet & operator|=(const FlagSet & other) { |
IanBenzMaxim | 7:9cd16581b578 | 125 | bits |= other.bits; |
IanBenzMaxim | 7:9cd16581b578 | 126 | return *this; |
IanBenzMaxim | 7:9cd16581b578 | 127 | } |
IanBenzMaxim | 7:9cd16581b578 | 128 | |
IanBenzMaxim | 7:9cd16581b578 | 129 | FlagSet & operator^=(const FlagSet & other) { |
IanBenzMaxim | 7:9cd16581b578 | 130 | bits ^= other.bits; |
IanBenzMaxim | 7:9cd16581b578 | 131 | return *this; |
IanBenzMaxim | 7:9cd16581b578 | 132 | } |
IanBenzMaxim | 7:9cd16581b578 | 133 | |
IanBenzMaxim | 11:3f3bf6bf5e6c | 134 | FlagSet operator~() const { return FlagSet(*this).flip(); } |
IanBenzMaxim | 7:9cd16581b578 | 135 | |
IanBenzMaxim | 7:9cd16581b578 | 136 | FlagSet & set() { |
IanBenzMaxim | 7:9cd16581b578 | 137 | bits.set(); |
IanBenzMaxim | 7:9cd16581b578 | 138 | return *this; |
IanBenzMaxim | 7:9cd16581b578 | 139 | } |
IanBenzMaxim | 7:9cd16581b578 | 140 | |
IanBenzMaxim | 7:9cd16581b578 | 141 | FlagSet & set(Flag flag, bool value = true) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 142 | const unsigned long rawFlag = static_cast<unsigned long>(flag); |
IanBenzMaxim | 7:9cd16581b578 | 143 | if (value) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 144 | bits |= rawFlag; |
IanBenzMaxim | 7:9cd16581b578 | 145 | } else { |
IanBenzMaxim | 11:3f3bf6bf5e6c | 146 | bits &= static_cast<unsigned long>(~rawFlag); |
IanBenzMaxim | 7:9cd16581b578 | 147 | } |
IanBenzMaxim | 7:9cd16581b578 | 148 | return *this; |
IanBenzMaxim | 7:9cd16581b578 | 149 | } |
IanBenzMaxim | 7:9cd16581b578 | 150 | |
IanBenzMaxim | 7:9cd16581b578 | 151 | FlagSet & reset() { |
IanBenzMaxim | 7:9cd16581b578 | 152 | bits.reset(); |
IanBenzMaxim | 7:9cd16581b578 | 153 | return *this; |
IanBenzMaxim | 7:9cd16581b578 | 154 | } |
IanBenzMaxim | 7:9cd16581b578 | 155 | |
IanBenzMaxim | 7:9cd16581b578 | 156 | FlagSet & reset(Flag flag) { return set(flag, false); } |
IanBenzMaxim | 7:9cd16581b578 | 157 | |
IanBenzMaxim | 7:9cd16581b578 | 158 | FlagSet & flip() { |
IanBenzMaxim | 7:9cd16581b578 | 159 | bits.flip(); |
IanBenzMaxim | 7:9cd16581b578 | 160 | return *this; |
IanBenzMaxim | 7:9cd16581b578 | 161 | } |
IanBenzMaxim | 7:9cd16581b578 | 162 | |
IanBenzMaxim | 7:9cd16581b578 | 163 | FlagSet & flip(Flag flag) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 164 | bits ^= static_cast<unsigned long>(flag); |
IanBenzMaxim | 7:9cd16581b578 | 165 | return *this; |
IanBenzMaxim | 7:9cd16581b578 | 166 | } |
IanBenzMaxim | 7:9cd16581b578 | 167 | |
IanBenzMaxim | 7:9cd16581b578 | 168 | /// @} |
IanBenzMaxim | 7:9cd16581b578 | 169 | |
IanBenzMaxim | 7:9cd16581b578 | 170 | /// @name Conversions |
IanBenzMaxim | 7:9cd16581b578 | 171 | /// @{ |
IanBenzMaxim | 7:9cd16581b578 | 172 | |
IanBenzMaxim | 7:9cd16581b578 | 173 | template <typename CharT, typename Traits, typename Allocator> |
IanBenzMaxim | 7:9cd16581b578 | 174 | std::basic_string<CharT, Traits, Allocator> to_string() const { |
IanBenzMaxim | 7:9cd16581b578 | 175 | return bits.template to_string<CharT, Traits, Allocator>(); |
IanBenzMaxim | 7:9cd16581b578 | 176 | } |
IanBenzMaxim | 7:9cd16581b578 | 177 | |
IanBenzMaxim | 7:9cd16581b578 | 178 | unsigned long to_ulong() const { return bits.to_ulong(); } |
IanBenzMaxim | 7:9cd16581b578 | 179 | |
IanBenzMaxim | 7:9cd16581b578 | 180 | /// @} |
IanBenzMaxim | 7:9cd16581b578 | 181 | |
IanBenzMaxim | 7:9cd16581b578 | 182 | template <typename CharT, typename Traits> |
IanBenzMaxim | 7:9cd16581b578 | 183 | friend std::basic_ostream<CharT, Traits> & |
IanBenzMaxim | 11:3f3bf6bf5e6c | 184 | operator<<(std::basic_ostream<CharT, Traits> & os, const FlagSet & x) { |
IanBenzMaxim | 11:3f3bf6bf5e6c | 185 | os << x.bits; |
IanBenzMaxim | 11:3f3bf6bf5e6c | 186 | return os; |
IanBenzMaxim | 11:3f3bf6bf5e6c | 187 | } |
IanBenzMaxim | 7:9cd16581b578 | 188 | |
IanBenzMaxim | 7:9cd16581b578 | 189 | template <typename CharT, class Traits> |
IanBenzMaxim | 7:9cd16581b578 | 190 | friend std::basic_istream<CharT, Traits> & |
IanBenzMaxim | 11:3f3bf6bf5e6c | 191 | operator>>(std::basic_istream<CharT, Traits> & is, FlagSet & x) { |
IanBenzMaxim | 11:3f3bf6bf5e6c | 192 | is >> x.bits; |
IanBenzMaxim | 11:3f3bf6bf5e6c | 193 | return is; |
IanBenzMaxim | 11:3f3bf6bf5e6c | 194 | } |
IanBenzMaxim | 11:3f3bf6bf5e6c | 195 | |
IanBenzMaxim | 11:3f3bf6bf5e6c | 196 | private: |
IanBenzMaxim | 11:3f3bf6bf5e6c | 197 | std::bitset<flagBits> bits; |
IanBenzMaxim | 7:9cd16581b578 | 198 | }; |
IanBenzMaxim | 7:9cd16581b578 | 199 | |
IanBenzMaxim | 7:9cd16581b578 | 200 | template <typename Flag, size_t flagBits> |
IanBenzMaxim | 7:9cd16581b578 | 201 | FlagSet<Flag, flagBits> operator&(const FlagSet<Flag, flagBits> & lhs, |
IanBenzMaxim | 7:9cd16581b578 | 202 | const FlagSet<Flag, flagBits> & rhs) { |
IanBenzMaxim | 7:9cd16581b578 | 203 | return FlagSet<Flag, flagBits>(lhs) &= rhs; |
IanBenzMaxim | 7:9cd16581b578 | 204 | } |
IanBenzMaxim | 7:9cd16581b578 | 205 | |
IanBenzMaxim | 7:9cd16581b578 | 206 | template <typename Flag, size_t flagBits> |
IanBenzMaxim | 7:9cd16581b578 | 207 | FlagSet<Flag, flagBits> operator|(const FlagSet<Flag, flagBits> & lhs, |
IanBenzMaxim | 7:9cd16581b578 | 208 | const FlagSet<Flag, flagBits> & rhs) { |
IanBenzMaxim | 7:9cd16581b578 | 209 | return FlagSet<Flag, flagBits>(lhs) |= rhs; |
IanBenzMaxim | 7:9cd16581b578 | 210 | } |
IanBenzMaxim | 7:9cd16581b578 | 211 | |
IanBenzMaxim | 7:9cd16581b578 | 212 | template <typename Flag, size_t flagBits> |
IanBenzMaxim | 7:9cd16581b578 | 213 | FlagSet<Flag, flagBits> operator^(const FlagSet<Flag, flagBits> & lhs, |
IanBenzMaxim | 7:9cd16581b578 | 214 | const FlagSet<Flag, flagBits> & rhs) { |
IanBenzMaxim | 7:9cd16581b578 | 215 | return FlagSet<Flag, flagBits>(lhs) ^= rhs; |
IanBenzMaxim | 7:9cd16581b578 | 216 | } |
IanBenzMaxim | 7:9cd16581b578 | 217 | |
IanBenzMaxim | 7:9cd16581b578 | 218 | } // namespace MaximInterfaceCore |
IanBenzMaxim | 7:9cd16581b578 | 219 | |
IanBenzMaxim | 7:9cd16581b578 | 220 | #endif |