diff --git a/examples/simple_example/main.cc b/examples/simple_example/main.cc index 85259849..18fd9c9e 100755 --- a/examples/simple_example/main.cc +++ b/examples/simple_example/main.cc @@ -6,7 +6,7 @@ #include using namespace drogon; -class A +class A:public DrObjectBase { public: void handle(const HttpRequestPtr& req, @@ -26,7 +26,7 @@ public: callback(*res); } }; -class B +class B:public DrObjectBase { public: void operator ()(const HttpRequestPtr& req,const std::function&callback,int p1,int p2) diff --git a/lib/inc/drogon/HttpApiBinder.h b/lib/inc/drogon/HttpApiBinder.h index 7a6ffa7b..8905d37f 100755 --- a/lib/inc/drogon/HttpApiBinder.h +++ b/lib/inc/drogon/HttpApiBinder.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -52,6 +53,12 @@ namespace drogon{ =0; virtual size_t paramCount()=0; virtual ~HttpApiBinderBase(){} + + protected: + + static std::map> _objMap; + static std::mutex _objMutex; + }; typedef std::shared_ptr HttpApiBinderBasePtr; template @@ -136,8 +143,22 @@ namespace drogon{ const HttpRequestPtr& req,std::functioncallback, Values&&... values) { - static typename traits::class_type obj; - (obj.*_func)(req,callback,std::move(values)...); + static auto className=drogon::DrObjectBase::demangle(typeid(typename traits::class_type).name()); + std::shared_ptr obj; + { + std::lock_guard guard(_objMutex); + if(_objMap.find(className)==_objMap.end()) + { + obj=std::shared_ptr(new typename traits::class_type); + _objMap[className]=obj; + } + else + { + obj=std::dynamic_pointer_cast(_objMap[className]); + } + } + assert(obj); + ((*obj).*_func)(req,callback,std::move(values)...); }; template diff --git a/lib/src/HttpAppFramework.cc b/lib/src/HttpAppFramework.cc index 86e05a15..50484176 100755 --- a/lib/src/HttpAppFramework.cc +++ b/lib/src/HttpAppFramework.cc @@ -38,7 +38,8 @@ namespace drogon { - + std::map> HttpApiBinderBase::_objMap; + std::mutex HttpApiBinderBase::_objMutex; class HttpAppFrameworkImpl:public HttpAppFramework { public: