From 10b68e6b5e7d12c7af51b960191e1be9eb788d2e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 27 Oct 2010 03:27:05 +0000 Subject: Interesting tweak to the variant and optparser classes. In the Variant, it would always fail if a const char * was passed in, it now converts these silently to Bu::FStrings, good to know... Also, the OptParser now uses a Variant for overrides, meaning it doesn't have to do extra parsing, and the amount of code you have to write may be significantly reduced. Pretty sweet, overall. There is one downside. For the moment if you use a non-standard type or object as the target of a parameter it always needs to have a formatter >> operator defined, even if you override and the formatter >> operator is never called. Hopefully we can get around this in the future. Also, it looks like it should be relatively trivial to create conversion functions for the variant, they'll just be global template functions that take two parameters, source type and target type. Should be good times. --- src/optparser.cpp | 36 ++++++++++++++++++------------------ src/optparser.h | 28 +++++++++++++++++++++++----- src/tools/myriad.cpp | 26 +++++++------------------- src/unitsuite.cpp | 2 +- src/variant.cpp | 6 ++++++ src/variant.h | 1 + 6 files changed, 56 insertions(+), 43 deletions(-) diff --git a/src/optparser.cpp b/src/optparser.cpp index 864d8ce..b81691d 100644 --- a/src/optparser.cpp +++ b/src/optparser.cpp @@ -45,7 +45,11 @@ void Bu::OptParser::parse( int argc, char **argv ) { sOpt.set( argv[j]+2 ); } - try + if( !hlOption.has( sOpt ) ) + { + optionError( "--" + sOpt ); + } + else { // Long param, cool, that's easy, first search for = Option *pOpt = hlOption.get( sOpt ); @@ -71,26 +75,28 @@ void Bu::OptParser::parse( int argc, char **argv ) } else if( sExtraParam.isSet() ) { - pOpt->pProxy->setValue( sExtraParam ); + pOpt->pProxy->setValueFromStr( sExtraParam ); } else if( argv[j+1] != '\0' ) { - pOpt->pProxy->setValue( argv[j+1] ); + pOpt->pProxy->setValueFromStr( argv[j+1] ); j++; } } } - catch( Bu::HashException &e ) - { - optionError( "--" + sOpt ); - } } else { int iCPos; for( iCPos = 1; argv[j][iCPos] != '\0'; iCPos++ ) { - try + if( !hsOption.has( argv[j][iCPos] ) ) + { + Bu::FString sOpt("-"); + sOpt += argv[j][iCPos]; + optionError( sOpt ); + } + else { Option *pOpt = hsOption.get( argv[j][iCPos] ); char buf[2] = {argv[j][iCPos], '\0'}; @@ -123,14 +129,14 @@ void Bu::OptParser::parse( int argc, char **argv ) } else if( argv[j][iCPos+1] != '\0' ) { - pOpt->pProxy->setValue( + pOpt->pProxy->setValueFromStr( argv[j]+iCPos+1 ); break; } else if( argv[j+1] ) { - pOpt->pProxy->setValue( + pOpt->pProxy->setValueFromStr( argv[j+1] ); j++; @@ -138,12 +144,6 @@ void Bu::OptParser::parse( int argc, char **argv ) } } } - catch( Bu::HashException &e ) - { - Bu::FString sOpt("-"); - sOpt += argv[j][iCPos]; - optionError( sOpt ); - } } } } @@ -176,12 +176,12 @@ void Bu::OptParser::addOption( const Option &opt ) hlOption.insert( opt.sOpt, &lOption.last() ); } -void Bu::OptParser::setOverride( char cOpt, const Bu::FString &sOverride ) +void Bu::OptParser::setOverride( char cOpt, const Bu::Variant &sOverride ) { hsOption.get( cOpt )->sOverride = sOverride; } -void Bu::OptParser::setOverride( const Bu::FString &sOpt, const Bu::FString &sOverride ) +void Bu::OptParser::setOverride( const Bu::FString &sOpt, const Bu::Variant &sOverride ) { hlOption.get( sOpt )->sOverride = sOverride; } diff --git a/src/optparser.h b/src/optparser.h index 2936a4b..7ec69e5 100644 --- a/src/optparser.h +++ b/src/optparser.h @@ -15,6 +15,7 @@ #include "bu/array.h" #include "bu/membuf.h" #include "bu/formatter.h" +#include "bu/variant.h" namespace Bu { @@ -40,7 +41,8 @@ namespace Bu _ValueProxy(); virtual ~_ValueProxy(); - virtual void setValue( const Bu::FString & )=0; + virtual void setValueFromStr( const Bu::FString & )=0; + virtual void setValue( const Bu::Variant &vVar )=0; virtual _ValueProxy *clone()=0; }; @@ -57,12 +59,28 @@ namespace Bu { } - virtual void setValue( const Bu::FString &sVal ) + virtual void setValueFromStr( const Bu::FString &sVal ) { Bu::MemBuf mb( sVal ); Bu::Formatter f( mb ); f >> v; } + + virtual void setValue( const Bu::Variant &vVar ) + { + if( vVar.getType() == typeid(ptype) ) + { + v = vVar.get(); + } + else if( vVar.getType() == typeid(Bu::FString) ) + { + setValueFromStr( vVar.get() ); + } + else + { + setValueFromStr( vVar.toString() ); + } + } virtual _ValueProxy *clone() { @@ -87,7 +105,7 @@ namespace Bu Bu::FString sHelp; OptionSignal sUsed; _ValueProxy *pProxy; - Bu::FString sOverride; + Bu::Variant sOverride; Bu::FString sHelpDefault; }; @@ -162,9 +180,9 @@ namespace Bu addOption( sUsed, cOpt, "", sHelp ); } - void setOverride( char cOpt, const Bu::FString &sOverride ); + void setOverride( char cOpt, const Bu::Variant &sOverride ); void setOverride( const Bu::FString &sOpt, - const Bu::FString &sOverride ); + const Bu::Variant &sOverride ); void setHelpDefault( const Bu::FString &sOpt, const Bu::FString &sTxt ); diff --git a/src/tools/myriad.cpp b/src/tools/myriad.cpp index 73ceba1..c6a3a4d 100644 --- a/src/tools/myriad.cpp +++ b/src/tools/myriad.cpp @@ -56,11 +56,11 @@ public: addOption( iStream, 's', "stream", "Substream to work with."); addOption( sSrc, "src", "Source file for copying into a Myriad file."); - setOverride( "create", "create" ); - setOverride( "info", "info" ); - setOverride( "new", "new" ); - setOverride( "dump", "dump" ); - setOverride( "put", "put" ); + setOverride( "create", modeCreate ); + setOverride( "info", modeInfo ); + setOverride( "new", modeStreamNew ); + setOverride( "dump", modeStreamDump ); + setOverride( "put", modeStreamPut ); parse( argc, argv ); } @@ -73,21 +73,9 @@ public: Bu::FString sSrc; }; -Bu::Formatter &operator>>( Bu::Formatter &f, Mode &m ) +Bu::Formatter &operator>>( Bu::Formatter &f, Mode &e ) { - Bu::FString sTok = f.readToken(); - if( sTok == "create" ) - m = modeCreate; - else if( sTok == "info" ) - m = modeInfo; - else if( sTok == "new" ) - m = modeStreamNew; - else if( sTok == "dump" ) - m = modeStreamDump; - else if( sTok == "put" ) - m = modeStreamPut; - else - m = modeNone; + sio << "Uh oh, the formatter was called..." << sio.nl; return f; } diff --git a/src/unitsuite.cpp b/src/unitsuite.cpp index 7a20128..ce6d037 100644 --- a/src/unitsuite.cpp +++ b/src/unitsuite.cpp @@ -40,7 +40,7 @@ int Bu::UnitSuite::run( int argc, char *argv[] ) p.addOption( Bu::slot( this, &Bu::UnitSuite::onListCases ), 'l', "list", "List available test cases." ); p.addOption( bCleanup, "no-cleanup", "Don't erase temp files."); - p.setOverride( "no-cleanup", "false" ); + p.setOverride( "no-cleanup", false ); p.addHelpOption(); p.parse( argc, argv ); diff --git a/src/variant.cpp b/src/variant.cpp index a66ec39..a239e0f 100644 --- a/src/variant.cpp +++ b/src/variant.cpp @@ -34,6 +34,12 @@ Bu::Variant::Variant( const Variant &v ) : } } +Bu::Variant::Variant( const char *t ) : + pCore( NULL ) +{ + set( Bu::FString( t ) ); +} + Bu::Variant::~Variant() { if( pCore ) diff --git a/src/variant.h b/src/variant.h index 5482ee3..8e1203c 100644 --- a/src/variant.h +++ b/src/variant.h @@ -98,6 +98,7 @@ namespace Bu public: Variant(); Variant( const Variant &v ); + Variant( const char *t ); template Variant( const t &v ) : pCore( new VariantType() ) -- cgit v1.2.3