Blender  V2.59
IntValue.cpp
Go to the documentation of this file.
00001 
00004 // IntValue.cpp: implementation of the CIntValue class.
00005 /*
00006 * Copyright (c) 1996-2000 Erwin Coumans <coockie@acm.org>
00007 *
00008 * Permission to use, copy, modify, distribute and sell this software
00009 * and its documentation for any purpose is hereby granted without fee,
00010 * provided that the above copyright notice appear in all copies and
00011 * that both that copyright notice and this permission notice appear
00012 * in supporting documentation.  Erwin Coumans makes no
00013 * representations about the suitability of this software for any
00014 * purpose.  It is provided "as is" without express or implied warranty.
00015 *
00016 */
00017 
00018 #include "IntValue.h"
00019 #include "ErrorValue.h"
00020 #include "FloatValue.h"
00021 #include "BoolValue.h"
00022 #include "StringValue.h"
00023 #include "VoidValue.h"
00024 
00026 // Construction/Destruction
00028 
00029 CIntValue::CIntValue()
00030 /*
00031 pre: false
00032 effect: constructs a new CIntValue
00033 */
00034 {
00035         
00036 #ifdef _DEBUG_
00037         m_textval = "Int illegal constructor";
00038 #endif
00039         m_pstrRep=NULL;
00040 }
00041 
00042 
00043 
00044 CIntValue::CIntValue(cInt innie)
00045 /*
00046 pre:
00047 effect: constructs a new CIntValue containing cInt innie
00048 */
00049 {
00050         m_int = innie;
00051         m_pstrRep=NULL;
00052 }
00053 
00054 
00055 
00056 CIntValue::CIntValue(cInt innie,const char *name,AllocationTYPE alloctype)
00057 {
00058         m_int = innie;
00059         SetName(name);
00060         
00061         if (alloctype==CValue::STACKVALUE)
00062         {
00063                 CValue::DisableRefCount();
00064         }
00065         m_pstrRep=NULL;
00066         
00067 }
00068 
00069 
00070 
00071 CIntValue::~CIntValue()
00072 /*
00073 pre:
00074 effect: deletes the object
00075 */
00076 {
00077         if (m_pstrRep)
00078                 delete m_pstrRep;
00079 }
00080 
00081 
00082 
00083 CValue* CIntValue::Calc(VALUE_OPERATOR op, CValue *val)
00084 /*
00085 pre:
00086 ret: a new object containing the result of applying operator op to this
00087 object and val
00088 */
00089 {
00090         //return val->CalcInt(op, this);
00091         switch (op) {
00092         case VALUE_POS_OPERATOR:
00093                 return new CIntValue (m_int);
00094                 break;
00095         case VALUE_NEG_OPERATOR:
00096                 return new CIntValue (-m_int);
00097                 break;
00098         case VALUE_NOT_OPERATOR:
00099                 return new CErrorValue (op2str(op) + "only allowed on booleans");
00100                 break;
00101         case VALUE_AND_OPERATOR:
00102         case VALUE_OR_OPERATOR:
00103                 return new CErrorValue(val->GetText() + op2str(op) + "only allowed on booleans");
00104                 break;
00105         default:
00106                 return val->CalcFinal(VALUE_INT_TYPE, op, this);
00107                 break;
00108         }
00109 }
00110 
00111 
00112 
00113 CValue* CIntValue::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val)
00114 /*
00115 pre: the type of val is dtype
00116 ret: a new object containing the result of applying operator op to val and
00117 this object
00118 */
00119 {
00120         CValue *ret;
00121         
00122         switch(dtype) {
00123         case VALUE_EMPTY_TYPE:
00124         case VALUE_INT_TYPE:
00125                 {
00126                         switch (op) {
00127                         case VALUE_MOD_OPERATOR:
00128                                 ret = new CIntValue (((CIntValue *) val)->GetInt() % m_int);
00129                                 break;
00130                         case VALUE_ADD_OPERATOR:
00131                                 ret = new CIntValue (((CIntValue *) val)->GetInt() + m_int);
00132                                 break;
00133                         case VALUE_SUB_OPERATOR:
00134                                 ret = new CIntValue (((CIntValue *) val)->GetInt() - m_int);
00135                                 break;
00136                         case VALUE_MUL_OPERATOR:
00137                                 ret = new CIntValue (((CIntValue *) val)->GetInt() * m_int);
00138                                 break;
00139                         case VALUE_DIV_OPERATOR:
00140                                 if (m_int == 0)
00141                                 {
00142                                         if (val->GetNumber() == 0)
00143                                         {
00144                                                 ret = new CErrorValue("Not a Number");
00145                                         } else
00146                                         {
00147                                                 ret = new CErrorValue("Division by zero");
00148                                         }
00149                                 }
00150                                 else
00151                                         ret = new CIntValue (((CIntValue *) val)->GetInt() / m_int);
00152                                 break;
00153                         case VALUE_EQL_OPERATOR:
00154                                 ret = new CBoolValue(((CIntValue *) val)->GetInt() == m_int);
00155                                 break;
00156                         case VALUE_NEQ_OPERATOR:
00157                                 ret = new CBoolValue(((CIntValue *) val)->GetInt() != m_int);
00158                                 break;
00159                         case VALUE_GRE_OPERATOR:
00160                                 ret = new CBoolValue(((CIntValue *) val)->GetInt() > m_int);
00161                                 break;
00162                         case VALUE_LES_OPERATOR:
00163                                 ret = new CBoolValue(((CIntValue *) val)->GetInt() < m_int);
00164                                 break;
00165                         case VALUE_GEQ_OPERATOR:
00166                                 ret = new CBoolValue(((CIntValue *) val)->GetInt() >= m_int);
00167                                 break;
00168                         case VALUE_LEQ_OPERATOR:
00169                                 ret = new CBoolValue(((CIntValue *) val)->GetInt() <= m_int);
00170                                 break;
00171                         case VALUE_NEG_OPERATOR:
00172                                 ret = new CIntValue (-m_int);
00173                                 break;
00174                         case VALUE_POS_OPERATOR:
00175                                 ret = new CIntValue (m_int);
00176                                 break;
00177                         default:
00178                                 ret = new CErrorValue("illegal operator. please send a bug report.");
00179                                 break;
00180                         }
00181                         break;
00182                 }
00183         case VALUE_FLOAT_TYPE:
00184                 {
00185                         switch (op) {
00186                         case VALUE_MOD_OPERATOR:
00187                                 ret = new CFloatValue(fmod(((CFloatValue *) val)->GetFloat(), m_int));
00188                                 break;
00189                         case VALUE_ADD_OPERATOR:
00190                                 ret = new CFloatValue (((CFloatValue *) val)->GetFloat() + m_int);
00191                                 break;
00192                         case VALUE_SUB_OPERATOR:
00193                                 ret = new CFloatValue (((CFloatValue *) val)->GetFloat() - m_int);
00194                                 break;
00195                         case VALUE_MUL_OPERATOR:
00196                                 ret = new CFloatValue (((CFloatValue *) val)->GetFloat() * m_int);
00197                                 break;
00198                         case VALUE_DIV_OPERATOR:
00199                                 if (m_int == 0)
00200                                         ret = new CErrorValue("Division by zero");
00201                                 else
00202                                         ret = new CFloatValue (((CFloatValue *) val)->GetFloat() / m_int);
00203                                 break;
00204                         case VALUE_EQL_OPERATOR:
00205                                 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() == m_int);
00206                                 break;
00207                         case VALUE_NEQ_OPERATOR:
00208                                 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() != m_int);
00209                                 break;
00210                         case VALUE_GRE_OPERATOR:
00211                                 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() > m_int);
00212                                 break;
00213                         case VALUE_LES_OPERATOR:
00214                                 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() < m_int);
00215                                 break;
00216                         case VALUE_GEQ_OPERATOR:
00217                                 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() >= m_int);
00218                                 break;
00219                         case VALUE_LEQ_OPERATOR:
00220                                 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() <= m_int);
00221                                 break;
00222                         default:
00223                                 ret = new CErrorValue("illegal operator. please send a bug report.");
00224                                 break;
00225                         }
00226                         break;
00227                 }
00228         case VALUE_STRING_TYPE:
00229                 {
00230                         switch(op) {
00231                         case VALUE_ADD_OPERATOR:
00232                                 ret = new CStringValue(val->GetText() + GetText(),"");
00233                                 break;
00234                         case VALUE_EQL_OPERATOR:
00235                         case VALUE_NEQ_OPERATOR:
00236                         case VALUE_GRE_OPERATOR:
00237                         case VALUE_LES_OPERATOR:
00238                         case VALUE_GEQ_OPERATOR:
00239                         case VALUE_LEQ_OPERATOR:
00240                                 ret = new CErrorValue("[Cannot compare string with integer]" + op2str(op) + GetText());
00241                                 break;
00242                         default:
00243                                 ret =  new CErrorValue("[operator not allowed on strings]" + op2str(op) + GetText());
00244                                 break;
00245                         }
00246                         break;
00247                 }
00248         case VALUE_BOOL_TYPE:
00249                 ret =  new CErrorValue("[operator not valid on boolean and integer]" + op2str(op) + GetText());
00250                 break;
00251                 /*
00252                 case VALUE_EMPTY_TYPE:
00253                 {
00254                 switch(op) {
00255                 
00256                   case VALUE_ADD_OPERATOR:
00257                   ret = new CIntValue (m_int);
00258                   break;
00259                   case VALUE_SUB_OPERATOR:
00260                   ret = new CIntValue (-m_int);
00261                   break;
00262                   default:
00263                   {
00264                   ret = new CErrorValue(op2str(op) +    GetText());
00265                   }
00266                   }
00267                   break;
00268                   }
00269                 */
00270         case VALUE_ERROR_TYPE:
00271                 ret = new CErrorValue(val->GetText() + op2str(op) +     GetText());
00272                 break;
00273         default:
00274                 ret = new CErrorValue("illegal type. contact your dealer (if any)");
00275                 break;
00276         }
00277         return ret;
00278 }
00279 
00280 
00281 
00282 cInt CIntValue::GetInt()
00283 /*
00284 pre:
00285 ret: the cInt stored in the object
00286 */
00287 {
00288         return m_int;
00289 }
00290 
00291 
00292 
00293 double CIntValue::GetNumber()
00294 {
00295         return (float) m_int;
00296 }
00297 
00298 
00299 
00300 const STR_String & CIntValue::GetText()
00301 {
00302         if (!m_pstrRep)
00303                 m_pstrRep=new STR_String();
00304         m_pstrRep->Format("%lld",m_int);
00305         
00306         return *m_pstrRep;
00307 }
00308 
00309 
00310 
00311 CValue* CIntValue::GetReplica() { 
00312         CIntValue* replica = new CIntValue(*this);
00313         replica->ProcessReplica();
00314         replica->m_pstrRep = NULL;
00315         
00316         return replica;
00317 }
00318 
00319 
00320 
00321 void CIntValue::SetValue(CValue* newval)
00322 {       
00323         m_int = (cInt)newval->GetNumber(); 
00324         SetModified(true);
00325 }
00326 
00327 
00328 #ifdef WITH_PYTHON
00329 PyObject* CIntValue::ConvertValueToPython()
00330 {
00331         if((m_int > INT_MIN) && (m_int < INT_MAX))
00332                 return PyLong_FromSsize_t(m_int);
00333         else
00334                 return PyLong_FromLongLong(m_int);
00335 }
00336 #endif // WITH_PYTHON