From 74d94888d1434ae87d7687604ca262f723bbb828 Mon Sep 17 00:00:00 2001 From: Vladislav Oleshko Date: Wed, 13 Apr 2022 20:49:22 +0300 Subject: [PATCH 1/2] Generic app constructor --- include/crow/app.h | 17 +++++++++++++++++ include/crow/utility.h | 40 ++++++++++++++++++++++++++++++++++++++++ tests/unittest.cpp | 13 +++++++++++++ 3 files changed, 70 insertions(+) diff --git a/include/crow/app.h b/include/crow/app.h index 7eb327983..564a00bde 100644 --- a/include/crow/app.h +++ b/include/crow/app.h @@ -58,6 +58,12 @@ namespace crow Crow() {} + /// Construct Crow with a subset of middleware + template + Crow(Ts&&... ts): + middlewares_(make_middleware_tuple(std::forward(ts)...)) + {} + /// Process an Upgrade request /// @@ -431,6 +437,17 @@ namespace crow cv_started_.wait(lock); } + private: + template + std::tuple make_middleware_tuple(Ts&&... ts) + { + auto fwd = std::forward_as_tuple((ts)...); + return std::make_tuple( + std::forward( + black_magic::tuple_extract(fwd))...); + } + + private: std::uint8_t timeout_{5}; uint16_t port_ = 80; diff --git a/include/crow/utility.h b/include/crow/utility.h index 8597950b6..27713e547 100644 --- a/include/crow/utility.h +++ b/include/crow/utility.h @@ -263,6 +263,46 @@ namespace crow struct has_type> : std::true_type {}; + // Find index of type in tuple + template + struct tuple_index; + + template + struct tuple_index> + { + static const int value = 0; + }; + + template + struct tuple_index> + { + static const int value = 1 + tuple_index>::value; + }; + + // Extract element from forward tuple or get default +#ifdef CROW_CAN_USE_CPP14 + template + typename std::enable_if::value, typename std::decay::type&&>::type + tuple_extract(Tup& tup) + { + return std::move(std::get(tup)); + } +#else + template + typename std::enable_if::value, T&&>::type + tuple_extract(Tup& tup) + { + return std::move(std::get::value>(tup)); + } +#endif + + template + typename std::enable_if::value, T>::type + tuple_extract(Tup&) + { + return T{}; + } + // Check F is callable with Args template struct is_callable diff --git a/tests/unittest.cpp b/tests/unittest.cpp index 4c980993f..6ab9cbf9f 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -1452,6 +1452,19 @@ TEST_CASE("local_middleware") app.stop(); } // local_middleware +struct OnlyMoveConstructor +{ + OnlyMoveConstructor(int) {} + OnlyMoveConstructor(const OnlyMoveConstructor&) = delete; + OnlyMoveConstructor(OnlyMoveConstructor&&) = default; +}; + +TEST_CASE("app_constructor") +{ + App + app(OnlyMoveConstructor(1), SecondMW{}); +} // app_constructor + TEST_CASE("middleware_cookieparser") { static char buf[2048]; From 946292747e867ffcac6eda301529a4e49b67a9c9 Mon Sep 17 00:00:00 2001 From: Vladislav Oleshko Date: Mon, 23 May 2022 20:17:49 +0300 Subject: [PATCH 2/2] Small fixes --- include/crow/utility.h | 32 ++++++++++++++++---------------- tests/unittest.cpp | 6 ++++-- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/include/crow/utility.h b/include/crow/utility.h index a20990a55..98206384e 100644 --- a/include/crow/utility.h +++ b/include/crow/utility.h @@ -265,6 +265,22 @@ namespace crow struct has_type> : std::true_type {}; + // Find index of type in tuple + template + struct tuple_index; + + template + struct tuple_index> + { + static const int value = 0; + }; + + template + struct tuple_index> + { + static const int value = 1 + tuple_index>::value; + }; + // Extract element from forward tuple or get default #ifdef CROW_CAN_USE_CPP14 template @@ -288,22 +304,6 @@ namespace crow { return T{}; } - - // Find index of type in tuple - template - struct tuple_index; - - template - struct tuple_index> - { - static const int value = 0; - }; - - template - struct tuple_index> - { - static const int value = 1 + tuple_index>::value; - }; // Kind of fold expressions in C++11 template diff --git a/tests/unittest.cpp b/tests/unittest.cpp index c4b916304..568a30e1a 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -1523,8 +1523,10 @@ struct OnlyMoveConstructor TEST_CASE("app_constructor") { - App - app(OnlyMoveConstructor(1), SecondMW{}); + App, SecondMW> + app1(OnlyMoveConstructor(1), SecondMW{}); + App, SecondMW> + app2(FirstMW{}, OnlyMoveConstructor(1)); } // app_constructor TEST_CASE("middleware_blueprint")