Thread: [Cppcms-users] How to set default response().out() stream precision?
Brought to you by:
artyom-beilis
From: <ele...@ex...> - 2012-06-18 06:58:35
|
Ive tried putting response().out().precision(9) into overloaded application::init() but that seems to break things. Whats the proper way? Thanks Petr |
From: <ele...@ex...> - 2012-06-18 07:29:23
|
Looking at the implementation of http_response::out() it writes out headers so no wonder set_redirect_header is broken() if I do out().precision(9). Would it be possible to expose the ostream without writing headers? |
From: Artyom B. <art...@ya...> - 2012-06-18 08:54:56
|
----- Original Message ----- > From: "ele...@ex..." <ele...@ex...> > To: cpp...@li... > Cc: > Sent: Monday, June 18, 2012 10:29 AM > Subject: Re: [Cppcms-users] How to set default response().out() stream precision? > > Looking at the implementation of http_response::out() it writes out > headers so no wonder set_redirect_header is broken() if I do > out().precision(9). > > Would it be possible to expose the ostream without writing headers? > No, the access to out() flushes the headers by design as the data stream goes after the headers so the headers should be written. See: http://cppcms.com/cppcms_ref/latest/classcppcms_1_1http_1_1response.html#a39e48c676a3f2c79b26d60b8d455658c Artyom Beilis -------------- CppCMS - C++ Web Framework: http://cppcms.com/ CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/ |
From: <ele...@ex...> - 2012-06-18 08:59:00
|
> No, the access to out() flushes the headers by design as the data stream > goes after the headers so the headers should be written. So you are saying that without the patch I just sent, there's no way to change ostream precision and the user is simply stuck with 6 decimal numbers(the default) Petr |
From: <ele...@ex...> - 2012-06-18 08:56:35
Attachments:
ostream.diff
|
I went ahead and made a patch my self. File attached. Goes ahead HEAD. Now doing response().ostream().precision(9) works. Petr |
From: Artyom B. <art...@ya...> - 2012-06-18 09:11:41
|
----- Original Message ----- > From: "ele...@ex..." <ele...@ex...> > To: cpp...@li... > Cc: > Sent: Monday, June 18, 2012 11:56 AM > Subject: Re: [Cppcms-users] How to set default response().out() stream precision? > > I went ahead and made a patch my self. > > File attached. Goes ahead HEAD. > > Now doing response().ostream().precision(9) works. > I would not apply this patch for several reasons. 1. I keep a right to change ostream object, for example, older version of CppCMS (1.0.1) used different ostream objects for different configurations/io-modes. 2. There is a locale stuff that should be defined 3. It would be very confusing. What happens if a user writes: response().ostream() << "foo"? It is bad idea to have different ostreams in response If you want to set precision... Do it right before rendering. Or if you want to setup other settings on "some_other_stream" and than use response().out().copyfmt(some_other_stream) Don't forget to configure the locale in some_other_stream Regards, Artyom Beilis -------------- CppCMS - C++ Web Framework: http://cppcms.com/ CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/ |
From: <ele...@ex...> - 2012-06-18 09:54:53
|
> If you want to set precision... Do it right before rendering. > Or if you want to setup other settings on "some_other_stream" > and than use response().out().copyfmt(some_other_stream) Sorry, but do you realize how ridiculous that is? For every "view" in the project you'd have to call a function to change precision. That's just pain in the ass. What if you are a bank and your clients keep losing money because some guy forgot to change precision for every render() call? |
From: <ele...@ex...> - 2012-06-21 09:51:38
|
Can you at least increase the default precision of the stream to lets say... 10? That would save me troubles having to patch cppcms on every deployment of this app. It makes more sense than 6 which is in my opinion too low for just about anything but the most basic applications. Cheers Petr |
From: Artyom B. <art...@ya...> - 2012-06-21 10:03:09
|
See changing defaults is very bad idea - especially at framework level. If you have some trouble in all over the code, that seems that you had some **design** problem at the beginning and you try to solve it by altering the framework itself. The correct way to solve it is to set the precision on template level where you need: <% gt "{1,precision =10}" using my_high_precision_value %> See: http://www.boost.org/doc/libs/1_49_0/libs/locale/doc/html/localized_text_formatting.html If you need to change it globally create a small function in you topmost application class that all your classes are derived from: void render(std::string const &template_name,cppcms::basi_context &c) { response().out() << std::setprecision(10); cppcms::application::render(template_name,c); } Or even better create render_with_high_precision(...) function and do small refactoring. Artyom Beilis -------------- CppCMS - C++ Web Framework: http://cppcms.com/ CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/ ----- Original Message ----- > From: "ele...@ex..." <ele...@ex...> > To: Artyom Beilis <art...@ya...>; cpp...@li... > Cc: > Sent: Thursday, June 21, 2012 12:51 PM > Subject: Re: [Cppcms-users] How to set default response().out() stream precision? > > Can you at least increase the default precision of the stream to lets > say... 10? That would save me troubles having to patch cppcms on every > deployment of this app. > > It makes more sense than 6 which is in my opinion too low for just about > anything but the most basic applications. > > Cheers > Petr > > > ------------------------------------------------------------------------------ > Live Security Virtual Conference > Exclusive live event will cover all the ways today's security and > threat landscape has changed and how IT managers can respond. Discussions > will include endpoint security, mobile security and the latest in malware > threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ > _______________________________________________ > Cppcms-users mailing list > Cpp...@li... > https://lists.sourceforge.net/lists/listinfo/cppcms-users > |
From: <ele...@ex...> - 2012-06-21 10:17:43
|
> <% gt "{1,precision =10}" using my_high_precision_value %> That's just not feasible. If I had to do it for every number on the page, id have to spend like a whole day changing it all. > If you need to change it globally create a small function in you topmost > application > class that all your classes are derived from: > > void render(std::string const &template_name,cppcms::basi_context &c) > { > response().out() << std::setprecision(10); > cppcms::application::render(template_name,c); > } Yes yes, I'm aware of this. However because the render functions are not virtual, so they are not intended for overloading. At least that's the way i've been taught so I did not want to do this either. Anyway, is there are a reason they are not virtual? There's other functions cppcms::application that are virtual. Petr |
From: Artyom B. <art...@ya...> - 2012-06-21 10:34:54
|
----- Original Message ----- > From: "ele...@ex..." <ele...@ex...> > To: Artyom Beilis <art...@ya...>; cpp...@li... > Cc: > Sent: Thursday, June 21, 2012 1:17 PM > Subject: Re: [Cppcms-users] How to set default response().out() stream precision? > >> <% gt "{1,precision =10}" using my_high_precision_value > %> > > That's just not feasible. If I had to do it for every number on the page, > id have to spend like a whole day changing it all. > >> If you need to change it globally create a small function in you topmost >> application >> class that all your classes are derived from: >> >> void render(std::string const &template_name,cppcms::basi_context > &c) >> { >> response().out() << std::setprecision(10); >> cppcms::application::render(template_name,c); >> } > > > Yes yes, I'm aware of this. However because the render functions are not > virtual, so they are not intended for overloading. At least that's the way > i've been taught so I did not want to do this either. > See, you can override non-virtual function as well but the parent classes you call the old one. In our case it is not used by parent (application) class, it rather provides convenience interface to base_content::app_guard g(content,my_app); service().views_pool().render(my_app.context().skin(),template_name,my_app.response().out(),content); So this is not a problem - may be not the most beautiful solution but it works and consistent with the standard. If you don't like this create some other "hp_render" function and refactor the code in much fewer places. > Anyway, is there are a reason they are not virtual? There's other > functions cppcms::application that are virtual. > > Petr > Generally the only functions that are virtual are thous that are expected to be overridden. I can suggest an another option, consider you have some global skin: <% template master uses master %> <html> <head> .... <% end template %> Just add a simple code like: <% template master uses master %> <% c++ out()<<std::setprecision(10) %> <html> <head> .... <% end template %> And every template that derives from the master page would include it... See, there are many things you can do to override the default of the standard C++ std::ostream. It is not a big problem. Also if you are looking for correct solution for global altering of response().out() settings at cppcms level you may want to add some callbacks to cppcms::http::response like void on_before_stream_output(booster::function<(cppcms::http::context &)> const &); void on_after_stream_output(booster::function<(cppcms::http::context &)> const &); Such a patch I can accept as it makes much more sense and may be actually useful for other things as well like altering a session or HTTP headers globally. Artyom Beilis -------------- CppCMS - C++ Web Framework: http://cppcms.com/ CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/ |
From: <ele...@ex...> - 2012-06-21 10:54:13
|
> void on_before_stream_output(booster::function<(cppcms::http::context > &)> const &); > void on_after_stream_output(booster::function<(cppcms::http::context &)> > const &); > > Such a patch I can accept as it makes much more sense and may be actually > useful > for other things as well like altering a session or HTTP headers globally. I like this solution. I'll set the precision in the "master" view for now and then in the next few days when I(hopefully) get some spare time I'll look into it. One more thing... are you keen on using the same const order? I've always been doing "const cppcms::http::context&" instead of "cppcms::http::context const &" Petr |
From: Artyom B. <art...@ya...> - 2012-06-21 11:06:33
|
----- Original Message ----- > From: "ele...@ex..." <ele...@ex...> > To: Artyom Beilis <art...@ya...>; cpp...@li... > Cc: > Sent: Thursday, June 21, 2012 1:54 PM > Subject: Re: [Cppcms-users] How to set default response().out() stream precision? > >> void on_before_stream_output(booster::function<(cppcms::http::context >> &)> const &); >> void on_after_stream_output(booster::function<(cppcms::http::context > &)> >> const &); >> >> Such a patch I can accept as it makes much more sense and may be actually >> useful >> for other things as well like altering a session or HTTP headers globally. > > I like this solution. > > I'll set the precision in the "master" view for now and then in > the next > few days when I(hopefully) get some spare time I'll look into it. > Ok > One more thing... are you keen on using the same const order? > > I've always been doing "const cppcms::http::context&" instead > of > "cppcms::http::context const &" > > Petr > Small note the const refers to booster::function not to http::context And yes because it is more consitent In C and C++ you read definitions from right to left and from inside out int const *p[10] array of size 10 of pointers to constant int. Now lets see three different definitions 1. const int **p; 2. int const **p; 3. int * const *p; 4. int ** const p 5. int const * const * const p; 6. const int * const * const p; Note that 1==2 and 5==6 But when you read for example 2, 3 and 4 4 - constant pointer to pointer to (mutable) int 3 - pointer to constant pointer to (mutable) int 2 - pointer to pointer to constant it. This makes the position of const much more consistent and easy to read especially for stuff like const int * const &p; Is harder to understand visually then int const * const &p; Artyom Beilis -------------- CppCMS - C++ Web Framework: http://cppcms.com/ CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/ > > ------------------------------------------------------------------------------ > Live Security Virtual Conference > Exclusive live event will cover all the ways today's security and > threat landscape has changed and how IT managers can respond. Discussions > will include endpoint security, mobile security and the latest in malware > threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ > _______________________________________________ > Cppcms-users mailing list > Cpp...@li... > https://lists.sourceforge.net/lists/listinfo/cppcms-users > |
From: <ele...@ex...> - 2012-06-21 11:38:44
|
> In C and C++ you read definitions from right to left and from inside out > > int const *p[10] Are you sure? in C ok, but in C++ any STL code ive ever seen reads "const class& o". Even Bjarne writes it that way. It's simply more understandable, at least to me. Im not arguing with you, its the way you like it, that's the way you'll get it. But I find it hardly "more understanding" If anything "const char* const p" is a lot more logical as saying "p is const and it's type is const char pointer" To each of their own I guess, i just personally can't stand a lonely * or &, it looks messy imo :) Petr |