1 /** \file gameengine/Expressions/BoolValue.cpp
5 // BoolValue.cpp: implementation of the CBoolValue class.
7 * Copyright (c) 1996-2000 Erwin Coumans <coockie@acm.org>
9 * Permission to use, copy, modify, distribute and sell this software
10 * and its documentation for any purpose is hereby granted without fee,
11 * provided that the above copyright notice appear in all copies and
12 * that both that copyright notice and this permission notice appear
13 * in supporting documentation. Erwin Coumans makes no
14 * representations about the suitability of this software for any
15 * purpose. It is provided "as is" without express or implied warranty.
19 #include "BoolValue.h"
20 #include "StringValue.h"
21 #include "ErrorValue.h"
22 #include "VoidValue.h"
24 //////////////////////////////////////////////////////////////////////
25 // Construction/Destruction
26 //////////////////////////////////////////////////////////////////////
28 const STR_String CBoolValue::sTrueString = "TRUE";
29 const STR_String CBoolValue::sFalseString = "FALSE";
31 CBoolValue::CBoolValue()
34 effect: constructs a new CBoolValue
37 trace("Bool constructor error");
42 CBoolValue::CBoolValue(bool inBool)
45 } // Constructs a new CBoolValue containing <inBool>
49 CBoolValue::CBoolValue(bool innie,const char *name,AllocationTYPE alloctype)
54 if (alloctype == CValue::STACKVALUE)
55 CValue::DisableRefCount();
60 void CBoolValue::SetValue(CValue* newval)
62 m_bool = (newval->GetNumber() != 0);
68 CValue* CBoolValue::Calc(VALUE_OPERATOR op, CValue *val)
71 ret: a new object containing the result of applying operator op to this
77 case VALUE_POS_OPERATOR:
78 case VALUE_NEG_OPERATOR:
80 return new CErrorValue (op2str(op) + GetText());
83 case VALUE_NOT_OPERATOR:
85 return new CBoolValue (!m_bool);
90 return val->CalcFinal(VALUE_BOOL_TYPE, op, this);
98 CValue* CBoolValue::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val)
100 pre: the type of val is dtype
101 ret: a new object containing the result of applying operator op to val and
109 case VALUE_EMPTY_TYPE:
110 case VALUE_BOOL_TYPE:
114 case VALUE_AND_OPERATOR:
116 ret = new CBoolValue (((CBoolValue *) val)->GetBool() && m_bool);
119 case VALUE_OR_OPERATOR:
121 ret = new CBoolValue (((CBoolValue *) val)->GetBool() || m_bool);
124 case VALUE_EQL_OPERATOR:
126 ret = new CBoolValue (((CBoolValue *) val)->GetBool() == m_bool);
129 case VALUE_NEQ_OPERATOR:
131 ret = new CBoolValue (((CBoolValue *) val)->GetBool() != m_bool);
134 case VALUE_NOT_OPERATOR:
136 return new CBoolValue (!m_bool);
141 ret = new CErrorValue(val->GetText() + op2str(op) +
142 "[operator not allowed on booleans]");
148 case VALUE_STRING_TYPE:
152 case VALUE_ADD_OPERATOR:
154 ret = new CStringValue(val->GetText() + GetText(),"");
159 ret = new CErrorValue(val->GetText() + op2str(op) + "[Only + allowed on boolean and string]");
166 ret = new CErrorValue("[type mismatch]" + op2str(op) + GetText());
174 bool CBoolValue::GetBool()
177 ret: the bool stored in the object
185 double CBoolValue::GetNumber()
187 return (double)m_bool;
192 const STR_String& CBoolValue::GetText()
194 return m_bool ? sTrueString : sFalseString;
199 CValue* CBoolValue::GetReplica()
201 CBoolValue* replica = new CBoolValue(*this);
202 replica->ProcessReplica();
208 PyObject* CBoolValue::ConvertValueToPython()
210 return PyBool_FromLong(m_bool != 0);
212 #endif // WITH_PYTHON