Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2004-09-21 11:48:44


John Torjo wrote:
>Hi Reece,
>
>This just popped into my head. A lot of confusion seems to come from how
>you pass decorators. What I'm trying to say is that it can be very
>difficult to get the decorator strings right (especially for more complex
>things like XML) from the first time.
>
>I would suggest every formatter takes only one string which parses and
>finds all decorators itself.
>Each formatter will have one or more escape sequences which it uses to find
>the decorators.
>
>Something like:
>
>// in this case, '%' is used for identifying an element
>// '*' is to mean "leave default" (don't change)
>
>std::vector<int> v;
>// equivalent to your "[ ", ", ", " ]"
>std::cout << formatob(v, "[ %, % ]");

This looks good :) On the implementation side it would require splitting the
string into the 2/3 components and constructing the decorators accordingly.
Maybe defining an "extract_decorators" function internally, so
wrapper_decorators and sequence_decorators can process this internally. How
about:

   std::cout << io::object( v )( "[ %, % ]" );

I am sure this would be feasible.

>// eq. to "<< ", " | ", " >>"
>std::cout << formatob(v, "<< % | % >>");
>
>// set only open/close decorators
>std::cout << formatob(v, "<< %*% >>");

Here, you would need special handling of string == "*". This should also be
implementable.

>// sets only the separator
>std::cout << formatob(v, "*% | %*");
>
>// for pair - you have "%1" and "%2"
>std::vector< std::pair<int,long> > vp;
>// write XML
>std::cout << formatob(vp,
> containerfmt("<elem> % </elem><elem> % </elem>",
> pairfrt("<int>%1</int><long>%2</long>")));

Here, you don't need %1 and %2. You just do: "<int>%</int><long>%</long>".

>How's that?

In terms of implementability, I don't see a problem. The two issues I have
are:
[1] What are the performance penalties for this?
[2] What if you want '%' or '*' in your format string, e.g. "[ a * b * c ]"?
You would thus need to implement some sort of escaping capability.

NOTE: This could sit along side using the 'decorate' function and Volodya's
idea for expressions, e.g.:

   io::object( v,
      "<<" + fmt::container( fmt::pair()( "<a>%</a><b>%</b>" )) + " >>"
   ).decorate( " : " );
   // output: << <a>2.75</a><b>1</b> : <a>3.14159</a><b>7</b> >>

Regards,
Reece

_________________________________________________________________
Use MSN Messenger to send music and pics to your friends
http://www.msn.co.uk/messenger


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk