From 57b8986638fc1b7a9a5824269250cd04b27da96a Mon Sep 17 00:00:00 2001 From: Wiebe Cazemier Date: Sun, 6 Mar 2022 11:41:24 +0100 Subject: [PATCH] Use std::make_shared and std::make_unique --- CMakeLists.txt | 2 +- acltree.cpp | 4 ++-- authplugin.cpp | 3 ++- configfileparser.cpp | 8 ++++---- listener.cpp | 2 +- mainapp.cpp | 19 ++++++++++--------- sessionsandsubscriptionsdb.cpp | 2 +- subscriptionstore.cpp | 6 +++--- 8 files changed, 24 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 46e510d..6924f56 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ project(FlashMQ VERSION 0.9.9 LANGUAGES CXX) add_definitions(-DOPENSSL_API_COMPAT=0x10100000L) add_definitions(-DFLASHMQ_VERSION=\"${PROJECT_VERSION}\") -set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) SET(CMAKE_CXX_FLAGS "-msse4.2") diff --git a/acltree.cpp b/acltree.cpp index 09b8085..aecc024 100644 --- a/acltree.cpp +++ b/acltree.cpp @@ -14,7 +14,7 @@ AclNode *AclNode::getChildren(const std::string &subtopic, bool registerPattern) if (!node) { - node.reset(new AclNode()); + node = std::make_unique(); if (registerPattern) { @@ -44,7 +44,7 @@ const AclNode *AclNode::getChildren(const std::string &subtopic) const AclNode *AclNode::getChildrenPlus() { if (!childrenPlus) - childrenPlus.reset(new AclNode()); + childrenPlus = std::make_unique(); return childrenPlus.get(); } diff --git a/authplugin.cpp b/authplugin.cpp index 2155e5c..0d6ed3d 100644 --- a/authplugin.cpp +++ b/authplugin.cpp @@ -417,7 +417,8 @@ void Authentication::loadMosquittoPasswordFile() try { std::ifstream infile(this->mosquittoPasswordFile, std::ios::in); - std::unique_ptr> passwordEntries_tmp(new std::unordered_map()); + std::unique_ptr> passwordEntries_tmp = + std::make_unique>(); for(std::string line; getline(infile, line ); ) { diff --git a/configfileparser.cpp b/configfileparser.cpp index d3f8213..f89e59c 100644 --- a/configfileparser.cpp +++ b/configfileparser.cpp @@ -121,7 +121,7 @@ ConfigFileParser::ConfigFileParser(const std::string &path) : validListenKeys.insert("inet4_bind_address"); validListenKeys.insert("inet6_bind_address"); - settings.reset(new Settings()); + settings = std::make_unique(); } void ConfigFileParser::loadFile(bool test) @@ -202,7 +202,7 @@ void ConfigFileParser::loadFile(bool test) ConfigParseLevel curParseLevel = ConfigParseLevel::Root; std::shared_ptr curListener; - std::unique_ptr tmpSettings(new Settings); + std::unique_ptr tmpSettings = std::make_unique(); // Then once we know the config file is valid, process it. for (std::string &line : lines) @@ -215,7 +215,7 @@ void ConfigFileParser::loadFile(bool test) if (matches[1].str() == "listen") { curParseLevel = ConfigParseLevel::Listen; - curListener.reset(new Listener); + curListener = std::make_shared(); } else { @@ -481,7 +481,7 @@ void ConfigFileParser::loadFile(bool test) std::unique_ptr ConfigFileParser::moveSettings() { std::unique_ptr tmp = std::move(settings); - settings.reset(new Settings); + settings = std::make_unique(); return tmp; } diff --git a/listener.cpp b/listener.cpp index 875bf84..adc30f9 100644 --- a/listener.cpp +++ b/listener.cpp @@ -83,7 +83,7 @@ void Listener::loadCertAndKeyFromConfig() if (!sslctx) { - sslctx.reset(new SslCtxManager()); + sslctx = std::make_unique(); SSL_CTX_set_options(sslctx->get(), SSL_OP_NO_SSLv3); // TODO: config option SSL_CTX_set_options(sslctx->get(), SSL_OP_NO_TLSv1); // TODO: config option } diff --git a/mainapp.cpp b/mainapp.cpp index 938abe4..d6e1231 100644 --- a/mainapp.cpp +++ b/mainapp.cpp @@ -23,6 +23,7 @@ License along with FlashMQ. If not, see . #include #include #include +#include #include #include @@ -36,12 +37,12 @@ License along with FlashMQ. If not, see . MainApp *MainApp::instance = nullptr; MainApp::MainApp(const std::string &configFilePath) : - subscriptionStore(new SubscriptionStore()) + subscriptionStore(std::make_shared()) { epollFdAccept = check(epoll_create(999)); taskEventFd = eventfd(0, EFD_NONBLOCK); - confFileParser.reset(new ConfigFileParser(configFilePath)); + confFileParser = std::make_unique(configFilePath); loadConfig(); this->num_threads = get_nprocs(); @@ -437,14 +438,14 @@ void MainApp::start() Authentication auth(settingsLocalCopy); ThreadGlobals::assign(&auth); - std::shared_ptr threaddata(new ThreadData(0, subscriptionStore, settings)); + std::shared_ptr threaddata = std::make_shared(0, subscriptionStore, settings); - std::shared_ptr client(new Client(fd, threaddata, nullptr, fuzzWebsockets, nullptr, settings, true)); - std::shared_ptr subscriber(new Client(fdnull, threaddata, nullptr, fuzzWebsockets, nullptr, settings, true)); + std::shared_ptr client = std::make_shared(fd, threaddata, nullptr, fuzzWebsockets, nullptr, settings, true); + std::shared_ptr subscriber = std::make_shared(fdnull, threaddata, nullptr, fuzzWebsockets, nullptr, settings, true); subscriber->setClientProperties(ProtocolVersion::Mqtt311, "subscriber", "subuser", true, 60, true); subscriber->setAuthenticated(true); - std::shared_ptr websocketsubscriber(new Client(fdnull2, threaddata, nullptr, true, nullptr, settings, true)); + std::shared_ptr websocketsubscriber = std::make_shared(fdnull2, threaddata, nullptr, true, nullptr, settings, true); websocketsubscriber->setClientProperties(ProtocolVersion::Mqtt311, "websocketsubscriber", "websocksubuser", true, 60, true); websocketsubscriber->setAuthenticated(true); websocketsubscriber->setFakeUpgraded(); @@ -483,7 +484,7 @@ void MainApp::start() for (int i = 0; i < num_threads; i++) { - std::shared_ptr t(new ThreadData(i, subscriptionStore, settings)); + std::shared_ptr t = std::make_shared(i, subscriptionStore, settings); t->start(&do_thread_work); threads.push_back(t); } @@ -542,7 +543,7 @@ void MainApp::start() SSL_set_fd(clientSSL, fd); } - std::shared_ptr client(new Client(fd, thread_data, clientSSL, listener->websocket, addr, settings)); + std::shared_ptr client = std::make_shared(fd, thread_data, clientSSL, listener->websocket, addr, settings); thread_data->giveClient(client); } else @@ -655,7 +656,7 @@ void MainApp::loadConfig() if (settings->listeners.empty()) { - std::shared_ptr defaultListener(new Listener()); + std::shared_ptr defaultListener = std::make_shared(); defaultListener->isValid(); settings->listeners.push_back(defaultListener); } diff --git a/sessionsandsubscriptionsdb.cpp b/sessionsandsubscriptionsdb.cpp index 79491e8..03ae120 100644 --- a/sessionsandsubscriptionsdb.cpp +++ b/sessionsandsubscriptionsdb.cpp @@ -88,7 +88,7 @@ SessionsAndSubscriptionsResult SessionsAndSubscriptionsDB::readDataV1() readCheck(buf.data(), 1, clientIdLength, f); std::string clientId(buf.data(), clientIdLength); - std::shared_ptr ses(new Session()); + std::shared_ptr ses = std::make_shared(); result.sessions.push_back(ses); ses->username = username; ses->client_id = clientId; diff --git a/subscriptionstore.cpp b/subscriptionstore.cpp index edb84c0..11c8d4c 100644 --- a/subscriptionstore.cpp +++ b/subscriptionstore.cpp @@ -122,7 +122,7 @@ SubscriptionNode *SubscriptionStore::getDeepestNode(const std::string &topic, co if (!node) { - node.reset(new SubscriptionNode(subtopic)); + node = std::make_unique(subtopic); } deepestNode = node.get(); } @@ -240,7 +240,7 @@ void SubscriptionStore::registerClientAndKickExistingOne(std::shared_ptr if (!session || client->getCleanSession() || originalClientDemandsSessionDestruction) { - session.reset(new Session()); + session = std::make_shared(); sessionsById[client->getClientId()] = session; } @@ -448,7 +448,7 @@ void SubscriptionStore::setRetainedMessage(const std::string &topic, const std:: if (!selectedChildren) { - selectedChildren.reset(new RetainedMessageNode()); + selectedChildren = std::make_unique(); } deepestNode = selectedChildren.get(); } -- libgit2 0.21.4