diff --git a/examples/static_link_example/main.cc b/examples/static_link_example/main.cc index d0ee77b3..f6a92742 100755 --- a/examples/static_link_example/main.cc +++ b/examples/static_link_example/main.cc @@ -1,7 +1,6 @@ #include #include #include -#include #include #include using namespace drogon; @@ -16,18 +15,27 @@ public: LOG_DEBUG<<"int haha="<>(&A::handle); - //drogon::HttpApiBinder binder(&A::handle); - // binder.test(); drogon::HttpAppFramework::instance().addListener("0.0.0.0",12345); drogon::HttpAppFramework::instance().addListener("0.0.0.0",8080); trantor::Logger::setLogLevel(trantor::Logger::TRACE); - drogon::HttpAppFramework::instance().registerHttpApiController("/api/v1","",bindPtr); + drogon::HttpAppFramework::registerHttpApiMethod("/api/v1/handle1","",&A::handle); + + drogon::HttpAppFramework::registerHttpApiMethod("/api/v1/handle2","",[](const HttpRequest&req,std::functioncallback,int a,float b){ + LOG_DEBUG<<"int a="< &pathParameter, const HttpRequest& req,std::functioncallback) override { run(pathParameter,req,callback); } - HttpApiBinder(FUNCTION func): - _func(func) + HttpApiBinder(FUNCTION &&func): + _func(std::forward(func)) { static_assert(traits::isHTTPApiFunction,"Your API handler function interface is wrong!"); } @@ -93,10 +94,26 @@ namespace drogon{ const HttpRequest& req,std::functioncallback, Values&&... values ) + { + callFunction(req,callback,std::move(values)...); + } + template + typename std::enable_if::type callFunction( + const HttpRequest& req,std::functioncallback, + Values&&... values) { //new object per time or create a object in constructor??? std::unique_ptr ptr(new typename traits::class_type); (ptr.get()->*_func)(req,callback,std::move(values)...); - } + }; + template + typename std::enable_if::type callFunction( + const HttpRequest& req,std::functioncallback, + Values&&... values) + { + _func(req,callback,std::move(values)...); + }; }; } \ No newline at end of file diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index fd130840..765b33c5 100755 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -60,6 +60,29 @@ namespace drogon const std::string ¶meterPattern, const HttpApiBinderBasePtr &binder, const std::vector &filters=std::vector())=0; + template + static void registerHttpApiMethod(const std::string &pathName, + const std::string ¶meterPattern, + FUNCTION && function, + const std::vector &filters=std::vector()) + { + static_assert(!std::is_bind_expression::value); + HttpApiBinderBasePtr binder=std::make_shared< + HttpApiBinder + >(std::forward(function)); + instance().registerHttpApiController(pathName,parameterPattern,binder,filters); + } +// template +// static void registerHttpApiBind(const std::string &pathName, +// const std::string ¶meterPattern, +// const std::function & function, +// const std::vector &filters=std::vector()) +// { +// HttpApiBinderBasePtr binder=std::make_shared< +// HttpApiBinder +// >(std::forward(function)); +// instance().registerHttpApiController(pathName,parameterPattern,binder,filters); +// } virtual void enableSession(const size_t timeout=0)=0; virtual void disableSession()=0; }; diff --git a/lib/inc/drogon/utils/FunctionTraits.h b/lib/inc/drogon/utils/FunctionTraits.h index 9d43f2eb..926104c0 100755 --- a/lib/inc/drogon/utils/FunctionTraits.h +++ b/lib/inc/drogon/utils/FunctionTraits.h @@ -34,9 +34,10 @@ namespace drogon{ ReturnType(ClassType::*)(Arguments...) > : FunctionTraits { static const bool isClassFunction=true; + typedef ClassType class_type; }; - + //class function template < typename ClassType, typename ReturnType, @@ -44,10 +45,32 @@ namespace drogon{ > struct FunctionTraits< ReturnType(ClassType::*)(const HttpRequest& req,std::functioncallback,Arguments...) + > : FunctionTraits { + static const bool isHTTPApiFunction=true; + + }; + //std::function + template < + typename ReturnType, + typename... Arguments + > + struct FunctionTraits< + ReturnType(*)(const HttpRequest& req,std::functioncallback,Arguments...) > : FunctionTraits { static const bool isHTTPApiFunction=true; - typedef ClassType class_type; - }; + + }; + //normal function + template < + typename ReturnType, + typename... Arguments + > + struct FunctionTraitscallback,Arguments...)>> + :FunctionTraits { + static const bool isHTTPApiFunction=true; + + }; template < typename ReturnType, @@ -67,6 +90,7 @@ namespace drogon{ static const std::size_t arity = sizeof...(Arguments); static const bool isHTTPApiFunction=false; + static const bool isClassFunction=false; }; }