From f36ccf851d011f10a61a7fb74a6634e9654bcc3e Mon Sep 17 00:00:00 2001 From: antao Date: Wed, 16 Jan 2019 19:26:45 +0800 Subject: [PATCH] Modify the getObj() function template --- lib/inc/drogon/HttpBinder.h | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/lib/inc/drogon/HttpBinder.h b/lib/inc/drogon/HttpBinder.h index 324581d6..d9985955 100755 --- a/lib/inc/drogon/HttpBinder.h +++ b/lib/inc/drogon/HttpBinder.h @@ -71,6 +71,16 @@ class HttpBinderBase virtual size_t paramCount() = 0; virtual ~HttpBinderBase() {} }; + +template +T &getControllerObj() +{ + //Initialization of function-local statics is guaranteed to occur only once even when + //called from multiple threads, and may be more efficient than the equivalent code using std::call_once. + static T obj; + return obj; +} + typedef std::shared_ptr HttpBinderBasePtr; template class HttpBinder : public HttpBinderBase @@ -147,10 +157,8 @@ class HttpBinder : public HttpBinderBase const HttpRequestPtr &req, std::function callback, Values &&... values) { - static auto className = drogon::DrObjectBase::demangle(typeid(typename traits::class_type).name()); - auto &obj=getObj(); - assert(obj); - ((*obj).*_func)(req, callback, std::move(values)...); + auto &obj = getControllerObj(); + (obj.*_func)(req, callback, std::move(values)...); }; template @@ -160,22 +168,6 @@ class HttpBinder : public HttpBinderBase { _func(req, callback, std::move(values)...); }; - template - typename std::enable_if &>::type getObj() - { - //Initialization of function-local statics is guaranteed to occur only once even when - //called from multiple threads, and may be more efficient than the equivalent code using std::call_once. - static auto obj = std::shared_ptr(new typename traits::class_type); - return obj; - } - template - typename std::enable_if &>::type getObj() - { - //This function will never be called - static auto obj = std::shared_ptr(nullptr); - assert(0); - return obj; - } }; } // namespace internal