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