subscribe_options.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright Takatoshi Kondo 2015
  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_SUBSCRIBE_OPTIONS_HPP)
  7. #define MQTT_SUBSCRIBE_OPTIONS_HPP
  8. #include <cstdint>
  9. #include <ostream>
  10. #include <mqtt/namespace.hpp>
  11. namespace MQTT_NS {
  12. enum class retain_handling : std::uint8_t
  13. {
  14. send = 0b00000000,
  15. send_only_new_subscription = 0b00010000,
  16. not_send = 0b00100000,
  17. };
  18. enum class rap : std::uint8_t
  19. {
  20. dont = 0b00000000,
  21. retain = 0b00001000,
  22. };
  23. enum class nl : std::uint8_t
  24. {
  25. no = 0b00000000,
  26. yes = 0b00000100,
  27. };
  28. enum class qos : std::uint8_t
  29. {
  30. at_most_once = 0b00000000,
  31. at_least_once = 0b00000001,
  32. exactly_once = 0b00000010,
  33. };
  34. struct subscribe_options final {
  35. constexpr subscribe_options() = delete;
  36. ~subscribe_options() = default;
  37. constexpr subscribe_options(subscribe_options &&) = default;
  38. constexpr subscribe_options(subscribe_options const&) = default;
  39. constexpr subscribe_options& operator=(subscribe_options &&) = default;
  40. constexpr subscribe_options& operator=(subscribe_options const&) = default;
  41. explicit constexpr subscribe_options(std::uint8_t value) : data_(value) { }
  42. constexpr subscribe_options(retain_handling value) : data_(static_cast<std::uint8_t>(value)) { }
  43. constexpr subscribe_options(rap value) : data_(static_cast<std::uint8_t>(value)) { }
  44. constexpr subscribe_options(nl value) : data_(static_cast<std::uint8_t>(value)) { }
  45. constexpr subscribe_options(qos value) : data_(static_cast<std::uint8_t>(value)) { }
  46. constexpr subscribe_options operator|(subscribe_options rhs) const { return subscribe_options(data_ | rhs.data_); }
  47. constexpr subscribe_options operator|(retain_handling rhs) const { return *this | subscribe_options(rhs); }
  48. constexpr subscribe_options operator|(rap rhs) const { return *this | subscribe_options(rhs); }
  49. constexpr subscribe_options operator|(nl rhs) const { return *this | subscribe_options(rhs); }
  50. constexpr subscribe_options operator|(qos rhs) const { return *this | subscribe_options(rhs); }
  51. constexpr subscribe_options& operator|=(subscribe_options rhs) { return (*this = (*this | rhs)); }
  52. constexpr subscribe_options& operator|=(retain_handling rhs) { return (*this = (*this | rhs)); }
  53. constexpr subscribe_options& operator|=(rap rhs) { return (*this = (*this | rhs)); }
  54. constexpr subscribe_options& operator|=(nl rhs) { return (*this = (*this | rhs)); }
  55. constexpr subscribe_options& operator|=(qos rhs) { return (*this = (*this | rhs)); }
  56. constexpr retain_handling get_retain_handling() const
  57. { return static_cast<retain_handling>(data_ & 0b00110000); }
  58. constexpr rap get_rap() const
  59. { return static_cast<rap>(data_ & 0b00001000); }
  60. constexpr nl get_nl() const
  61. { return static_cast<nl>(data_ & 0b00000100); }
  62. constexpr qos get_qos() const
  63. { return static_cast<qos>(data_ & 0b00000011); }
  64. explicit constexpr operator std::uint8_t() const { return data_; }
  65. private:
  66. std::uint8_t data_;
  67. };
  68. constexpr subscribe_options operator|(retain_handling lhs, rap rhs) { return subscribe_options(lhs) | rhs; }
  69. constexpr subscribe_options operator|(retain_handling lhs, nl rhs) { return subscribe_options(lhs) | rhs; }
  70. constexpr subscribe_options operator|(retain_handling lhs, qos rhs) { return subscribe_options(lhs) | rhs; }
  71. constexpr subscribe_options operator|(rap lhs, retain_handling rhs) { return subscribe_options(lhs) | rhs; }
  72. constexpr subscribe_options operator|(rap lhs, nl rhs) { return subscribe_options(lhs) | rhs; }
  73. constexpr subscribe_options operator|(rap lhs, qos rhs) { return subscribe_options(lhs) | rhs; }
  74. constexpr subscribe_options operator|(nl lhs, retain_handling rhs) { return subscribe_options(lhs) | rhs; }
  75. constexpr subscribe_options operator|(nl lhs, rap rhs) { return subscribe_options(lhs) | rhs; }
  76. constexpr subscribe_options operator|(nl lhs, qos rhs) { return subscribe_options(lhs) | rhs; }
  77. constexpr subscribe_options operator|(qos lhs, retain_handling rhs) { return subscribe_options(lhs) | rhs; }
  78. constexpr subscribe_options operator|(qos lhs, rap rhs) { return subscribe_options(lhs) | rhs; }
  79. constexpr subscribe_options operator|(qos lhs, nl rhs) { return subscribe_options(lhs) | rhs; }
  80. constexpr char const* retain_handling_to_str(retain_handling v) {
  81. switch(v) {
  82. case retain_handling::send: return "send";
  83. case retain_handling::send_only_new_subscription: return "send_only_new_subscription";
  84. case retain_handling::not_send: return "not_send";
  85. default: return "invalid_retain_handling";
  86. }
  87. }
  88. inline
  89. std::ostream& operator<<(std::ostream& os, retain_handling val)
  90. {
  91. os << retain_handling_to_str(val);
  92. return os;
  93. }
  94. constexpr char const* rap_to_str(rap v) {
  95. switch(v) {
  96. case rap::dont: return "dont";
  97. case rap::retain: return "retain";
  98. default: return "invalid_rap";
  99. }
  100. }
  101. inline
  102. std::ostream& operator<<(std::ostream& os, rap val)
  103. {
  104. os << rap_to_str(val);
  105. return os;
  106. }
  107. constexpr char const* nl_to_str(nl v) {
  108. switch(v) {
  109. case nl::no: return "no";
  110. case nl::yes: return "yes";
  111. default: return "invalid_nl";
  112. }
  113. }
  114. inline
  115. std::ostream& operator<<(std::ostream& os, nl val)
  116. {
  117. os << nl_to_str(val);
  118. return os;
  119. }
  120. constexpr char const* qos_to_str(qos v) {
  121. switch(v) {
  122. case qos::at_most_once: return "at_most_once";
  123. case qos::at_least_once: return "at_least_once";
  124. case qos::exactly_once: return "exactly_once";
  125. default: return "invalid_qos";
  126. }
  127. }
  128. inline
  129. std::ostream& operator<<(std::ostream& os, qos val)
  130. {
  131. os << qos_to_str(val);
  132. return os;
  133. }
  134. } // namespace MQTT_NS
  135. #endif // MQTT_SUBSCRIBE_OPTIONS_HPP