null_bitmap_traits.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // Copyright (c) 2019-2023 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BHO_MYSQL_IMPL_INTERNAL_PROTOCOL_NULL_BITMAP_TRAITS_HPP
  8. #define BHO_MYSQL_IMPL_INTERNAL_PROTOCOL_NULL_BITMAP_TRAITS_HPP
  9. #include <asio2/bho/assert.hpp>
  10. #include <cstddef>
  11. #include <cstdint>
  12. namespace bho {
  13. namespace mysql {
  14. namespace detail {
  15. class null_bitmap_traits
  16. {
  17. std::size_t offset_;
  18. std::size_t num_fields_;
  19. constexpr std::size_t byte_pos(std::size_t field_pos) const noexcept { return (field_pos + offset_) / 8; }
  20. constexpr std::size_t bit_pos(std::size_t field_pos) const noexcept { return (field_pos + offset_) % 8; }
  21. public:
  22. constexpr null_bitmap_traits(std::size_t offset, std::size_t num_fields) noexcept
  23. : offset_(offset), num_fields_{num_fields} {};
  24. constexpr std::size_t offset() const noexcept { return offset_; }
  25. constexpr std::size_t num_fields() const noexcept { return num_fields_; }
  26. constexpr std::size_t byte_count() const noexcept { return (num_fields_ + 7 + offset_) / 8; }
  27. bool is_null(const std::uint8_t* null_bitmap_begin, std::size_t field_pos) const noexcept
  28. {
  29. BHO_ASSERT(field_pos < num_fields_);
  30. return null_bitmap_begin[byte_pos(field_pos)] & (1 << bit_pos(field_pos));
  31. }
  32. void set_null(std::uint8_t* null_bitmap_begin, std::size_t field_pos) const noexcept
  33. {
  34. BHO_ASSERT(field_pos < num_fields_);
  35. null_bitmap_begin[byte_pos(field_pos)] |= (1 << bit_pos(field_pos));
  36. }
  37. };
  38. constexpr std::size_t stmt_execute_null_bitmap_offset = 0;
  39. constexpr std::size_t binary_row_null_bitmap_offset = 2;
  40. } // namespace detail
  41. } // namespace mysql
  42. } // namespace bho
  43. #endif /* INCLUDE_NULL_BITMAP_HPP_ */