aboutsummaryrefslogtreecommitdiff
path: root/src/dictionary.cpp
blob: 385960fd28743fb5d4a3a504130056b236df0954 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "gats/dictionary.h"

#include "gats/boolean.h"
#include "gats/integer.h"
#include "gats/float.h"
#include "gats/string.h"
#include "gats/list.h"

template<>
uint32_t Bu::__calcHashCode<Gats::String>( const Gats::String &s )
{
	return __calcHashCode( dynamic_cast<const Bu::FString &>(s) );
}

Gats::Dictionary::Dictionary()
{
}

Gats::Dictionary::~Dictionary()
{
	for( iterator i = begin(); i; i++ )
	{
		delete *i;
	}
}

void Gats::Dictionary::write( Bu::Stream &rOut ) const
{
	rOut.write("d", 1 );
	for( const_iterator i= begin(); i; i++ )
	{
		i.getKey().write( rOut );
		(*i)->write( rOut );
	}
	rOut.write("e", 1 );
}

void Gats::Dictionary::read( Bu::Stream &rIn, char cType )
{
	for(;;)
	{
		char cNext;
		rIn.read( &cNext, 1 );
		if( cNext == 'e' )
			break;
		if( cNext != 's' )
			throw Bu::ExceptionBase("You can only use strings as keys.");
		Gats::String sKey;
		sKey.read( rIn, cNext );

		((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert(
			sKey, Gats::Object::read( rIn )
			);
	}
}

void Gats::Dictionary::insert( const Bu::FString &sKey, int64_t i )
{
	((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert(
		sKey, new Gats::Integer( i )
		);
}

void Gats::Dictionary::insert( const Bu::FString &sKey, bool b )
{
	Bu::Hash<Gats::String, Gats::Object *>::insert(
		sKey, new Gats::Boolean( b )
		);
}

void Gats::Dictionary::insert( const Bu::FString &sKey, double d )
{
//	Bu::Hash<Gats::String, Gats::Object *>::insert(
//		sKey, new Gats::Float( d )
//		);
}

void Gats::Dictionary::insert( const Bu::FString &sKey, const Bu::FString &s )
{
	Bu::Hash<Gats::String, Gats::Object *>::insert(
		sKey, new Gats::String( s )
		);
}

bool Gats::Dictionary::getBool( const Bu::FString &sKey )
{
	Gats::Boolean *pOb = dynamic_cast<Gats::Boolean *>( get( sKey ) );
	if( pOb )
		throw Bu::ExceptionBase("Cannot cast item '%s' to bool.",
			sKey.getStr() );

	return pOb->getValue();
}

int64_t Gats::Dictionary::getInt( const Bu::FString &sKey )
{
	Gats::Integer *pOb = dynamic_cast<Gats::Integer *>( get( sKey ) );
	if( pOb )
		throw Bu::ExceptionBase("Cannot cast item '%s' to int.",
			sKey.getStr() );

	return pOb->getValue();
}

double Gats::Dictionary::getFloat( const Bu::FString &sKey )
{/*
	Gats::Boolean *pOb = dynamic_cast<Gats::Boolean *>( get( sKey ) );
	if( pOb )
		throw Bu::ExceptionBase("Cannot cast item '%s' to bool.",
			sKey.getStr() );

	return pOb->getValue();*/
	return 0.0;
}

Bu::FString Gats::Dictionary::getStr( const Bu::FString &sKey )
{
	Gats::String *pOb = dynamic_cast<Gats::String *>( get( sKey ) );
	if( pOb )
		throw Bu::ExceptionBase("Cannot cast item '%s' to string.",
			sKey.getStr() );

	return *pOb;
}

Gats::List *Gats::Dictionary::getList( const Bu::FString &sKey )
{
	Gats::List *pOb = dynamic_cast<Gats::List *>( get( sKey ) );
	if( pOb )
		throw Bu::ExceptionBase("Cannot cast item '%s' to list.",
			sKey.getStr() );

	return pOb;
}

Gats::Dictionary *Gats::Dictionary::getDict( const Bu::FString &sKey )
{
	Gats::Dictionary *pOb = dynamic_cast<Gats::Dictionary *>( get( sKey ) );
	if( pOb )
		throw Bu::ExceptionBase("Cannot cast item '%s' to dictionary.",
			sKey.getStr() );

	return pOb;
}