194 points · by jeremyjh · 12y ago · 63 comments
I have a couple of questions, and that is what is the reason for only having .h files with no .cpp files in the main part of the microframework? Did something force you to do it that way or was it done like that because you like that code structure?
Inlining everything is bad because:
- it makes the binary much bigger.
- the smallest code change forces library users (applications) to be recompiled.
For libraries it's usually better to go to the other extreme: hide as much code and data as possible in the source files. See, for example, https://en.wikipedia.org/wiki/Opaque_pointer. It greatly reduces the need for applications to be recompiled when the library is updated.It's actually very unusual to put all function implementations inline (edit: as in, inside the class declaration) in C++. I wonder whether the author is a heavy Java user.
To check whether handler is valid with given URL at compile time, `url' (string literal) argument requires in compile time and in run time. const char* value is invalid for template argument and argument of non-constexpr function cannot be constexpr value. Thus I used macro to provied `url' argument twice; in template argument through constexpr function and in argument.
You might be interested in metaparse [1] which can greatly simplify compile time parsing of strings but has a very steep learning curve.
crow::black_magic::is_equ_p(...)
I haven't yet tried the framework, but I already like you. CROW_ROUTE(app, "/about")
([](Response res){
res.send("About Crow example");
});
Why you might ask? So you can do this: CROW_ROUTE(app, "/about")
([](Response res){
responses.push_back(res);
});
And then some independent method could come along and do the res.send() when it is ready. The connection would hang until res.send() or similar is called on it. There would also be methods on the Response object so you can see if the connection is still alive etc, and maybe the ability to set timeouts directly on the Response object.[edit] This would allow people using your framework to implement long polling without locking up an io_service thread for each connection. It would also make it easier to add support for web sockets etc at a later date.
[edit2] This is how NodeJS works. Both a request and a response object is passed to the callback, then you can do for example:
function callback (req, res) {
setTimeout(function(){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}, 5000);
}I think supporting both way is better if there is a enough explanation. I don't want to drop a simpler way to do the same job.
CROW_ROUTE(app, "/about")
([](){
return "About Crow example";
});
CROW_ROUTE(app, "/about")
([](Response res){
res.send("About Crow example");
});Also, you said this adds complexity and overhead. I dispute that it adds complexity. For most people: "return stuff" vs "res.send(stuff)". And I wouldn't assume that it would add any overhead either. If you disagree, let me know when you've read the code and understand how boost::asio works.
The difference is a big reason why Apache 2.2 would slow to a crawl and eat up gigabytes of RAM at 100% processor utilization on the same load that Nginx could handle with 10Mb of RAM and 20% processor utilization. [1] (I understand more recent versions of Apache now support polling [2].)
[1] See, for example, this explanation: http://stackoverflow.com/questions/2583350/is-epoll-the-esse...
For example, this is how I handle routing: http://goo.gl/G3jvGp
A simple static map of regexes and pointers to methods will do the task nicely, and the principle is extendible if I need to create or compose complex apps.
There are still things which I would need to encapsulate to make it a rapid development framework (such as QUERY_STRING handling in http://goo.gl/l7Ff75) but it's surprisingly non-painful.
The full trie implementation caught my eye, are there no suitable alternatives in std or boost?
Mixed tabs and spaces (?) cause strange display in github in json.h from 555 through 670 or so.
Also at the start of json.h it seems a shame to repeat __builtin_expect rules, how about #if defined() || ?
[1] helped me understand it a bit, but Im not sure I see a compelling use case. Does it just provide a shorthand for calling functions on primitive types (and strings) or have I misunderstood? While requiring less characters might it not decrease readability?
Maybe this technique could be applied in printf or other C++ APIs using format string.
It's not new, it has been in GCC for quite a number of years (not sure how to check this quickly).
UPDATE: I found a list (https://ohse.de/uwe/articles/gcc-attributes.html#func-format) that says format was added in GCC 2.3-3.4 (whatever the range means). GCC 3.4.0 was released on April 18 2004. Now I'm sad I didn't say "ten years" above, as my original hunch was. :)
Will have a proper look at it later.
Also, Flask was created as a joke; it obviously took off; but there's something there about not taking it super super serious.
I agree, I find this annoying.