From 30e841d5b6f082bce1f98c6df198aeea28a2d95b Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 17 Mar 2011 23:14:52 +0000 Subject: Tweaks to the variant class make it much cooler, and there's a test/tool called format, it's a proof of concept of a text formatter. I think it's gonna' rock. --- src/lexer.cpp | 1 + src/optparser.h | 5 ++- src/tools/format.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/variant.cpp | 31 ++++++++---------- src/variant.h | 20 +++++------- 5 files changed, 113 insertions(+), 32 deletions(-) create mode 100644 src/tools/format.cpp (limited to 'src') diff --git a/src/lexer.cpp b/src/lexer.cpp index 7dc2b23..a071695 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -6,6 +6,7 @@ */ #include "bu/lexer.h" +#include "bu/membuf.h" Bu::Lexer::Lexer() { diff --git a/src/optparser.h b/src/optparser.h index 4142e22..e653180 100644 --- a/src/optparser.h +++ b/src/optparser.h @@ -78,7 +78,10 @@ namespace Bu } else { - setValueFromStr( vVar.toString() ); + Bu::MemBuf mb; + Bu::Formatter f( mb ); +// f << vVar; + setValueFromStr( mb.getString() ); } } diff --git a/src/tools/format.cpp b/src/tools/format.cpp new file mode 100644 index 0000000..6a2f3bd --- /dev/null +++ b/src/tools/format.cpp @@ -0,0 +1,88 @@ +#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; +} + diff --git a/src/variant.cpp b/src/variant.cpp index fd0511f..f7c87ad 100644 --- a/src/variant.cpp +++ b/src/variant.cpp @@ -7,6 +7,8 @@ #include "bu/variant.h" +#include "bu/membuf.h" + namespace Bu { Formatter &operator<<( Formatter &f, const String &s ); @@ -49,16 +51,17 @@ Bu::Variant::~Variant() } } -bool Bu::Variant::isSet() const +Bu::String Bu::Variant::toString() const { - return pCore != NULL; + Bu::MemBuf mb; + Bu::Formatter f( mb ); + f << *this; + return mb.getString(); } -Bu::String Bu::Variant::toString() const +bool Bu::Variant::isSet() const { - if( !pCore ) - return "***NO DATA***"; - return pCore->toString(); + return pCore != NULL; } const std::type_info &Bu::Variant::getType() const @@ -87,18 +90,10 @@ Bu::Variant &Bu::Variant::operator=( const Bu::Variant &rhs ) Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::Variant &v ) { - return f << v.toString(); -} - -template<> Bu::String Bu::VariantType::toString() const -{ - Bu::String s; - s.format("%d", data ); - return s; -} + if( !v.pCore ) + return f << "(null)"; -template<> Bu::String Bu::VariantType::toString() const -{ - return data?"true":"false"; + v.pCore->format( f ); + return f; } diff --git a/src/variant.h b/src/variant.h index 2b88efe..a4f09af 100644 --- a/src/variant.h +++ b/src/variant.h @@ -10,7 +10,6 @@ #include #include -#include #include namespace Bu @@ -26,9 +25,9 @@ namespace Bu VariantTypeRoot(); virtual ~VariantTypeRoot(); - virtual Bu::String toString() const=0; virtual const std::type_info &getType() const=0; virtual VariantTypeRoot *clone() const=0; + virtual void format( Bu::Formatter &f ) const=0; }; template @@ -65,12 +64,9 @@ namespace Bu return data; } - virtual Bu::String toString() const + virtual void format( Formatter &f ) const { - MemBuf mb; - Formatter f( mb ); f << data; - return mb.getString(); } virtual const std::type_info &getType() const @@ -111,6 +107,7 @@ namespace Bu */ class Variant { + friend Bu::Formatter &operator<<( Bu::Formatter &f, const Variant &v ); public: Variant(); Variant( const Variant &v ); @@ -123,8 +120,8 @@ namespace Bu } virtual ~Variant(); - bool isSet() const; Bu::String toString() const; + bool isSet() const; const std::type_info &getType() const; Variant &operator=( const Variant &rhs ); @@ -219,17 +216,14 @@ namespace Bu private: VariantTypeRoot *pCore; }; - +/* template Bu::Formatter &operator<<( Bu::Formatter &f, const VariantType &vt ) { - return f << vt.toString(); - } + return f << vt.getData; + }*/ Bu::Formatter &operator<<( Bu::Formatter &f, const Variant &v ); - - template<> Bu::String VariantType::toString() const; - template<> Bu::String VariantType::toString() const; }; #endif -- cgit v1.2.3