Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic app constructor #408

Merged
merged 3 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions include/crow/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ namespace crow
Crow()
{}

/// Construct Crow with a subset of middleware
template<typename... Ts>
Crow(Ts&&... ts):
middlewares_(make_middleware_tuple(std::forward<Ts>(ts)...))
{}

/// Process an Upgrade request

///
Expand Down Expand Up @@ -492,6 +498,17 @@ namespace crow
cv_started_.wait(lock);
}

private:
template<typename... Ts>
std::tuple<Middlewares...> make_middleware_tuple(Ts&&... ts)
{
auto fwd = std::forward_as_tuple((ts)...);
return std::make_tuple(
std::forward<Middlewares>(
black_magic::tuple_extract<Middlewares, decltype(fwd)>(fwd))...);
}


private:
std::uint8_t timeout_{5};
uint16_t port_ = 80;
Expand Down
24 changes: 24 additions & 0 deletions include/crow/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,30 @@ namespace crow
static const int value = 1 + tuple_index<T, std::tuple<Types...>>::value;
};

// Extract element from forward tuple or get default
#ifdef CROW_CAN_USE_CPP14
template<typename T, typename Tup>
typename std::enable_if<has_type<T&, Tup>::value, typename std::decay<T>::type&&>::type
tuple_extract(Tup& tup)
{
return std::move(std::get<T&>(tup));
}
#else
template<typename T, typename Tup>
typename std::enable_if<has_type<T&, Tup>::value, T&&>::type
tuple_extract(Tup& tup)
{
return std::move(std::get<tuple_index<T&, Tup>::value>(tup));
}
#endif

template<typename T, typename Tup>
typename std::enable_if<!has_type<T&, Tup>::value, T>::type
tuple_extract(Tup&)
{
return T{};
}

// Kind of fold expressions in C++11
template<bool...>
struct bool_pack;
Expand Down
15 changes: 15 additions & 0 deletions tests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,21 @@ TEST_CASE("local_middleware")
app.stop();
} // local_middleware

struct OnlyMoveConstructor
{
OnlyMoveConstructor(int) {}
OnlyMoveConstructor(const OnlyMoveConstructor&) = delete;
OnlyMoveConstructor(OnlyMoveConstructor&&) = default;
};

TEST_CASE("app_constructor")
{
App<NullMiddleware, OnlyMoveConstructor, FirstMW<false>, SecondMW<false>>
app1(OnlyMoveConstructor(1), SecondMW<false>{});
App<NullMiddleware, OnlyMoveConstructor, FirstMW<false>, SecondMW<false>>
app2(FirstMW<false>{}, OnlyMoveConstructor(1));
} // app_constructor

TEST_CASE("middleware_blueprint")
{
// Same logic as middleware_context, but middleware is added with blueprints
Expand Down