Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Value.h

Go to the documentation of this file.
00001 #ifdef WIN32
00002 #pragma warning (disable:4786)
00003 #endif //WIN32
00004 
00005 // Value.h: interface for the CValue class.
00006 /*
00007  * Copyright (c) 1996-2000 Erwin Coumans <[email protected]>

00008  *
00009  * Permission to use, copy, modify, distribute and sell this software

00010  * and its documentation for any purpose is hereby granted without fee,

00011  * provided that the above copyright notice appear in all copies and

00012  * that both that copyright notice and this permission notice appear

00013  * in supporting documentation.  Erwin Coumans makes no

00014  * representations about the suitability of this software for any

00015  * purpose.  It is provided "as is" without express or implied warranty.

00016  *
00017  */
00018 
00041 #ifndef __VALUE_H__
00042 #define __VALUE_H__
00043 
00044 #include <map>          // array functionality for the propertylist
00045 #include "StdString.h"  // CCString class
00046 
00047 
00048 
00049 
00050 #ifndef GEN_NO_ASSERT
00051 #undef  assert
00052 #define assert(exp)                     ((void)NULL)
00053 #endif
00054 
00055 
00056 #ifndef GEN_NO_TRACE
00057 #undef  trace
00058 #define trace(exp)                      ((void)NULL)
00059 #endif
00060 
00061 #ifndef GEN_NO_DEBUG
00062 #undef  debug
00063 #define debug(exp)                      ((void)NULL)
00064 #endif
00065 
00066 
00067 
00068 
00069 #ifndef GEN_NO_ASSERTD
00070 #undef  assertd
00071 #define assertd(exp)                    ((void)NULL)
00072 #endif
00073 
00074 
00075 #ifndef USE_PRAGMA_ONCE
00076 #ifdef WIN32
00077         #pragma once
00078 
00079 #endif //WIN32
00080 #endif
00081 
00082 #define EDITOR_LEVEL_VERSION 0x06
00083 
00084 enum VALUE_OPERATOR {
00085         
00086         VALUE_ADD_OPERATOR,                     // +
00087         VALUE_SUB_OPERATOR,                     // -
00088         VALUE_MUL_OPERATOR,                     // *
00089         VALUE_DIV_OPERATOR,                     // /
00090         VALUE_NEG_OPERATOR,                     // -
00091         VALUE_AND_OPERATOR,                     // &&
00092         VALUE_OR_OPERATOR,                      // ||
00093         VALUE_EQL_OPERATOR,                     // ==
00094         VALUE_NEQ_OPERATOR,                     // !=
00095         VALUE_GRE_OPERATOR,                     // >
00096         VALUE_LES_OPERATOR,                     // <
00097         VALUE_GEQ_OPERATOR,                     // >=
00098         VALUE_LEQ_OPERATOR,                     // <=
00099         VALUE_NOT_OPERATOR,                     // !
00100         VALUE_NO_OPERATOR                       // no operation at all
00101 };
00102 
00103 enum VALUE_DATA_TYPE {
00104         VALUE_NO_TYPE,                          // abstract baseclass
00105         VALUE_INT_TYPE,
00106         VALUE_FLOAT_TYPE,
00107         VALUE_STRING_TYPE,
00108         VALUE_BOOL_TYPE,
00109         VALUE_ERROR_TYPE,
00110         VALUE_EMPTY_TYPE,
00111         VALUE_SOLID_TYPE,
00112         VALUE_COMBISOLID_TYPE,
00113         VALUE_VECTOR_TYPE,
00114         VALUE_MENU_TYPE,
00115         VALUE_ACTOR_TYPE,
00116         VALUE_MAX_TYPE                          //only here to provide number of types
00117 };
00118 
00119 
00120 
00121 #ifdef _DEBUG
00122 //extern int gRefCountValue;            // debugonly variable to check if all CValue Refences are Dereferenced at programexit
00123 #endif
00124 
00125 struct HashableInt 
00126 {
00127         HashableInt(int id)                                                                                                                     : mData(id) { }
00128 
00129         unsigned long                           Hash() const                                                                                    { return 0;} 
00130         
00131         bool                            operator==(HashableInt rhs)                                                             { return mData == rhs.mData; }
00132         
00133         int                                     mData;
00134 };
00135 
00136 
00137 //
00138 // Bitfield that stores the flags for each CValue derived class
00139 //
00140 struct ValueFlags {
00141         ValueFlags() :
00142                 Modified(true),
00143                 Selected(false),
00144                 Affected(false),
00145                 ReleaseRequested(false),
00146                 Error(false),
00147                 RefCountDisabled(false),
00148                 HasProperties(false),
00149                 HasName(false),
00150                 Visible(true),
00151                 CustomFlag1(false),
00152                 CustomFlag2(false)
00153         {
00154         }
00155 
00156         unsigned short Modified : 1;
00157         unsigned short Selected : 1;
00158         unsigned short Affected : 1;
00159         unsigned short ReleaseRequested : 1;
00160         unsigned short Error : 1;
00161         unsigned short RefCountDisabled : 1;
00162         unsigned short HasProperties : 1;
00163         unsigned short HasName : 1;
00164         unsigned short Visible : 1;
00165         unsigned short CustomFlag1 : 1;
00166         unsigned short CustomFlag2 : 1;
00167 
00168         
00169 };
00170 
00174 class CAction
00175 {
00176 public:
00177         CAction() {
00178         };
00179         virtual ~CAction(){
00180         };
00181         virtual void Execute() const =0;
00182 };
00183 
00184 //
00185 // CValue
00186 //
00187 // Base class for all editor functionality, flexible object type that allows
00188 // calculations and uses reference counting for memory management.
00189 // 
00190 //
00191 
00196 #define KX_PYMETHOD(class_name, method_name)                    \

