diff options
author | Mike Buland <eichlan@xagasoft.com> | 2010-08-18 16:17:15 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2010-08-18 16:17:15 +0000 |
commit | 9dc8cc535ef5fc4ea78f967fe285fe4424ff4458 (patch) | |
tree | a8a267bf5b8f8ca74bc767964fe47a97b33be01a /src | |
parent | cebb8eca644ff772b169178810372971176ebe9d (diff) | |
download | libgats-9dc8cc535ef5fc4ea78f967fe285fe4424ff4458.tar.gz libgats-9dc8cc535ef5fc4ea78f967fe285fe4424ff4458.tar.bz2 libgats-9dc8cc535ef5fc4ea78f967fe285fe4424ff4458.tar.xz libgats-9dc8cc535ef5fc4ea78f967fe285fe4424ff4458.zip |
getting it all tuned up.
Diffstat (limited to 'src')
-rw-r--r-- | src/dictionary.cpp | 24 | ||||
-rw-r--r-- | src/dictionary.h | 6 | ||||
-rw-r--r-- | src/float.h | 17 | ||||
-rw-r--r-- | src/object.cpp | 4 | ||||
-rw-r--r-- | src/unit/basic.unit | 29 |
5 files changed, 73 insertions, 7 deletions
diff --git a/src/dictionary.cpp b/src/dictionary.cpp index 385960f..2223c8b 100644 --- a/src/dictionary.cpp +++ b/src/dictionary.cpp | |||
@@ -54,6 +54,13 @@ void Gats::Dictionary::read( Bu::Stream &rIn, char cType ) | |||
54 | } | 54 | } |
55 | } | 55 | } |
56 | 56 | ||
57 | void Gats::Dictionary::insert( const Bu::FString &sKey, int32_t i ) | ||
58 | { | ||
59 | ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert( | ||
60 | sKey, new Gats::Integer( i ) | ||
61 | ); | ||
62 | } | ||
63 | |||
57 | void Gats::Dictionary::insert( const Bu::FString &sKey, int64_t i ) | 64 | void Gats::Dictionary::insert( const Bu::FString &sKey, int64_t i ) |
58 | { | 65 | { |
59 | ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert( | 66 | ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert( |
@@ -75,6 +82,13 @@ void Gats::Dictionary::insert( const Bu::FString &sKey, double d ) | |||
75 | // ); | 82 | // ); |
76 | } | 83 | } |
77 | 84 | ||
85 | void Gats::Dictionary::insert( const Bu::FString &sKey, const char *s ) | ||
86 | { | ||
87 | Bu::Hash<Gats::String, Gats::Object *>::insert( | ||
88 | sKey, new Gats::String( s ) | ||
89 | ); | ||
90 | } | ||
91 | |||
78 | void Gats::Dictionary::insert( const Bu::FString &sKey, const Bu::FString &s ) | 92 | void Gats::Dictionary::insert( const Bu::FString &sKey, const Bu::FString &s ) |
79 | { | 93 | { |
80 | Bu::Hash<Gats::String, Gats::Object *>::insert( | 94 | Bu::Hash<Gats::String, Gats::Object *>::insert( |
@@ -85,7 +99,7 @@ void Gats::Dictionary::insert( const Bu::FString &sKey, const Bu::FString &s ) | |||
85 | bool Gats::Dictionary::getBool( const Bu::FString &sKey ) | 99 | bool Gats::Dictionary::getBool( const Bu::FString &sKey ) |
86 | { | 100 | { |
87 | Gats::Boolean *pOb = dynamic_cast<Gats::Boolean *>( get( sKey ) ); | 101 | Gats::Boolean *pOb = dynamic_cast<Gats::Boolean *>( get( sKey ) ); |
88 | if( pOb ) | 102 | if( !pOb ) |
89 | throw Bu::ExceptionBase("Cannot cast item '%s' to bool.", | 103 | throw Bu::ExceptionBase("Cannot cast item '%s' to bool.", |
90 | sKey.getStr() ); | 104 | sKey.getStr() ); |
91 | 105 | ||
@@ -95,7 +109,7 @@ bool Gats::Dictionary::getBool( const Bu::FString &sKey ) | |||
95 | int64_t Gats::Dictionary::getInt( const Bu::FString &sKey ) | 109 | int64_t Gats::Dictionary::getInt( const Bu::FString &sKey ) |
96 | { | 110 | { |
97 | Gats::Integer *pOb = dynamic_cast<Gats::Integer *>( get( sKey ) ); | 111 | Gats::Integer *pOb = dynamic_cast<Gats::Integer *>( get( sKey ) ); |
98 | if( pOb ) | 112 | if( !pOb ) |
99 | throw Bu::ExceptionBase("Cannot cast item '%s' to int.", | 113 | throw Bu::ExceptionBase("Cannot cast item '%s' to int.", |
100 | sKey.getStr() ); | 114 | sKey.getStr() ); |
101 | 115 | ||
@@ -116,7 +130,7 @@ double Gats::Dictionary::getFloat( const Bu::FString &sKey ) | |||
116 | Bu::FString Gats::Dictionary::getStr( const Bu::FString &sKey ) | 130 | Bu::FString Gats::Dictionary::getStr( const Bu::FString &sKey ) |
117 | { | 131 | { |
118 | Gats::String *pOb = dynamic_cast<Gats::String *>( get( sKey ) ); | 132 | Gats::String *pOb = dynamic_cast<Gats::String *>( get( sKey ) ); |
119 | if( pOb ) | 133 | if( !pOb ) |
120 | throw Bu::ExceptionBase("Cannot cast item '%s' to string.", | 134 | throw Bu::ExceptionBase("Cannot cast item '%s' to string.", |
121 | sKey.getStr() ); | 135 | sKey.getStr() ); |
122 | 136 | ||
@@ -126,7 +140,7 @@ Bu::FString Gats::Dictionary::getStr( const Bu::FString &sKey ) | |||
126 | Gats::List *Gats::Dictionary::getList( const Bu::FString &sKey ) | 140 | Gats::List *Gats::Dictionary::getList( const Bu::FString &sKey ) |
127 | { | 141 | { |
128 | Gats::List *pOb = dynamic_cast<Gats::List *>( get( sKey ) ); | 142 | Gats::List *pOb = dynamic_cast<Gats::List *>( get( sKey ) ); |
129 | if( pOb ) | 143 | if( !pOb ) |
130 | throw Bu::ExceptionBase("Cannot cast item '%s' to list.", | 144 | throw Bu::ExceptionBase("Cannot cast item '%s' to list.", |
131 | sKey.getStr() ); | 145 | sKey.getStr() ); |
132 | 146 | ||
@@ -136,7 +150,7 @@ Gats::List *Gats::Dictionary::getList( const Bu::FString &sKey ) | |||
136 | Gats::Dictionary *Gats::Dictionary::getDict( const Bu::FString &sKey ) | 150 | Gats::Dictionary *Gats::Dictionary::getDict( const Bu::FString &sKey ) |
137 | { | 151 | { |
138 | Gats::Dictionary *pOb = dynamic_cast<Gats::Dictionary *>( get( sKey ) ); | 152 | Gats::Dictionary *pOb = dynamic_cast<Gats::Dictionary *>( get( sKey ) ); |
139 | if( pOb ) | 153 | if( !pOb ) |
140 | throw Bu::ExceptionBase("Cannot cast item '%s' to dictionary.", | 154 | throw Bu::ExceptionBase("Cannot cast item '%s' to dictionary.", |
141 | sKey.getStr() ); | 155 | sKey.getStr() ); |
142 | 156 | ||
diff --git a/src/dictionary.h b/src/dictionary.h index 3bcaec6..8f4b949 100644 --- a/src/dictionary.h +++ b/src/dictionary.h | |||
@@ -19,11 +19,13 @@ namespace Gats | |||
19 | virtual Type getType() const { return typeDictionary; } | 19 | virtual Type getType() const { return typeDictionary; } |
20 | virtual void write( Bu::Stream &rOut ) const; | 20 | virtual void write( Bu::Stream &rOut ) const; |
21 | virtual void read( Bu::Stream &rIn, char cType ); | 21 | virtual void read( Bu::Stream &rIn, char cType ); |
22 | 22 | ||
23 | void insert( const Bu::FString &sKey, const char *s ); | ||
24 | void insert( const Bu::FString &sKey, const Bu::FString &s ); | ||
25 | void insert( const Bu::FString &sKey, int32_t i ); | ||
23 | void insert( const Bu::FString &sKey, int64_t i ); | 26 | void insert( const Bu::FString &sKey, int64_t i ); |
24 | void insert( const Bu::FString &sKey, bool b ); | 27 | void insert( const Bu::FString &sKey, bool b ); |
25 | void insert( const Bu::FString &sKey, double d ); | 28 | void insert( const Bu::FString &sKey, double d ); |
26 | void insert( const Bu::FString &sKey, const Bu::FString &s ); | ||
27 | using Bu::Hash<Gats::String, Gats::Object *>::insert; | 29 | using Bu::Hash<Gats::String, Gats::Object *>::insert; |
28 | 30 | ||
29 | bool getBool( const Bu::FString &sKey ); | 31 | bool getBool( const Bu::FString &sKey ); |
diff --git a/src/float.h b/src/float.h index e69de29..cf20010 100644 --- a/src/float.h +++ b/src/float.h | |||
@@ -0,0 +1,17 @@ | |||
1 | #ifndef GATS_FLOAT_H | ||
2 | #define GATS_FLOAT_H | ||
3 | |||
4 | namespace Gats | ||
5 | { | ||
6 | class Float | ||
7 | { | ||
8 | public: | ||
9 | Float(); | ||
10 | Float( double f ); | ||
11 | virtual ~Float(); | ||
12 | |||
13 | private: | ||
14 | }; | ||
15 | } | ||
16 | |||
17 | #endif | ||
diff --git a/src/object.cpp b/src/object.cpp index 3d7765e..4c1f3ca 100644 --- a/src/object.cpp +++ b/src/object.cpp | |||
@@ -41,6 +41,10 @@ Gats::Object *Gats::Object::read( Bu::Stream &rIn ) | |||
41 | pObj = new Gats::List(); | 41 | pObj = new Gats::List(); |
42 | break; | 42 | break; |
43 | 43 | ||
44 | case 'd': | ||
45 | pObj = new Gats::Dictionary(); | ||
46 | break; | ||
47 | |||
44 | case 'e': | 48 | case 'e': |
45 | return NULL; | 49 | return NULL; |
46 | 50 | ||
diff --git a/src/unit/basic.unit b/src/unit/basic.unit index 9389c70..744d679 100644 --- a/src/unit/basic.unit +++ b/src/unit/basic.unit | |||
@@ -119,4 +119,33 @@ suite Basic | |||
119 | unitFailed("Bad string."); | 119 | unitFailed("Bad string."); |
120 | } | 120 | } |
121 | } | 121 | } |
122 | |||
123 | test dictionary | ||
124 | { | ||
125 | MemBuf mb; | ||
126 | |||
127 | { | ||
128 | Gats::Dictionary dict; | ||
129 | dict.insert("bool", true ); | ||
130 | dict.insert("int", 33403055 ); | ||
131 | dict.insert("str", "Hey there" ); | ||
132 | dict.write( mb ); | ||
133 | } | ||
134 | |||
135 | mb.setPos( 0 ); | ||
136 | |||
137 | { | ||
138 | Gats::Object *pRead = Gats::Object::read( mb ); | ||
139 | unitTest( pRead != NULL ); | ||
140 | Gats::Dictionary *pDict = dynamic_cast<Gats::Dictionary *>(pRead); | ||
141 | unitTest( pDict != NULL ); | ||
142 | |||
143 | unitTest( pDict->getBool("bool") == true ); | ||
144 | unitTest( pDict->getInt("int") == 33403055 ); | ||
145 | unitTest( pDict->getStr("str") == "Hey there" ); | ||
146 | unitTest( pDict->getSize() == 3 ); | ||
147 | |||
148 | delete pDict; | ||
149 | } | ||
150 | } | ||
122 | } | 151 | } |