Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

FloatValue.cpp

Go to the documentation of this file.
00001 // FloatValue.cpp: implementation of the CFloatValue class.
00002 /*
00003  * Copyright (c) 1996-2000 Erwin Coumans <[email protected]>

00004  *
00005  * Permission to use, copy, modify, distribute and sell this software

00006  * and its documentation for any purpose is hereby granted without fee,

00007  * provided that the above copyright notice appear in all copies and

00008  * that both that copyright notice and this permission notice appear

00009  * in supporting documentation.  Erwin Coumans makes no

00010  * representations about the suitability of this software for any

00011  * purpose.  It is provided "as is" without express or implied warranty.

00012  *
00013  */
00014 
00015 
00016 #include "FloatValue.h"
00017 #include "IntValue.h"
00018 #include "StringValue.h"
00019 #include "BoolValue.h"
00020 #include "ErrorValue.h"
00021 #include "VoidValue.h"
00022 
00024 //#include "FactoryManager.h"
00025 
00027 // Construction/Destruction
00029 
00030 CFloatValue::CFloatValue()
00031 /*
00032 pre: false

00033 effect: constructs a new CFloatValue

00034 */
00035 {
00036         //m_float = 0.0;//-123.456;
00037         m_pstrRep=NULL;
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 CFloatValue::CFloatValue(float fl,CCString name,AllocationTYPE alloctype)
00051 /*
00052 pre:

00053 effect: constructs a new CFloatValue containing value fl

00054 */
00055 {
00056         
00057         m_float = fl;
00058         SetName(name);
00059         if (alloctype==CValue::STACKVALUE)
00060         {
00061                 CValue::DisableRefCount();
00062 
00063         }
00064         m_pstrRep=NULL;
00065 }
00066 
00067 CFloatValue::~CFloatValue()
00068 /*
00069 pre:

00070 effect: deletes the object

00071 */
00072 {
00073         if (m_pstrRep)
00074                 delete m_pstrRep;
00075 }
00076 
00077 CValue* CFloatValue::Calc(VALUE_OPERATOR op, CValue *val)
00078 /*
00079 pre:

00080 ret: a new object containing the result of applying operator op to this

00081          object and val

00082 */
00083 {
00084         //return val->CalcFloat(op, this);
00085         switch (op) {
00086         case VALUE_NEG_OPERATOR:
00087                 return new CFloatValue (-m_float);
00088                 break;
00089         case VALUE_NOT_OPERATOR:
00090                 return new CErrorValue (op2str(op) + "only allowed on booleans");
00091                 break;
00092         case VALUE_AND_OPERATOR:
00093         case VALUE_OR_OPERATOR:
00094                 return new CErrorValue(val->GetText() + op2str(op) + "only allowed on booleans");
00095                 break;
00096         default:
00097                 return val->CalcFinal(VALUE_FLOAT_TYPE, op, this);
00098                 break;
00099         }
00100 }
00101 
00102 CValue* CFloatValue::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val)
00103 /*
00104 pre: the type of val is dtype

00105 ret: a new object containing the result of applying operator op to val and

00106          this object

00107 */
00108 {
00109         CValue *ret;
00110 
00111         switch(dtype) {
00112         case VALUE_INT_TYPE: {
00113                 switch (op) {
00114                 case VALUE_ADD_OPERATOR:
00115                         ret = new CFloatValue(((CIntValue *) val)->GetInt() + m_float);
00116                         break;
00117                 case VALUE_SUB_OPERATOR:
00118                         ret = new CFloatValue(((CIntValue *) val)->GetInt() - m_float);
00119                         break;
00120                 case VALUE_MUL_OPERATOR:
00121                         ret = new CFloatValue(((CIntValue *) val)->GetInt() * m_float);
00122                         break;
00123                 case VALUE_DIV_OPERATOR:
00124                         if (m_float == 0)
00125                                 ret = new CErrorValue("Division by zero");
00126                         else
00127                                 ret = new CFloatValue (((CIntValue *) val)->GetInt() / m_float);
00128                         break;
00129                 case VALUE_EQL_OPERATOR:
00130                         ret = new CBoolValue(((CIntValue *) val)->GetInt() == m_float);
00131                         break;
00132                 case VALUE_NEQ_OPERATOR:
00133                         ret = new CBoolValue(((CIntValue *) val)->GetInt() != m_float);
00134                         break;
00135                 case VALUE_GRE_OPERATOR:
00136                         ret = new CBoolValue(((CIntValue *) val)->GetInt() > m_float);
00137                         break;
00138                 case VALUE_LES_OPERATOR:
00139                         ret = new CBoolValue(((CIntValue *) val)->GetInt() < m_float);
00140                         break;
00141                 case VALUE_GEQ_OPERATOR:
00142                         ret = new CBoolValue(((CIntValue *) val)->GetInt() >= m_float);
00143                         break;
00144                 case VALUE_LEQ_OPERATOR:
00145                         ret = new CBoolValue(((CIntValue *) val)->GetInt() <= m_float);
00146                         break;
00147                 default:
00148                         ret = new CErrorValue("illegal operator. please send a bug report.");
00149                         break;
00150                 }
00151                 break;
00152                                                 }
00153         case VALUE_EMPTY_TYPE:
00154         case VALUE_FLOAT_TYPE:
00155         {
00156                 switch (op) {
00157                 case VALUE_ADD_OPERATOR:
00158                         ret = new CFloatValue(((CFloatValue *) val)->GetFloat() + m_float);
00159                         break;
00160                 case VALUE_SUB_OPERATOR:
00161                         ret = new CFloatValue(((CFloatValue *) val)->GetFloat() - m_float);
00162                         break;
00163                 case VALUE_MUL_OPERATOR:
00164                         ret = new CFloatValue(((CFloatValue *) val)->GetFloat() * m_float);
00165                         break;
00166                 case VALUE_DIV_OPERATOR:
00167                         if (m_float == 0)
00168                                 ret = new CErrorValue("Division by zero");
00169                         else
00170                                 ret = new CFloatValue (((CFloatValue *) val)->GetFloat() / m_float);
00171                         break;
00172                 case VALUE_EQL_OPERATOR:
00173                         ret = new CBoolValue(((CFloatValue *) val)->GetFloat() == m_float);
00174                         break;
00175                 case VALUE_NEQ_OPERATOR:
00176                         ret = new CBoolValue(((CFloatValue *) val)->GetFloat() != m_float);
00177                         break;
00178                 case VALUE_GRE_OPERATOR:
00179                         ret = new CBoolValue(((CFloatValue *) val)->GetFloat() > m_float);
00180                         break;
00181                 case VALUE_LES_OPERATOR:
00182                         ret = new CBoolValue(((CFloatValue *) val)->GetFloat() < m_float);
00183                         break;
00184                 case VALUE_GEQ_OPERATOR:
00185                         ret = new CBoolValue(((CFloatValue *) val)->GetFloat() >= m_float);
00186                         break;
00187                 case VALUE_LEQ_OPERATOR:
00188                         ret = new CBoolValue(((CFloatValue *) val)->GetFloat() <= m_float);
00189                         break;
00190                 case VALUE_NEG_OPERATOR:
00191                         ret = new CFloatValue (-m_float);
00192                         break;
00193 
00194                 default:
00195                         ret = new CErrorValue("illegal operator. please send a bug report.");
00196                         break;
00197                 }
00198                 break;
00199         }
00200         case VALUE_STRING_TYPE:
00201         {
00202                 switch(op) {
00203                 case VALUE_ADD_OPERATOR:
00204                         ret = new CStringValue(val->GetText() + GetText(),"");
00205                         break;
00206                 case VALUE_EQL_OPERATOR:
00207                 case VALUE_NEQ_OPERATOR:
00208                 case VALUE_GRE_OPERATOR:
00209                 case VALUE_LES_OPERATOR:
00210                 case VALUE_GEQ_OPERATOR:
00211                 case VALUE_LEQ_OPERATOR:
00212                         ret = new CErrorValue("[Cannot compare string with float]" + op2str(op) + GetText());
00213                         break;
00214                 default:
00215                         ret =  new CErrorValue("[operator not allowed on strings]" + op2str(op) + GetText());
00216                         break;
00217                 }
00218                 break;
00219         }
00220         case VALUE_BOOL_TYPE:
00221                 ret =  new CErrorValue("[operator not valid on boolean and float]" + op2str(op) + GetText());
00222                 break;
00223         case VALUE_ERROR_TYPE:
00224                 ret = new CErrorValue(val->GetText() + op2str(op) + GetText());
00225                 break;
00226         default:
00227                 ret = new CErrorValue("illegal type. contact your dealer (if any)");
00228                 break;
00229         }
00230         return ret;
00231 }
00232 
00233 float CFloatValue::GetFloat()
00234 /*
00235 pre:

00236 ret: the float stored in the object

00237 */
00238 {
00239         return m_float;
00240 }
00241 
00242 
00243 
00244 float CFloatValue::GetNumber()

00245 {
00246         return m_float;
00247 }
00248 
00249 
00250 /*
00251 */
00252 
00253 /*void CFloatValue::Configure(CValue* menuvalue)

00254 {

00255 
00256         if (menuvalue)

00257         {

00258                 AddConfigurationData(menuvalue);

00259         } else 

00260         {

00261                 // todo: create own menu

00262         }

00263         

00264 }

00265 */
00266 
00267 
00268 const CCString & CFloatValue::GetText()

00269 {
00270         if (!m_pstrRep)
00271                 m_pstrRep=new CCString();
00272 
00273         m_pstrRep->Format("%f",m_float);
00274         return *m_pstrRep;
00275 }
00276 
00277 CValue* CFloatValue::GetReplica() { 
00278         CFloatValue* replica = new CFloatValue(*this);//m_float,GetName());
00279         replica->m_pstrRep = NULL;
00280         CValue::AddDataToReplica(replica);
00281                 return replica;
00282         };
00283 

Generated at Thu Feb 1 13:03:02 2001 for Ketsji Game Engine by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000