1 /** \file gameengine/Expressions/IntValue.cpp
4 // IntValue.cpp: implementation of the CIntValue class.
6 * Copyright (c) 1996-2000 Erwin Coumans <coockie@acm.org>
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.
19 #include "ErrorValue.h"
20 #include "FloatValue.h"
21 #include "BoolValue.h"
22 #include "StringValue.h"
23 #include "VoidValue.h"
25 //////////////////////////////////////////////////////////////////////
26 // Construction/Destruction
27 //////////////////////////////////////////////////////////////////////
29 CIntValue::CIntValue()
32 effect: constructs a new CIntValue
37 m_textval = "Int illegal constructor";
44 CIntValue::CIntValue(cInt innie)
47 effect: constructs a new CIntValue containing cInt innie
56 CIntValue::CIntValue(cInt innie,const char *name,AllocationTYPE alloctype)
61 if (alloctype==CValue::STACKVALUE)
63 CValue::DisableRefCount();
71 CIntValue::~CIntValue()
74 effect: deletes the object
83 CValue* CIntValue::Calc(VALUE_OPERATOR op, CValue *val)
86 ret: a new object containing the result of applying operator op to this
90 //return val->CalcInt(op, this);
92 case VALUE_POS_OPERATOR:
93 return new CIntValue (m_int);
95 case VALUE_NEG_OPERATOR:
96 return new CIntValue (-m_int);
98 case VALUE_NOT_OPERATOR:
99 return new CErrorValue (op2str(op) + "only allowed on booleans");
101 case VALUE_AND_OPERATOR:
102 case VALUE_OR_OPERATOR:
103 return new CErrorValue(val->GetText() + op2str(op) + "only allowed on booleans");
106 return val->CalcFinal(VALUE_INT_TYPE, op, this);
113 CValue* CIntValue::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val)
115 pre: the type of val is dtype
116 ret: a new object containing the result of applying operator op to val and
123 case VALUE_EMPTY_TYPE:
127 case VALUE_MOD_OPERATOR:
128 ret = new CIntValue (((CIntValue *) val)->GetInt() % m_int);
130 case VALUE_ADD_OPERATOR:
131 ret = new CIntValue (((CIntValue *) val)->GetInt() + m_int);
133 case VALUE_SUB_OPERATOR:
134 ret = new CIntValue (((CIntValue *) val)->GetInt() - m_int);
136 case VALUE_MUL_OPERATOR:
137 ret = new CIntValue (((CIntValue *) val)->GetInt() * m_int);
139 case VALUE_DIV_OPERATOR:
142 if (val->GetNumber() == 0)
144 ret = new CErrorValue("Not a Number");
147 ret = new CErrorValue("Division by zero");
151 ret = new CIntValue (((CIntValue *) val)->GetInt() / m_int);
153 case VALUE_EQL_OPERATOR:
154 ret = new CBoolValue(((CIntValue *) val)->GetInt() == m_int);
156 case VALUE_NEQ_OPERATOR:
157 ret = new CBoolValue(((CIntValue *) val)->GetInt() != m_int);
159 case VALUE_GRE_OPERATOR:
160 ret = new CBoolValue(((CIntValue *) val)->GetInt() > m_int);
162 case VALUE_LES_OPERATOR:
163 ret = new CBoolValue(((CIntValue *) val)->GetInt() < m_int);
165 case VALUE_GEQ_OPERATOR:
166 ret = new CBoolValue(((CIntValue *) val)->GetInt() >= m_int);
168 case VALUE_LEQ_OPERATOR:
169 ret = new CBoolValue(((CIntValue *) val)->GetInt() <= m_int);
171 case VALUE_NEG_OPERATOR:
172 ret = new CIntValue (-m_int);
174 case VALUE_POS_OPERATOR:
175 ret = new CIntValue (m_int);
178 ret = new CErrorValue("illegal operator. please send a bug report.");
183 case VALUE_FLOAT_TYPE:
186 case VALUE_MOD_OPERATOR:
187 ret = new CFloatValue(fmod(((CFloatValue *) val)->GetFloat(), m_int));
189 case VALUE_ADD_OPERATOR:
190 ret = new CFloatValue (((CFloatValue *) val)->GetFloat() + m_int);
192 case VALUE_SUB_OPERATOR:
193 ret = new CFloatValue (((CFloatValue *) val)->GetFloat() - m_int);
195 case VALUE_MUL_OPERATOR:
196 ret = new CFloatValue (((CFloatValue *) val)->GetFloat() * m_int);
198 case VALUE_DIV_OPERATOR:
200 ret = new CErrorValue("Division by zero");
202 ret = new CFloatValue (((CFloatValue *) val)->GetFloat() / m_int);
204 case VALUE_EQL_OPERATOR:
205 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() == m_int);
207 case VALUE_NEQ_OPERATOR:
208 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() != m_int);
210 case VALUE_GRE_OPERATOR:
211 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() > m_int);
213 case VALUE_LES_OPERATOR:
214 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() < m_int);
216 case VALUE_GEQ_OPERATOR:
217 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() >= m_int);
219 case VALUE_LEQ_OPERATOR:
220 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() <= m_int);
223 ret = new CErrorValue("illegal operator. please send a bug report.");
228 case VALUE_STRING_TYPE:
231 case VALUE_ADD_OPERATOR:
232 ret = new CStringValue(val->GetText() + GetText(),"");
234 case VALUE_EQL_OPERATOR:
235 case VALUE_NEQ_OPERATOR:
236 case VALUE_GRE_OPERATOR:
237 case VALUE_LES_OPERATOR:
238 case VALUE_GEQ_OPERATOR:
239 case VALUE_LEQ_OPERATOR:
240 ret = new CErrorValue("[Cannot compare string with integer]" + op2str(op) + GetText());
243 ret = new CErrorValue("[operator not allowed on strings]" + op2str(op) + GetText());
248 case VALUE_BOOL_TYPE:
249 ret = new CErrorValue("[operator not valid on boolean and integer]" + op2str(op) + GetText());
252 case VALUE_EMPTY_TYPE:
256 case VALUE_ADD_OPERATOR:
257 ret = new CIntValue (m_int);
259 case VALUE_SUB_OPERATOR:
260 ret = new CIntValue (-m_int);
264 ret = new CErrorValue(op2str(op) + GetText());
270 case VALUE_ERROR_TYPE:
271 ret = new CErrorValue(val->GetText() + op2str(op) + GetText());
274 ret = new CErrorValue("illegal type. contact your dealer (if any)");
282 cInt CIntValue::GetInt()
285 ret: the cInt stored in the object
293 double CIntValue::GetNumber()
295 return (float) m_int;
300 const STR_String & CIntValue::GetText()
303 m_pstrRep=new STR_String();
304 m_pstrRep->Format("%lld",m_int);
311 CValue* CIntValue::GetReplica() {
312 CIntValue* replica = new CIntValue(*this);
313 replica->ProcessReplica();
314 replica->m_pstrRep = NULL;
321 void CIntValue::SetValue(CValue* newval)
323 m_int = (cInt)newval->GetNumber();
329 PyObject* CIntValue::ConvertValueToPython()
331 if((m_int > INT_MIN) && (m_int < INT_MAX))
332 return PyLong_FromSsize_t(m_int);
334 return PyLong_FromLongLong(m_int);
336 #endif // WITH_PYTHON