/** * * FunctionTraits.h * An Tao * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license * that can be found in the License file. * * Drogon * */ #pragma once #include #include #include #include #include #ifdef __cpp_impl_coroutine #include #endif namespace drogon { class HttpRequest; class HttpResponse; using HttpRequestPtr = std::shared_ptr; using HttpResponsePtr = std::shared_ptr; namespace internal { #ifdef __cpp_impl_coroutine template using resumable_type = is_resumable; #else template struct resumable_type : std::false_type { }; #endif template struct FunctionTraits; // functor,lambda,std::function... template struct FunctionTraits : public FunctionTraits::type::operator())> { static const bool isClassFunction = false; static const bool isDrObjectClass = false; using class_type = void; static const std::string name() { return std::string("Functor"); } }; // class instance method of const object template struct FunctionTraits : FunctionTraits { static const bool isClassFunction = true; static const bool isDrObjectClass = std::is_base_of, ClassType>::value; using class_type = ClassType; static const std::string name() { return std::string("Class Function"); } }; // class instance method of non-const object template struct FunctionTraits : FunctionTraits { static const bool isClassFunction = true; static const bool isDrObjectClass = std::is_base_of, ClassType>::value; using class_type = ClassType; static const std::string name() { return std::string("Class Function"); } }; // normal function for HTTP handling template struct FunctionTraits< ReturnType (*)(const HttpRequestPtr &req, std::function &&callback, Arguments...)> : FunctionTraits { static const bool isHTTPFunction = !resumable_type::value; static const bool isCoroutine = false; using class_type = void; using first_param_type = HttpRequestPtr; using return_type = ReturnType; }; template struct FunctionTraits< ReturnType (*)(HttpRequestPtr &req, std::function &&callback, Arguments...)> : FunctionTraits { static const bool isHTTPFunction = false; using class_type = void; }; #ifdef __cpp_impl_coroutine template struct FunctionTraits< AsyncTask (*)(HttpRequestPtr req, std::function callback, Arguments...)> : FunctionTraits { static const bool isHTTPFunction = true; static const bool isCoroutine = true; using class_type = void; using first_param_type = HttpRequestPtr; using return_type = AsyncTask; }; template struct FunctionTraits< Task<> (*)(HttpRequestPtr req, std::function callback, Arguments...)> : FunctionTraits { static const bool isHTTPFunction = true; static const bool isCoroutine = true; using class_type = void; using first_param_type = HttpRequestPtr; using return_type = Task<>; }; template struct FunctionTraits (*)(HttpRequestPtr req, Arguments...)> : FunctionTraits { static const bool isHTTPFunction = true; static const bool isCoroutine = true; using class_type = void; using first_param_type = HttpRequestPtr; using return_type = Task; }; #endif template struct FunctionTraits< ReturnType (*)(HttpRequestPtr &&req, std::function &&callback, Arguments...)> : FunctionTraits { static const bool isHTTPFunction = false; using class_type = void; }; // normal function for HTTP handling template struct FunctionTraits< ReturnType (*)(T &&customReq, std::function &&callback, Arguments...)> : FunctionTraits { static const bool isHTTPFunction = !resumable_type::value; static const bool isCoroutine = false; using class_type = void; using first_param_type = T; using return_type = ReturnType; }; // normal function template struct FunctionTraits { using result_type = ReturnType; template using argument = typename std::tuple_element>::type; static const std::size_t arity = sizeof...(Arguments); using class_type = void; using return_type = ReturnType; static const bool isHTTPFunction = false; static const bool isClassFunction = false; static const bool isDrObjectClass = false; static const bool isCoroutine = false; static const std::string name() { return std::string("Normal or Static Function"); } }; } // namespace internal } // namespace drogon