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