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