rename and negate DISABLE_PYTHON --> WITH_PYTHON
[blender.git] / source / gameengine / Expressions / BoolValue.cpp
1
2 // BoolValue.cpp: implementation of the CBoolValue class.
3 /*
4  * Copyright (c) 1996-2000 Erwin Coumans <coockie@acm.org>
5  *
6  * Permission to use, copy, modify, distribute and sell this software
7  * and its documentation for any purpose is hereby granted without fee,
8  * provided that the above copyright notice appear in all copies and
9  * that both that copyright notice and this permission notice appear
10  * in supporting documentation.  Erwin Coumans makes no
11  * representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  */
15
16 #include "BoolValue.h"
17 #include "StringValue.h"
18 #include "ErrorValue.h"
19 #include "VoidValue.h"
20
21 //////////////////////////////////////////////////////////////////////
22 // Construction/Destruction
23 //////////////////////////////////////////////////////////////////////
24
25 const STR_String CBoolValue::sTrueString  = "TRUE";
26 const STR_String CBoolValue::sFalseString = "FALSE";
27
28 CBoolValue::CBoolValue()
29 /*
30 pre: false
31 effect: constructs a new CBoolValue
32 */
33 {
34         trace("Bool constructor error");
35 }
36
37
38
39 CBoolValue::CBoolValue(bool inBool)
40 : m_bool(inBool)
41 {
42 } // Constructs a new CBoolValue containing <inBool>
43
44
45
46 CBoolValue::CBoolValue(bool innie,const char *name,AllocationTYPE alloctype)
47 {
48         m_bool = innie;
49         SetName(name);
50
51         if (alloctype == CValue::STACKVALUE)
52                 CValue::DisableRefCount();
53 }
54
55
56
57 void CBoolValue::SetValue(CValue* newval)
58 {
59         m_bool = (newval->GetNumber() != 0);
60         SetModified(true);
61 }
62
63
64
65 CValue* CBoolValue::Calc(VALUE_OPERATOR op, CValue *val)
66 /*
67 pre:
68 ret: a new object containing the result of applying operator op to this
69 object and val
70 */
71 {
72         switch (op)
73         {
74         case VALUE_POS_OPERATOR:
75         case VALUE_NEG_OPERATOR:
76                 {
77                         return new CErrorValue (op2str(op) + GetText());
78                         break;
79                 }
80         case VALUE_NOT_OPERATOR:
81                 {
82                         return new CBoolValue (!m_bool);
83                         break;
84                 }
85         default:
86                 {
87                         return val->CalcFinal(VALUE_BOOL_TYPE, op, this);
88                         break;
89                 }
90         }
91 }
92
93
94
95 CValue* CBoolValue::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val)
96 /*
97 pre: the type of val is dtype
98 ret: a new object containing the result of applying operator op to val and
99 this object
100 */
101 {
102         CValue *ret;
103         
104         switch(dtype)
105         {
106         case VALUE_EMPTY_TYPE:
107         case VALUE_BOOL_TYPE:
108                 {
109                         switch(op)
110                         {
111                         case VALUE_AND_OPERATOR:
112                                 {
113                                         ret = new CBoolValue (((CBoolValue *) val)->GetBool() && m_bool);
114                                         break;
115                                 }
116                         case VALUE_OR_OPERATOR:
117                                 {
118                                         ret = new CBoolValue (((CBoolValue *) val)->GetBool() || m_bool);
119                                         break;
120                                 }
121                         case VALUE_EQL_OPERATOR:
122                                 {
123                                         ret = new CBoolValue (((CBoolValue *) val)->GetBool() == m_bool);
124                                         break;
125                                 }
126                         case VALUE_NEQ_OPERATOR:
127                                 {
128                                         ret = new CBoolValue (((CBoolValue *) val)->GetBool() != m_bool);
129                                         break;
130                                 }
131                         case VALUE_NOT_OPERATOR:
132                                 {
133                                         return new CBoolValue (!m_bool);
134                                         break;
135                                 }
136                         default:
137                                 {
138                                         ret =  new CErrorValue(val->GetText() + op2str(op) +
139                                                 "[operator not allowed on booleans]");
140                                         break;
141                                 }
142                         }
143                         break;
144                 }
145         case VALUE_STRING_TYPE:
146                 {
147                         switch(op)
148                         {
149                         case VALUE_ADD_OPERATOR:
150                                 {
151                                         ret = new CStringValue(val->GetText() + GetText(),"");
152                                         break;
153                                 }
154                         default:
155                                 {
156                                         ret =  new CErrorValue(val->GetText() + op2str(op) + "[Only + allowed on boolean and string]");
157                                         break;
158                                 }
159                         }
160                         break;
161                 }
162         default:
163                 ret =  new CErrorValue("[type mismatch]" + op2str(op) + GetText());
164         }
165
166         return ret;
167 }
168
169
170
171 bool CBoolValue::GetBool()
172 /*
173 pre:
174 ret: the bool stored in the object
175 */
176 {
177         return m_bool;
178 }
179
180
181
182 double CBoolValue::GetNumber()
183 {
184         return (double)m_bool;
185 }
186
187
188
189 const STR_String& CBoolValue::GetText()
190 {
191         return m_bool ? sTrueString : sFalseString;
192 }
193
194
195
196 CValue* CBoolValue::GetReplica()
197 {
198         CBoolValue* replica = new CBoolValue(*this);
199         replica->ProcessReplica();
200         
201         return replica;
202 }
203
204 #ifdef WITH_PYTHON
205 PyObject* CBoolValue::ConvertValueToPython()
206 {
207         return PyBool_FromLong(m_bool != 0);
208 }
209 #endif // WITH_PYTHON