Blender  V2.59
Value.h
Go to the documentation of this file.
00001 /*
00002  * Value.h: interface for the CValue class.
00003  * $Id: Value.h 35063 2011-02-22 10:33:14Z jesterking $
00004  * Copyright (c) 1996-2000 Erwin Coumans <coockie@acm.org>
00005  *
00006  * Permission to use, copy, modify, distribute and sell this software
00007  * and its documentation for any purpose is hereby granted without fee,
00008  * provided that the above copyright notice appear in all copies and
00009  * that both that copyright notice and this permission notice appear
00010  * in supporting documentation.  Erwin Coumans makes no
00011  * representations about the suitability of this software for any
00012  * purpose.  It is provided "as is" without express or implied warranty.
00013  *
00014  */
00015 
00020 #if defined(WIN32) && !defined(FREE_WINDOWS)
00021 #pragma warning (disable:4786)
00022 #endif //WIN32
00023 
00024 #ifndef __VALUE_H__
00025 #define __VALUE_H__
00026 
00027 #include <map>          // array functionality for the propertylist
00028 #include "STR_String.h" // STR_String class
00029 
00030 #ifdef WITH_CXX_GUARDEDALLOC
00031 #include "MEM_guardedalloc.h"
00032 #endif
00033 
00034 #ifndef GEN_NO_ASSERT
00035 #undef  assert
00036 #define assert(exp)                     ((void)NULL)
00037 #endif
00038 
00039 
00040 #ifndef GEN_NO_TRACE
00041 #undef  trace
00042 #define trace(exp)                      ((void)NULL)
00043 #endif
00044 
00045 #ifndef GEN_NO_DEBUG
00046 #undef  debug
00047 #define debug(exp)                      ((void)NULL)
00048 #endif
00049 
00050 #ifndef GEN_NO_ASSERTD
00051 #undef  assertd
00052 #define assertd(exp)                    ((void)NULL)
00053 #endif
00054 
00055 
00056 #ifndef USE_PRAGMA_ONCE
00057 #ifdef WIN32
00058         #pragma once
00059 
00060 #endif //WIN32
00061 #endif
00062 
00063 enum VALUE_OPERATOR {
00064         
00065         VALUE_MOD_OPERATOR,                     // %
00066         VALUE_ADD_OPERATOR,                     // +
00067         VALUE_SUB_OPERATOR,                     // -
00068         VALUE_MUL_OPERATOR,                     // *
00069         VALUE_DIV_OPERATOR,                     // /
00070         VALUE_NEG_OPERATOR,                     // -
00071         VALUE_POS_OPERATOR,                     // +
00072         VALUE_AND_OPERATOR,                     // &&
00073         VALUE_OR_OPERATOR,                      // ||
00074         VALUE_EQL_OPERATOR,                     // ==
00075         VALUE_NEQ_OPERATOR,                     // !=
00076         VALUE_GRE_OPERATOR,                     // >
00077         VALUE_LES_OPERATOR,                     // <
00078         VALUE_GEQ_OPERATOR,                     // >=
00079         VALUE_LEQ_OPERATOR,                     // <=
00080         VALUE_NOT_OPERATOR,                     // !
00081         VALUE_NO_OPERATOR                       // no operation at all
00082 };
00083 
00084 enum VALUE_DATA_TYPE {
00085         VALUE_NO_TYPE,                          // abstract baseclass
00086         VALUE_INT_TYPE,
00087         VALUE_FLOAT_TYPE,
00088         VALUE_STRING_TYPE,
00089         VALUE_BOOL_TYPE,
00090         VALUE_ERROR_TYPE,
00091         VALUE_EMPTY_TYPE,
00092         VALUE_SOLID_TYPE,
00093         VALUE_COMBISOLID_TYPE,
00094         VALUE_VECTOR_TYPE,
00095         VALUE_MENU_TYPE,
00096         VALUE_ACTOR_TYPE,
00097         VALUE_MAX_TYPE                          //only here to provide number of types
00098 };
00099 
00100 
00101 
00102 #ifdef _DEBUG
00103 //extern int gRefCountValue;            // debugonly variable to check if all CValue Refences are Dereferenced at programexit
00104 #endif
00105 
00106 struct HashableInt 
00107 {
00108         HashableInt(int id)                                                                                                                     : mData(id) { }
00109 
00110         unsigned long                           Hash() const                                                                                    { return 0;} 
00111         
00112         bool                            operator==(HashableInt rhs)                                                             { return mData == rhs.mData; }
00113         
00114         int                                     mData;
00115 };
00116 
00117 
00118 //
00119 // Bitfield that stores the flags for each CValue derived class
00120 //
00121 struct ValueFlags {
00122         ValueFlags() :
00123                 Modified(true),
00124                 Selected(false),
00125                 Affected(false),
00126                 ReleaseRequested(false),
00127                 Error(false),
00128                 RefCountDisabled(false),
00129                 HasProperties(false),
00130                 HasName(false),
00131                 Visible(true),
00132                 CustomFlag1(false),
00133                 CustomFlag2(false)
00134         {
00135         }
00136 
00137         unsigned short Modified : 1;
00138         unsigned short Selected : 1;
00139         unsigned short Affected : 1;
00140         unsigned short ReleaseRequested : 1;
00141         unsigned short Error : 1;
00142         unsigned short RefCountDisabled : 1;
00143         unsigned short HasProperties : 1;
00144         unsigned short HasName : 1;
00145         unsigned short Visible : 1;
00146         unsigned short CustomFlag1 : 1;
00147         unsigned short CustomFlag2 : 1;
00148 
00149         
00150 };
00151 
00155 class CAction
00156 {
00157 public:
00158         CAction() {
00159         };
00160         virtual ~CAction(){
00161         };
00162         virtual void Execute() const =0;
00163         
00164         
00165 #ifdef WITH_CXX_GUARDEDALLOC
00166 public:
00167         void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:CAction"); }
00168         void operator delete( void *mem ) { MEM_freeN(mem); }
00169 #endif
00170 };
00171 
00172 
00173 #include "PyObjectPlus.h"
00174 #ifdef WITH_PYTHON
00175 #include "object.h"
00176 #endif
00177 
00201 class CValue  : public PyObjectPlus
00202 
00203 {
00204 Py_Header;
00205 public:
00206         enum AllocationTYPE {
00207                 STACKVALUE              = 0,
00208                 HEAPVALUE               = 1
00209         };
00210         
00211         enum DrawTYPE {
00212                 STARTFRAME              = 0,
00213                 ENDFRAME                = 1,
00214                 INTERFRAME              = 2
00215         };
00216 
00217 
00218         // Construction / Destruction
00219         CValue();
00220 
00221 #ifdef WITH_PYTHON
00222         //static PyObject*      PyMake(PyObject*,PyObject*);
00223         virtual PyObject *py_repr(void)
00224         {
00225                 return PyUnicode_FromString((const char*)GetText());
00226         }
00227 
00228         virtual PyObject*       ConvertValueToPython() {
00229                 return NULL;
00230         }
00231 
00232         virtual CValue* ConvertPythonToValue(PyObject* pyobj, const char *error_prefix);
00233         
00234         static PyObject * pyattr_get_name(void * self, const KX_PYATTRIBUTE_DEF * attrdef);
00235         
00236         virtual PyObject* ConvertKeysToPython( void );
00237 #endif // WITH_PYTHON
00238 
00239         
00240         
00241         // Expression Calculation
00242         virtual CValue*         Calc(VALUE_OPERATOR op, CValue *val) = 0;
00243         virtual CValue*         CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val) = 0;
00244         virtual void            SetOwnerExpression(class CExpression* expr);
00245 
00246         
00247 
00248         void                            Execute(const CAction& a)
00249         {
00250                 a.Execute();
00251         };
00252 
00254         int                                     GetRefCount()                                                                                   
00255         { 
00256                 return m_refcount; 
00257         }
00258 
00259         // Add a reference to this value
00260         CValue*                         AddRef()                                                                                                
00261         {
00262                 // Increase global reference count, used to see at the end of the program
00263                 // if all CValue-derived classes have been dereferenced to 0
00264                 //debug(gRefCountValue++);
00265         #ifdef _DEBUG
00266                 //gRefCountValue++;
00267         #endif
00268                 m_refcount++; 
00269                 return this;
00270         }
00271 
00272         // Release a reference to this value (when reference count reaches 0, the value is removed from the heap)
00273         int                     Release()                                                               
00274         {
00275                 // Decrease global reference count, used to see at the end of the program
00276                 // if all CValue-derived classes have been dereferenced to 0
00277                 //debug(gRefCountValue--);
00278         #ifdef _DEBUG
00279                 //gRefCountValue--;
00280         #endif
00281                 // Decrease local reference count, if it reaches 0 the object should be freed
00282                 if (--m_refcount > 0)
00283                 {
00284                         // Reference count normal, return new reference count
00285                         return m_refcount;
00286                 }
00287                 else
00288                 {
00289                         // Reference count reached 0, delete ourselves and return 0
00290         //              MT_assert(m_refcount==0, "Reference count reached sub-zero, object released too much");
00291                         
00292                         delete this;
00293                         return 0;
00294                 }
00295         }
00296 
00297 
00299         virtual void            SetProperty(const STR_String& name,CValue* ioProperty);                                         // Set property <ioProperty>, overwrites and releases a previous property with the same name if needed
00300         virtual void            SetProperty(const char* name,CValue* ioProperty);
00301         virtual CValue*         GetProperty(const char* inName);                                                        // Get pointer to a property with name <inName>, returns NULL if there is no property named <inName>
00302         virtual CValue*         GetProperty(const STR_String & inName);
00303         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>
00304         float                           GetPropertyNumber(const STR_String& inName,float defnumber);
00305         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
00306         virtual vector<STR_String>      GetPropertyNames();
00307         virtual void            ClearProperties();                                                                              // Clear all properties
00308 
00309         virtual void            SetPropertiesModified(bool inModified);                                 // Set all properties' modified flag to <inModified>
00310         virtual bool            IsAnyPropertyModified();                                                                // Check if any of the properties in this value have been modified
00311 
00312         virtual CValue*         GetProperty(int inIndex);                                                               // Get property number <inIndex>
00313         virtual int                     GetPropertyCount();                                                                             // Get the amount of properties assiocated with this value
00314 
00315         virtual CValue*         FindIdentifier(const STR_String& identifiername);
00319         virtual void            SetColorOperator(VALUE_OPERATOR op);
00320 
00321         virtual const STR_String &      GetText() = 0;
00322         virtual double          GetNumber() = 0;
00323         double*                         ZeroVector() { return m_sZeroVec; };
00324         virtual double*         GetVector3(bool bGetTransformedVec = false);
00325 
00326         virtual STR_String&     GetName() = 0;                                                                                  // Retrieve the name of the value
00327         virtual void            SetName(const char *name) = 0;                                                          // Set the name of the value
00330         virtual void            SetValue(CValue* newval);
00331         virtual CValue*         GetReplica() =0;
00332         virtual void                    ProcessReplica();
00333         //virtual CValue*               Copy() = 0;
00334         
00335         
00336         STR_String                              op2str(VALUE_OPERATOR op);
00337                 
00338         // setting / getting flags
00339         inline void                     SetSelected(bool bSelected)                                                             { m_ValFlags.Selected = bSelected; }
00340         virtual void            SetModified(bool bModified)                                                             { m_ValFlags.Modified = bModified; }
00341         virtual void            SetAffected(bool bAffected=true)                                                { m_ValFlags.Affected = bAffected; }
00342         inline void                     SetReleaseRequested(bool bReleaseRequested)                             { m_ValFlags.ReleaseRequested=bReleaseRequested; }
00343         inline void                     SetError(bool err)                                                                              { m_ValFlags.Error=err; }
00344         inline void                     SetVisible (bool vis)                                                                   { m_ValFlags.Visible=vis; }
00345                                                                                                                                                                 
00346         virtual bool            IsModified()                                                                                    { return m_ValFlags.Modified; }
00347         inline bool                     IsError()                                                                                               { return m_ValFlags.Error; }
00348         virtual bool            IsAffected()                                                                                    { return m_ValFlags.Affected || m_ValFlags.Modified; }
00349         virtual bool            IsSelected()                                                                                    { return m_ValFlags.Selected; }
00350         inline bool                     IsReleaseRequested()                                                                    { return m_ValFlags.ReleaseRequested; }
00351         virtual bool            IsVisible()                                                                                             { return m_ValFlags.Visible;}
00352         virtual void            SetCustomFlag1(bool bCustomFlag)                                                { m_ValFlags.CustomFlag1 = bCustomFlag;};
00353         virtual bool            IsCustomFlag1()                                                                                 { return m_ValFlags.CustomFlag1;};
00354 
00355         virtual void            SetCustomFlag2(bool bCustomFlag)                                                { m_ValFlags.CustomFlag2 = bCustomFlag;};
00356         virtual bool            IsCustomFlag2()                                                                                 { return m_ValFlags.CustomFlag2;};
00357 
00358 protected:                                                                                                                                              
00359         virtual void            DisableRefCount();                                                                              // Disable reference counting for this value
00360         //virtual void          AddDataToReplica(CValue* replica);                                              
00361         virtual                         ~CValue();
00362 private:
00363         // Member variables                                                                                                                     
00364         std::map<STR_String,CValue*>*           m_pNamedPropertyArray;                                                                  // Properties for user/game etc
00365         ValueFlags                      m_ValFlags;                                                                                             // Frequently used flags in a bitfield (low memoryusage)
00366         int                                     m_refcount;                                                                                             // Reference Counter    
00367         static  double m_sZeroVec[3];   
00368 
00369 };
00370 
00371 
00372 
00373 //
00374 // Declare a CValue or CExpression or CWhatever to be serialized by the editor.
00375 //
00376 // This macro introduces the EdSerialize() function (which must be implemented by
00377 // the client) and the EdIdSerialize() function (which is implemented by this macro).
00378 //
00379 // The generated Copy() function returns a pointer to <root_base_class_name> type
00380 // of object. So, for *any* CValue-derived object this should be set to CValue,
00381 // for *any* CExpression-derived object this should be set to CExpression.
00382 //
00383 #define PLUGIN_DECLARE_SERIAL(class_name, root_base_class_name)                                                                                 \
00384 public:                                                                                                                                                                                                 \
00385         virtual root_base_class_name *  Copy()                                  { return new class_name; }                                      \
00386         virtual bool EdSerialize(CompressorArchive& arch,class CFactoryManager* facmgr,bool bIsStoring);    \
00387         virtual bool EdIdSerialize(CompressorArchive& arch,class CFactoryManager* facmgr,bool bIsStoring)       \
00388 {                                                                                                                                                                                                               \
00389         if (bIsStoring)                                                                                                                                                                         \
00390                 arch.StoreString(#class_name);                                                                                                                                  \
00391                                                                                                                                                                                                                 \
00392         return false;                                                                                                                                                                           \
00393 }                                                                                                                                                                                                               \
00394         
00395 
00399 
00400 // CPropValue is a CValue derived class, that implements the identification (String name)
00401 // SetName() / GetName(), 
00402 // normal classes should derive from CPropValue, real lightweight classes straight from CValue
00403 
00404 
00405 class CPropValue : public CValue
00406 {
00407 public:
00408         CPropValue() :
00409           CValue(),
00410                 m_strNewName()
00411 
00412         {
00413         }
00414         
00415         virtual ~CPropValue()
00416         {
00417         }
00418         
00419         virtual void                    SetName(const char *name) {
00420                 m_strNewName = name;
00421         }
00422         
00423         virtual STR_String&                     GetName() {
00424                 //STR_String namefromprop = GetPropertyText("Name");
00425                 //if (namefromprop.Length() > 0)
00426                 //      return namefromprop;
00427                 return m_strNewName;
00428         };                                              // name of Value
00429         
00430 protected:
00431         STR_String                                      m_strNewName;                               // Identification
00432 
00433 
00434 #ifdef WITH_CXX_GUARDEDALLOC
00435 public:
00436         void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:CPropValue"); }
00437         void operator delete( void *mem ) { MEM_freeN(mem); }
00438 #endif
00439 };
00440 
00441 #endif // !defined _VALUEBASECLASS_H
00442