Loading...
Searching...
No Matches
create_options.h
Go to the documentation of this file.
/*******************************************************************************
* Copyright (c) 2020-2024 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_create_options_h
#define __mqtt_create_options_h
#include <variant>
#include "MQTTAsync.h"
#include "mqtt/iclient_persistence.h"
#include "mqtt/types.h"
namespace mqtt {
struct no_persistence
{
};
constexpr no_persistence NO_PERSISTENCE{};
using persistence_type = std::variant<no_persistence, string, iclient_persistence*>;
class create_options
{
MQTTAsync_createOptions opts_ MQTTAsync_createOptions_initializer5;
string serverURI_{};
string clientId_{};
persistence_type persistence_{};
friend class async_client;
friend class create_options_builder;
public:
using ptr_t = std::shared_ptr<create_options>;
using const_ptr_t = std::shared_ptr<const create_options>;
create_options() {}
explicit create_options(int mqttVersion) : create_options() {
opts_.MQTTVersion = mqttVersion;
}
create_options(int mqttVersion, int maxBufferedMessages);
explicit create_options(const string& serverURI, const string& clientId = string{})
: serverURI_{serverURI}, clientId_{clientId} {}
create_options(
const string& serverURI, const string& clientId, const persistence_type& persistence
)
: serverURI_{serverURI}, clientId_{clientId}, persistence_{persistence} {}
create_options(
const string& serverURI, const string& clientId, int maxBufferedMessages,
const persistence_type& persistence
)
: serverURI_{serverURI}, clientId_{clientId}, persistence_{persistence} {
opts_.maxBufferedMessages = maxBufferedMessages;
}
create_options(
const string& serverURI, const string& clientId, const create_options& opts,
const persistence_type& persistence
)
: opts_{opts.opts_},
serverURI_{serverURI},
clientId_{clientId},
persistence_{persistence} {}
create_options(const create_options& opts)
: opts_{opts.opts_},
serverURI_{opts.serverURI_},
clientId_{opts.clientId_},
persistence_{opts.persistence_} {}
create_options(create_options&& opts)
: opts_{opts.opts_},
serverURI_{std::move(opts.serverURI_)},
clientId_{std::move(opts.clientId_)},
persistence_{std::move(opts.persistence_)} {}
create_options& operator=(const create_options& rhs);
create_options& operator=(create_options&& rhs);
void set_server_uri(const string& serverURI) { serverURI_ = serverURI; };
const string& get_server_uri() const noexcept { return serverURI_; };
void set_client_id(const string& clientId) { clientId_ = clientId; }
const string& get_client_id() const noexcept { return clientId_; }
void set_persistence(const persistence_type& persistence) { persistence_ = persistence; }
const persistence_type& get_persistence() const noexcept { return persistence_; }
bool get_send_while_disconnected() const { return to_bool(opts_.sendWhileDisconnected); }
void set_send_while_disconnected(bool on, bool anyTime = false) {
opts_.sendWhileDisconnected = to_int(on);
opts_.allowDisconnectedSendAtAnyTime = to_int(anyTime);
}
int get_max_buffered_messages() const { return opts_.maxBufferedMessages; }
void set_max_buffered_messages(int n) { opts_.maxBufferedMessages = n; }
int mqtt_version() const { return opts_.MQTTVersion; }
void set_mqtt_version(int ver) { opts_.MQTTVersion = ver; }
bool get_delete_oldest_messages() const { return to_bool(opts_.deleteOldestMessages); }
void set_delete_oldest_messages(bool on) { opts_.deleteOldestMessages = to_int(on); }
bool get_restore_messages() const { return to_bool(opts_.restoreMessages); }
void set_restore_messages(bool on) { opts_.restoreMessages = to_int(on); }
bool get_persist_qos0() const { return to_bool(opts_.persistQoS0); }
void set_persist_qos0(bool on) { opts_.persistQoS0 = to_int(on); }
};
using create_options_ptr = create_options::ptr_t;
class create_options_builder
{
create_options opts_;
public:
using self = create_options_builder;
create_options_builder() {}
auto server_uri(const string& serverURI) -> self& {
opts_.set_server_uri(serverURI);
return *this;
}
auto client_id(const string& clientId) -> self& {
opts_.set_client_id(clientId);
return *this;
}
auto persistence(const persistence_type& persistence) -> self& {
opts_.set_persistence(persistence);
return *this;
}
auto send_while_disconnected(bool on = true, bool anyTime = false) -> self& {
opts_.opts_.sendWhileDisconnected = to_int(on);
opts_.opts_.allowDisconnectedSendAtAnyTime = to_int(anyTime);
return *this;
}
auto max_buffered_messages(int n) -> self& {
opts_.opts_.maxBufferedMessages = n;
return *this;
}
auto mqtt_version(int ver) -> self& {
opts_.opts_.MQTTVersion = ver;
return *this;
}
auto delete_oldest_messages(bool on = true) -> self& {
opts_.opts_.deleteOldestMessages = to_int(on);
return *this;
}
auto restore_messages(bool on = true) -> self& {
opts_.opts_.restoreMessages = to_int(on);
return *this;
}
auto persist_qos0(bool on = true) -> self& {
opts_.opts_.persistQoS0 = to_int(on);
return *this;
}
create_options finalize() { return opts_; }
};
} // namespace mqtt
#endif // __mqtt_create_options_h
mqtt::async_client
Definition async_client.h:121
mqtt::create_options_builder
Definition create_options.h:312
mqtt::create_options_builder::delete_oldest_messages
auto delete_oldest_messages(bool on=true) -> self &
Definition create_options.h:388
mqtt::create_options_builder::restore_messages
auto restore_messages(bool on=true) -> self &
Definition create_options.h:400
mqtt::create_options_builder::create_options_builder
create_options_builder()
Definition create_options.h:322
mqtt::create_options_builder::mqtt_version
auto mqtt_version(int ver) -> self &
Definition create_options.h:377
mqtt::create_options_builder::persistence
auto persistence(const persistence_type &persistence) -> self &
Definition create_options.h:344
mqtt::create_options_builder::server_uri
auto server_uri(const string &serverURI) -> self &
Definition create_options.h:328
mqtt::create_options_builder::send_while_disconnected
auto send_while_disconnected(bool on=true, bool anyTime=false) -> self &
Definition create_options.h:359
mqtt::create_options_builder::finalize
create_options finalize()
Definition create_options.h:418
mqtt::create_options_builder::client_id
auto client_id(const string &clientId) -> self &
Definition create_options.h:336
mqtt::create_options_builder::max_buffered_messages
auto max_buffered_messages(int n) -> self &
Definition create_options.h:369
mqtt::create_options_builder::persist_qos0
auto persist_qos0(bool on=true) -> self &
Definition create_options.h:410
mqtt::create_options
Definition create_options.h:60
mqtt::create_options::set_restore_messages
void set_restore_messages(bool on)
Definition create_options.h:288
mqtt::create_options::get_server_uri
const string & get_server_uri() const noexcept
Definition create_options.h:199
mqtt::create_options::create_options
create_options(int mqttVersion)
Definition create_options.h:91
mqtt::create_options::get_send_while_disconnected
bool get_send_while_disconnected() const
Definition create_options.h:224
mqtt::create_options::create_options
create_options(int mqttVersion, int maxBufferedMessages)
mqtt::create_options::get_restore_messages
bool get_restore_messages() const
Definition create_options.h:281
mqtt::create_options::create_options
create_options(const create_options &opts)
Definition create_options.h:173
mqtt::create_options::set_send_while_disconnected
void set_send_while_disconnected(bool on, bool anyTime=false)
Definition create_options.h:235
mqtt::create_options::mqtt_version
int mqtt_version() const
Definition create_options.h:253
mqtt::create_options::create_options
create_options()
Definition create_options.h:86
mqtt::create_options::set_client_id
void set_client_id(const string &clientId)
Definition create_options.h:204
mqtt::create_options::create_options
create_options(create_options &&opts)
Definition create_options.h:182
mqtt::create_options::get_delete_oldest_messages
bool get_delete_oldest_messages() const
Definition create_options.h:267
mqtt::create_options::get_max_buffered_messages
int get_max_buffered_messages() const
Definition create_options.h:243
mqtt::create_options::create_options
create_options(const string &serverURI, const string &clientId, int maxBufferedMessages, const persistence_type &persistence)
Definition create_options.h:142
mqtt::create_options::create_options
create_options(const string &serverURI, const string &clientId, const create_options &opts, const persistence_type &persistence)
Definition create_options.h:161
mqtt::create_options::operator=
create_options & operator=(create_options &&rhs)
mqtt::create_options::set_max_buffered_messages
void set_max_buffered_messages(int n)
Definition create_options.h:248
mqtt::create_options::operator=
create_options & operator=(const create_options &rhs)
mqtt::create_options::set_persistence
void set_persistence(const persistence_type &persistence)
Definition create_options.h:214
mqtt::create_options::get_persist_qos0
bool get_persist_qos0() const
Definition create_options.h:294
mqtt::create_options::create_options
create_options(const string &serverURI, const string &clientId, const persistence_type &persistence)
Definition create_options.h:125
mqtt::create_options::set_delete_oldest_messages
void set_delete_oldest_messages(bool on)
Definition create_options.h:274
mqtt::create_options::ptr_t
std::shared_ptr< create_options > ptr_t
Definition create_options.h:79
mqtt::create_options::create_options
create_options(const string &serverURI, const string &clientId=string{})
Definition create_options.h:111
mqtt::create_options::const_ptr_t
std::shared_ptr< const create_options > const_ptr_t
Definition create_options.h:81
mqtt::create_options::get_client_id
const string & get_client_id() const noexcept
Definition create_options.h:209
mqtt::create_options::get_persistence
const persistence_type & get_persistence() const noexcept
Definition create_options.h:219
mqtt::create_options::set_mqtt_version
void set_mqtt_version(int ver)
Definition create_options.h:258
mqtt::create_options::set_persist_qos0
void set_persist_qos0(bool on)
Definition create_options.h:300
mqtt::create_options::set_server_uri
void set_server_uri(const string &serverURI)
Definition create_options.h:194
iclient_persistence.h
mqtt
Definition async_client.h:60
mqtt::NO_PERSISTENCE
constexpr no_persistence NO_PERSISTENCE
Definition create_options.h:43
mqtt::to_bool
bool to_bool(int n)
Definition types.h:107
mqtt::create_options_ptr
create_options::ptr_t create_options_ptr
Definition create_options.h:304
mqtt::to_int
int to_int(bool b)
Definition types.h:113
mqtt::persistence_type
std::variant< no_persistence, string, iclient_persistence * > persistence_type
Definition create_options.h:52
mqtt::no_persistence
Definition create_options.h:39
types.h