aboutsummaryrefslogtreecommitdiff
path: root/c++-libbu++/src/dictionary.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'c++-libbu++/src/dictionary.cpp')
-rw-r--r--c++-libbu++/src/dictionary.cpp380
1 files changed, 380 insertions, 0 deletions
diff --git a/c++-libbu++/src/dictionary.cpp b/c++-libbu++/src/dictionary.cpp
new file mode 100644
index 0000000..b59d652
--- /dev/null
+++ b/c++-libbu++/src/dictionary.cpp
@@ -0,0 +1,380 @@
1#include "gats/dictionary.h"
2
3#include "gats/boolean.h"
4#include "gats/integer.h"
5#include "gats/float.h"
6#include "gats/string.h"
7#include "gats/list.h"
8
9#include <bu/formatter.h>
10
11template<>
12uint32_t Bu::__calcHashCode<Gats::String>( const Gats::String &s )
13{
14 return __calcHashCode( dynamic_cast<const Bu::String &>(s) );
15}
16
17Gats::Dictionary::Dictionary()
18{
19}
20
21Gats::Dictionary::~Dictionary()
22{
23 for( iterator i = begin(); i; i++ )
24 {
25 delete *i;
26 }
27}
28
29Gats::Object *Gats::Dictionary::clone() const
30{
31 Gats::Dictionary *pClone = new Gats::Dictionary;
32 for( const_iterator i = begin(); i; i++ )
33 {
34 Bu::String s(i.getKey());
35 pClone->insert( s.clone(), (*i)->clone() );
36 }
37
38 return pClone;
39}
40
41void Gats::Dictionary::write( Bu::Stream &rOut ) const
42{
43 rOut.write("d", 1 );
44 for( const_iterator i= begin(); i; i++ )
45 {
46 i.getKey().write( rOut );
47 (*i)->write( rOut );
48 }
49 rOut.write("e", 1 );
50}
51
52void Gats::Dictionary::read( Bu::Stream &rIn, char cType )
53{
54 for(;;)
55 {
56 char cNext;
57 rIn.read( &cNext, 1 );
58 if( cNext == 'e' )
59 break;
60 if( cNext != 's' )
61 throw Bu::ExceptionBase("You can only use strings as keys.");
62 Gats::String sKey;
63 sKey.read( rIn, cNext );
64
65 ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert(
66 sKey, Gats::Object::read( rIn )
67 );
68 }
69}
70
71void Gats::Dictionary::insert( const Bu::String &sKey, char i )
72{
73 ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert(
74 sKey, new Gats::Integer( i )
75 );
76}
77
78void Gats::Dictionary::insert( const Bu::String &sKey, unsigned char i )
79{
80 ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert(
81 sKey, new Gats::Integer( i )
82 );
83}
84
85void Gats::Dictionary::insert( const Bu::String &sKey, signed char i )
86{
87 ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert(
88 sKey, new Gats::Integer( i )
89 );
90}
91
92void Gats::Dictionary::insert( const Bu::String &sKey, unsigned short i )
93{
94 ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert(
95 sKey, new Gats::Integer( i )
96 );
97}
98
99void Gats::Dictionary::insert( const Bu::String &sKey, signed short i )
100{
101 ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert(
102 sKey, new Gats::Integer( i )
103 );
104}
105
106void Gats::Dictionary::insert( const Bu::String &sKey, unsigned int i )
107{
108 ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert(
109 sKey, new Gats::Integer( i )
110 );
111}
112
113void Gats::Dictionary::insert( const Bu::String &sKey, signed int i )
114{
115 ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert(
116 sKey, new Gats::Integer( i )
117 );
118}
119
120void Gats::Dictionary::insert( const Bu::String &sKey, unsigned long i )
121{
122 ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert(
123 sKey, new Gats::Integer( i )
124 );
125}
126
127void Gats::Dictionary::insert( const Bu::String &sKey, signed long i )
128{
129 ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert(
130 sKey, new Gats::Integer( i )
131 );
132}
133
134void Gats::Dictionary::insert( const Bu::String &sKey, unsigned long long i )
135{
136 ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert(
137 sKey, new Gats::Integer( i )
138 );
139}
140
141void Gats::Dictionary::insert( const Bu::String &sKey, signed long long i )
142{
143 ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert(
144 sKey, new Gats::Integer( i )
145 );
146}
147/*
148void Gats::Dictionary::insert( const Bu::String &sKey, bool b )
149{
150 Bu::Hash<Gats::String, Gats::Object *>::insert(
151 sKey, new Gats::Boolean( b )
152 );
153}*/
154
155void Gats::Dictionary::insert( const Bu::String &sKey, float d )
156{
157 Bu::Hash<Gats::String, Gats::Object *>::insert(
158 sKey, new Gats::Float( d )
159 );
160}
161
162void Gats::Dictionary::insert( const Bu::String &sKey, double d )
163{
164 Bu::Hash<Gats::String, Gats::Object *>::insert(
165 sKey, new Gats::Float( d )
166 );
167}
168
169void Gats::Dictionary::insert( const Bu::String &sKey, const char *s )
170{
171 Bu::Hash<Gats::String, Gats::Object *>::insert(
172 sKey, new Gats::String( s )
173 );
174}
175
176void Gats::Dictionary::insert( const Bu::String &sKey, const Bu::String &s )
177{
178 Bu::Hash<Gats::String, Gats::Object *>::insert(
179 sKey, new Gats::String( s )
180 );
181}
182
183void Gats::Dictionary::insertBool( const Bu::String &sKey, bool b )
184{
185 Bu::Hash<Gats::String, Gats::Object *>::insert(
186 sKey, new Gats::Boolean( b )
187 );
188}
189
190void Gats::Dictionary::insertInt( const Bu::String &sKey, int64_t i )
191{
192 Bu::Hash<Gats::String, Gats::Object *>::insert(
193 sKey, new Gats::Integer( i )
194 );
195}
196
197void Gats::Dictionary::insertFloat( const Bu::String &sKey, double d )
198{
199 Bu::Hash<Gats::String, Gats::Object *>::insert(
200 sKey, new Gats::Float( d )
201 );
202}
203
204void Gats::Dictionary::insertStr( const Bu::String &sKey, const Bu::String &s )
205{
206 Bu::Hash<Gats::String, Gats::Object *>::insert(
207 sKey, new Gats::String( s )
208 );
209}
210
211void Gats::Dictionary::insertList( const Bu::String &sKey, Gats::List *pL )
212{
213 Bu::Hash<Gats::String, Gats::Object *>::insert(
214 sKey, pL
215 );
216}
217
218void Gats::Dictionary::insertDict( const Bu::String &sKey,
219 Gats::Dictionary *pD )
220{
221 Bu::Hash<Gats::String, Gats::Object *>::insert(
222 sKey, pD
223 );
224}
225
226Gats::List *Gats::Dictionary::insertList( const Bu::String &sKey )
227{
228 Gats::List *pLst = new Gats::List();
229 insertList( sKey, pLst );
230 return pLst;
231}
232
233Gats::Dictionary *Gats::Dictionary::insertDict( const Bu::String &sKey )
234{
235 Gats::Dictionary *pDict = new Gats::Dictionary();
236 insertDict( sKey, pDict );
237 return pDict;
238}
239
240bool Gats::Dictionary::getBool( const Bu::String &sKey )
241{
242 Gats::Boolean *pOb = dynamic_cast<Gats::Boolean *>( get( sKey ) );
243 if( !pOb )
244 throw Bu::ExceptionBase("Cannot use %s item '%s' as bool.",
245 typeToStr( get( sKey )->getType() ), sKey.getStr() );
246
247 return pOb->getValue();
248}
249
250int64_t Gats::Dictionary::getInt( const Bu::String &sKey )
251{
252 Gats::Integer *pOb = dynamic_cast<Gats::Integer *>( get( sKey ) );
253 if( !pOb )
254 throw Bu::ExceptionBase("Cannot use %s item '%s' as int.",
255 typeToStr( get( sKey )->getType() ), sKey.getStr() );
256
257 return pOb->getValue();
258}
259
260double Gats::Dictionary::getFloat( const Bu::String &sKey )
261{
262 Gats::Float *pOb = dynamic_cast<Gats::Float *>( get( sKey ) );
263 if( !pOb )
264 throw Bu::ExceptionBase("Cannot use %s item '%s' as float.",
265 typeToStr( get( sKey )->getType() ), sKey.getStr() );
266
267 return pOb->getValue();
268}
269
270Bu::String Gats::Dictionary::getStr( const Bu::String &sKey )
271{
272 Gats::String *pOb = dynamic_cast<Gats::String *>( get( sKey ) );
273 if( !pOb )
274 throw Bu::ExceptionBase("Cannot use %s item '%s' as string.",
275 typeToStr( get( sKey )->getType() ), sKey.getStr() );
276
277 return *pOb;
278}
279
280Gats::List *Gats::Dictionary::getList( const Bu::String &sKey )
281{
282 Gats::List *pOb = dynamic_cast<Gats::List *>( get( sKey ) );
283 if( !pOb )
284 throw Bu::ExceptionBase("Cannot use %s item '%s' as list.",
285 typeToStr( get( sKey )->getType() ), sKey.getStr() );
286
287 return pOb;
288}
289
290Gats::Dictionary *Gats::Dictionary::getDict( const Bu::String &sKey )
291{
292 Gats::Dictionary *pOb = dynamic_cast<Gats::Dictionary *>( get( sKey ) );
293 if( !pOb )
294 throw Bu::ExceptionBase("Cannot use %s item '%s' as dictionary.",
295 typeToStr( get( sKey )->getType() ), sKey.getStr() );
296
297 return pOb;
298}
299
300bool Gats::Dictionary::getBool( const Bu::String &sKey ) const
301{
302 Gats::Boolean *pOb = dynamic_cast<Gats::Boolean *>( get( sKey ) );
303 if( !pOb )
304 throw Bu::ExceptionBase("Cannot use %s item '%s' as bool.",
305 typeToStr( get( sKey )->getType() ), sKey.getStr() );
306
307 return pOb->getValue();
308}
309
310int64_t Gats::Dictionary::getInt( const Bu::String &sKey ) const
311{
312 Gats::Integer *pOb = dynamic_cast<Gats::Integer *>( get( sKey ) );
313 if( !pOb )
314 throw Bu::ExceptionBase("Cannot use %s item '%s' as int.",
315 typeToStr( get( sKey )->getType() ), sKey.getStr() );
316
317 return pOb->getValue();
318}
319
320double Gats::Dictionary::getFloat( const Bu::String &sKey ) const
321{
322 Gats::Float *pOb = dynamic_cast<Gats::Float *>( get( sKey ) );
323 if( !pOb )
324 throw Bu::ExceptionBase("Cannot use %s item '%s' as float.",
325 typeToStr( get( sKey )->getType() ), sKey.getStr() );
326
327 return pOb->getValue();
328}
329
330Bu::String Gats::Dictionary::getStr( const Bu::String &sKey ) const
331{
332 Gats::String *pOb = dynamic_cast<Gats::String *>( get( sKey ) );
333 if( !pOb )
334 throw Bu::ExceptionBase("Cannot use %s item '%s' as string.",
335 typeToStr( get( sKey )->getType() ), sKey.getStr() );
336
337 return *pOb;
338}
339
340Gats::List *Gats::Dictionary::getList( const Bu::String &sKey ) const
341{
342 Gats::List *pOb = dynamic_cast<Gats::List *>( get( sKey ) );
343 if( !pOb )
344 throw Bu::ExceptionBase("Cannot use %s item '%s' as list.",
345 typeToStr( get( sKey )->getType() ), sKey.getStr() );
346
347 return pOb;
348}
349
350Gats::Dictionary *Gats::Dictionary::getDict( const Bu::String &sKey ) const
351{
352 Gats::Dictionary *pOb = dynamic_cast<Gats::Dictionary *>( get( sKey ) );
353 if( !pOb )
354 throw Bu::ExceptionBase("Cannot use %s item '%s' as dictionary.",
355 typeToStr( get( sKey )->getType() ), sKey.getStr() );
356
357 return pOb;
358}
359
360Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Dictionary &d )
361{
362 f << "(dict) {";
363 f.incIndent();
364 int iMax = 0;
365 for( Gats::Dictionary::const_iterator i = d.begin(); i; i++ )
366 {
367 if( i.getKey().getSize() > iMax )
368 iMax = i.getKey().getSize();
369 }
370 iMax += 2;
371 for( Gats::Dictionary::const_iterator i = d.begin(); i; i++ )
372 {
373 f << f.nl << Bu::Fmt( iMax ) << i.getKey() + ": " << *i.getValue();
374 }
375 f.decIndent();
376 f << f.nl << "}";
377
378 return f;
379}
380