two_or_four_byte_util.hpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright Takatoshi Kondo 2021
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #if !defined(MQTT_TWO_OR_FOUR_BYTE_UTIL_HPP)
  7. #define MQTT_TWO_OR_FOUR_BYTE_UTIL_HPP
  8. #include <cstdint>
  9. #include <cstdlib>
  10. #include <mqtt/namespace.hpp>
  11. #include <mqtt/two_byte_util.hpp>
  12. #include <mqtt/four_byte_util.hpp>
  13. namespace MQTT_NS {
  14. template <std::size_t Bytes>
  15. struct two_or_four_byte_type;
  16. template <>
  17. struct two_or_four_byte_type<2> {
  18. using type = std::uint16_t;
  19. };
  20. template <>
  21. struct two_or_four_byte_type<4> {
  22. using type = std::uint32_t;
  23. };
  24. template <std::size_t Bytes>
  25. struct make_two_or_four_byte;
  26. template <>
  27. struct make_two_or_four_byte<2> {
  28. template <typename It>
  29. static constexpr std::uint16_t apply(It b, It e) {
  30. return make_uint16_t(b, e);
  31. }
  32. };
  33. template <>
  34. struct make_two_or_four_byte<4> {
  35. template <typename It>
  36. static constexpr std::uint32_t apply(It b, It e) {
  37. return make_uint32_t(b, e);
  38. }
  39. };
  40. template <std::size_t Bytes>
  41. struct add_two_or_four_byte_to_buf;
  42. template <>
  43. struct add_two_or_four_byte_to_buf<2> {
  44. template <typename T>
  45. static void apply(T& buf, std::uint16_t two_or_four_byte) {
  46. add_uint16_t_to_buf(buf, two_or_four_byte);
  47. }
  48. };
  49. template <>
  50. struct add_two_or_four_byte_to_buf<4> {
  51. template <typename T>
  52. static void apply(T& buf, std::uint32_t two_or_four_byte) {
  53. add_uint32_t_to_buf(buf, two_or_four_byte);
  54. }
  55. };
  56. } // namespace MQTT_NS
  57. #endif // MQTT_TWO_OR_FOUR_BYTE_UTIL_HPP