From 5712a6042accdfbf1cbceefb278a984e0ea5cfe4 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 7 Apr 2021 09:12:10 +0200 Subject: [PATCH] DCE factory rework --- .../examples/ap_to_pppos/main/NetworkDCE.cpp | 2 +- .../cxx_include/esp_modem_dce_factory.hpp | 91 ++++++------------- 2 files changed, 31 insertions(+), 62 deletions(-) diff --git a/esp_modem/examples/ap_to_pppos/main/NetworkDCE.cpp b/esp_modem/examples/ap_to_pppos/main/NetworkDCE.cpp index 99891b4b6..03c8800d1 100644 --- a/esp_modem/examples/ap_to_pppos/main/NetworkDCE.cpp +++ b/esp_modem/examples/ap_to_pppos/main/NetworkDCE.cpp @@ -19,7 +19,7 @@ public: template static DCE_T* create(const config *cfg, Args&&... args) { - return build_generic_DCE< /* Object to create */ DCE_T, /* vanilla pointer */ DCE_T *, /* module */ T> + return build_generic_DCE (cfg, std::forward(args)...); } }; diff --git a/esp_modem/include/cxx_include/esp_modem_dce_factory.hpp b/esp_modem/include/cxx_include/esp_modem_dce_factory.hpp index dd31805cc..ef6530ba3 100644 --- a/esp_modem/include/cxx_include/esp_modem_dce_factory.hpp +++ b/esp_modem/include/cxx_include/esp_modem_dce_factory.hpp @@ -23,25 +23,22 @@ class FactoryHelper { public: static std::unique_ptr create_pdp_context(std::string &apn); - template - static auto make(T* &t, Args&&... args) -> std::remove_reference_t + template + static auto make(Args&&... args) -> typename std::enable_if::value, T*>::type { - t = new T(std::forward(args)...); - return true; + return new T(std::forward(args)...); } - template - static bool make(std::shared_ptr &t, Args&&... args) + template + static auto make(Args&&... args) -> typename std::enable_if>::value, std::shared_ptr>::type { - t = std::make_shared(std::forward(args)...); - return true; + return std::make_shared(std::forward(args)...); } - template - static bool make(std::unique_ptr &t, Args&&... args) + template , typename ...Args> + static auto make(Args&&... args) -> typename std::enable_if>::value, std::unique_ptr>::type { - t = std::unique_ptr(new T(std::forward(args)...)); - return true; + return std::unique_ptr(new T(std::forward(args)...)); } }; @@ -50,13 +47,6 @@ template class Builder { static_assert(std::is_base_of::value, "Builder must be used only for Module classes"); public: -// explicit Builder(std::shared_ptr dte): dte(std::move(dte)) -// { -// esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_PPP(); -// netif = esp_netif_new(&netif_config); -// throw_if_false(netif != nullptr, "Cannot create default PPP netif"); -// } - Builder(std::shared_ptr x, esp_netif_t* esp_netif): dte(std::move(x)), module(nullptr), netif(esp_netif) { throw_if_false(netif != nullptr, "Null netif"); @@ -72,24 +62,23 @@ public: throw_if_false(module == nullptr, "module was captured or created but never used"); } - template - bool create_module(Ptr &t, const esp_modem_dce_config *config) + auto create_module(const esp_modem_dce_config *config) -> Ptr { - return FactoryHelper::make(t, dte, config); + return FactoryHelper::make(dte, config); } template - bool create(Ptr &t, const esp_modem_dce_config *config) + auto create(const esp_modem_dce_config *config) -> Ptr { if (dte == nullptr) - return false; + return nullptr; if (module == nullptr) { - if (!create_module(module, config)) { - return false; - } + module = create_module(config); + if (module == nullptr) + return nullptr; } - return FactoryHelper::make(t, std::move(dte), std::move(module), netif); + return FactoryHelper::make(std::move(dte), std::move(module), netif); } private: @@ -109,23 +98,23 @@ class Factory { public: explicit Factory(Modem modem): m(modem) {} - template + template static std::unique_ptr build_unique(const config *cfg, Args&&... args) { - return build_generic_DCE, T>(cfg, std::forward(args)...); + return build_generic_DCE>(cfg, std::forward(args)...); } - template + template static DCE* build(const config *cfg, Args&&... args) { - return build_generic_DCE(cfg, std::forward(args)...); + return build_generic_DCE(cfg, std::forward(args)...); } - template - static std::shared_ptr build_shared_module(const config *cfg, Args&&... args) + template + std::shared_ptr build_shared_module(const config *cfg, Args&&... args) { - return build_module_T, T>(cfg, std::forward(args)...); + return build_module_T(cfg, std::forward(args)...); } @@ -161,43 +150,23 @@ public: return nullptr; } - private: Modem m; - template - static bool build_module_T(U &t, const config *cfg, Args&&... args) - { - Builder b(std::forward(args)...); - return b.create_module(t, cfg); - } - - template - static T build_module_T(const config *cfg, Args&&... args) - { - T module; - if (build_module_T(module, cfg, std::forward(args)...)) - return module; - return nullptr; - } - - template - static bool build_generic_DCE(DcePtr &t, const config *cfg, Args&&... args) + template , typename ...Args> + Module build_module_T(const config *cfg, Args&&... args) { Builder b(std::forward(args)...); - return b.template create(t, cfg); + return b.template create_module(cfg); } protected: - template + template , typename DcePtr = Dce*, typename ...Args> static DcePtr build_generic_DCE(const config *cfg, Args&&... args) { - DcePtr dce; - if (build_generic_DCE(dce, cfg, std::forward(args)...)) - return dce; - return nullptr; + Builder b(std::forward(args)...); + return b.template create(cfg); } - }; } // namespace esp_modem::dce_factory