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

Check for cache #104

Closed
axx opened this issue Apr 22, 2019 · 9 comments
Closed

Check for cache #104

axx opened this issue Apr 22, 2019 · 9 comments

Comments

@axx
Copy link

axx commented Apr 22, 2019

Hi,

Newbie question. I'm trying to understand the code in http_server.hpp and I'm having a hard time understanding line 172:

bool b = true;
((b&&(b = need_cache(std::forward<AP>(ap))), false),...);

Could you please explain why there's a false? Eclipse tells me there's a syntax error, by the way.

Thanks in advance,
Allister

@qicosmos
Copy link
Owner

It's fold expression in c++17, you need choose c++17.

@axx
Copy link
Author

axx commented Apr 22, 2019

OK, I found this documentation: https://en.cppreference.com/w/cpp/language/fold.
So just to confirm that I understood correctly, line 172 corresponds to a unary right fold:

( pack op ... )

where op is the comma operator and pack is (b&&(b = need_cache(std::forward<AP>(ap))), false)?

@qicosmos
Copy link
Owner

the op is the comma expression

@axx
Copy link
Author

axx commented Apr 22, 2019

While I still can't wrap my head around this fold expression, I tried to see what it does, so I wrote this code:
https://gist.github.com/axx/4160b06a0f3e2832c1b2c5087870db4a
The output is like this:

./a.out                  
do_something(log_t{})
does not have type enable_cache
==========
do_something(enable_cache{ false })
skip cache
==========
do_something(enable_cache{ true })
need cache
==========
do_something(log_t{}, enable_cache{ false })
skip cache
==========
do_something(log_t{}, enable_cache{ true })
skip cache
==========

The last function call looks weird to me, since I passed a enable_cache{ true } but we still eventually skip cache (because of the log_t{}).
Is that the intended behavior?

@qicosmos
Copy link
Owner

I will explain the intention for you tomorrow:)

@qicosmos
Copy link
Owner

qicosmos commented Apr 23, 2019

Well, there is bug here, i have fixed it right now, thanks for your checking.
Ok, i would like to explain the intention and code now.

server.set_http_handler<GET, POST>("/a", &person::foo, enable_cache{ false }, log_t{});
server.set_http_handler<GET, POST>("/b", &person::foo1, enable_cache{ true }, log_t{});
server.set_http_handler<GET, POST>("/b", &person::foo1, log_t{}, enable_cache{ true });

Firstly, i don't want to limit the sequence of enable_cache, as you can see, the enbale_cache can be every where;
Secondly, i need to check the real value of enable_cache, maybe true, maybe false. I need to check it's true or false, furthermore, if the argument is not enable_cache, it is false.
Thirdly, i want to do an optimization, if the enable_cache value is true, i will finish checking the cache value, so i use a bool value to control the checking.

    server.set_http_handler<GET, POST>("/a", &person::foo, log_t{});
server.set_http_handler<GET, POST>("/a", &person::foo, enable_cache{ false }, log_t{});
server.set_http_handler<GET, POST>("/b", &person::foo1, enable_cache{ true }, log_t{});
server.set_http_handler<GET, POST>("/b", &person::foo1, log_t{}, enable_cache{ true });

In the first line, the need_cache will be false, as there is not enable_cache;
In the second line , the need_cache will be false, as the enable_cache value is false;
In the third line, the need_cache will be true, as the enable_cache value is true, and i won't do check for the next arguments, because i have got the true enable_cache.
In the last line, the need_cache will be true, as the enable_cache value is true; when i check the first argument log_t{}, the result is false, i will continue to check the remaining arguments until it' true.

@axx
Copy link
Author

axx commented Apr 23, 2019

Thanks for the explanation. I think I finally understood the logic here. That said, I think this line could still be simplified from:

((!b&&(b = need_cache(std::forward<AP>(ap))), false),...);

to this (removing the false expression since it's not doing anything):

((!b&&(b = need_cache(std::forward<AP>(ap)))),...);

@qicosmos
Copy link
Owner

yes, you're right, i will remove the false expression.

@axx
Copy link
Author

axx commented Apr 23, 2019

Cool! I'm closing this issue then. Thanks for your patience :)

@axx axx closed this as completed Apr 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants