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