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

Fix example-chat #743

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions include/crow/http_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ namespace crow
need_to_call_after_handlers_ = false;
if (!is_invalid_request)
{
res.complete_request_handler_ = nullptr;
res.complete_request_handler_.clear();
auto self = this->shared_from_this();
res.is_alive_helper_ = [self]() -> bool {
return self->adaptor_.is_open();
Expand Down Expand Up @@ -202,7 +202,7 @@ namespace crow
void complete_request()
{
CROW_LOG_INFO << "Response: " << this << ' ' << req_.raw_url << ' ' << res.code << ' ' << close_connection_;
res.is_alive_helper_ = nullptr;
res.is_alive_helper_.clear();

if (need_to_call_after_handlers_)
{
Expand Down Expand Up @@ -271,8 +271,8 @@ namespace crow
private:
void prepare_buffers()
{
res.complete_request_handler_ = nullptr;
res.is_alive_helper_ = nullptr;
res.complete_request_handler_.clear();
res.is_alive_helper_.clear();

if (!adaptor_.is_open())
{
Expand Down
4 changes: 2 additions & 2 deletions include/crow/http_response.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ namespace crow

private:
bool completed_{};
std::function<void()> complete_request_handler_;
std::function<bool()> is_alive_helper_;
crow::utility::lambda_wrapper<void> complete_request_handler_;
crow::utility::lambda_wrapper<bool> is_alive_helper_;
static_file_info file_info;
};
} // namespace crow
50 changes: 46 additions & 4 deletions include/crow/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ namespace crow
{
return p == s.size() ? 0 :
s[p] == '<' ? (
is_int(s, p) ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 1 :
is_int(s, p) ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 1 :
is_uint(s, p) ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 2 :
is_float(s, p) ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 3 :
is_str(s, p) ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 4 :
Expand Down Expand Up @@ -489,7 +489,6 @@ namespace crow

namespace detail
{

template<class T, std::size_t N, class... Args>
struct get_index_of_element_from_tuple_by_type_impl
{
Expand Down Expand Up @@ -715,8 +714,8 @@ namespace crow
// a special device. Thus we search for the string (case-insensitive), and then check if the string ends or if
// is has a dangerous follow up character (.:\/)
auto sanitizeSpecialFile = [](std::string& source, unsigned ofs, const char* pattern, bool includeNumber, char replacement) {
unsigned i = ofs;
size_t len = source.length();
unsigned i = ofs;
size_t len = source.length();
const char* p = pattern;
while (*p)
{
Expand Down Expand Up @@ -897,5 +896,48 @@ namespace crow

return v.substr(begin, end - begin);
}

class call_error : public std::runtime_error
{
public:
call_error() : std::runtime_error{"call error"}
{
}
};

template <typename ReturnType, typename... Args>
class lambda_wrapper
{
public:
lambda_wrapper() = default;

lambda_wrapper(std::function<ReturnType(Args...)> func) : valid_(true), func_(func)
{
}

void clear() { valid_ = false; }

operator bool() const { return valid_; }

lambda_wrapper<ReturnType, Args...>& operator=(std::function<ReturnType(Args...)> func)
{
valid_ = true;
func_ = func;
return *this;
}

ReturnType operator()(Args... args) const
{
if (valid_)
{
return func_(args...);
}
throw call_error();
}
Comment on lines +929 to +936
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand what lambda_wrapper does 🙂 After all, std just throws as well

https://en.cppreference.com/w/cpp/utility/functional/function/operator()

Exceptions
Throws std::bad_function_call if *this does not store a callable function target, i.e. !*this == true.


private:
mutable bool valid_{};
std::function<ReturnType(Args...)> func_;
};
} // namespace utility
} // namespace crow
22 changes: 22 additions & 0 deletions tests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3445,3 +3445,25 @@ TEST_CASE("lexical_cast")
CHECK(utility::lexical_cast<string>(4) == "4");
CHECK(utility::lexical_cast<float>("10", 2) == 10.0f);
}

TEST_CASE("lambda_wrapper")
{
utility::lambda_wrapper<int, std::string> empty;
CHECK(empty != true);
CHECK_THROWS_AS(empty("hello"), crow::utility::call_error);

utility::lambda_wrapper<int> const_val([]()->int { return 42; });
CHECK(const_val == true);
CHECK(const_val() == 42);

utility::lambda_wrapper<int, int, int> plus([](int a, int b)->int { return a + b; });
CHECK(plus == true);
CHECK(plus(2, 3) == 5);

const std::string name{"Motoko"};
utility::lambda_wrapper<void, std::string&> func([name](std::string& str) { str = name; });
CHECK(func == true);
std::string strval;
func(strval);
CHECK(strval == name);
}
Loading