Zane Shelley | 871adec | 2019-07-30 11:01:39 -0500 | [diff] [blame] | 1 | #pragma once |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 2 | |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 3 | #include <stdint.h> |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 4 | |
Zane Shelley | 871adec | 2019-07-30 11:01:39 -0500 | [diff] [blame] | 5 | namespace libhei |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 6 | { |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 7 | |
| 8 | class BitStringBuffer; |
| 9 | |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 10 | //############################################################################## |
| 11 | // BitString class |
| 12 | //############################################################################## |
| 13 | |
| 14 | /** |
| 15 | * A BitString is general purpose class providing the ability to manipulate |
| 16 | * individual bits within an allocated section of contiguous memory. |
| 17 | * |
| 18 | * A BitString does not "own" the memory, it only accesses and manipulates the |
| 19 | * bits in the range specified. Users will need to ensure memory is allocated |
| 20 | * and deallocated appropriately. As an alternative, a BitStringBuffer is a |
| 21 | * BitString that will allocate and maintain its own memory. |
| 22 | * |
| 23 | * The length of a BitString is only limited by the amount of memory that |
| 24 | * contains the data buffer. |
| 25 | * |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 26 | * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 27 | * |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 28 | * - The bit positions are ordered 0 to n (left to right), where n is the bit |
| 29 | * length minus one. |
| 30 | * - The data stored in memory is assumed to be in big-endian byte format. |
| 31 | * |
| 32 | * So, for example: |
| 33 | * |
| 34 | * uint8_t a[2]; // 16 bits of memory |
| 35 | * BitString bs { 16, a }; // init BitString for a |
| 36 | * bs.setFieldRight( 0, 16, 0x1122 ); // set all 16 bits to 0x1122 |
| 37 | * |
| 38 | * Results in: |
| 39 | * |
| 40 | * a[0] == bs.getFieldRight(0, 8) (i.e. 0x11) |
| 41 | * a[1] == bs.getFieldRight(8, 8) (i.e. 0x22) |
| 42 | * |
| 43 | * It is very important you do NOT do this: |
| 44 | * |
| 45 | * uint16_t x = 0x1122; // 16 bits of memory |
| 46 | * BitString bs { 16, &x }; // init BitString for x |
| 47 | * |
| 48 | * The results are undefined, or at least not portable. For example: |
| 49 | * |
| 50 | * Big-endian: |
| 51 | * x is stored in memory as |0x11|0x22|. |
| 52 | * Therefore, bs.getFieldRight(0, 8) returns 0x11. |
| 53 | * |
| 54 | * Little-endian: |
| 55 | * x is stored in memory as |0x22|0x11|. |
| 56 | * Therefore, bs.getFieldRight(0, 8) returns 0x22. |
| 57 | * |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 58 | */ |
| 59 | class BitString |
| 60 | { |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 61 | private: // constants |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 62 | |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 63 | static const uint32_t UINT64_BIT_LEN; |
| 64 | static const uint32_t UINT8_BIT_LEN; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 65 | |
| 66 | public: // functions |
| 67 | |
| 68 | /** |
| 69 | * @brief Constructor |
| 70 | * @param i_bitLen The number of bits in the bit string. |
| 71 | * @param i_bufAddr The starting address of the memory buffer. |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 72 | * @param i_offset By default, position 0 will be the first bit of the |
| 73 | * buffer's start address. However, this parameter can be |
| 74 | * used to indicate that position 0 actually starts |
| 75 | * somewhere in the middle of the buffer. |
| 76 | * @pre Use getMinBytes() to calulate the minimum number of bytes needed |
| 77 | * to allocate sufficient memory space for this bit string. |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 78 | */ |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 79 | BitString( uint32_t i_bitLen, void * i_bufAddr, |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 80 | uint32_t i_offset = 0 ) : |
| 81 | iv_bitLen(i_bitLen), iv_bufAddr(i_bufAddr), iv_offset(i_offset) |
| 82 | {} |
| 83 | |
| 84 | /** @brief Destructor */ |
| 85 | virtual ~BitString() {} |
| 86 | |
| 87 | /** @return The number of bits in the bit string buffer. */ |
| 88 | uint32_t getBitLen() const { return iv_bitLen; } |
| 89 | |
| 90 | /** @return The address of the bit string buffer. Note that this may |
| 91 | * return nullptr. */ |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 92 | void * getBufAddr() const { return iv_bufAddr; } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * @param i_bitLen The number of bits for a bit string. |
| 96 | * @param i_offset Optional starting position of the bit string within the |
| 97 | * memory buffer. |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 98 | * @return The minimum number of bytes required to allocate sufficient |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 99 | * memory space for a bit string. |
| 100 | */ |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 101 | static uint32_t getMinBytes( uint32_t i_bitLen, uint32_t i_offset = 0 ) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 102 | { |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 103 | return (i_bitLen + i_offset + UINT8_BIT_LEN-1) / UINT8_BIT_LEN; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @brief Returns a left-justified value of the given length from the bit |
| 108 | * string starting at the given position. |
| 109 | * @param i_pos The starting position of the target range. |
| 110 | * @param i_len The number of bits of the target range. |
| 111 | * @return The value of the field range specified (left-justified). |
| 112 | * @pre nullptr != getBufAddr() |
| 113 | * @pre 0 < i_len |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 114 | * @pre i_len <= UINT64_BIT_LEN |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 115 | * @pre i_pos + i_len <= getBitLen() |
| 116 | */ |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 117 | uint64_t getFieldLeft( uint32_t i_pos, uint32_t i_len ) const |
| 118 | { |
| 119 | return getFieldRight(i_pos, i_len) << (UINT64_BIT_LEN - i_len); |
| 120 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * @brief Returns a right-justified value of the given length from the bit |
| 124 | * string starting at the given position. |
| 125 | * @param i_pos The starting position of the target range. |
| 126 | * @param i_len The number of bits of the target range. |
| 127 | * @return The value of the field range specified (right-justified). |
| 128 | * @pre nullptr != getBufAddr() |
| 129 | * @pre 0 < i_len |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 130 | * @pre i_len <= UINT64_BIT_LEN |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 131 | * @pre i_pos + i_len <= getBitLen() |
| 132 | */ |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 133 | uint64_t getFieldRight( uint32_t i_pos, uint32_t i_len ) const; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * @brief Sets a left-justified value of the given length into the bit |
| 137 | * string starting at the given position. |
| 138 | * @param i_pos The starting position of the target range. |
| 139 | * @param i_len The number of bits of the target range. |
| 140 | * @param i_val The left-justified value to set. |
| 141 | * @pre nullptr != getBufAddr() |
| 142 | * @pre 0 < i_len |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 143 | * @pre i_len <= UINT64_BIT_LEN |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 144 | * @pre i_pos + i_len <= getBitLen() |
| 145 | */ |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 146 | void setFieldLeft( uint32_t i_pos, uint32_t i_len, uint64_t i_val ); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * @brief Sets a right-justified value of the given length into the bit |
| 150 | * string starting at the given position. |
| 151 | * @param i_pos The starting position of the target range. |
| 152 | * @param i_len The number of bits of the target range. |
| 153 | * @param i_val The right-justified value to set. |
| 154 | * @pre nullptr != getBufAddr() |
| 155 | * @pre 0 < i_len |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 156 | * @pre i_len <= UINT64_BIT_LEN |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 157 | * @pre i_pos + i_len <= getBitLen() |
| 158 | */ |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 159 | void setFieldRight( uint32_t i_pos, uint32_t i_len, uint64_t i_val ) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 160 | { |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 161 | setFieldLeft( i_pos, i_len, i_val << (UINT64_BIT_LEN - i_len) ); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @param i_pos The target position. |
| 166 | * @return True if the bit at the given position is set(1), false otherwise. |
| 167 | * @pre i_pos < getBitLen(). |
| 168 | */ |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 169 | bool isBitSet( uint32_t i_pos ) const |
| 170 | { |
| 171 | return 0 != getFieldRight(i_pos, 1); |
| 172 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 173 | |
| 174 | /** |
| 175 | * @brief Sets the target position to 1. |
| 176 | * @param i_pos The target position. |
| 177 | * @pre i_pos < getBitLen(). |
| 178 | */ |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 179 | void setBit( uint32_t i_pos ) { setFieldRight( i_pos, 1, 1 ); } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 180 | |
| 181 | /** @brief Sets the entire bit string to 1's. */ |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 182 | void setAll() { setPattern(UINT64_MAX); } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 183 | |
| 184 | /** |
| 185 | * @brief Sets the target position to 0. |
| 186 | * @param i_pos The target position. |
| 187 | * @pre i_pos < getBitLen(). |
| 188 | */ |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 189 | void clearBit( uint32_t i_pos ) { setFieldRight( i_pos, 1, 0 ); } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 190 | |
| 191 | /** @brief Sets the entire bit string to 0's. */ |
| 192 | void clearAll() { setPattern(0); } |
| 193 | |
| 194 | /** |
| 195 | * @brief Sets a range within the string based on the pattern and length |
| 196 | * provided. |
| 197 | * @param i_sPos Starting position of this string. |
| 198 | * @param i_sLen The length of the target range. |
| 199 | * @param i_pattern The pattern to set (right justified). |
| 200 | * @param i_pLen The length of the pattern. |
| 201 | * @pre nullptr != getBufAddr() |
| 202 | * @pre 0 < i_sLen |
| 203 | * @pre i_sPos + i_sLen <= getBitLen() |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 204 | * @pre 0 < i_pLen <= UINT64_BIT_LEN |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 205 | * @post The pattern is repeated/truncated as needed. |
| 206 | * |
| 207 | * Examples: i_sPos(0), i_sLen(10), i_pattern(0xA), i_pLen(4) |
| 208 | * Old String: 0000000000 |
| 209 | * New String: 1010101010 |
| 210 | * |
| 211 | * i_sPos(3), i_sLen(4), i_pattern(0x3), i_pLen(3) |
| 212 | * Old String: 0001001000 |
| 213 | * New String: 0000110000 |
| 214 | */ |
| 215 | void setPattern( uint32_t i_sPos, uint32_t i_sLen, |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 216 | uint64_t i_pattern, uint32_t i_pLen ); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 217 | |
| 218 | /** |
| 219 | * @brief Sets entire string based on the pattern and length provided. |
| 220 | * @param i_pattern The pattern to set (right justified). |
| 221 | * @param i_pLen The length of the pattern. |
| 222 | * @note See definition above for prerequisites. |
| 223 | * @post The entire string is filled with the pattern. |
| 224 | * @post The pattern is repeated/truncated as needed. |
| 225 | */ |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 226 | void setPattern( uint64_t i_pattern, uint32_t i_pLen ) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 227 | { |
| 228 | setPattern( 0, getBitLen(), i_pattern, i_pLen ); |
| 229 | } |
| 230 | |
| 231 | /** |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 232 | * @brief Sets entire string based on the pattern provided. |
| 233 | * @param i_pattern The pattern to set (right justified). |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 234 | * @note See definition above for prerequisites. |
| 235 | * @post The entire string is filled with the pattern. |
| 236 | * @post The pattern is repeated/truncated as needed. |
| 237 | */ |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 238 | void setPattern( uint64_t i_pattern ) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 239 | { |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 240 | setPattern( i_pattern, sizeof(i_pattern) * 8 ); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** |
| 244 | * @brief Set bits in this string based on the given string. |
| 245 | * @param i_sStr The source string. |
| 246 | * @param i_sPos The starting position of the source string. |
| 247 | * @param i_sLen The number of bits to copy from the source string. |
| 248 | * @param i_dPos The starting position of the this string. |
| 249 | * @pre nullptr != getBufAddr() |
| 250 | * @pre nullptr != i_sStr.getBufAddr() |
| 251 | * @pre 0 < i_sLen |
| 252 | * @pre i_sPos + i_sLen <= i_sStr.getBitLen() |
| 253 | * @pre i_dPos < getBitLen() |
| 254 | * @post Source bits in given range are copied to this starting at i_dPos. |
| 255 | * @note If the length of the given string is greater than the length of |
| 256 | * this string, then the extra bits are ignored. |
| 257 | * @note If the length of the given string is less than the length of this |
| 258 | * string, then the extra bits in this string are not modified. |
| 259 | * @note This string and the source string may specify overlapping memory. |
| 260 | */ |
| 261 | void setString( const BitString & i_sStr, uint32_t i_sPos, |
| 262 | uint32_t i_sLen, uint32_t i_dPos = 0 ); |
| 263 | |
| 264 | /** |
| 265 | * @brief Set bits in this string based on the provided string. |
| 266 | * @param i_sStr The source string. |
| 267 | * @note This will try to copy as much of the source as possible to this |
| 268 | * string, starting with the first bit in each string. |
| 269 | * @note See the other definition of this function for details and |
| 270 | * restrictions. |
| 271 | */ |
| 272 | void setString( const BitString & i_sStr ) |
| 273 | { |
| 274 | setString( i_sStr, 0, i_sStr.getBitLen() ); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * @brief Masks (clears) any bits set in this string that correspond to bits |
| 279 | * set in the given string (this & ~mask). |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 280 | * @param i_mask The mask string (right justified). |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 281 | * @note If the length of the given string is greater than the length of |
| 282 | * this string, then the extra bits are ignored. |
| 283 | * @note If the length of the given string is less than the length of this |
| 284 | * string, then the extra bits in this string are not modified. |
| 285 | */ |
| 286 | void maskString( const BitString & i_mask ); |
| 287 | |
| 288 | /** |
| 289 | * @param i_str The string to compare. |
| 290 | * @return True if the strings are equivalent, false otherwise. |
| 291 | * @pre Both strings must be of equal length and have same values to be |
| 292 | * equal. |
| 293 | */ |
| 294 | bool isEqual( const BitString & i_str ) const; |
| 295 | |
| 296 | /** @return True if there are no bit set(1) in this bit string, false |
| 297 | * otherwise. */ |
| 298 | bool isZero() const; |
| 299 | |
| 300 | /** |
| 301 | * @param i_pos The starting position of the target range. |
| 302 | * @param i_len The length of the target range. |
| 303 | * @return The number of bits that are set(1) in given range of this string. |
| 304 | * @pre nullptr != getBufAddr() |
| 305 | * @pre i_pos + i_len <= getBitLen() |
| 306 | */ |
| 307 | uint32_t getSetCount( uint32_t i_pos, uint32_t i_len ) const; |
| 308 | |
| 309 | /** @return The number of bits that are set(1) in this string. */ |
| 310 | uint32_t getSetCount() const { return getSetCount( 0, getBitLen() ); } |
| 311 | |
| 312 | /** @brief Comparison operator. */ |
| 313 | bool operator==( const BitString & i_str ) const { return isEqual(i_str); } |
| 314 | |
| 315 | /** @brief Bitwise NOT operator. */ |
| 316 | BitStringBuffer operator~() const; |
| 317 | |
| 318 | /** @brief Bitwise AND operator. */ |
| 319 | BitStringBuffer operator&( const BitString & i_bs ) const; |
| 320 | |
| 321 | /** @brief Bitwise OR operator. */ |
| 322 | BitStringBuffer operator|( const BitString & i_bs ) const; |
| 323 | |
| 324 | /** @brief Right shift operator. */ |
| 325 | BitStringBuffer operator>>( uint32_t i_shift ) const; |
| 326 | |
| 327 | /** @brief Left shift operator. */ |
| 328 | BitStringBuffer operator<<( uint32_t i_shift ) const; |
| 329 | |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 330 | /** |
| 331 | * @brief Explicitly disables assignment from BitStringBuffer. |
| 332 | * |
| 333 | * Allowing this would be dangerous if the BitStringBuffer goes out of scope |
| 334 | * because the BitString would point to memory that is no longer in context. |
| 335 | */ |
| 336 | BitString & operator=( const BitStringBuffer & i_bsb ) = delete; |
| 337 | |
| 338 | /** |
| 339 | * @brief Explicitly disables copy from BitStringBuffer. |
| 340 | * |
| 341 | * Allowing this would be dangerous if the BitStringBuffer goes out of scope |
| 342 | * because the BitString would point to memory that is no longer in context. |
| 343 | */ |
| 344 | BitString( const BitStringBuffer & i_bsb ) = delete; |
| 345 | |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 346 | protected: // functions |
| 347 | |
| 348 | /** |
| 349 | * @param i_newBufAddr The starting address of the new bit string buffer. |
| 350 | * @pre Before calling this function, make sure you deallocate the old |
| 351 | * buffer to avoid memory leaks. |
| 352 | */ |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 353 | void setBufAddr( void * i_newBufAddr ) { iv_bufAddr = i_newBufAddr; } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 354 | |
| 355 | /** @param i_newBitLen The new bit length of this bit string buffer. */ |
| 356 | void setBitLen( uint32_t i_newBitLen ) { iv_bitLen = i_newBitLen; } |
| 357 | |
| 358 | private: // functions |
| 359 | |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 360 | /** |
| 361 | * @brief Given a bit position within the bit string, this function returns |
| 362 | * the address that contains the bit position and the bit position |
| 363 | * relative to that address. |
| 364 | * @param o_relPos The returned relative position. |
| 365 | * @param i_absPos The inputted absolute position. |
| 366 | * @return The relative address. |
| 367 | * @pre nullptr != getBufAddr() |
| 368 | * @pre i_absPos < getBitLen() |
| 369 | */ |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 370 | uint8_t * getRelativePosition( uint32_t & o_relPos, |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 371 | uint32_t i_absPos ) const; |
| 372 | |
| 373 | private: // instance variables |
| 374 | |
| 375 | uint32_t iv_bitLen; ///< The bit length of this buffer. |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 376 | void * iv_bufAddr; ///< The beginning address of this buffer. |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 377 | uint32_t iv_offset; ///< Start position offset |
| 378 | }; |
| 379 | |
| 380 | //############################################################################## |
| 381 | // BitStringBuffer class |
| 382 | //############################################################################## |
| 383 | |
| 384 | /** A BitStringBuffer is a BitString that maintains its own buffer in memory. It |
| 385 | * guarantees that sufficient memory is allocated and deallocated in the |
| 386 | * constructor and destructor, respectively. In addition, the assignment |
| 387 | * operator will adjust the amount of memory needed, as necessary, for the |
| 388 | * assignment. */ |
| 389 | class BitStringBuffer : public BitString |
| 390 | { |
| 391 | public: // functions |
| 392 | |
| 393 | /** |
| 394 | * @brief Constructor |
| 395 | * @param i_bitLen Number of bits in the string. |
| 396 | */ |
| 397 | explicit BitStringBuffer( uint32_t i_bitLen ); |
| 398 | |
| 399 | /** @brief Destructor */ |
| 400 | ~BitStringBuffer(); |
| 401 | |
| 402 | /** @brief Copy constructor from BitString */ |
| 403 | BitStringBuffer( const BitString & i_bs ); |
| 404 | |
| 405 | /** @brief Copy constructor from BitStringBuffer */ |
| 406 | BitStringBuffer( const BitStringBuffer & i_bsb ); |
| 407 | |
| 408 | /** @brief Assignment from BitString */ |
| 409 | BitStringBuffer & operator=( const BitString & i_bs ); |
| 410 | |
| 411 | /** @brief Assignment from BitStringBuffer */ |
| 412 | BitStringBuffer & operator=( const BitStringBuffer & i_bsb ); |
| 413 | |
| 414 | private: // functions |
| 415 | |
| 416 | /** @brief Deallocates the old buffer, if needed, and initializes the new |
| 417 | * buffer. */ |
| 418 | void initBuffer(); |
| 419 | }; |
| 420 | |
Zane Shelley | 871adec | 2019-07-30 11:01:39 -0500 | [diff] [blame] | 421 | } // end namespace libhei |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 422 | |