First batch of GE API cleanup.
[blender.git] / source / gameengine / Expressions / Value.h
1 /*
2  * Value.h: interface for the CValue class.
3  * $Id$
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 #ifdef WIN32
17 #pragma warning (disable:4786)
18 #endif //WIN32
19
20 /////////////////////////////////////////////////////////////////////////////////////
21 //// Baseclass CValue
22 //// Together with CExpression, CValue and it's derived classes can be used to
23 //// parse expressions into a parsetree with error detecting/correcting capabilities
24 //// also expandible by a CFactory pluginsystem 
25 //// 
26 //// Features:
27 //// Reference Counting (AddRef() / Release())
28 //// Calculations (Calc() / CalcFinal())
29 //// Configuration (Configure())
30 //// Serialization (EdSerialize() / EdIdSerialize() / EdPtrSerialize() and macro PLUGIN_DECLARE_SERIAL
31 //// Property system (SetProperty() / GetProperty() / FindIdentifier())
32 //// Replication (GetReplica())
33 //// Flags (IsSelected() / IsModified() / SetSelected()...)
34 //// 
35 //// Some small editor-specific things added
36 //// A helperclass CompressorArchive handles the serialization
37 //// 
38 /////////////////////////////////////////////////////////////////////////////////////
39 #ifndef __VALUE_H__
40 #define __VALUE_H__
41
42 #include <map>          // array functionality for the propertylist
43 #include "STR_String.h" // STR_String class
44
45 #ifndef GEN_NO_ASSERT
46 #undef  assert
47 #define assert(exp)                     ((void)NULL)
48 #endif
49
50
51 #ifndef GEN_NO_TRACE
52 #undef  trace
53 #define trace(exp)                      ((void)NULL)
54 #endif
55
56 #ifndef GEN_NO_DEBUG
57 #undef  debug
58 #define debug(exp)                      ((void)NULL)
59 #endif
60
61 #ifndef GEN_NO_ASSERTD
62 #undef  assertd
63 #define assertd(exp)                    ((void)NULL)
64 #endif
65
66
67 #ifndef USE_PRAGMA_ONCE
68 #ifdef WIN32
69         #pragma once
70
71 #endif //WIN32
72 #endif
73
74 #define EDITOR_LEVEL_VERSION 0x06
75
76 enum VALUE_OPERATOR {
77         
78         VALUE_ADD_OPERATOR,                     // +
79         VALUE_SUB_OPERATOR,                     // -
80         VALUE_MUL_OPERATOR,                     // *
81         VALUE_DIV_OPERATOR,                     // /
82         VALUE_NEG_OPERATOR,                     // -
83         VALUE_POS_OPERATOR,                     // +
84         VALUE_AND_OPERATOR,                     // &&
85         VALUE_OR_OPERATOR,                      // ||
86         VALUE_EQL_OPERATOR,                     // ==
87         VALUE_NEQ_OPERATOR,                     // !=
88         VALUE_GRE_OPERATOR,                     // >
89         VALUE_LES_OPERATOR,                     // <
90         VALUE_GEQ_OPERATOR,                     // >=
91         VALUE_LEQ_OPERATOR,                     // <=
92         VALUE_NOT_OPERATOR,                     // !
93         VALUE_NO_OPERATOR                       // no operation at all
94 };
95
96 enum VALUE_DATA_TYPE {
97         VALUE_NO_TYPE,                          // abstract baseclass
98         VALUE_INT_TYPE,
99         VALUE_FLOAT_TYPE,
100         VALUE_STRING_TYPE,
101         VALUE_BOOL_TYPE,
102         VALUE_ERROR_TYPE,
103         VALUE_EMPTY_TYPE,
104         VALUE_SOLID_TYPE,
105         VALUE_COMBISOLID_TYPE,
106         VALUE_VECTOR_TYPE,
107         VALUE_MENU_TYPE,
108         VALUE_ACTOR_TYPE,
109         VALUE_MAX_TYPE                          //only here to provide number of types
110 };
111
112
113
114 #ifdef _DEBUG
115 //extern int gRefCountValue;            // debugonly variable to check if all CValue Refences are Dereferenced at programexit
116 #endif
117
118 struct HashableInt 
119 {
120         HashableInt(int id)                                                                                                                     : mData(id) { }
121
122         unsigned long                           Hash() const                                                                                    { return 0;} ////}gHash(&mData, sizeof(int));}
123         
124         bool                            operator==(HashableInt rhs)                                                             { return mData == rhs.mData; }
125         
126         int                                     mData;
127 };
128
129
130 //
131 // Bitfield that stores the flags for each CValue derived class
132 //
133 struct ValueFlags {
134         ValueFlags() :
135                 Modified(true),
136                 Selected(false),
137                 Affected(false),
138                 ReleaseRequested(false),
139                 Error(false),
140                 RefCountDisabled(false),
141                 HasProperties(false),
142                 HasName(false),
143                 Visible(true),
144                 CustomFlag1(false),
145                 CustomFlag2(false)
146         {
147         }
148
149         unsigned short Modified : 1;
150         unsigned short Selected : 1;
151         unsigned short Affected : 1;
152         unsigned short ReleaseRequested : 1;
153         unsigned short Error : 1;
154         unsigned short RefCountDisabled : 1;
155         unsigned short HasProperties : 1;
156         unsigned short HasName : 1;
157         unsigned short Visible : 1;
158         unsigned short CustomFlag1 : 1;
159         unsigned short CustomFlag2 : 1;
160
161         
162 };
163
164 /**
165  *      Base Class for all Actions performed on CValue's. Can be extended for undo/redo system in future.
166 */
167 class CAction
168 {
169 public:
170         CAction() {
171         };
172         virtual ~CAction(){
173         };
174         virtual void Execute() const =0;
175 };
176
177 //
178 // CValue
179 //
180 // Base class for all editor functionality, flexible object type that allows
181 // calculations and uses reference counting for memory management.
182 // 
183 //
184
185
186
187
188 #ifndef NO_EXP_PYTHON_EMBEDDING
189 #include "PyObjectPlus.h"
190 #include "object.h"
191 class CValue  : public PyObjectPlus
192 #else
193 class CValue  
194 #endif //NO_EXP_PYTHON_EMBEDDING
195
196
197 {
198 #ifndef NO_EXP_PYTHON_EMBEDDING
199 Py_Header;
200 #endif //NO_EXP_PYTHON_EMBEDDING
201 public:
202         enum AllocationTYPE {
203                 STACKVALUE              = 0,
204                 HEAPVALUE               = 1
205         };
206         
207         enum DrawTYPE {
208                 STARTFRAME              = 0,
209                 ENDFRAME                = 1,
210                 INTERFRAME              = 2
211         };
212
213
214         // Construction / Destruction
215 #ifndef NO_EXP_PYTHON_EMBEDDING
216
217         CValue(PyTypeObject *T = &Type);
218         //static PyObject*      PyMake(PyObject*,PyObject*);
219         virtual PyObject *_repr(void)
220         {
221                 return Py_BuildValue("s",(const char*)GetText());
222         }
223
224
225
226         virtual PyObject*                       _getattr(const STR_String& attr);
227
228         void    SpecialRelease()
229         {
230                 int i=0;
231                 if (ob_refcnt == 0)
232                 {
233                         _Py_NewReference(this);
234                         
235                 } else
236                 {
237                         i++;
238                 }
239                 Release();
240         }
241         static void PyDestructor(PyObject *P)                           // python wrapper
242         {
243           ((CValue*)P)->SpecialRelease();
244         };
245
246         virtual PyObject*       ConvertValueToPython() {
247                 return NULL;
248         }
249
250         virtual CValue* ConvertPythonToValue(PyObject* pyobj);
251
252
253         virtual int                             _delattr(const STR_String& attr);
254         virtual int                             _setattr(const STR_String& attr,PyObject* value);
255         
256         virtual PyObject* ConvertKeysToPython( void );
257         
258         KX_PYMETHOD_NOARGS(CValue,GetName);
259
260 #else
261         CValue();
262 #endif //NO_EXP_PYTHON_EMBEDDING
263
264         
265         
266         // Expression Calculation
267         virtual CValue*         Calc(VALUE_OPERATOR op, CValue *val) = 0;
268         virtual CValue*         CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val) = 0;
269         virtual void            SetOwnerExpression(class CExpression* expr);
270
271         
272
273         void                            Execute(const CAction& a)
274         {
275                 a.Execute();
276         };
277
278         /// Reference Counting
279         int                                     GetRefCount()                                                                                   { return m_refcount; }
280         virtual CValue*         AddRef();                                                                                               // Add a reference to this value
281         virtual int                     Release();                                                                                              // Release a reference to this value (when reference count reaches 0, the value is removed from the heap)
282
283         /// Property Management
284         virtual void            SetProperty(const STR_String& name,CValue* ioProperty);                                         // Set property <ioProperty>, overwrites and releases a previous property with the same name if needed
285         virtual CValue*         GetProperty(const STR_String & inName);                                                 // Get pointer to a property with name <inName>, returns NULL if there is no property named <inName>
286         STR_String                      GetPropertyText(const STR_String & inName,const STR_String& deftext="");                                                // Get text description of property with name <inName>, returns an empty string if there is no property named <inName>
287         float                           GetPropertyNumber(const STR_String& inName,float defnumber);
288         virtual bool            RemoveProperty(const STR_String & inName);                                              // Remove the property named <inName>, returns true if the property was succesfully removed, false if property was not found or could not be removed
289         virtual vector<STR_String>      GetPropertyNames();
290         virtual void            ClearProperties();                                                                              // Clear all properties
291
292         virtual void            SetPropertiesModified(bool inModified);                                 // Set all properties' modified flag to <inModified>
293         virtual bool            IsAnyPropertyModified();                                                                // Check if any of the properties in this value have been modified
294
295         virtual CValue*         GetProperty(int inIndex);                                                               // Get property number <inIndex>
296         virtual int                     GetPropertyCount();                                                                             // Get the amount of properties assiocated with this value
297
298         virtual void            CloneProperties(CValue* replica);
299         virtual CValue*         FindIdentifier(const STR_String& identifiername);
300         /** Set the wireframe color of this value depending on the CSG
301          * operator type <op>
302          * @attention: not implemented */
303         virtual void            SetColorOperator(VALUE_OPERATOR op);
304
305         virtual const STR_String &      GetText() = 0;
306         virtual float           GetNumber() = 0;
307         double*                         ZeroVector() { return m_sZeroVec; };
308         virtual double*         GetVector3(bool bGetTransformedVec = false);
309
310         virtual STR_String      GetName() = 0;                                                                                  // Retrieve the name of the value
311         virtual void            SetName(STR_String name) = 0;                                                           // Set the name of the value
312         virtual void            ReplicaSetName(STR_String name) = 0;
313         /** Sets the value to this cvalue.
314          * @attention this particular function should never be called. Why not abstract? */
315         virtual void            SetValue(CValue* newval);
316         virtual CValue*         GetReplica() =0;
317         //virtual CValue*               Copy() = 0;
318         
319         
320         STR_String                              op2str(VALUE_OPERATOR op);
321         /** enable/disable display of deprecation warnings */
322         static void                     SetDeprecationWarnings(bool ignoreDeprecationWarnings);
323         /** Shows a deprecation warning */
324         static void                     ShowDeprecationWarning(const char* method,const char* prop);
325                 
326         // setting / getting flags
327         inline void                     SetSelected(bool bSelected)                                                             { m_ValFlags.Selected = bSelected; }
328         virtual void            SetModified(bool bModified)                                                             { m_ValFlags.Modified = bModified; }
329         virtual void            SetAffected(bool bAffected=true)                                                { m_ValFlags.Affected = bAffected; }
330         inline void                     SetReleaseRequested(bool bReleaseRequested)                             { m_ValFlags.ReleaseRequested=bReleaseRequested; }
331         inline void                     SetError(bool err)                                                                              { m_ValFlags.Error=err; }
332         inline void                     SetVisible (bool vis)                                                                   { m_ValFlags.Visible=vis; }
333                                                                                                                                                                 
334         virtual bool            IsModified()                                                                                    { return m_ValFlags.Modified; }
335         inline bool                     IsError()                                                                                               { return m_ValFlags.Error; }
336         virtual bool            IsAffected()                                                                                    { return m_ValFlags.Affected || m_ValFlags.Modified; }
337         virtual bool            IsSelected()                                                                                    { return m_ValFlags.Selected; }
338         inline bool                     IsReleaseRequested()                                                                    { return m_ValFlags.ReleaseRequested; }
339         virtual bool            IsVisible()                                                                                             { return m_ValFlags.Visible;}
340         virtual void            SetCustomFlag1(bool bCustomFlag)                                                { m_ValFlags.CustomFlag1 = bCustomFlag;};
341         virtual bool            IsCustomFlag1()                                                                                 { return m_ValFlags.CustomFlag1;};
342
343         virtual void            SetCustomFlag2(bool bCustomFlag)                                                { m_ValFlags.CustomFlag2 = bCustomFlag;};
344         virtual bool            IsCustomFlag2()                                                                                 { return m_ValFlags.CustomFlag2;};
345                                                                                                                                                                 
346 protected:                                                                                                                                              
347         virtual void            DisableRefCount();                                                                              // Disable reference counting for this value
348         virtual void            AddDataToReplica(CValue* replica);                                              
349         virtual                         ~CValue();
350 private:
351         // Member variables                                                                                                                     
352         std::map<STR_String,CValue*>*           m_pNamedPropertyArray;                                                                  // Properties for user/game etc
353         ValueFlags                      m_ValFlags;                                                                                             // Frequently used flags in a bitfield (low memoryusage)
354         int                                     m_refcount;                                                                                             // Reference Counter    
355         static  double m_sZeroVec[3];   
356         static bool                     m_ignore_deprecation_warnings;
357
358 };
359
360
361
362 //
363 // Declare a CValue or CExpression or CWhatever to be serialized by the editor.
364 //
365 // This macro introduces the EdSerialize() function (which must be implemented by
366 // the client) and the EdIdSerialize() function (which is implemented by this macro).
367 //
368 // The generated Copy() function returns a pointer to <root_base_class_name> type
369 // of object. So, for *any* CValue-derived object this should be set to CValue,
370 // for *any* CExpression-derived object this should be set to CExpression.
371 //
372 #define PLUGIN_DECLARE_SERIAL(class_name, root_base_class_name)                                                                                 \
373 public:                                                                                                                                                                                                 \
374         virtual root_base_class_name *  Copy()                                  { return new class_name; }                                      \
375         virtual bool EdSerialize(CompressorArchive& arch,class CFactoryManager* facmgr,bool bIsStoring);    \
376         virtual bool EdIdSerialize(CompressorArchive& arch,class CFactoryManager* facmgr,bool bIsStoring)       \
377 {                                                                                                                                                                                                               \
378         if (bIsStoring)                                                                                                                                                                         \
379                 arch.StoreString(#class_name);                                                                                                                                  \
380                                                                                                                                                                                                                 \
381         return false;                                                                                                                                                                           \
382 }                                                                                                                                                                                                               \
383         
384
385 ////////////////////////////////////////////////////////////////////////////////
386 ////////////////////////////////////////////////////////////////////////////////
387 ////////////////////////////////////////////////////////////////////////////////
388
389 // CPropValue is a CValue derived class, that implements the identification (String name)
390 // SetName() / GetName(), 
391 // normal classes should derive from CPropValue, real lightweight classes straight from CValue
392
393
394 class CPropValue : public CValue
395 {
396 public:
397
398 #ifndef NO_EXP_PYTHON_EMBEDDING 
399         CPropValue(PyTypeObject* T=&Type) :
400           CValue(T),
401 #else
402         CPropValue() :
403 #endif //NO_EXP_PYTHON_EMBEDDING
404                 m_pstrNewName(NULL)
405
406         {
407         }
408         
409         virtual ~CPropValue()
410         {
411                 if (m_pstrNewName)
412                 {
413                         delete m_pstrNewName;
414                         m_pstrNewName = NULL;
415                 }
416         }
417         
418         virtual void                    SetName(STR_String name) {
419                 if (m_pstrNewName)
420                 {
421                         delete m_pstrNewName;
422                         m_pstrNewName = NULL;   
423                 }
424                 if (name.Length())
425                         m_pstrNewName = new STR_String(name);
426         }
427         virtual void                    ReplicaSetName(STR_String name) {
428                 m_pstrNewName=NULL;
429                 if (name.Length())
430                         m_pstrNewName = new STR_String(name);
431         }
432         
433         virtual STR_String                      GetName() {
434                 //STR_String namefromprop = GetPropertyText("Name");
435                 //if (namefromprop.Length() > 0)
436                 //      return namefromprop;
437                 
438                 if (m_pstrNewName)
439                 {
440                         return *m_pstrNewName;
441                 }
442                 return STR_String("");
443         };                                              // name of Value
444         
445 protected:
446         STR_String*                                     m_pstrNewName;                              // Identification
447 };
448
449 #endif // !defined _VALUEBASECLASS_H
450