bitfield_dynamic.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*************************************************************************/
  2. /* bitfield_dynamic.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef BITFIELD_DYNAMIC_H
  31. #define BITFIELD_DYNAMIC_H
  32. #include "core/error_macros.h"
  33. class BitFieldDynamic {
  34. public:
  35. ~BitFieldDynamic() { destroy(); }
  36. private:
  37. // prevent copying (see effective C++ scott meyers)
  38. // there is no implementation for copy constructor, hence compiler will complain if you try to copy
  39. // feel free to add one if needed...
  40. BitFieldDynamic &operator=(const BitFieldDynamic &);
  41. public:
  42. // create automatically blanks
  43. void create(uint32_t p_num_bits, bool p_blank = true);
  44. void destroy();
  45. // public funcs
  46. uint32_t get_num_bits() const { return _num_bits; }
  47. uint32_t get_bit(uint32_t p_bit) const;
  48. void set_bit(uint32_t p_bit, uint32_t p_set);
  49. bool check_and_set(uint32_t p_bit);
  50. void blank(bool p_set_or_zero = false);
  51. void invert();
  52. void copy_from(const BitFieldDynamic &p_source);
  53. // loading / saving
  54. uint8_t *get_data() { return _data; }
  55. const uint8_t *get_data() const { return _data; }
  56. uint32_t get_num_bytes() const { return _num_bytes; }
  57. protected:
  58. // member vars
  59. uint8_t *_data = nullptr;
  60. uint32_t _num_bytes = 0;
  61. uint32_t _num_bits = 0;
  62. };
  63. inline uint32_t BitFieldDynamic::get_bit(uint32_t p_bit) const {
  64. DEV_ASSERT(_data);
  65. uint32_t byte_number = p_bit >> 3; // divide by 8
  66. DEV_ASSERT(byte_number < _num_bytes);
  67. uint8_t uc = _data[byte_number];
  68. uint32_t bit_set = uc & (1 << (p_bit & 7));
  69. return bit_set;
  70. }
  71. inline bool BitFieldDynamic::check_and_set(uint32_t p_bit) {
  72. DEV_ASSERT(_data);
  73. uint32_t byte_number = p_bit >> 3; // divide by 8
  74. DEV_ASSERT(byte_number < _num_bytes);
  75. uint8_t &uc = _data[byte_number];
  76. uint32_t mask = (1 << (p_bit & 7));
  77. uint32_t bit_set = uc & mask;
  78. if (bit_set) {
  79. return false;
  80. }
  81. // set
  82. uc = uc | mask;
  83. return true;
  84. }
  85. inline void BitFieldDynamic::set_bit(uint32_t p_bit, uint32_t p_set) {
  86. DEV_ASSERT(_data);
  87. uint32_t byte_number = p_bit >> 3; // divide by 8
  88. DEV_ASSERT(byte_number < _num_bytes);
  89. uint8_t uc = _data[byte_number];
  90. uint32_t mask = 1 << (p_bit & 7);
  91. if (p_set) {
  92. uc = uc | mask;
  93. } else {
  94. uc &= ~mask;
  95. }
  96. _data[byte_number] = uc;
  97. }
  98. #endif