will_options.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*******************************************************************************
  2. * Copyright (c) 2016 Guilherme M. Ferreira <guilherme.maciel.ferreira@gmail.com>
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Guilherme M. Ferreira - initial implementation and documentation
  15. * Frank Pagliughi - added copy & move operations
  16. *******************************************************************************/
  17. #include "mqtt/will_options.h"
  18. #include <utility>
  19. #include <cstring>
  20. namespace mqtt {
  21. #if __cplusplus < 201703L
  22. constexpr int will_options::DFLT_QOS;
  23. constexpr bool will_options::DFLT_RETAINED;
  24. #endif
  25. const MQTTAsync_willOptions will_options::DFLT_C_STRUCT = MQTTAsync_willOptions_initializer;
  26. /////////////////////////////////////////////////////////////////////////////
  27. will_options::will_options() : opts_(DFLT_C_STRUCT)
  28. {
  29. set_topic(string());
  30. // set_payload(binary_ref());
  31. }
  32. will_options::will_options(string_ref top,
  33. const void *payload, size_t payloadlen,
  34. int qos, bool retained)
  35. : opts_(DFLT_C_STRUCT)
  36. {
  37. opts_.qos = qos;
  38. opts_.retained = retained;
  39. set_topic(std::move(top));
  40. set_payload(binary_ref(static_cast<const binary_ref::value_type*>(payload), payloadlen));
  41. }
  42. will_options::will_options(const topic& top,
  43. const void *payload, size_t payloadlen,
  44. int qos, bool retained)
  45. : will_options(top.get_name(), payload, payloadlen, qos, retained)
  46. {
  47. }
  48. will_options::will_options(string_ref top, binary_ref payload,
  49. int qos, bool retained)
  50. : opts_(DFLT_C_STRUCT)
  51. {
  52. opts_.qos = qos;
  53. opts_.retained = retained;
  54. set_topic(std::move(top));
  55. set_payload(std::move(payload));
  56. }
  57. will_options::will_options(string_ref top, const string& payload,
  58. int qos, bool retained)
  59. : opts_(DFLT_C_STRUCT)
  60. {
  61. opts_.qos = qos;
  62. opts_.retained = retained;
  63. set_topic(std::move(top));
  64. set_payload(payload);
  65. }
  66. will_options::will_options(const message& msg)
  67. : will_options(msg.get_topic(), msg.get_payload(), msg.get_qos(), msg.is_retained())
  68. {
  69. }
  70. will_options::will_options(const will_options& opt) : opts_(opt.opts_)
  71. {
  72. set_topic(opt.topic_);
  73. set_payload(opt.payload_);
  74. }
  75. will_options::will_options(will_options&& other) : opts_(other.opts_)
  76. {
  77. set_topic(std::move(other.topic_));
  78. set_payload(std::move(other.payload_));
  79. }
  80. will_options& will_options::operator=(const will_options& rhs)
  81. {
  82. if (&rhs != this) {
  83. opts_ = rhs.opts_;
  84. set_topic(rhs.topic_);
  85. set_payload(rhs.payload_);
  86. }
  87. return *this;
  88. }
  89. will_options& will_options::operator=(will_options&& rhs)
  90. {
  91. if (&rhs != this) {
  92. opts_ = rhs.opts_;
  93. set_topic(std::move(rhs.topic_));
  94. set_payload(std::move(rhs.payload_));
  95. }
  96. return *this;
  97. }
  98. void will_options::set_topic(string_ref top)
  99. {
  100. topic_ = top ? std::move(top) : string_ref(string());
  101. opts_.topicName = topic_.c_str();
  102. }
  103. void will_options::set_payload(binary_ref msg)
  104. {
  105. // The C struct payload must not be nullptr for will options
  106. payload_ = msg ? std::move(msg) : binary_ref(binary());
  107. opts_.payload.len = (int) payload_.size();
  108. opts_.payload.data = payload_.data();
  109. }
  110. /////////////////////////////////////////////////////////////////////////////
  111. } // end namespace mqtt