1 
2 /* Battle Tanks Game
3  * Copyright (C) 2006-2009 Battle Tanks team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  */
19 
20 /*
21  * Additional rights can be granted beyond the GNU General Public License
22  * on the terms provided in the Exception. If you modify this file,
23  * you may extend this exception to your version of the file,
24  * but you are not obligated to do so. If you do not wish to provide this
25  * exception without modification, you must delete this exception statement
26  * from your version and license this file solely under the GPL without exception.
27 */
28 #include "var.h"
29 #include "mrt/exception.h"
30 #include "mrt/serializator.h"
31 #include <assert.h>
32 #include <stdlib.h>
33 
serialize(mrt::Serializator & s) const34 void Var::serialize(mrt::Serializator &s) const {
35 		if (type.empty())
36 			throw_ex(("cannot serialize empty variable"));
37 		int t = type[0];
38 		s.add(t);
39 		if (t == 'i')
40 			s.add(i);
41 		else if (t == 'b')
42 			s.add(b);
43 		else if (t == 's')
44 			s.add(this->s);
45 		else if (t == 'f')
46 			s.add(f);
47 	}
48 
deserialize(const mrt::Serializator & s)49 void Var::deserialize(const mrt::Serializator &s) {
50 		int t;
51 		s.get(t);
52 		switch(t) {
53 			case 'i':
54 				type = "int";
55 				s.get(i);
56 			break;
57 			case 'b':
58 				type = "bool";
59 				s.get(b);
60 			break;
61 			case 's':
62 				type = "string";
63 				s.get(this->s);
64 			break;
65 			case 'f':
66 				type = "float";
67 				s.get(f);
68 			break;
69 			default:
70 				throw_ex(("unknown type %02x recv'ed", t));
71 		}
72 	}
73 
74 
75 
check(const std::string & t) const76 void Var::check(const std::string &t) const {
77 		if (type != t)
78 			throw_ex(("invalid type requested(%s), real type: %s", t.c_str(), type.c_str()));
79 	}
80 
toString() const81 const std::string Var::toString() const {
82 		assert(!type.empty());
83 		if (type == "int")
84 			return mrt::format_string("%d", i);
85 		else if (type == "bool")
86 			return b?"true":"false";
87 		else if (type == "float")
88 			return mrt::format_string("%g", f);
89 		else if (type == "string")
90 			return mrt::format_string("%s", s.c_str());
91 		throw_ex(("cannot convert %s to string", type.c_str()));
92 		return "";//stub
93 	}
94 
fromString(const std::string & str)95 void Var::fromString(const std::string &str) {
96 		assert(!type.empty());
97 
98 		if (type == "int")
99 			i = atoi(str.c_str());
100 		else if (type == "bool") {
101 			if (str == "true") {
102 				b = true;
103 			} else if (str == "false") {
104 				b = false;
105 			} else throw_ex(("'%s' used as boolean value.", str.c_str()));
106 		} else if (type == "float")
107 			f = atof(str.c_str());
108 		else if (type == "string")
109 			s = str;
110 		else throw_ex(("cannot construct %s from string", type.c_str()));
111 	}
112