00197         PyObject* Py##method_name(PyObject* self, PyObject* args, PyObject* kwds); \

00198         static PyObject* sPy##method_name( PyObject* self, PyObject* args, PyObject* kwds) { \

00199                 return ((class_name*) self)->Py##method_name(self, args, kwds);         \

00200         }; \

00201 
00202 #define KX_PYMETHOD_DOC(class_name, method_name)                        \

00203         PyObject* Py##method_name(PyObject* self, PyObject* args, PyObject* kwds); \

00204         static PyObject* sPy##method_name( PyObject* self, PyObject* args, PyObject* kwds) { \

00205                 return ((class_name*) self)->Py##method_name(self, args, kwds);         \

00206         }; \

00207     static char method_name##_doc[]; \

00208 
00209 /* The line above should remain empty */
00210 
00211 #ifndef NO_EXP_PYTHON_EMBEDDING
00212 #include "PyObjectPlus.h"
00213 #include "object.h"
00214 class CValue  : public PyObjectPlus
00215 #else
00216 class CValue  
00217 #endif //NO_EXP_PYTHON_EMBEDDING
00218 
00219 
00220 {
00221 #ifndef NO_EXP_PYTHON_EMBEDDING
00222 Py_Header;
00223 #endif //NO_EXP_PYTHON_EMBEDDING
00224 public:
00225         enum AllocationTYPE {
00226                 STACKVALUE              = 0,
00227                 HEAPVALUE               = 1
00228         };
00229         
00230         enum DrawTYPE {
00231                 STARTFRAME              = 0,
00232                 ENDFRAME                = 1,
00233                 INTERFRAME              = 2
00234         };
00235 
00236 
00237         // Construction / Destruction
00238 #ifndef NO_EXP_PYTHON_EMBEDDING
00239 
00240         CValue(PyTypeObject *T = &Type);
00241         static PyObject*        PyMake(PyObject*,PyObject*);
00242         virtual PyObject *_repr(void)

00243         {
00244                 return Py_BuildValue("s",(const char*)GetText());
00245         }
00246 
00247 
00248 
00249         PyObject*                       _getattr(char* attr);
00250 
00251         void    SpecialRelease()

00252         {
00253                 int i=0;
00254                 if (ob_refcnt == 0)
00255                 {
00256                         _Py_NewReference(this);
00257                         
00258                 } else
00259                 {
00260                         i++;
00261                 }
00262                 Release();
00263         }
00264         static void PyDestructor(PyObject *P)                           // python wrapper
00265         {
00266           ((CValue*)P)->SpecialRelease();
00267         };
00268 
00269         virtual PyObject*       ConvertValueToPython() {
00270                 return NULL;
00271         }
00272 
00273         virtual CValue* ConvertPythonToValue(PyObject* pyobj);
00274 
00275 
00276         int                                     _setattr(char* attr,PyObject* value);
00277         void PrintHello() {
00278           printf("Hello\n");
00279         }
00280         PyObject*                       PyPrintHello(PyObject* args)

00281         {
00282                 PrintHello();
00283                 Py_Return;
00284         }
00285         static PyObject*        sPyPrintHello(PyObject* self,PyObject* args,PyObject* kwds)

00286         {
00287                 return ((CValue*)self)->PyPrintHello(args);
00288         }
00289         
00290         KX_PYMETHOD(CValue,GetName);
00291 
00292 #else
00293         CValue();
00294 #endif //NO_EXP_PYTHON_EMBEDDING
00295 
00296         
00297         
00298         // Expression Calculation
00299         virtual CValue*         Calc(VALUE_OPERATOR op, CValue *val) = 0;
00300         virtual CValue*         CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val) = 0;
00301         virtual void            SetOwnerExpression(class CExpression* expr)                             { }
00302 
00303         
00304 
00305         void                            Execute(const CAction& a)

