Initial revision
[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
17
18 #include "BoolValue.h"
19 #include "StringValue.h"
20 #include "ErrorValue.h"
21 #include "VoidValue.h"
22 //#include "FactoryManager.h"
23
24 //////////////////////////////////////////////////////////////////////
25 // Construction/Destruction
26 //////////////////////////////////////////////////////////////////////
27
28
29 CBoolValue::CBoolValue()
30 /*
31 pre: false
32 effect: constructs a new CBoolValue
33 */
34 {
35         trace("Bool constructor error");
36 }
37
38
39
40 CBoolValue::CBoolValue(bool inBool)
41 : m_bool(inBool)
42 {
43 } // Constructs a new CBoolValue containing <inBool>
44
45
46
47 CBoolValue::CBoolValue(bool innie,STR_String name,AllocationTYPE alloctype)
48 {
49         m_bool = innie;
50         SetName(name);
51
52         if (alloctype == CValue::STACKVALUE)
53                 CValue::DisableRefCount();
54 }
55
56
57
58 void CBoolValue::SetValue(CValue* newval)
59 {
60         m_bool = (newval->GetNumber() != 0);
61         SetModified(true);
62 }
63
64
65
66 CValue* CBoolValue::Calc(VALUE_OPERATOR op, CValue *val)
67 /*
68 pre:
69 ret: a new object containing the result of applying operator op to this
70 object and val
71 */
72 {
73         switch (op)
74         {
75         case VALUE_POS_OPERATOR:
76         case VALUE_NEG_OPERATOR:
77                 {
78                         return new CErrorValue (op2str(op) + GetText());
79                         break;
80                 }
81         case VALUE_NOT_OPERATOR:
82                 {
83                         return new CBoolValue (!m_bool);
84                         break;
85                 }
86         default:
87                 {
88                         return val->CalcFinal(VALUE_BOOL_TYPE, op, this);
89                         break;
90                 }
91         }
92 }
93
94
95
96 CValue* CBoolValue::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val)
97 /*
98 pre: the type of val is dtype
99 ret: a new object containing the result of applying operator op to val and
100 this object
101 */
102 {
103         CValue *ret;
104         
105         switch(dtype)
106         {
107         case VALUE_EMPTY_TYPE:
108         case VALUE_BOOL_TYPE:
109                 {
110                         switch(op)
111                         {
112                         case VALUE_AND_OPERATOR:
113                                 {
114                                         ret = new CBoolValue (((CBoolValue *) val)->GetBool() && m_bool);
115                                         break;
116                                 }
117                         case VALUE_OR_OPERATOR:
118                                 {
119                                         ret = new CBoolValue (((CBoolValue *) val)->GetBool() || m_bool);
120                                         break;
121                                 }
122                         case VALUE_EQL_OPERATOR:
123                                 {
124                                         ret = new CBoolValue (((CBoolValue *) val)->GetBool() == m_bool);
125                                         break;
126                                 }
127                         case VALUE_NEQ_OPERATOR:
128                                 {
129                                         ret = new CBoolValue (((CBoolValue *) val)->GetBool() != m_bool);
130                                         break;
131                                 }
132                         case VALUE_NOT_OPERATOR:
133                                 {
134                                         return new CBoolValue (!m_bool);
135                                         break;
136                                 }
137                         default:
138                                 {
139                                         ret =  new CErrorValue(val->GetText() + op2str(op) +
140                                                 "[operator not allowed on booleans]");
141                                         break;
142                                 }
143                         }
144                         break;
145                 }
146         case VALUE_STRING_TYPE:
147                 {
148                         switch(op)
149                         {
150                         case VALUE_ADD_OPERATOR:
151                                 {
152                                         ret = new CStringValue(val->GetText() + GetText(),"");
153                                         break;
154                                 }
155                         default:
156                                 {
157                                         ret =  new CErrorValue(val->GetText() + op2str(op) + "[Only + allowed on boolean and string]");
158                                         break;
159                                 }
160                         }
161                         break;
162                 }
163         default:
164                 ret =  new CErrorValue("[type mismatch]" + op2str(op) + GetText());
165         }
166
167         return ret;
168 }
169
170
171
172 bool CBoolValue::GetBool()
173 /*
174 pre:
175 ret: the bool stored in the object
176 */
177 {
178         return m_bool;
179 }
180
181
182
183 float CBoolValue::GetNumber()
184 {
185         return (float)m_bool;
186 }
187
188
189
190 const STR_String& CBoolValue::GetText()
191 {
192         static STR_String sTrueString  = STR_String("TRUE");
193         static STR_String sFalseString = STR_String("FALSE");
194         
195         return m_bool ? sTrueString : sFalseString;
196 }
197
198
199
200 CValue* CBoolValue::GetReplica()
201 {
202         CBoolValue* replica = new CBoolValue(*this);
203         CValue::AddDataToReplica(replica);
204         
205         return replica;
206 }
207
208
209
210 PyObject* CBoolValue::ConvertValueToPython()
211 {
212         return PyInt_FromLong(m_bool != 0);
213 }