#include #include #include #include #include using namespace Bu; class Fmter { public: Fmter( const Bu::String &sSrc ) : sSrc( sSrc ) { } template Fmter &arg( const T &x ) { lParm.append( Pair( x ) ); return *this; } template Fmter &arg( const T &x, Bu::Formatter::Fmt f ) { lParm.append( Pair( x, f ) ); return *this; } operator Bu::String() const { Bu::MemBuf mbOut; Bu::Formatter f( mbOut ); ParmList::const_iterator i = lParm.begin(); for( Bu::String::const_iterator s = sSrc.begin(); s; s++ ) { if( *s == '%' ) { f << (*i).format << (*i).value; i++; } else { f << *s; } } return mbOut.getString(); } private: const Bu::String &sSrc; class Pair { public: template Pair( const T &v ) : value( v ) { } template Pair( const T &v, Bu::Formatter::Fmt f ) : value( v ), format( f ) { } Bu::Variant value; Bu::Formatter::Fmt format; }; typedef Bu::List ParmList; ParmList lParm; }; Bu::Formatter &operator<<( Bu::Formatter &f, const Fmter &r ) { return f << (Bu::String)r; } int main() { sio << Fmter("A word is % and a number is % %.").arg("Hello").arg(75, Fmt::hex() ).arg(" - ") << sio.nl; }