From e5f2e1ce7faeb8fb4609f7291e77d5b682685057 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 5 Sep 2012 20:44:55 +0000 Subject: This'll make *everything* rebuild. String formatters now support the end() call, which will force substitution and return a string. They now also support ending actions, which let us do great stuff like printing stuff out after formatting finished...and other stuff. --- src/stable/sio.cpp | 38 +++++++++++++++++++++++++++++--------- src/stable/sio.h | 8 ++++---- src/stable/string.cpp | 19 +++++++++++++++---- src/stable/string.h | 32 +++++++++++++++++++++++++++----- src/tests/print.cpp | 13 +++++++++++++ 5 files changed, 88 insertions(+), 22 deletions(-) create mode 100644 src/tests/print.cpp diff --git a/src/stable/sio.cpp b/src/stable/sio.cpp index 02ba631..e7f7565 100644 --- a/src/stable/sio.cpp +++ b/src/stable/sio.cpp @@ -10,25 +10,45 @@ Bu::StdStream Bu::sioRaw; Bu::Formatter Bu::sio( Bu::sioRaw ); -Bu::size Bu::print( Bu::Stream &s, const Bu::String &str ) +class PrintEndAction : public Bu::String::FormatProxyEndAction { - return s.write( str.getStr(), str.getSize() ); +public: + PrintEndAction( Bu::Stream &s, bool bEndLn ) : + s( s ), + bEndLn( bEndLn ) + { + } + + virtual void operator()( const Bu::String &sFinal ) + { + s.write( sFinal.getStr(), sFinal.getSize() ); + if( bEndLn ) + { + s.write("\n", 1); + s.flush(); + } + } + + Bu::Stream &s; + bool bEndLn; +}; + +Bu::String::FormatProxy Bu::print( Bu::Stream &s, const Bu::String &str ) +{ + return str.format( new PrintEndAction( s, false ) ); } -Bu::size Bu::print( const Bu::String &str ) +Bu::String::FormatProxy Bu::print( const Bu::String &str ) { return print( sioRaw, str ); } -Bu::size Bu::println( Bu::Stream &s, const Bu::String &str ) +Bu::String::FormatProxy Bu::println( Bu::Stream &s, const Bu::String &str ) { - Bu::size sRet = s.write( str.getStr(), str.getSize() ); - sRet += s.write("\n", 1 ); - s.flush(); - return sRet; + return str.format( new PrintEndAction( s, true ) ); } -Bu::size Bu::println( const Bu::String &str ) +Bu::String::FormatProxy Bu::println( const Bu::String &str ) { return println( sioRaw, str ); } diff --git a/src/stable/sio.h b/src/stable/sio.h index cc7b098..d9761b2 100644 --- a/src/stable/sio.h +++ b/src/stable/sio.h @@ -16,11 +16,11 @@ namespace Bu extern Bu::StdStream sioRaw; extern Bu::Formatter sio; - Bu::size print( Bu::Stream &s, const Bu::String &str ); - Bu::size print( const Bu::String &str ); + Bu::String::FormatProxy print( Bu::Stream &s, const Bu::String &str ); + Bu::String::FormatProxy print( const Bu::String &str ); - Bu::size println( Bu::Stream &s, const Bu::String &str ); - Bu::size println( const Bu::String &str ); + Bu::String::FormatProxy println( Bu::Stream &s, const Bu::String &str ); + Bu::String::FormatProxy println( const Bu::String &str ); }; #endif diff --git a/src/stable/string.cpp b/src/stable/string.cpp index 4cef6b1..d1f76aa 100644 --- a/src/stable/string.cpp +++ b/src/stable/string.cpp @@ -1228,16 +1228,23 @@ bool Bu::String::isFlat() const // Sub-class Bu::String::FormatProxy // -Bu::String::FormatProxy::FormatProxy( const String &rFmt ) : - rFmt( rFmt ) +Bu::String::FormatProxy::FormatProxy( const String &rFmt, + String::FormatProxyEndAction *pAct ) : + rFmt( rFmt ), + pAct( pAct ), + bOpen( true ) { } Bu::String::FormatProxy::~FormatProxy() { + if( pAct && bOpen ) + end(); + + delete pAct; } -Bu::String::FormatProxy::operator Bu::String() const +Bu::String Bu::String::FormatProxy::end() const { int iCount = lArgs.getSize(); ArgList::const_iterator *aArg = @@ -1287,8 +1294,12 @@ Bu::String::FormatProxy::operator Bu::String() const } s++; } - + + bOpen = false; delete[] aArg; + + if( pAct ) + (*pAct)( mbOut.getString() ); return mbOut.getString(); } diff --git a/src/stable/string.h b/src/stable/string.h index 82b903b..c92a704 100644 --- a/src/stable/string.h +++ b/src/stable/string.h @@ -946,12 +946,21 @@ namespace Bu void flatten() const; bool isFlat() const; + public: + class FormatProxyEndAction + { + public: + virtual void operator()( const Bu::String &sFinal )=0; + }; + class FormatProxy { + friend class Bu::String; + private: + FormatProxy( const String &rFmt, FormatProxyEndAction *pAct=NULL ); + public: - FormatProxy( const String &rFmt ); virtual ~FormatProxy(); - template FormatProxy &arg( const T &x ) { @@ -968,7 +977,8 @@ namespace Bu return *this; } - operator String() const; + String end() const; + operator String() const { return end(); } private: const String &rFmt; @@ -993,20 +1003,32 @@ namespace Bu }; typedef Bu::List ArgList; ArgList lArgs; + FormatProxyEndAction *pAct; + mutable bool bOpen; }; public: template - FormatProxy arg( const ArgType &x ) + FormatProxy arg( const ArgType &x ) const { return FormatProxy( *this ).arg( x ); } template - FormatProxy arg( const ArgType &x, const Bu::Fmt &f ) + FormatProxy arg( const ArgType &x, const Bu::Fmt &f ) const { return FormatProxy( *this ).arg( x, f ); } + + FormatProxy format() const + { + return FormatProxy( *this ); + } + + FormatProxy format( FormatProxyEndAction *pEndAction ) const + { + return FormatProxy( *this, pEndAction ); + } }; template String operator+( const T *pLeft, const String &rRight ) diff --git a/src/tests/print.cpp b/src/tests/print.cpp new file mode 100644 index 0000000..7d55554 --- /dev/null +++ b/src/tests/print.cpp @@ -0,0 +1,13 @@ +#include + +int main() +{ + Bu::print("hello there %1!\n").arg("Bob"); + + Bu::println("This is %1 crazy, like %2 times over!"). + arg("totally").arg( 47.2 ); + Bu::println("This is unsubstituted?"); + + return 0; +} + -- cgit v1.2.3