Loading...
Searching...
No Matches
disconnect_options.h
Go to the documentation of this file.
/****************************************************************************
* Copyright (c) 2016-2017 Frank Pagliughi <fpagliughi@mindspring.com>
*
* 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_disconnect_options_h
#define __mqtt_disconnect_options_h
#include <chrono>
#include "MQTTAsync.h"
#include "mqtt/properties.h"
#include "mqtt/token.h"
#include "mqtt/types.h"
namespace mqtt {
class disconnect_options
{
static constexpr MQTTAsync_disconnectOptions DFLT_C_STRUCT
MQTTAsync_disconnectOptions_initializer;
static constexpr MQTTAsync_disconnectOptions DFLT_C_STRUCT5
MQTTAsync_disconnectOptions_initializer5;
MQTTAsync_disconnectOptions opts_{DFLT_C_STRUCT};
token_ptr tok_;
properties props_;
friend class async_client;
friend class disconnect_options_builder;
void update_c_struct();
disconnect_options(const MQTTAsync_disconnectOptions& copts) : opts_{copts} {}
public:
disconnect_options() {}
disconnect_options(int timeout) : disconnect_options() { set_timeout(timeout); }
template <class Rep, class Period>
disconnect_options(const std::chrono::duration<Rep, Period>& to) : disconnect_options() {
set_timeout(to);
}
disconnect_options(const disconnect_options& opt);
disconnect_options(disconnect_options&& opt);
static disconnect_options v3() { return disconnect_options{DFLT_C_STRUCT}; }
static disconnect_options v5() { return disconnect_options{DFLT_C_STRUCT5}; }
disconnect_options& operator=(const disconnect_options& opt);
disconnect_options& operator=(disconnect_options&& opt);
#if defined(UNIT_TESTS)
const MQTTAsync_disconnectOptions& c_struct() const { return opts_; }
#endif
std::chrono::milliseconds get_timeout() const {
return std::chrono::milliseconds(opts_.timeout);
}
void set_timeout(int timeout) { opts_.timeout = timeout; }
template <class Rep, class Period>
void set_timeout(const std::chrono::duration<Rep, Period>& to) {
// TODO: check range
set_timeout((int)to_milliseconds_count(to));
}
void set_token(const token_ptr& tok, int mqttVersion);
token_ptr get_token() const { return tok_; }
const properties& get_properties() const { return props_; }
properties& get_properties() { return props_; }
void set_properties(const properties& props) {
props_ = props;
opts_.properties = props_.c_struct();
}
void set_properties(properties&& props) {
props_ = std::move(props);
opts_.properties = props_.c_struct();
}
ReasonCode get_reason_code() const { return ReasonCode(opts_.reasonCode); }
void set_reason_code(ReasonCode code) { opts_.reasonCode = MQTTReasonCodes(code); }
};
class disconnect_options_builder
{
disconnect_options opts_;
disconnect_options_builder(const MQTTAsync_disconnectOptions& copts) : opts_{copts} {}
public:
using self = disconnect_options_builder;
disconnect_options_builder() {}
static disconnect_options_builder v3() {
return disconnect_options_builder{disconnect_options::DFLT_C_STRUCT};
}
static disconnect_options_builder v5() {
return disconnect_options_builder{disconnect_options::DFLT_C_STRUCT5};
}
auto properties(mqtt::properties&& props) -> self& {
opts_.set_properties(std::move(props));
return *this;
}
auto properties(const mqtt::properties& props) -> self& {
opts_.set_properties(props);
return *this;
}
template <class Rep, class Period>
auto timeout(const std::chrono::duration<Rep, Period>& to) -> self& {
opts_.set_timeout(to);
return *this;
}
auto reason_code(ReasonCode code) -> self& {
opts_.set_reason_code(code);
return *this;
}
disconnect_options finalize() { return opts_; }
};
} // namespace mqtt
#endif // __mqtt_disconnect_options_h
mqtt::disconnect_options_builder::v3
static disconnect_options_builder v3()
Definition disconnect_options.h:224
mqtt::disconnect_options_builder::properties
auto properties(const mqtt::properties &props) -> self &
Definition disconnect_options.h:246
mqtt::disconnect_options_builder::disconnect_options_builder
disconnect_options_builder()
Definition disconnect_options.h:219
mqtt::disconnect_options_builder::v5
static disconnect_options_builder v5()
Definition disconnect_options.h:231
mqtt::disconnect_options_builder::finalize
disconnect_options finalize()
Definition disconnect_options.h:272
mqtt::disconnect_options_builder::timeout
auto timeout(const std::chrono::duration< Rep, Period > &to) -> self &
Definition disconnect_options.h:256
mqtt::disconnect_options_builder::reason_code
auto reason_code(ReasonCode code) -> self &
Definition disconnect_options.h:264
mqtt::disconnect_options_builder::properties
auto properties(mqtt::properties &&props) -> self &
Definition disconnect_options.h:238
mqtt::disconnect_options_builder::self
disconnect_options_builder self
Definition disconnect_options.h:215
mqtt::disconnect_options
Definition disconnect_options.h:41
mqtt::disconnect_options::v5
static disconnect_options v5()
Definition disconnect_options.h:110
mqtt::disconnect_options::set_timeout
void set_timeout(int timeout)
Definition disconnect_options.h:139
mqtt::disconnect_options::disconnect_options
disconnect_options()
Definition disconnect_options.h:77
mqtt::disconnect_options::disconnect_options_builder
friend class disconnect_options_builder
Definition disconnect_options.h:63
mqtt::disconnect_options::get_properties
const properties & get_properties() const
Definition disconnect_options.h:166
mqtt::disconnect_options::get_reason_code
ReasonCode get_reason_code() const
Definition disconnect_options.h:192
mqtt::disconnect_options::set_timeout
void set_timeout(const std::chrono::duration< Rep, Period > &to)
Definition disconnect_options.h:146
mqtt::disconnect_options::get_properties
properties & get_properties()
Definition disconnect_options.h:171
mqtt::disconnect_options::operator=
disconnect_options & operator=(const disconnect_options &opt)
mqtt::disconnect_options::set_token
void set_token(const token_ptr &tok, int mqttVersion)
mqtt::disconnect_options::set_properties
void set_properties(const properties &props)
Definition disconnect_options.h:176
mqtt::disconnect_options::set_reason_code
void set_reason_code(ReasonCode code)
Definition disconnect_options.h:197
mqtt::disconnect_options::operator=
disconnect_options & operator=(disconnect_options &&opt)
mqtt::disconnect_options::disconnect_options
disconnect_options(const disconnect_options &opt)
mqtt::disconnect_options::disconnect_options
disconnect_options(const std::chrono::duration< Rep, Period > &to)
Definition disconnect_options.h:88
mqtt::disconnect_options::get_token
token_ptr get_token() const
Definition disconnect_options.h:161
mqtt::disconnect_options::get_timeout
std::chrono::milliseconds get_timeout() const
Definition disconnect_options.h:131
mqtt::disconnect_options::async_client
friend class async_client
Definition disconnect_options.h:60
mqtt::disconnect_options::v3
static disconnect_options v3()
Definition disconnect_options.h:105
mqtt::disconnect_options::disconnect_options
disconnect_options(disconnect_options &&opt)
mqtt::disconnect_options::disconnect_options
disconnect_options(int timeout)
Definition disconnect_options.h:82
mqtt::disconnect_options::set_properties
void set_properties(properties &&props)
Definition disconnect_options.h:184
mqtt::properties
Definition properties.h:293
mqtt::properties::properties
properties()
Definition properties.h:358
mqtt
Definition async_client.h:60
mqtt::ReasonCode
ReasonCode
Definition reason_code.h:39
mqtt::token_ptr
token::ptr_t token_ptr
Definition token.h:513
mqtt::to_milliseconds_count
long to_milliseconds_count(const std::chrono::duration< Rep, Period > &dur)
Definition types.h:95
properties.h
token.h
types.h