Skip to content

Commit

Permalink
#524 Fixed copy and move operations for 'subscribe_options'. Added un…
Browse files Browse the repository at this point in the history
…it tests.
  • Loading branch information
fpagliughi committed Jan 4, 2025
1 parent 17e4ee1 commit 1e7d090
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 61 deletions.
64 changes: 59 additions & 5 deletions include/mqtt/response_options.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
/////////////////////////////////////////////////////////////////////////////
/// @file response_options.h
/// Implementation of the class 'response_options'
/// @date 26-Aug-2016
/// @date 26-Aug-2019
/////////////////////////////////////////////////////////////////////////////

/*******************************************************************************
* Copyright (c) 2019-2025 Frank Pagliughi <[email protected]>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Frank Pagliughi - initial implementation and documentation
*******************************************************************************/

#ifndef __mqtt_response_options_h
#define __mqtt_response_options_h

Expand Down Expand Up @@ -70,16 +86,26 @@ class response_options
* @param other The other options to copy to this one.
*/
response_options(const response_options& other);
/**
* Move constructor.
* @param other The other options to move into this one.
*/
response_options(response_options&& other);
/**
* Copy operator.
* @param rhs The other options to copy to this one.
*/
response_options& operator=(const response_options& rhs);
/**
* Move operator.
* @param rhs The other options to move into this one.
*/
response_options& operator=(response_options&& rhs);
/**
* Expose the underlying C struct for the unit tests.
*/
#if defined(UNIT_TESTS)
const MQTTAsync_responseOptions& c_struct() const { return opts_; }
const auto& c_struct() const { return opts_; }
#endif
/**
* Sets the MQTT protocol version used for the response.
Expand Down Expand Up @@ -112,6 +138,18 @@ class response_options
props_ = std::move(props);
opts_.properties = props_.c_struct();
}
/**
* Gets the options for a single topic subscription.
* @return The subscribe options.
*/
subscribe_options get_subscribe_options() const {
return subscribe_options{opts_.subscribeOptions};
}
/**
* Sets the options for a multi-topic subscription.
* @return The vector of the subscribe options.
*/
std::vector<subscribe_options> get_subscribe_many_options() const;
/**
* Sets the options for a single topic subscription.
* @param opts The subscribe options.
Expand All @@ -121,7 +159,15 @@ class response_options
* Sets the options for a multi-topic subscription.
* @param opts A vector of the subscribe options.
*/
void set_subscribe_options(const std::vector<subscribe_options>& opts);
void set_subscribe_many_options(const std::vector<subscribe_options>& opts);
/**
* Sets the options for a multi-topic subscription.
* @param opts A vector of the subscribe options.
* @sa set_subscribe_options
*/
void set_subscribe_options(const std::vector<subscribe_options>& opts) {
set_subscribe_many_options(opts);
}
};

/////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -183,6 +229,14 @@ class response_options_builder
opts_.set_subscribe_options(opts);
return *this;
}
/**
* Sets the options for a multi-topic subscription.
* @param opts A vector of the subscribe options.
*/
auto subscribe_many_opts(const std::vector<subscribe_options>& opts) -> self& {
opts_.set_subscribe_options(opts);
return *this;
}
/**
* Sets the options for a multi-topic subscription.
* @param opts A vector of the subscribe options.
Expand All @@ -192,8 +246,8 @@ class response_options_builder
return *this;
}
/**
* Finish building the options and return them.
* @return The option struct as built.
* Finish building the response options and return them.
* @return The response option struct as built.
*/
response_options finalize() { return opts_; }
};
Expand Down
13 changes: 10 additions & 3 deletions include/mqtt/subscribe_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,19 @@ class subscribe_options
opts_.retainAsPublished = retainAsPublished ? 1 : 0;
opts_.retainHandling = (unsigned char)retainHandling;
}
/**
* Expose the underlying C struct for the unit tests.
*/
/**
* Creates the set of subscribe options from an underlying C struct.
* @param opts The Paho C subscribe options
*/
explicit subscribe_options(MQTTSubscribe_options opts) : opts_{opts} {}

#if defined(UNIT_TESTS)
/**
* Expose the underlying C struct for the unit tests.
*/
const auto& c_struct() const { return opts_; }
#endif

/**
* Gets the value of the "no local" flag.
* @return Whether the server should send back our own publications, if
Expand Down
46 changes: 39 additions & 7 deletions src/response_options.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// response_options.cpp

/*******************************************************************************
* Copyright (c) 2019-2024 Frank Pagliughi <[email protected]>
* Copyright (c) 2019-2025 Frank Pagliughi <[email protected]>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
Expand Down Expand Up @@ -30,18 +30,43 @@ response_options::
}

response_options::response_options(const response_options& other)
: opts_(other.opts_), tok_(other.tok_), props_(other.props_)
: opts_{other.opts_}, tok_{other.tok_}, props_{other.props_}, subOpts_{other.subOpts_}
{
update_c_struct();
}

response_options::response_options(response_options&& other)
: opts_{other.opts_},
tok_{std::move(other.tok_)},
props_{std::move(other.props_)},
subOpts_{std::move(other.subOpts_)}
{
update_c_struct();
}

response_options& response_options::operator=(const response_options& rhs)
{
opts_ = rhs.opts_;
tok_ = rhs.tok_;
props_ = rhs.props_;
if (&rhs != this) {
opts_ = rhs.opts_;
tok_ = rhs.tok_;
props_ = rhs.props_;
subOpts_ = rhs.subOpts_;

update_c_struct();
update_c_struct();
}
return *this;
}

response_options& response_options::operator=(response_options&& rhs)
{
if (&rhs != this) {
opts_ = rhs.opts_;
tok_ = std::move(rhs.tok_);
props_ = std::move(rhs.props_);
subOpts_ = std::move(rhs.subOpts_);

update_c_struct();
}
return *this;
}

Expand Down Expand Up @@ -80,7 +105,14 @@ void response_options::set_subscribe_options(const subscribe_options& opts)
opts_.subscribeOptions = opts.opts_;
}

void response_options::set_subscribe_options(const std::vector<subscribe_options>& opts)
std::vector<subscribe_options> response_options::get_subscribe_many_options() const
{
std::vector<subscribe_options> opts;
for (const auto& opt : subOpts_) opts.push_back(subscribe_options{opt});
return opts;
}

void response_options::set_subscribe_many_options(const std::vector<subscribe_options>& opts)
{
subOpts_.clear();
for (const auto& opt : opts) subOpts_.push_back(opt.opts_);
Expand Down
Loading

0 comments on commit 1e7d090

Please sign in to comment.