aboutsummaryrefslogtreecommitdiff
path: root/src/object.cpp
blob: af81017a8fc69e254903f109a85820ca92cc7546 (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
#include "gats/object.h"

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

#include <bu/formatter.h>
#include <bu/stream.h>

Gats::Object::Object()
{
}

Gats::Object::~Object()
{
}

Gats::Object *Gats::Object::read( Bu::Stream &rIn )
{
	char buf;
	rIn.read( &buf, 1 );
	Object *pObj = NULL;
	switch( buf )
	{
		case 'i':
			pObj = new Gats::Integer();
			break;

		case 's':
			pObj = new Gats::String();
			break;

		case '0':
		case '1':
			pObj = new Gats::Boolean();
			break;

		case 'l':
			pObj = new Gats::List();
			break;

		case 'd':
			pObj = new Gats::Dictionary();
			break;

		case 'f':  // Normal floats
		case 'F':  // Special float values
			pObj = new Gats::Float();
			break;

		case 'e':
			return NULL;

		default:
			throw Bu::ExceptionBase("Invalid Gats type discovered: %c.", buf );
	}

	pObj->read( rIn, buf );

	return pObj;
}

Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Object &obj )
{
	switch( obj.getType() )
	{
		case Gats::typeDictionary:
			return f << dynamic_cast<const Gats::Dictionary &>(obj);
		
		case Gats::typeList:
			return f << dynamic_cast<const Gats::List &>(obj);
		
		case Gats::typeString:
			return f << dynamic_cast<const Gats::String &>(obj);
		
		case Gats::typeInteger:
			return f << dynamic_cast<const Gats::Integer &>(obj);
		
		case Gats::typeFloat:
			return f << dynamic_cast<const Gats::Float &>(obj);
		
		case Gats::typeBoolean:
			return f << dynamic_cast<const Gats::Boolean &>(obj);

		default:
			return f << "***ERROR: Bad Gats type***";
	}
}

Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Type &t )
{
	switch( t )
	{
		case Gats::typeDictionary:	return f << "dictionary";
		case Gats::typeList:		return f << "list";
		case Gats::typeString:		return f << "string";
		case Gats::typeInteger:		return f << "integer";
		case Gats::typeFloat:		return f << "float";
		case Gats::typeBoolean:		return f << "boolean";
	}

	return f << "***unknown***";
}

const char *Gats::typeToStr( Gats::Type t )
{
	switch( t )
	{
		case Gats::typeDictionary:	return "dictionary";
		case Gats::typeList:		return "list";
		case Gats::typeString:		return "string";
		case Gats::typeInteger:		return "integer";
		case Gats::typeFloat:		return "float";
		case Gats::typeBoolean:		return "boolean";
	}

	return "***unknown***";
}