py api cleanup, replace use...
[blender.git] / source / gameengine / Expressions / IntValue.cpp
1 /** \file gameengine/Expressions/IntValue.cpp
2  *  \ingroup expressions
3  */
4 // IntValue.cpp: implementation of the CIntValue class.
5 /*
6 * Copyright (c) 1996-2000 Erwin Coumans <coockie@acm.org>
7 *
8 * Permission to use, copy, modify, distribute and sell this software
9 * and its documentation for any purpose is hereby granted without fee,
10 * provided that the above copyright notice appear in all copies and
11 * that both that copyright notice and this permission notice appear
12 * in supporting documentation.  Erwin Coumans makes no
13 * representations about the suitability of this software for any
14 * purpose.  It is provided "as is" without express or implied warranty.
15 *
16 */
17
18 #include "IntValue.h"
19 #include "ErrorValue.h"
20 #include "FloatValue.h"
21 #include "BoolValue.h"
22 #include "StringValue.h"
23 #include "VoidValue.h"
24
25 //////////////////////////////////////////////////////////////////////
26 // Construction/Destruction
27 //////////////////////////////////////////////////////////////////////
28
29 CIntValue::CIntValue()
30 /*
31 pre: false
32 effect: constructs a new CIntValue
33 */
34 {
35         
36 #ifdef _DEBUG_
37         m_textval = "Int illegal constructor";
38 #endif
39         m_pstrRep=NULL;
40 }
41
42
43
44 CIntValue::CIntValue(cInt innie)
45 /*
46 pre:
47 effect: constructs a new CIntValue containing cInt innie
48 */
49 {
50         m_int = innie;
51         m_pstrRep=NULL;
52 }
53
54
55
56 CIntValue::CIntValue(cInt innie,const char *name,AllocationTYPE alloctype)
57 {
58         m_int = innie;
59         SetName(name);
60         
61         if (alloctype==CValue::STACKVALUE)
62         {
63                 CValue::DisableRefCount();
64         }
65         m_pstrRep=NULL;
66         
67 }
68
69
70
71 CIntValue::~CIntValue()
72 /*
73 pre:
74 effect: deletes the object
75 */
76 {
77         if (m_pstrRep)
78                 delete m_pstrRep;
79 }
80
81
82
83 CValue* CIntValue::Calc(VALUE_OPERATOR op, CValue *val)
84 /*
85 pre:
86 ret: a new object containing the result of applying operator op to this
87 object and val
88 */
89 {
90         //return val->CalcInt(op, this);
91         switch (op) {
92         case VALUE_POS_OPERATOR:
93                 return new CIntValue (m_int);
94                 break;
95         case VALUE_NEG_OPERATOR:
96                 return new CIntValue (-m_int);
97                 break;
98         case VALUE_NOT_OPERATOR:
99                 return new CErrorValue (op2str(op) + "only allowed on booleans");
100                 break;
101         case VALUE_AND_OPERATOR:
102         case VALUE_OR_OPERATOR:
103                 return new CErrorValue(val->GetText() + op2str(op) + "only allowed on booleans");
104                 break;
105         default:
106                 return val->CalcFinal(VALUE_INT_TYPE, op, this);
107                 break;
108         }
109 }
110
111 /*
112  * pre: the type of val is dtype
113  * ret: a new object containing the result of applying operator op to val and
114  * this object
115  */
116 CValue* CIntValue::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val)
117 {
118         CValue *ret;
119         
120         switch (dtype) {
121                 case VALUE_EMPTY_TYPE:
122                 case VALUE_INT_TYPE:
123                 {
124                         switch (op) {
125                                 case VALUE_MOD_OPERATOR:
126                                         ret = new CIntValue (((CIntValue *) val)->GetInt() % m_int);
127                                         break;
128                                 case VALUE_ADD_OPERATOR:
129                                         ret = new CIntValue (((CIntValue *) val)->GetInt() + m_int);
130                                         break;
131                                 case VALUE_SUB_OPERATOR:
132                                         ret = new CIntValue (((CIntValue *) val)->GetInt() - m_int);
133                                         break;
134                                 case VALUE_MUL_OPERATOR:
135                                         ret = new CIntValue (((CIntValue *) val)->GetInt() * m_int);
136                                         break;
137                                 case VALUE_DIV_OPERATOR:
138                                         if (m_int == 0) {
139                                                 if (val->GetNumber() == 0) {
140                                                         ret = new CErrorValue("Not a Number");
141                                                 }
142                                                 else {
143                                                         ret = new CErrorValue("Division by zero");
144                                                 }
145                                         }
146                                         else
147                                                 ret = new CIntValue (((CIntValue *) val)->GetInt() / m_int);
148                                         break;
149                                 case VALUE_EQL_OPERATOR:
150                                         ret = new CBoolValue(((CIntValue *) val)->GetInt() == m_int);
151                                         break;
152                                 case VALUE_NEQ_OPERATOR:
153                                         ret = new CBoolValue(((CIntValue *) val)->GetInt() != m_int);
154                                         break;
155                                 case VALUE_GRE_OPERATOR:
156                                         ret = new CBoolValue(((CIntValue *) val)->GetInt() > m_int);
157                                         break;
158                                 case VALUE_LES_OPERATOR:
159                                         ret = new CBoolValue(((CIntValue *) val)->GetInt() < m_int);
160                                         break;
161                                 case VALUE_GEQ_OPERATOR:
162                                         ret = new CBoolValue(((CIntValue *) val)->GetInt() >= m_int);
163                                         break;
164                                 case VALUE_LEQ_OPERATOR:
165                                         ret = new CBoolValue(((CIntValue *) val)->GetInt() <= m_int);
166                                         break;
167                                 case VALUE_NEG_OPERATOR:
168                                         ret = new CIntValue (-m_int);
169                                         break;
170                                 case VALUE_POS_OPERATOR:
171                                         ret = new CIntValue (m_int);
172                                         break;
173                                 default:
174                                         ret = new CErrorValue("illegal operator. please send a bug report.");
175                                         break;
176                         }
177                         break;
178                 }
179                 case VALUE_FLOAT_TYPE:
180                 {
181                         switch (op) {
182                                 case VALUE_MOD_OPERATOR:
183                                         ret = new CFloatValue(fmod(((CFloatValue *) val)->GetFloat(), m_int));
184                                         break;
185                                 case VALUE_ADD_OPERATOR:
186                                         ret = new CFloatValue (((CFloatValue *) val)->GetFloat() + m_int);
187                                         break;
188                                 case VALUE_SUB_OPERATOR:
189                                         ret = new CFloatValue (((CFloatValue *) val)->GetFloat() - m_int);
190                                         break;
191                                 case VALUE_MUL_OPERATOR:
192                                         ret = new CFloatValue (((CFloatValue *) val)->GetFloat() * m_int);
193                                         break;
194                                 case VALUE_DIV_OPERATOR:
195                                         if (m_int == 0)
196                                                 ret = new CErrorValue("Division by zero");
197                                         else
198                                                 ret = new CFloatValue (((CFloatValue *) val)->GetFloat() / m_int);
199                                         break;
200                                 case VALUE_EQL_OPERATOR:
201                                         ret = new CBoolValue(((CFloatValue *) val)->GetFloat() == m_int);
202                                         break;
203                                 case VALUE_NEQ_OPERATOR:
204                                         ret = new CBoolValue(((CFloatValue *) val)->GetFloat() != m_int);
205                                         break;
206                                 case VALUE_GRE_OPERATOR:
207                                         ret = new CBoolValue(((CFloatValue *) val)->GetFloat() > m_int);
208                                         break;
209                                 case VALUE_LES_OPERATOR:
210                                         ret = new CBoolValue(((CFloatValue *) val)->GetFloat() < m_int);
211                                         break;
212                                 case VALUE_GEQ_OPERATOR:
213                                         ret = new CBoolValue(((CFloatValue *) val)->GetFloat() >= m_int);
214                                         break;
215                                 case VALUE_LEQ_OPERATOR:
216                                         ret = new CBoolValue(((CFloatValue *) val)->GetFloat() <= m_int);
217                                         break;
218                                 default:
219                                         ret = new CErrorValue("illegal operator. please send a bug report.");
220                                         break;
221                         }
222                         break;
223                 }
224                 case VALUE_STRING_TYPE:
225                 {
226                         switch (op) {
227                                 case VALUE_ADD_OPERATOR:
228                                         ret = new CStringValue(val->GetText() + GetText(),"");
229                                         break;
230                                 case VALUE_EQL_OPERATOR:
231                                 case VALUE_NEQ_OPERATOR:
232                                 case VALUE_GRE_OPERATOR:
233                                 case VALUE_LES_OPERATOR:
234                                 case VALUE_GEQ_OPERATOR:
235                                 case VALUE_LEQ_OPERATOR:
236                                         ret = new CErrorValue("[Cannot compare string with integer]" + op2str(op) + GetText());
237                                         break;
238                                 default:
239                                         ret =  new CErrorValue("[operator not allowed on strings]" + op2str(op) + GetText());
240                                         break;
241                         }
242                         break;
243                 }
244                 case VALUE_BOOL_TYPE:
245                         ret =  new CErrorValue("[operator not valid on boolean and integer]" + op2str(op) + GetText());
246                         break;
247 #if 0
248                 case VALUE_EMPTY_TYPE:
249                 {
250                         switch (op) {
251                                 case VALUE_ADD_OPERATOR:
252                                         ret = new CIntValue (m_int);
253                                         break;
254                                 case VALUE_SUB_OPERATOR:
255                                         ret = new CIntValue (-m_int);
256                                         break;
257                                 default:
258                                 {
259                                         ret = new CErrorValue(op2str(op) +      GetText());
260                                 }
261                         }
262                         break;
263                 }
264 #endif
265         case VALUE_ERROR_TYPE:
266                 ret = new CErrorValue(val->GetText() + op2str(op) +     GetText());
267                 break;
268         default:
269                 ret = new CErrorValue("illegal type. contact your dealer (if any)");
270                 break;
271         }
272         return ret;
273 }
274
275
276 /**
277  * pre:
278  * ret: the cInt stored in the object
279  */
280 cInt CIntValue::GetInt()
281 {
282         return m_int;
283 }
284
285
286
287 double CIntValue::GetNumber()
288 {
289         return (double) m_int;
290 }
291
292
293
294 const STR_String & CIntValue::GetText()
295 {
296         if (!m_pstrRep)
297                 m_pstrRep=new STR_String();
298         m_pstrRep->Format("%lld",m_int);
299         
300         return *m_pstrRep;
301 }
302
303
304
305 CValue* CIntValue::GetReplica()
306 {
307         CIntValue* replica = new CIntValue(*this);
308         replica->ProcessReplica();
309         replica->m_pstrRep = NULL;
310         
311         return replica;
312 }
313
314
315
316 void CIntValue::SetValue(CValue* newval)
317 {       
318         m_int = (cInt)newval->GetNumber(); 
319         SetModified(true);
320 }
321
322
323 #ifdef WITH_PYTHON
324 PyObject *CIntValue::ConvertValueToPython()
325 {
326         return PyLong_FromLongLong(m_int);
327 }
328 #endif // WITH_PYTHON