00306         {
00307                 a.Execute();
00308         };
00309 
00311         int                                     GetRefCount()                                                                                   { return m_refcount; }
00312         virtual CValue*         AddRef();                                                                                               // Add a reference to this value
00313         virtual int                     Release();                                                                                              // Release a reference to this value (when reference count reaches 0, the value is removed from the heap)
00314 
00316         virtual void            SetProperty(const CCString& name,CValue* ioProperty);                                           // Set property <ioProperty>, overwrites and releases a previous property with the same name if needed
00317         virtual CValue*         GetProperty(const CCString & inName);                                                   // Get pointer to a property with name <inName>, returns NULL if there is no property named <inName>
00318         CCString                        GetPropertyText(const CCString & inName,const CCString& deftext="");                                            // Get text description of property with name <inName>, returns an empty string if there is no property named <inName>
00319         float                           GetPropertyNumber(const CCString& inName,float defnumber);
00320         virtual bool            RemoveProperty(const CCString & 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
00321         virtual void            ClearProperties();                                                                              // Clear all properties
00322 
00323         virtual void            SetPropertiesModified(bool inModified);                                 // Set all properties' modified flag to <inModified>
00324         virtual bool            IsAnyPropertyModified();                                                                // Check if any of the properties in this value have been modified
00325 
00326         virtual CValue*         GetProperty(int inIndex);                                                               // Get property number <inIndex>
00327         virtual int                     GetPropertyCount();                                                                             // Get the amount of properties assiocated with this value
00328 
00329         virtual void            CloneProperties(CValue* replica);
00330         virtual CValue*         FindIdentifier(const CCString& identifiername);
00331         virtual void            SetColorOperator(VALUE_OPERATOR op)                                             { } // Set the wireframe color of this value depending on the CSG operator type <op>
00332 
00333         virtual const CCString &        GetText() = 0;
00334         virtual float           GetNumber() = 0;
00335         double*                         ZeroVector() { return m_sZeroVec; };
00336         virtual double*         GetVector3(bool bGetTransformedVec = false);
00337 
00338         virtual CCString        GetName() = 0;                                                                                  // Retrieve the name of the value
00339         virtual void            SetName(CCString name) = 0;                                                             // Set the name of the value
00340         virtual void            ReplicaSetName(CCString name) = 0;
00341         virtual void            SetValue(CValue* newval)                                                                { assertd(newval->GetNumber() == 10121969);     } // no one should get here
00342         virtual CValue*         GetReplica() =0;
00343         //virtual CValue*               Copy() = 0;
00344         
00345         
00346         CCString                                op2str(VALUE_OPERATOR op);
00347                 
00348         // setting / getting flags
00349         inline void                     SetSelected(bool bSelected)                                                             { m_ValFlags.Selected = bSelected; }
00350         virtual void            SetModified(bool bModified)                                                             { m_ValFlags.Modified = bModified; }
00351         virtual void            SetAffected(bool bAffected=true)                                                { m_ValFlags.Affected = bAffected; }
00352         inline void                     SetReleaseRequested(bool bReleaseRequested)                             { m_ValFlags.ReleaseRequested=bReleaseRequested; }
00353         inline void                     SetError(bool err)                                                                              { m_ValFlags.Error=err; }
00354         inline void                     SetVisible (bool vis)                                                                   { m_ValFlags.Visible=vis; }
00355                                                                                                                                                                 
00356         virtual bool            IsModified()                                                                                    { return m_ValFlags.Modified; }
00357         inline bool                     IsError()                                                                                               { return m_ValFlags.Error; }
00358         virtual bool            IsAffected()                                                                                    { return m_ValFlags.Affected || m_ValFlags.Modified; }
00359         virtual bool            IsSelected()                                                                                    { return m_ValFlags.Selected; }
00360         inline bool                     IsReleaseRequested()                                                                    { return m_ValFlags.ReleaseRequested; }
00361         virtual bool            IsVisible()                                                                                             { return m_ValFlags.Visible;}
00362         virtual void            SetCustomFlag1(bool bCustomFlag)                                                { m_ValFlags.CustomFlag1 = bCustomFlag;};
00363         virtual bool            IsCustomFlag1()                                                                                 { return m_ValFlags.CustomFlag1;};
00364 
00365         virtual void            SetCustomFlag2(bool bCustomFlag)                                                { m_ValFlags.CustomFlag2 = bCustomFlag;};
00366         virtual bool            IsCustomFlag2()                                                                                 { return m_ValFlags.CustomFlag2;};
00367                                                                                                                                                                 
00368 protected:                                                                                                                                              
00369         virtual void            DisableRefCount();                                                                              // Disable reference counting for this value
00370         virtual void            AddDataToReplica(CValue* replica);                                              
00371         virtual                         ~CValue();
00372 private:                                                                                                                                                
00373         // Member variables                                                                                                                     
00374         std::map<const CCString,CValue*>*               m_pNamedPropertyArray;                                                                  // Properties for user/game etc
00375         ValueFlags                      m_ValFlags;                                                                                             // Frequently used flags in a bitfield (low memoryusage)
00376         int                                     m_refcount;                                                                                             // Reference Counter    
00377         static  double m_sZeroVec[3];   
00378         
00379 };
00380 
00381 
00382 
00383 //
00384 // Declare a CValue or CExpression or CWhatever to be serialized by the editor.
00385 //
00386 // This macro introduces the EdSerialize() function (which must be implemented by
00387 // the client) and the EdIdSerialize() function (which is implemented by this macro).
00388 //
00389 // The generated Copy() function returns a pointer to <root_base_class_name> type
00390 // of object. So, for *any* CValue-derived object this should be set to CValue,
00391 // for *any* CExpression-derived object this should be set to CExpression.
00392 //
00393 #define PLUGIN_DECLARE_SERIAL(class_name, root_base_class_name)                                                                                 \

