Blender  V2.59
BoolValue.cpp
Go to the documentation of this file.
00001 
00005 // BoolValue.cpp: implementation of the CBoolValue class.
00006 /*
00007  * Copyright (c) 1996-2000 Erwin Coumans <coockie@acm.org>
00008  *
00009  * Permission to use, copy, modify, distribute and sell this software
00010  * and its documentation for any purpose is hereby granted without fee,
00011  * provided that the above copyright notice appear in all copies and
00012  * that both that copyright notice and this permission notice appear
00013  * in supporting documentation.  Erwin Coumans makes no
00014  * representations about the suitability of this software for any
00015  * purpose.  It is provided "as is" without express or implied warranty.
00016  *
00017  */
00018 
00019 #include "BoolValue.h"
00020 #include "StringValue.h"
00021 #include "ErrorValue.h"
00022 #include "VoidValue.h"
00023 
00025 // Construction/Destruction
00027 
00028 const STR_String CBoolValue::sTrueString  = "TRUE";
00029 const STR_String CBoolValue::sFalseString = "FALSE";
00030 
00031 CBoolValue::CBoolValue()
00032 /*
00033 pre: false
00034 effect: constructs a new CBoolValue
00035 */
00036 {
00037         trace("Bool constructor error");
00038 }
00039 
00040 
00041 
00042 CBoolValue::CBoolValue(bool inBool)
00043 : m_bool(inBool)
00044 {
00045 } // Constructs a new CBoolValue containing <inBool>
00046 
00047 
00048 
00049 CBoolValue::CBoolValue(bool innie,const char *name,AllocationTYPE alloctype)
00050 {
00051         m_bool = innie;
00052         SetName(name);
00053 
00054         if (alloctype == CValue::STACKVALUE)
00055                 CValue::DisableRefCount();
00056 }
00057 
00058 
00059 
00060 void CBoolValue::SetValue(CValue* newval)
00061 {
00062         m_bool = (newval->GetNumber() != 0);
00063         SetModified(true);
00064 }
00065 
00066 
00067 
00068 CValue* CBoolValue::Calc(VALUE_OPERATOR op, CValue *val)
00069 /*
00070 pre:
00071 ret: a new object containing the result of applying operator op to this
00072 object and val
00073 */
00074 {
00075         switch (op)
00076         {
00077         case VALUE_POS_OPERATOR:
00078         case VALUE_NEG_OPERATOR:
00079                 {
00080                         return new CErrorValue (op2str(op) + GetText());
00081                         break;
00082                 }
00083         case VALUE_NOT_OPERATOR:
00084                 {
00085                         return new CBoolValue (!m_bool);
00086                         break;
00087                 }
00088         default:
00089                 {
00090                         return val->CalcFinal(VALUE_BOOL_TYPE, op, this);
00091                         break;
00092                 }
00093         }
00094 }
00095 
00096 
00097 
00098 CValue* CBoolValue::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val)
00099 /*
00100 pre: the type of val is dtype
00101 ret: a new object containing the result of applying operator op to val and
00102 this object
00103 */
00104 {
00105         CValue *ret;
00106         
00107         switch(dtype)
00108         {
00109         case VALUE_EMPTY_TYPE:
00110         case VALUE_BOOL_TYPE:
00111                 {
00112                         switch(op)
00113                         {
00114                         case VALUE_AND_OPERATOR:
00115                                 {
00116                                         ret = new CBoolValue (((CBoolValue *) val)->GetBool() && m_bool);
00117                                         break;
00118                                 }
00119                         case VALUE_OR_OPERATOR:
00120                                 {
00121                                         ret = new CBoolValue (((CBoolValue *) val)->GetBool() || m_bool);
00122                                         break;
00123                                 }
00124                         case VALUE_EQL_OPERATOR:
00125                                 {
00126                                         ret = new CBoolValue (((CBoolValue *) val)->GetBool() == m_bool);
00127                                         break;
00128                                 }
00129                         case VALUE_NEQ_OPERATOR:
00130                                 {
00131                                         ret = new CBoolValue (((CBoolValue *) val)->GetBool() != m_bool);
00132                                         break;
00133                                 }
00134                         case VALUE_NOT_OPERATOR:
00135                                 {
00136                                         return new CBoolValue (!m_bool);
00137                                         break;
00138                                 }
00139                         default:
00140                                 {
00141                                         ret =  new CErrorValue(val->GetText() + op2str(op) +
00142                                                 "[operator not allowed on booleans]");
00143                                         break;
00144                                 }
00145                         }
00146                         break;
00147                 }
00148         case VALUE_STRING_TYPE:
00149                 {
00150                         switch(op)
00151                         {
00152                         case VALUE_ADD_OPERATOR:
00153                                 {
00154                                         ret = new CStringValue(val->GetText() + GetText(),"");
00155                                         break;
00156                                 }
00157                         default:
00158                                 {
00159                                         ret =  new CErrorValue(val->GetText() + op2str(op) + "[Only + allowed on boolean and string]");
00160                                         break;
00161                                 }
00162                         }
00163                         break;
00164                 }
00165         default:
00166                 ret =  new CErrorValue("[type mismatch]" + op2str(op) + GetText());
00167         }
00168 
00169         return ret;
00170 }
00171 
00172 
00173 
00174 bool CBoolValue::GetBool()
00175 /*
00176 pre:
00177 ret: the bool stored in the object
00178 */
00179 {
00180         return m_bool;
00181 }
00182 
00183 
00184 
00185 double CBoolValue::GetNumber()
00186 {
00187         return (double)m_bool;
00188 }
00189 
00190 
00191 
00192 const STR_String& CBoolValue::GetText()
00193 {
00194         return m_bool ? sTrueString : sFalseString;
00195 }
00196 
00197 
00198 
00199 CValue* CBoolValue::GetReplica()
00200 {
00201         CBoolValue* replica = new CBoolValue(*this);
00202         replica->ProcessReplica();
00203         
00204         return replica;
00205 }
00206 
00207 #ifdef WITH_PYTHON
00208 PyObject* CBoolValue::ConvertValueToPython()
00209 {
00210         return PyBool_FromLong(m_bool != 0);
00211 }
00212 #endif // WITH_PYTHON