blender/source/gameengine/Expressions/ListValue.h
Campbell Barton 8d2cb5bea4 BGE Python API
This changes how the BGE classes and Python work together, which hasnt changed since blender went opensource.
The main difference is PyObjectPlus - the base class for most game engine classes, no longer inherit from PyObject, and cannot be cast to a PyObject.

This has the advantage that the BGE does not have to keep 2 reference counts valid for C++ and Python.

Previously C++ classes would never be freed while python held a reference, however this reference could be problematic eg: a GameObject that isnt in a scene anymore should not be used by python, doing so could even crash blender in some cases.

Instead PyObjectPlus has a member "PyObject *m_proxy" which is lazily initialized when python needs it. m_proxy reference counts are managed by python, though it should never be freed while the C++ class exists since it holds a reference to avoid making and freeing it all the time.
When the C++ class is free'd it sets the m_proxy reference to NULL, If python accesses this variable it will raise a RuntimeError, (check the isValid attribute to see if its valid without raising an error).
- This replaces the m_zombie bool and IsZombie() tests added recently.

In python return values that used to be..
 return value->AddRef();
Are now
 return value->GetProxy();
or...
 return value->NewProxy(true); // true means python owns this C++ value which will be deleted when the PyObject is freed
2009-04-19 12:46:39 +00:00

87 lines
2.4 KiB
C++

/*
* ListValue.h: interface for the CListValue class.
* $Id$
* Copyright (c) 1996-2000 Erwin Coumans <coockie@acm.org>
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Erwin Coumans makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*/
#if !defined _LISTVALUE_H
#define _LISTVALUE_H
#include "Value.h"
class CListValue : public CPropValue
{
Py_Header;
//PLUGIN_DECLARE_SERIAL (CListValue,CValue)
public:
CListValue(PyTypeObject *T = &Type);
virtual ~CListValue();
void AddConfigurationData(CValue* menuvalue);
void Configure(CValue* menuvalue);
void Add(CValue* value);
/** @attention not implemented yet :( */
virtual CValue* Calc(VALUE_OPERATOR op,CValue *val);
virtual CValue* CalcFinal(VALUE_DATA_TYPE dtype,
VALUE_OPERATOR op,
CValue* val);
virtual double GetNumber();
virtual CValue* GetReplica();
public:
void MergeList(CListValue* otherlist);
bool RemoveValue(CValue* val);
void SetReleaseOnDestruct(bool bReleaseContents);
bool SearchValue(CValue* val);
CValue* FindValue(const STR_String & name);
void ReleaseAndRemoveAll();
virtual void SetModified(bool bModified);
virtual inline bool IsModified();
void Remove(int i);
void Resize(int num);
void SetValue(int i,CValue* val);
CValue* GetValue(int i){ assertd(i < m_pValueArray.size()); return m_pValueArray[i];}
int GetCount() { return m_pValueArray.size();};
virtual const STR_String & GetText();
bool CheckEqual(CValue* first,CValue* second);
virtual PyObject* py_getattro(PyObject* attr);
virtual PyObject* py_repr(void) {
PyObject *py_proxy= this->GetProxy();
PyObject *py_list= PySequence_List(py_proxy);
PyObject *py_string= PyObject_Repr(py_list);
Py_DECREF(py_list);
Py_DECREF(py_proxy);
return py_string;
}
KX_PYMETHOD_O(CListValue,append);
KX_PYMETHOD_NOARGS(CListValue,reverse);
KX_PYMETHOD_O(CListValue,index);
KX_PYMETHOD_O(CListValue,count);
KX_PYMETHOD_O(CListValue,from_id);
private:
std::vector<CValue*> m_pValueArray;
bool m_bReleaseContents;
};
#endif // !defined _LISTVALUE_H