From 0f3e95f3400098fe7674852d83607c8255239a1c Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Thu, 12 Dec 2019 20:44:40 +0900 Subject: [PATCH] Remove custom deleter on pimpl pattern Custom deleter is needed when 'template constructor' uses. Because 'template constructor' is defined in header and the compiler has to know how to delete impl instance. Signed-off-by: Sangwan Kwon --- src/vist/rmi/exposer.cpp | 66 +++++++++++++++++++++--------------------------- src/vist/rmi/exposer.hpp | 13 ++++++---- src/vist/rmi/remote.cpp | 7 ++--- src/vist/rmi/remote.hpp | 17 +++++++------ 4 files changed, 48 insertions(+), 55 deletions(-) diff --git a/src/vist/rmi/exposer.cpp b/src/vist/rmi/exposer.cpp index ce79695..e33c515 100644 --- a/src/vist/rmi/exposer.cpp +++ b/src/vist/rmi/exposer.cpp @@ -34,51 +34,48 @@ using namespace vist::transport; class Exposer::Impl { public: - explicit Impl(Exposer& exposer, const std::string& path); + explicit Impl(Exposer& exposer, const std::string& path) + { + auto dispatcher = [&exposer](Message message) -> Message { + std::string function = message.signature; + auto iter = exposer.functorMap.find(function); + if (iter == exposer.functorMap.end()) + THROW(ErrCode::RuntimeError) << "Faild to find function."; - void start(void); - void stop(void); + DEBUG(VIST) << "Remote method invokation: " << function; -private: - std::unique_ptr server; -}; + auto functor = iter->second; + auto result = functor->invoke(message.buffer); -Exposer::Impl::Impl(Exposer& exposer, const std::string& path) -{ - auto dispatcher = [&exposer](Message message) -> Message { - std::string function = message.signature; - auto iter = exposer.functorMap.find(function); - if (iter == exposer.functorMap.end()) - THROW(ErrCode::RuntimeError) << "Faild to find function."; + Message reply(Message::Type::Reply, function); + reply.enclose(result); - DEBUG(VIST) << "Remote method invokation: " << function; + return reply; + }; - auto functor = iter->second; - auto result = functor->invoke(message.buffer); + this->server = std::make_unique(path, dispatcher); + } - Message reply(Message::Type::Reply, function); - reply.enclose(result); + inline void start() + { + this->server->run(); + } - return reply; - }; - - this->server = std::make_unique(path, dispatcher); -} + inline void stop() + { + this->server->stop(); + } -void Exposer::Impl::start(void) -{ - this->server->run(); -} - -void Exposer::Impl::stop(void) -{ - this->server->stop(); -} +private: + std::unique_ptr server; +}; Exposer::Exposer(const std::string& path) : pImpl(new Impl(*this, path)) { } +Exposer::~Exposer() = default; + void Exposer::start(void) { this->pImpl->start(); @@ -89,10 +86,5 @@ void Exposer::stop(void) this->pImpl->stop(); } -void Exposer::ImplDeleter::operator()(Impl* ptr) -{ - delete ptr; -} - } // namespace rmi } // namespace vist diff --git a/src/vist/rmi/exposer.hpp b/src/vist/rmi/exposer.hpp index ac9d25e..e4c2a99 100644 --- a/src/vist/rmi/exposer.hpp +++ b/src/vist/rmi/exposer.hpp @@ -33,6 +33,13 @@ namespace rmi { class Exposer final { public: explicit Exposer(const std::string& path); + ~Exposer(); + + Exposer(const Exposer&) = delete; + Exposer& operator=(const Exposer&) = delete; + + Exposer(Exposer&&) = default; + Exposer& operator=(Exposer&&) = default; void start(void); void stop(void); @@ -42,13 +49,9 @@ public: private: class Impl; - struct ImplDeleter - { - void operator()(Impl*); - }; klass::FunctorMap functorMap; - std::unique_ptr pImpl; + std::unique_ptr pImpl; }; template diff --git a/src/vist/rmi/remote.cpp b/src/vist/rmi/remote.cpp index c041b0d..7d24371 100644 --- a/src/vist/rmi/remote.cpp +++ b/src/vist/rmi/remote.cpp @@ -51,15 +51,12 @@ Remote::Remote(const std::string& path) : pImpl(new Impl(path)) { } +Remote::~Remote() = default; + Message Remote::request(Message message) { return pImpl->request(message); } -void Remote::ImplDeleter::operator()(Impl* ptr) -{ - delete ptr; -} - } // namespace rmi } // namespace vist diff --git a/src/vist/rmi/remote.hpp b/src/vist/rmi/remote.hpp index ab95d28..ae05319 100644 --- a/src/vist/rmi/remote.hpp +++ b/src/vist/rmi/remote.hpp @@ -32,9 +32,16 @@ namespace rmi { using namespace vist::transport; -class Remote { +class Remote final { public: explicit Remote(const std::string& path); + ~Remote(); + + Remote(const Remote&) = delete; + Remote& operator=(const Remote&) = delete; + + Remote(Remote&&) = default; + Remote& operator=(Remote&&) = default; template R invoke(const std::string& name, Args&&... args); @@ -43,13 +50,7 @@ private: Message request(Message message); class Impl; - /// Let compiler know how to destroy Impl - struct ImplDeleter - { - void operator()(Impl*); - }; - - std::unique_ptr pImpl; + std::unique_ptr pImpl; }; template -- 2.7.4