summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-03-17 23:14:52 +0000
committerMike Buland <eichlan@xagasoft.com>2011-03-17 23:14:52 +0000
commit30e841d5b6f082bce1f98c6df198aeea28a2d95b (patch)
tree68727dcfae385733e4acebcb52bdde6486ebee88
parent4f96052772c46ceba8ca074e4206646661462533 (diff)
downloadlibbu++-30e841d5b6f082bce1f98c6df198aeea28a2d95b.tar.gz
libbu++-30e841d5b6f082bce1f98c6df198aeea28a2d95b.tar.bz2
libbu++-30e841d5b6f082bce1f98c6df198aeea28a2d95b.tar.xz
libbu++-30e841d5b6f082bce1f98c6df198aeea28a2d95b.zip
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.
-rw-r--r--src/lexer.cpp1
-rw-r--r--src/optparser.h5
-rw-r--r--src/tools/format.cpp88
-rw-r--r--src/variant.cpp31
-rw-r--r--src/variant.h20
5 files changed, 113 insertions, 32 deletions
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 @@
6 */ 6 */
7 7
8#include "bu/lexer.h" 8#include "bu/lexer.h"
9#include "bu/membuf.h"
9 10
10Bu::Lexer::Lexer() 11Bu::Lexer::Lexer()
11{ 12{
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
78 } 78 }
79 else 79 else
80 { 80 {
81 setValueFromStr( vVar.toString() ); 81 Bu::MemBuf mb;
82 Bu::Formatter f( mb );
83// f << vVar;
84 setValueFromStr( mb.getString() );
82 } 85 }
83 } 86 }
84 87
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 @@
1#include <bu/string.h>
2#include <bu/formatter.h>
3#include <bu/sio.h>
4#include <bu/variant.h>
5#include <bu/membuf.h>
6
7using namespace Bu;
8
9class Fmter
10{
11public:
12 Fmter( const Bu::String &sSrc ) :
13 sSrc( sSrc )
14 {
15 }
16
17 template<typename T>
18 Fmter &arg( const T &x )
19 {
20 lParm.append( Pair( x ) );
21
22 return *this;
23 }
24
25 template<typename T>
26 Fmter &arg( const T &x, Bu::Formatter::Fmt f )
27 {
28 lParm.append( Pair( x, f ) );
29
30 return *this;
31 }
32
33 operator Bu::String() const
34 {
35 Bu::MemBuf mbOut;
36 Bu::Formatter f( mbOut );
37 ParmList::const_iterator i = lParm.begin();
38 for( Bu::String::const_iterator s = sSrc.begin(); s; s++ )
39 {
40 if( *s == '%' )
41 {
42 f << (*i).format << (*i).value;
43 i++;
44 }
45 else
46 {
47 f << *s;
48 }
49 }
50
51 return mbOut.getString();
52 }
53
54private:
55 const Bu::String &sSrc;
56 class Pair
57 {
58 public:
59 template<typename T>
60 Pair( const T &v ) :
61 value( v )
62 {
63 }
64
65 template<typename T>
66 Pair( const T &v, Bu::Formatter::Fmt f ) :
67 value( v ),
68 format( f )
69 {
70 }
71
72 Bu::Variant value;
73 Bu::Formatter::Fmt format;
74 };
75 typedef Bu::List<Pair> ParmList;
76 ParmList lParm;
77};
78
79Bu::Formatter &operator<<( Bu::Formatter &f, const Fmter &r )
80{
81 return f << (Bu::String)r;
82}
83
84int main()
85{
86 sio << Fmter("A word is % and a number is % %.").arg("Hello").arg(75, Fmt::hex() ).arg(" - ") << sio.nl;
87}
88
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 @@
7 7
8#include "bu/variant.h" 8#include "bu/variant.h"
9 9
10#include "bu/membuf.h"
11
10namespace Bu 12namespace Bu
11{ 13{
12 Formatter &operator<<( Formatter &f, const String &s ); 14 Formatter &operator<<( Formatter &f, const String &s );
@@ -49,16 +51,17 @@ Bu::Variant::~Variant()
49 } 51 }
50} 52}
51 53
52bool Bu::Variant::isSet() const 54Bu::String Bu::Variant::toString() const
53{ 55{
54 return pCore != NULL; 56 Bu::MemBuf mb;
57 Bu::Formatter f( mb );
58 f << *this;
59 return mb.getString();
55} 60}
56 61
57Bu::String Bu::Variant::toString() const 62bool Bu::Variant::isSet() const
58{ 63{
59 if( !pCore ) 64 return pCore != NULL;
60 return "***NO DATA***";
61 return pCore->toString();
62} 65}
63 66
64const std::type_info &Bu::Variant::getType() const 67const std::type_info &Bu::Variant::getType() const
@@ -87,18 +90,10 @@ Bu::Variant &Bu::Variant::operator=( const Bu::Variant &rhs )
87 90
88Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::Variant &v ) 91Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::Variant &v )
89{ 92{
90 return f << v.toString(); 93 if( !v.pCore )
91} 94 return f << "(null)";
92
93template<> Bu::String Bu::VariantType<int>::toString() const
94{
95 Bu::String s;
96 s.format("%d", data );
97 return s;
98}
99 95
100template<> Bu::String Bu::VariantType<bool>::toString() const 96 v.pCore->format( f );
101{ 97 return f;
102 return data?"true":"false";
103} 98}
104 99
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 @@
10 10
11#include <bu/string.h> 11#include <bu/string.h>
12#include <typeinfo> 12#include <typeinfo>
13#include <bu/membuf.h>
14#include <bu/formatter.h> 13#include <bu/formatter.h>
15 14
16namespace Bu 15namespace Bu
@@ -26,9 +25,9 @@ namespace Bu
26 VariantTypeRoot(); 25 VariantTypeRoot();
27 virtual ~VariantTypeRoot(); 26 virtual ~VariantTypeRoot();
28 27
29 virtual Bu::String toString() const=0;
30 virtual const std::type_info &getType() const=0; 28 virtual const std::type_info &getType() const=0;
31 virtual VariantTypeRoot *clone() const=0; 29 virtual VariantTypeRoot *clone() const=0;
30 virtual void format( Bu::Formatter &f ) const=0;
32 }; 31 };
33 32
34 template<class t> 33 template<class t>
@@ -65,12 +64,9 @@ namespace Bu
65 return data; 64 return data;
66 } 65 }
67 66
68 virtual Bu::String toString() const 67 virtual void format( Formatter &f ) const
69 { 68 {
70 MemBuf mb;
71 Formatter f( mb );
72 f << data; 69 f << data;
73 return mb.getString();
74 } 70 }
75 71
76 virtual const std::type_info &getType() const 72 virtual const std::type_info &getType() const
@@ -111,6 +107,7 @@ namespace Bu
111 */ 107 */
112 class Variant 108 class Variant
113 { 109 {
110 friend Bu::Formatter &operator<<( Bu::Formatter &f, const Variant &v );
114 public: 111 public:
115 Variant(); 112 Variant();
116 Variant( const Variant &v ); 113 Variant( const Variant &v );
@@ -123,8 +120,8 @@ namespace Bu
123 } 120 }
124 virtual ~Variant(); 121 virtual ~Variant();
125 122
126 bool isSet() const;
127 Bu::String toString() const; 123 Bu::String toString() const;
124 bool isSet() const;
128 const std::type_info &getType() const; 125 const std::type_info &getType() const;
129 126
130 Variant &operator=( const Variant &rhs ); 127 Variant &operator=( const Variant &rhs );
@@ -219,17 +216,14 @@ namespace Bu
219 private: 216 private:
220 VariantTypeRoot *pCore; 217 VariantTypeRoot *pCore;
221 }; 218 };
222 219/*
223 template<class t> 220 template<class t>
224 Bu::Formatter &operator<<( Bu::Formatter &f, const VariantType<t> &vt ) 221 Bu::Formatter &operator<<( Bu::Formatter &f, const VariantType<t> &vt )
225 { 222 {
226 return f << vt.toString(); 223 return f << vt.getData;
227 } 224 }*/
228 225
229 Bu::Formatter &operator<<( Bu::Formatter &f, const Variant &v ); 226 Bu::Formatter &operator<<( Bu::Formatter &f, const Variant &v );
230
231 template<> Bu::String VariantType<int>::toString() const;
232 template<> Bu::String VariantType<bool>::toString() const;
233}; 227};
234 228
235#endif 229#endif