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