diff options
author | Mike Buland <eichlan@xagasoft.com> | 2010-10-27 03:27:05 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2010-10-27 03:27:05 +0000 |
commit | 10b68e6b5e7d12c7af51b960191e1be9eb788d2e (patch) | |
tree | ccc6375726b9acaf53b1bb67bec81747b0bde223 /src/optparser.h | |
parent | 68b8b5136677435a84da7d277e78470ac4a41a64 (diff) | |
download | libbu++-10b68e6b5e7d12c7af51b960191e1be9eb788d2e.tar.gz libbu++-10b68e6b5e7d12c7af51b960191e1be9eb788d2e.tar.bz2 libbu++-10b68e6b5e7d12c7af51b960191e1be9eb788d2e.tar.xz libbu++-10b68e6b5e7d12c7af51b960191e1be9eb788d2e.zip |
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.
Diffstat (limited to 'src/optparser.h')
-rw-r--r-- | src/optparser.h | 28 |
1 files changed, 23 insertions, 5 deletions
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 @@ | |||
15 | #include "bu/array.h" | 15 | #include "bu/array.h" |
16 | #include "bu/membuf.h" | 16 | #include "bu/membuf.h" |
17 | #include "bu/formatter.h" | 17 | #include "bu/formatter.h" |
18 | #include "bu/variant.h" | ||
18 | 19 | ||
19 | namespace Bu | 20 | namespace Bu |
20 | { | 21 | { |
@@ -40,7 +41,8 @@ namespace Bu | |||
40 | _ValueProxy(); | 41 | _ValueProxy(); |
41 | virtual ~_ValueProxy(); | 42 | virtual ~_ValueProxy(); |
42 | 43 | ||
43 | virtual void setValue( const Bu::FString & )=0; | 44 | virtual void setValueFromStr( const Bu::FString & )=0; |
45 | virtual void setValue( const Bu::Variant &vVar )=0; | ||
44 | virtual _ValueProxy *clone()=0; | 46 | virtual _ValueProxy *clone()=0; |
45 | }; | 47 | }; |
46 | 48 | ||
@@ -57,12 +59,28 @@ namespace Bu | |||
57 | { | 59 | { |
58 | } | 60 | } |
59 | 61 | ||
60 | virtual void setValue( const Bu::FString &sVal ) | 62 | virtual void setValueFromStr( const Bu::FString &sVal ) |
61 | { | 63 | { |
62 | Bu::MemBuf mb( sVal ); | 64 | Bu::MemBuf mb( sVal ); |
63 | Bu::Formatter f( mb ); | 65 | Bu::Formatter f( mb ); |
64 | f >> v; | 66 | f >> v; |
65 | } | 67 | } |
68 | |||
69 | virtual void setValue( const Bu::Variant &vVar ) | ||
70 | { | ||
71 | if( vVar.getType() == typeid(ptype) ) | ||
72 | { | ||
73 | v = vVar.get<ptype>(); | ||
74 | } | ||
75 | else if( vVar.getType() == typeid(Bu::FString) ) | ||
76 | { | ||
77 | setValueFromStr( vVar.get<Bu::FString>() ); | ||
78 | } | ||
79 | else | ||
80 | { | ||
81 | setValueFromStr( vVar.toString() ); | ||
82 | } | ||
83 | } | ||
66 | 84 | ||
67 | virtual _ValueProxy *clone() | 85 | virtual _ValueProxy *clone() |
68 | { | 86 | { |
@@ -87,7 +105,7 @@ namespace Bu | |||
87 | Bu::FString sHelp; | 105 | Bu::FString sHelp; |
88 | OptionSignal sUsed; | 106 | OptionSignal sUsed; |
89 | _ValueProxy *pProxy; | 107 | _ValueProxy *pProxy; |
90 | Bu::FString sOverride; | 108 | Bu::Variant sOverride; |
91 | Bu::FString sHelpDefault; | 109 | Bu::FString sHelpDefault; |
92 | }; | 110 | }; |
93 | 111 | ||
@@ -162,9 +180,9 @@ namespace Bu | |||
162 | addOption( sUsed, cOpt, "", sHelp ); | 180 | addOption( sUsed, cOpt, "", sHelp ); |
163 | } | 181 | } |
164 | 182 | ||
165 | void setOverride( char cOpt, const Bu::FString &sOverride ); | 183 | void setOverride( char cOpt, const Bu::Variant &sOverride ); |
166 | void setOverride( const Bu::FString &sOpt, | 184 | void setOverride( const Bu::FString &sOpt, |
167 | const Bu::FString &sOverride ); | 185 | const Bu::Variant &sOverride ); |
168 | 186 | ||
169 | void setHelpDefault( const Bu::FString &sOpt, const Bu::FString &sTxt ); | 187 | void setHelpDefault( const Bu::FString &sOpt, const Bu::FString &sTxt ); |
170 | 188 | ||