00394 public:                                                                                                                                                                                                 \

00395         virtual root_base_class_name *  Copy()                                  { return new class_name; }                                      \

00396         virtual bool EdSerialize(CompressorArchive& arch,class CFactoryManager* facmgr,bool bIsStoring);    \

00397         virtual bool EdIdSerialize(CompressorArchive& arch,class CFactoryManager* facmgr,bool bIsStoring)       \

00398 {                                                                                                                                                                                                               \

00399         if (bIsStoring)                                                                                                                                                                         \

00400                 arch.StoreString(#class_name);                                                                                                                                  \

00401                                                                                                                                                                                                                 \

00402         return false;                                                                                                                                                                           \

00403 }                                                                                                                                                                                                               \

00404         
00405 
00409 
00410 // CPropValue is a CValue derived class, that implements the identification (String name)
00411 // SetName() / GetName(), 
00412 // normal classes should derive from CPropValue, real lightweight classes straight from CValue
00413 
00414 
00415 class CPropValue : public CValue
00416 {
00417 public:
00418 
00419 #ifndef NO_EXP_PYTHON_EMBEDDING 
00420         CPropValue(PyTypeObject* T=&Type) :
00421           CValue(T),
00422 #else
00423         CPropValue() :
00424 #endif //NO_EXP_PYTHON_EMBEDDING
00425                 m_pstrNewName(NULL)
00426 
00427         {
00428         }
00429         
00430         virtual ~CPropValue()

00431         {
00432                 if (m_pstrNewName)
00433                 {
00434                         delete m_pstrNewName;
00435                         m_pstrNewName = NULL;
00436                 }
00437         }
00438         
00439         virtual void                    SetName(CCString name) {
00440                 if (m_pstrNewName)
00441                 {
00442                         delete m_pstrNewName;
00443                         m_pstrNewName = NULL;   
00444                 }
00445                 if (name.Length())
00446                         m_pstrNewName = new CCString(name);
00447         }
00448         virtual void                    ReplicaSetName(CCString name) {
00449                 m_pstrNewName=NULL;
00450                 if (name.Length())
00451                         m_pstrNewName = new CCString(name);
00452         }
00453         
00454         virtual CCString                        GetName() {
00455                 //CCString namefromprop = GetPropertyText("Name");
00456                 //if (namefromprop.Length() > 0)
00457                 //      return namefromprop;
00458                 
00459                 if (m_pstrNewName)
00460                 {
00461                         return *m_pstrNewName;
00462                 }
00463                 return CCString("");
00464         };                                              // name of Value
00465         
00466 protected:
00467         CCString*                                       m_pstrNewName;                              // Identification
00468 };
00469 
00470 
00471 
00472 #endif // !defined _VALUEBASECLASS_H

Generated at Thu Feb 1 13:03:13 2001 for Ketsji Game Engine by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000