Blender V2.61 - r43446
|
00001 00004 // FloatValue.cpp: implementation of the CFloatValue 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 "FloatValue.h" 00019 #include "IntValue.h" 00020 #include "StringValue.h" 00021 #include "BoolValue.h" 00022 #include "ErrorValue.h" 00023 #include "VoidValue.h" 00024 00026 // Construction/Destruction 00028 00029 CFloatValue::CFloatValue() 00030 /* 00031 pre: false 00032 effect: constructs a new CFloatValue 00033 */ 00034 { 00035 m_pstrRep=NULL; 00036 } 00037 00038 00039 00040 CFloatValue::CFloatValue(float fl) 00041 /* 00042 pre: 00043 effect: constructs a new CFloatValue containing value fl 00044 */ 00045 { 00046 m_float = fl; 00047 m_pstrRep=NULL; 00048 } 00049 00050 00051 00052 CFloatValue::CFloatValue(float fl,const char *name,AllocationTYPE alloctype) 00053 /* 00054 pre: 00055 effect: constructs a new CFloatValue containing value fl 00056 */ 00057 { 00058 00059 m_float = fl; 00060 SetName(name); 00061 if (alloctype==CValue::STACKVALUE) 00062 { 00063 CValue::DisableRefCount(); 00064 00065 } 00066 m_pstrRep=NULL; 00067 } 00068 00069 00070 00071 CFloatValue::~CFloatValue() 00072 /* 00073 pre: 00074 effect: deletes the object 00075 */ 00076 { 00077 if (m_pstrRep) 00078 delete m_pstrRep; 00079 } 00080 00081 00082 00083 CValue* CFloatValue::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->CalcFloat(op, this); 00091 switch (op) 00092 { 00093 case VALUE_POS_OPERATOR: 00094 return new CFloatValue (m_float); 00095 break; 00096 case VALUE_NEG_OPERATOR: 00097 return new CFloatValue (-m_float); 00098 break; 00099 case VALUE_NOT_OPERATOR: 00100 return new CErrorValue (op2str(op) + "only allowed on booleans"); 00101 break; 00102 case VALUE_AND_OPERATOR: 00103 case VALUE_OR_OPERATOR: 00104 return new CErrorValue(val->GetText() + op2str(op) + "only allowed on booleans"); 00105 break; 00106 default: 00107 return val->CalcFinal(VALUE_FLOAT_TYPE, op, this); 00108 break; 00109 } 00110 } 00111 00112 00113 00114 CValue* CFloatValue::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val) 00115 /* 00116 pre: the type of val is dtype 00117 ret: a new object containing the result of applying operator op to val and 00118 this object 00119 */ 00120 { 00121 CValue *ret; 00122 00123 switch(dtype) 00124 { 00125 case VALUE_INT_TYPE: 00126 { 00127 switch (op) 00128 { 00129 case VALUE_MOD_OPERATOR: 00130 ret = new CFloatValue(fmod(((CIntValue *) val)->GetInt(), m_float)); 00131 break; 00132 case VALUE_ADD_OPERATOR: 00133 ret = new CFloatValue(((CIntValue *) val)->GetInt() + m_float); 00134 break; 00135 case VALUE_SUB_OPERATOR: 00136 ret = new CFloatValue(((CIntValue *) val)->GetInt() - m_float); 00137 break; 00138 case VALUE_MUL_OPERATOR: 00139 ret = new CFloatValue(((CIntValue *) val)->GetInt() * m_float); 00140 break; 00141 case VALUE_DIV_OPERATOR: 00142 if (m_float == 0) 00143 ret = new CErrorValue("Division by zero"); 00144 else 00145 ret = new CFloatValue (((CIntValue *) val)->GetInt() / m_float); 00146 break; 00147 case VALUE_EQL_OPERATOR: 00148 ret = new CBoolValue(((CIntValue *) val)->GetInt() == m_float); 00149 break; 00150 case VALUE_NEQ_OPERATOR: 00151 ret = new CBoolValue(((CIntValue *) val)->GetInt() != m_float); 00152 break; 00153 case VALUE_GRE_OPERATOR: 00154 ret = new CBoolValue(((CIntValue *) val)->GetInt() > m_float); 00155 break; 00156 case VALUE_LES_OPERATOR: 00157 ret = new CBoolValue(((CIntValue *) val)->GetInt() < m_float); 00158 break; 00159 case VALUE_GEQ_OPERATOR: 00160 ret = new CBoolValue(((CIntValue *) val)->GetInt() >= m_float); 00161 break; 00162 case VALUE_LEQ_OPERATOR: 00163 ret = new CBoolValue(((CIntValue *) val)->GetInt() <= m_float); 00164 break; 00165 default: 00166 ret = new CErrorValue("illegal operator. please send a bug report."); 00167 break; 00168 } 00169 break; 00170 } 00171 case VALUE_EMPTY_TYPE: 00172 case VALUE_FLOAT_TYPE: 00173 { 00174 switch (op) 00175 { 00176 case VALUE_MOD_OPERATOR: 00177 ret = new CFloatValue(fmod(((CFloatValue *) val)->GetFloat(), m_float)); 00178 break; 00179 case VALUE_ADD_OPERATOR: 00180 ret = new CFloatValue(((CFloatValue *) val)->GetFloat() + m_float); 00181 break; 00182 case VALUE_SUB_OPERATOR: 00183 ret = new CFloatValue(((CFloatValue *) val)->GetFloat() - m_float); 00184 break; 00185 case VALUE_MUL_OPERATOR: 00186 ret = new CFloatValue(((CFloatValue *) val)->GetFloat() * m_float); 00187 break; 00188 case VALUE_DIV_OPERATOR: 00189 if (m_float == 0) 00190 ret = new CErrorValue("Division by zero"); 00191 else 00192 ret = new CFloatValue (((CFloatValue *) val)->GetFloat() / m_float); 00193 break; 00194 case VALUE_EQL_OPERATOR: 00195 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() == m_float); 00196 break; 00197 case VALUE_NEQ_OPERATOR: 00198 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() != m_float); 00199 break; 00200 case VALUE_GRE_OPERATOR: 00201 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() > m_float); 00202 break; 00203 case VALUE_LES_OPERATOR: 00204 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() < m_float); 00205 break; 00206 case VALUE_GEQ_OPERATOR: 00207 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() >= m_float); 00208 break; 00209 case VALUE_LEQ_OPERATOR: 00210 ret = new CBoolValue(((CFloatValue *) val)->GetFloat() <= m_float); 00211 break; 00212 case VALUE_NEG_OPERATOR: 00213 ret = new CFloatValue (-m_float); 00214 break; 00215 case VALUE_POS_OPERATOR: 00216 ret = new CFloatValue (m_float); 00217 break; 00218 00219 default: 00220 ret = new CErrorValue("illegal operator. please send a bug report."); 00221 break; 00222 } 00223 break; 00224 } 00225 case VALUE_STRING_TYPE: 00226 { 00227 switch(op) 00228 { 00229 case VALUE_ADD_OPERATOR: 00230 ret = new CStringValue(val->GetText() + GetText(),""); 00231 break; 00232 case VALUE_EQL_OPERATOR: 00233 case VALUE_NEQ_OPERATOR: 00234 case VALUE_GRE_OPERATOR: 00235 case VALUE_LES_OPERATOR: 00236 case VALUE_GEQ_OPERATOR: 00237 case VALUE_LEQ_OPERATOR: 00238 ret = new CErrorValue("[Cannot compare string with float]" + op2str(op) + GetText()); 00239 break; 00240 default: 00241 ret = new CErrorValue("[operator not allowed on strings]" + op2str(op) + GetText()); 00242 break; 00243 } 00244 break; 00245 } 00246 case VALUE_BOOL_TYPE: 00247 ret = new CErrorValue("[operator not valid on boolean and float]" + op2str(op) + GetText()); 00248 break; 00249 case VALUE_ERROR_TYPE: 00250 ret = new CErrorValue(val->GetText() + op2str(op) + GetText()); 00251 break; 00252 default: 00253 ret = new CErrorValue("illegal type. contact your dealer (if any)"); 00254 break; 00255 } 00256 return ret; 00257 } 00258 00259 00260 00261 void CFloatValue::SetFloat(float fl) 00262 { 00263 m_float = fl; 00264 SetModified(true); 00265 } 00266 00267 00268 00269 float CFloatValue::GetFloat() 00270 /* 00271 pre: 00272 ret: the float stored in the object 00273 */ 00274 { 00275 return m_float; 00276 } 00277 00278 00279 00280 double CFloatValue::GetNumber() 00281 { 00282 return m_float; 00283 } 00284 00285 00286 00287 void CFloatValue::SetValue(CValue* newval) 00288 { 00289 m_float = (float)newval->GetNumber(); 00290 SetModified(true); 00291 } 00292 00293 00294 00295 const STR_String & CFloatValue::GetText() 00296 { 00297 if (!m_pstrRep) 00298 m_pstrRep = new STR_String(); 00299 00300 m_pstrRep->Format("%f",m_float); 00301 return *m_pstrRep; 00302 } 00303 00304 00305 00306 CValue* CFloatValue::GetReplica() 00307 { 00308 CFloatValue* replica = new CFloatValue(*this); 00309 replica->m_pstrRep = NULL; /* should be in CFloatValue::ProcessReplica() but its not defined, no matter */ 00310 replica->ProcessReplica(); 00311 00312 return replica; 00313 } 00314 00315 00316 #ifdef WITH_PYTHON 00317 PyObject* CFloatValue::ConvertValueToPython() 00318 { 00319 return PyFloat_FromDouble(m_float); 00320 } 00321 #endif // WITH_PYTHON