Blender V2.61 - r43446

Value.h

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