aboutsummaryrefslogtreecommitdiff
path: root/src/dictionary.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2010-08-14 07:12:29 +0000
committerMike Buland <eichlan@xagasoft.com>2010-08-14 07:12:29 +0000
commit1b797548dff7e2475826ba29a71c3f496008988f (patch)
tree2a81ee2e8fa2f17fd95410aabbf44533d35a727a /src/dictionary.cpp
downloadlibgats-1b797548dff7e2475826ba29a71c3f496008988f.tar.gz
libgats-1b797548dff7e2475826ba29a71c3f496008988f.tar.bz2
libgats-1b797548dff7e2475826ba29a71c3f496008988f.tar.xz
libgats-1b797548dff7e2475826ba29a71c3f496008988f.zip
libgats gets it's own repo. The rest of the history is in my misc repo.
Diffstat (limited to 'src/dictionary.cpp')
-rw-r--r--src/dictionary.cpp145
1 files changed, 145 insertions, 0 deletions
diff --git a/src/dictionary.cpp b/src/dictionary.cpp
new file mode 100644
index 0000000..385960f
--- /dev/null
+++ b/src/dictionary.cpp
@@ -0,0 +1,145 @@
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
9template<>
10uint32_t Bu::__calcHashCode<Gats::String>( const Gats::String &s )
11{
12 return __calcHashCode( dynamic_cast<const Bu::FString &>(s) );
13}
14
15Gats::Dictionary::Dictionary()
16{
17}
18
19Gats::Dictionary::~Dictionary()
20{
21 for( iterator i = begin(); i; i++ )
22 {
23 delete *i;
24 }
25}
26
27void Gats::Dictionary::write( Bu::Stream &rOut ) const
28{
29 rOut.write("d", 1 );
30 for( const_iterator i= begin(); i; i++ )
31 {
32 i.getKey().write( rOut );
33 (*i)->write( rOut );
34 }
35 rOut.write("e", 1 );
36}
37
38void Gats::Dictionary::read( Bu::Stream &rIn, char cType )
39{
40 for(;;)
41 {
42 char cNext;
43 rIn.read( &cNext, 1 );
44 if( cNext == 'e' )
45 break;
46 if( cNext != 's' )
47 throw Bu::ExceptionBase("You can only use strings as keys.");
48 Gats::String sKey;
49 sKey.read( rIn, cNext );
50
51 ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert(
52 sKey, Gats::Object::read( rIn )
53 );
54 }
55}
56
57void Gats::Dictionary::insert( const Bu::FString &sKey, int64_t i )
58{
59 ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert(
60 sKey, new Gats::Integer( i )
61 );
62}
63
64void Gats::Dictionary::insert( const Bu::FString &sKey, bool b )
65{
66 Bu::Hash<Gats::String, Gats::Object *>::insert(
67 sKey, new Gats::Boolean( b )
68 );
69}
70
71void Gats::Dictionary::insert( const Bu::FString &sKey, double d )
72{
73// Bu::Hash<Gats::String, Gats::Object *>::insert(
74// sKey, new Gats::Float( d )
75// );
76}
77
78void Gats::Dictionary::insert( const Bu::FString &sKey, const Bu::FString &s )
79{
80 Bu::Hash<Gats::String, Gats::Object *>::insert(
81 sKey, new Gats::String( s )
82 );
83}
84
85bool Gats::Dictionary::getBool( const Bu::FString &sKey )
86{
87 Gats::Boolean *pOb = dynamic_cast<Gats::Boolean *>( get( sKey ) );
88 if( pOb )
89 throw Bu::ExceptionBase("Cannot cast item '%s' to bool.",
90 sKey.getStr() );
91
92 return pOb->getValue();
93}
94
95int64_t Gats::Dictionary::getInt( const Bu::FString &sKey )
96{
97 Gats::Integer *pOb = dynamic_cast<Gats::Integer *>( get( sKey ) );
98 if( pOb )
99 throw Bu::ExceptionBase("Cannot cast item '%s' to int.",
100 sKey.getStr() );
101
102 return pOb->getValue();
103}
104
105double Gats::Dictionary::getFloat( const Bu::FString &sKey )
106{/*
107 Gats::Boolean *pOb = dynamic_cast<Gats::Boolean *>( get( sKey ) );
108 if( pOb )
109 throw Bu::ExceptionBase("Cannot cast item '%s' to bool.",
110 sKey.getStr() );
111
112 return pOb->getValue();*/
113 return 0.0;
114}
115
116Bu::FString Gats::Dictionary::getStr( const Bu::FString &sKey )
117{
118 Gats::String *pOb = dynamic_cast<Gats::String *>( get( sKey ) );
119 if( pOb )
120 throw Bu::ExceptionBase("Cannot cast item '%s' to string.",
121 sKey.getStr() );
122
123 return *pOb;
124}
125
126Gats::List *Gats::Dictionary::getList( const Bu::FString &sKey )
127{
128 Gats::List *pOb = dynamic_cast<Gats::List *>( get( sKey ) );
129 if( pOb )
130 throw Bu::ExceptionBase("Cannot cast item '%s' to list.",
131 sKey.getStr() );
132
133 return pOb;
134}
135
136Gats::Dictionary *Gats::Dictionary::getDict( const Bu::FString &sKey )
137{
138 Gats::Dictionary *pOb = dynamic_cast<Gats::Dictionary *>( get( sKey ) );
139 if( pOb )
140 throw Bu::ExceptionBase("Cannot cast item '%s' to dictionary.",
141 sKey.getStr() );
142
143 return pOb;
144}
145