RE: [boost] Re: Formal review of "Output Formatters" library beginstoday

David Abrahams wrote:
"Reece Dunn" <msclrhd@hotmail.com> writes:
I'm generally not a great fan of statefulness, but this also seems like a reasonable thing to want:
namespace io = boost::io; std::cout << io::sequence_delimiters("{ ", " }"); std::cout << io::sequence( vec ); // output: { 1, 2, 3 }
You can achieve something like this using:
io::formatter< const char * > fmt( "{ ", " }" ); std::cout << io::sequence( vec ).format( fmt );
If there are any better alternatives, I am willing to listen.
Well, I think my alternative is better if you want a stateful change, since yours doesn't accomplish that. My suggestion is certainly consistent with normal io manipulators. The point is to stream a bunch of sequences without having to repeat the format part.
I understand what you are saying and yes your version is neater. However, implementing something like this would have performance penalties (because you will need to create/store the preset values at run-time). It will also change the way all sequence constructs are rendered. (n-ary types have a different rendreing so won't be affected). As I understand, this is the desired effect. But then: [1] how do you select sequence or n-ary formatting? [2] what about extending this to incorporate - for example - tree or string formatting? [3] how do you extend this to formatter types that have additional delimeters? [4] how do you know when to extract the default delimeter values from the stream? Adding a formatter as a parameter to io::sequence, etc. would further complicate function lookup. Perhapse if I had an is_format_object< T
::value trait I could use MPL wizardry to distinguish between format object and delimeter formatter, allowing:
io::formatter< const char * > fmt( "{ ", " }" ); std::cout << io::sequence( vec, fmt ); // output: { a, b, c } The problem with this is: what if you want to specify a format object and a delimeter formatter? This might work, but the implementation becomes more complex. Another idea would be to overload an operator like: std::cout << io::sequence( vec )( fmt ); // output: { a, b, c } or std::cout << io::sequence( vec )[ fmt ]; // output: { a, b, c } but this would further add to the confusion. If operator() was adopted, it would be possible to have that as a shorthand for format(). For example: std::cout << io::sequence( vec )( " | " ); // output: [ 1 | 2 | 3 ] Regards, Reece _________________________________________________________________ Express yourself with cool new emoticons http://www.msn.co.uk/specials/myemo

Well, I think my alternative is better if you want a stateful change, since yours doesn't accomplish that. My suggestion is certainly consistent with normal io manipulators. The point is to stream a bunch of sequences without having to repeat the format part.
I understand what you are saying and yes your version is neater. However, implementing something like this would have performance penalties (because you will need to create/store the preset values at run-time).
The penalties are small. Anyway, using std::istream/ostream have penalties, compared to using raw FILEs ;) That said, in this case there shouldn't be many penalties. Again, you can use ios_base::pword and ios_base::iword for this. It's an advanced technique, but I think it can prove very neat and flexible.
It will also change the way all sequence constructs are rendered. (n-ary types have a different rendreing so won't be affected). As I understand, this is the desired effect. But then:
You can make it possible to associate a fmt object with a delimeter object. Example: pairfmt() with openclose_formatter. basicfmt() with formatter. Etc. Then, you can store (default) delimeter objects within the stream. Once you want to output/input something, it requires a specific delim object. You'll somehow use ios_base::pword/iword to get a reference to that object.
[3] how do you extend this to formatter types that have additional delimeters? see above.
[4] how do you know when to extract the default delimeter values from the stream?
see above. Best, John -- John Torjo -- john@torjo.com Contributing editor, C/C++ Users Journal -- "Win32 GUI Generics" -- generics & GUI do mix, after all -- http://www.torjo.com/win32gui/ -- v1.4.0 - save_dlg - true binding of your data to UI controls! + easily add validation rules (win32gui/examples/smart_dlg)
participants (2)
-
John Torjo
-
Reece Dunn