blender/source/gameengine/Expressions/ListValue.cpp

696 lines
15 KiB
C++
Raw Normal View History

/** \file gameengine/Expressions/ListValue.cpp
* \ingroup expressions
*/
2002-10-12 11:37:38 +00:00
// ListValue.cpp: implementation of the CListValue class.
//
//////////////////////////////////////////////////////////////////////
/*
* 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.
*
*/
#include <stdio.h>
2002-10-12 11:37:38 +00:00
#include "ListValue.h"
#include "StringValue.h"
#include "VoidValue.h"
#include <algorithm>
BGE performance, 4th round: logic This commit extends the technique of dynamic linked list to the logic system to eliminate as much as possible temporaries, map lookup or full scan. The logic engine is now free of memory allocation, which is an important stability factor. The overhead of the logic system is reduced by a factor between 3 and 6 depending on the logic setup. This is the speed-up you can expect on a logic setup using simple bricks. Heavy bricks like python controllers and ray sensors will still take about the same time to execute so the speed up will be less important. The core of the logic engine has been much reworked but the functionality is still the same except for one thing: the priority system on the execution of controllers. The exact same remark applies to actuators but I'll explain for controllers only: Previously, it was possible, with the "executePriority" attribute to set a controller to run before any other controllers in the game. Other than that, the sequential execution of controllers, as defined in Blender was guaranteed by default. With the new system, the sequential execution of controllers is still guaranteed but only within the controllers of one object. the user can no longer set a controller to run before any other controllers in the game. The "executePriority" attribute controls the execution of controllers within one object. The priority is a small number starting from 0 for the first controller and incrementing for each controller. If this missing feature is a must, a special method can be implemented to set a controller to run before all other controllers. Other improvements: - Systematic use of reference in parameter passing to avoid unnecessary data copy - Use pre increment in iterator instead of post increment to avoid temporary allocation - Use const char* instead of STR_String whenever possible to avoid temporary allocation - Fix reference counting bugs (memory leak) - Fix a crash in certain cases of state switching and object deletion - Minor speed up in property sensor - Removal of objects during the game is a lot faster
2009-05-10 20:53:58 +00:00
#include "BoolValue.h"
2002-10-12 11:37:38 +00:00
#include "BLI_sys_types.h" /* for intptr_t support */
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CListValue::CListValue()
: CPropValue()
{
m_bReleaseContents=true;
}
CListValue::~CListValue()
{
if (m_bReleaseContents) {
for (unsigned int i=0;i<m_pValueArray.size();i++) {
m_pValueArray[i]->Release();
}
}
}
static STR_String gstrListRep=STR_String("List");
const STR_String & CListValue::GetText()
{
gstrListRep = "[";
STR_String commastr = "";
for (int i=0;i<GetCount();i++)
{
gstrListRep += commastr;
gstrListRep += GetValue(i)->GetText();
commastr = ",";
}
gstrListRep += "]";
return gstrListRep;
}
CValue* CListValue::GetReplica()
{
CListValue* replica = new CListValue(*this);
replica->ProcessReplica();
replica->m_bReleaseContents=true; // for copy, complete array is copied for now...
// copy all values
int numelements = m_pValueArray.size();
unsigned int i=0;
replica->m_pValueArray.resize(numelements);
for (i=0;i<m_pValueArray.size();i++)
replica->m_pValueArray[i] = m_pValueArray[i]->GetReplica();
return replica;
};
void CListValue::SetValue(int i, CValue *val)
{
assertd(i < m_pValueArray.size());
m_pValueArray[i]=val;
}
void CListValue::Resize(int num)
{
m_pValueArray.resize(num);
}
void CListValue::Remove(int i)
{
assertd(i<m_pValueArray.size());
m_pValueArray.erase(m_pValueArray.begin()+i);
}
void CListValue::ReleaseAndRemoveAll()
{
for (unsigned int i=0;i<m_pValueArray.size();i++)
m_pValueArray[i]->Release();
m_pValueArray.clear();//.Clear();
}
CValue* CListValue::FindValue(const STR_String &name)
{
for (int i=0; i < GetCount(); i++)
if (GetValue(i)->GetName() == name)
return GetValue(i);
return NULL;
}
CValue* CListValue::FindValue(const char *name)
{
for (int i=0; i < GetCount(); i++)
if (GetValue(i)->GetName() == name)
return GetValue(i);
return NULL;
}
bool CListValue::SearchValue(CValue *val)
{
for (int i=0;i<GetCount();i++)
if (val == GetValue(i))
return true;
return false;
}
void CListValue::SetReleaseOnDestruct(bool bReleaseContents)
{
m_bReleaseContents = bReleaseContents;
}
bool CListValue::RemoveValue(CValue *val)
{
bool result=false;
for (int i=GetCount()-1;i>=0;i--)
if (val == GetValue(i))
{
Remove(i);
result=true;
}
return result;
}
void CListValue::MergeList(CListValue *otherlist)
{
int numelements = this->GetCount();
int numotherelements = otherlist->GetCount();
Resize(numelements+numotherelements);
for (int i=0;i<numotherelements;i++)
{
SetValue(i+numelements,otherlist->GetValue(i)->AddRef());
}
}
bool CListValue::CheckEqual(CValue* first,CValue* second)
{
bool result = false;
CValue* eqval = ((CValue*)first)->Calc(VALUE_EQL_OPERATOR,(CValue*)second);
if (eqval==NULL)
return false;
const STR_String& text = eqval->GetText();
if (&text==&CBoolValue::sTrueString)
{
result = true;
}
eqval->Release();
return result;
}
/* ---------------------------------------------------------------------
* Some stuff taken from the header
* --------------------------------------------------------------------- */
CValue* CListValue::Calc(VALUE_OPERATOR op,CValue *val)
{
//assert(false); // todo: implement me!
static int error_printed = 0;
if (error_printed==0) {
fprintf(stderr, "CValueList::Calc not yet implemented\n");
error_printed = 1;
}
return NULL;
}
CValue* CListValue::CalcFinal(VALUE_DATA_TYPE dtype,
VALUE_OPERATOR op,
CValue* val)
{
//assert(false); // todo: implement me!
static int error_printed = 0;
if (error_printed==0) {
fprintf(stderr, "CValueList::CalcFinal not yet implemented\n");
error_printed = 1;
}
return NULL;
}
void CListValue::Add(CValue* value)
{
m_pValueArray.push_back(value);
}
double CListValue::GetNumber()
{
return -1;
}
int CListValue::GetValueType()
{
return VALUE_LIST_TYPE;
}
void CListValue::SetModified(bool bModified)
{
CValue::SetModified(bModified);
int numels = GetCount();
for (int i=0;i<numels;i++)
GetValue(i)->SetModified(bModified);
}
bool CListValue::IsModified()
{
bool bmod = CValue::IsModified(); //normal own flag
int numels = GetCount();
for (int i=0;i<numels;i++)
bmod = bmod || GetValue(i)->IsModified();
return bmod;
}
#ifdef WITH_PYTHON
/* --------------------------------------------------------------------- */
/* Python interface ---------------------------------------------------- */
/* --------------------------------------------------------------------- */
2012-09-16 04:58:18 +00:00
static Py_ssize_t listvalue_bufferlen(PyObject *self)
2002-10-12 11:37:38 +00:00
{
CListValue *list= static_cast<CListValue *>(BGE_PROXY_REF(self));
if (list==NULL)
return 0;
return (Py_ssize_t)list->GetCount();
2002-10-12 11:37:38 +00:00
}
2012-09-16 04:58:18 +00:00
static PyObject *listvalue_buffer_item(PyObject *self, Py_ssize_t index)
2002-10-12 11:37:38 +00:00
{
CListValue *list= static_cast<CListValue *>(BGE_PROXY_REF(self));
CValue *cval;
if (list==NULL) {
2013-04-04 23:16:23 +00:00
PyErr_SetString(PyExc_SystemError, "val = CList[i], " BGE_PROXY_ERROR_MSG);
return NULL;
}
int count = list->GetCount();
if (index < 0)
index = count+index;
if (index < 0 || index >= count) {
PyErr_SetString(PyExc_IndexError, "CList[i]: Python ListIndex out of range in CValueList");
return NULL;
2002-10-12 11:37:38 +00:00
}
cval= list->GetValue(index);
2012-09-16 04:58:18 +00:00
PyObject *pyobj = cval->ConvertValueToPython();
if (pyobj)
return pyobj;
else
return cval->GetProxy();
2002-10-12 11:37:38 +00:00
}
/* just slice it into a python list... */
static PyObject *listvalue_buffer_slice(CListValue *list, Py_ssize_t start, Py_ssize_t stop)
{
PyObject *newlist;
Py_ssize_t i, j;
/* caller needs to validate negative index */
#if 0
Py_ssize_t len = list->GetCount();
if (start > len) start = len;
if (stop > len) stop = len;
#endif
newlist = PyList_New(stop - start);
if (!newlist)
return NULL;
for (i = start, j = 0; i < stop; i++, j++) {
PyObject *pyobj = list->GetValue(i)->ConvertValueToPython();
if (!pyobj) {
pyobj = list->GetValue(i)->GetProxy();
}
PyList_SET_ITEM(newlist, j, pyobj);
}
return newlist;
}
static PyObject *listvalue_mapping_subscript(PyObject *self, PyObject *key)
{
CListValue *list= static_cast<CListValue *>(BGE_PROXY_REF(self));
if (list==NULL) {
2013-04-04 23:16:23 +00:00
PyErr_SetString(PyExc_SystemError, "value = CList[i], " BGE_PROXY_ERROR_MSG);
return NULL;
}
if (PyUnicode_Check(key)) {
CValue *item = ((CListValue*) list)->FindValue(_PyUnicode_AsString(key));
if (item) {
2012-09-16 04:58:18 +00:00
PyObject *pyobj = item->ConvertValueToPython();
if (pyobj)
return pyobj;
else
return item->GetProxy();
}
}
else if (PyIndex_Check(key)) {
Py_ssize_t index = PyLong_AsSsize_t(key);
return listvalue_buffer_item(self, index); /* wont add a ref */
2004-06-07 11:03:12 +00:00
}
else if (PySlice_Check(key)) {
Py_ssize_t start, stop, step, slicelength;
if (PySlice_GetIndicesEx(key, list->GetCount(), &start, &stop, &step, &slicelength) < 0)
return NULL;
2002-10-12 11:37:38 +00:00
if (slicelength <= 0) {
return PyList_New(0);
}
else if (step == 1) {
return listvalue_buffer_slice(list, start, stop);
}
else {
PyErr_SetString(PyExc_TypeError, "CList[slice]: slice steps not supported");
return NULL;
}
}
2002-10-12 11:37:38 +00:00
PyErr_Format(PyExc_KeyError,
"CList[key]: '%R' key not in list", key);
return NULL;
2002-10-12 11:37:38 +00:00
}
/* clist + list, return a list that python owns */
2012-09-16 04:58:18 +00:00
static PyObject *listvalue_buffer_concat(PyObject *self, PyObject *other)
2002-10-12 11:37:38 +00:00
{
CListValue *listval= static_cast<CListValue *>(BGE_PROXY_REF(self));
Py_ssize_t i, numitems, numitems_orig;
if (listval==NULL) {
2013-04-04 23:16:23 +00:00
PyErr_SetString(PyExc_SystemError, "CList+other, " BGE_PROXY_ERROR_MSG);
return NULL;
}
numitems_orig= listval->GetCount();
2002-10-12 11:37:38 +00:00
// for now, we support CListValue concatenated with items
// and CListValue concatenated to Python Lists
// and CListValue concatenated with another CListValue
2012-03-18 07:38:51 +00:00
/* Shallow copy, don't use listval->GetReplica(), it will screw up with KX_GameObjects */
CListValue* listval_new = new CListValue();
if (PyList_Check(other))
2002-10-12 11:37:38 +00:00
{
CValue* listitemval;
2002-10-12 11:37:38 +00:00
bool error = false;
numitems = PyList_GET_SIZE(other);
/* copy the first part of the list */
listval_new->Resize(numitems_orig + numitems);
for (i=0;i<numitems_orig;i++)
listval_new->SetValue(i, listval->GetValue(i)->AddRef());
2002-10-12 11:37:38 +00:00
for (i=0;i<numitems;i++)
{
listitemval = listval->ConvertPythonToValue(PyList_GetItem(other,i), true, "cList + pyList: CListValue, ");
if (listitemval) {
listval_new->SetValue(i+numitems_orig, listitemval);
} else {
error= true;
break;
2002-10-12 11:37:38 +00:00
}
}
if (error) {
2012-03-18 07:38:51 +00:00
listval_new->Resize(numitems_orig+i); /* resize so we don't try release NULL pointers */
listval_new->Release();
return NULL; /* ConvertPythonToValue above sets the error */
}
}
else if (PyObject_TypeCheck(other, &CListValue::Type)) {
// add items from otherlist to this list
CListValue* otherval = static_cast<CListValue *>(BGE_PROXY_REF(other));
if (otherval==NULL) {
listval_new->Release();
2013-04-04 23:16:23 +00:00
PyErr_SetString(PyExc_SystemError, "CList+other, " BGE_PROXY_ERROR_MSG);
return NULL;
2002-10-12 11:37:38 +00:00
}
numitems = otherval->GetCount();
/* copy the first part of the list */
2012-03-18 07:38:51 +00:00
listval_new->Resize(numitems_orig + numitems); /* resize so we don't try release NULL pointers */
for (i=0;i<numitems_orig;i++)
listval_new->SetValue(i, listval->GetValue(i)->AddRef());
/* now copy the other part of the list */
for (i=0;i<numitems;i++)
listval_new->SetValue(i+numitems_orig, otherval->GetValue(i)->AddRef());
2002-10-12 11:37:38 +00:00
}
return listval_new->NewProxy(true); /* python owns this list */
2002-10-12 11:37:38 +00:00
}
static int listvalue_buffer_contains(PyObject *self_v, PyObject *value)
{
2012-10-22 08:15:51 +00:00
CListValue *self = static_cast<CListValue *>(BGE_PROXY_REF(self_v));
2012-10-22 08:15:51 +00:00
if (self == NULL) {
2013-04-04 23:16:23 +00:00
PyErr_SetString(PyExc_SystemError, "val in CList, " BGE_PROXY_ERROR_MSG);
return -1;
}
if (PyUnicode_Check(value)) {
if (self->FindValue((const char *)_PyUnicode_AsString(value))) {
return 1;
}
}
else if (PyObject_TypeCheck(value, &CValue::Type)) { /* not dict like at all but this worked before __contains__ was used */
CValue *item= static_cast<CValue *>(BGE_PROXY_REF(value));
for (int i=0; i < self->GetCount(); i++)
if (self->GetValue(i) == item) // Com
return 1;
} // not using CheckEqual
return 0;
}
2002-10-12 11:37:38 +00:00
static PySequenceMethods listvalue_as_sequence = {
listvalue_bufferlen,//(inquiry)buffer_length, /*sq_length*/
listvalue_buffer_concat, /*sq_concat*/
2011-09-25 12:31:21 +00:00
NULL, /*sq_repeat*/
listvalue_buffer_item, /*sq_item*/
2009-08-10 00:07:34 +00:00
// TODO, slicing in py3
NULL, // listvalue_buffer_slice, /*sq_slice*/
2011-09-25 12:31:21 +00:00
NULL, /*sq_ass_item*/
NULL, /*sq_ass_slice*/
(objobjproc)listvalue_buffer_contains, /* sq_contains */
(binaryfunc) NULL, /* sq_inplace_concat */
(ssizeargfunc) NULL, /* sq_inplace_repeat */
2002-10-12 11:37:38 +00:00
};
/* Is this one used ? */
static PyMappingMethods instance_as_mapping = {
listvalue_bufferlen, /*mp_length*/
listvalue_mapping_subscript, /*mp_subscript*/
NULL /*mp_ass_subscript*/
2002-10-12 11:37:38 +00:00
};
PyTypeObject CListValue::Type = {
PyVarObject_HEAD_INIT(NULL, 0)
2002-10-12 11:37:38 +00:00
"CListValue", /*tp_name*/
sizeof(PyObjectPlus_Proxy), /*tp_basicsize*/
2002-10-12 11:37:38 +00:00
0, /*tp_itemsize*/
/* methods */
2011-09-25 12:31:21 +00:00
py_base_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
2011-09-25 12:31:21 +00:00
0, /*tp_compare*/
py_base_repr, /*tp_repr*/
2002-10-12 11:37:38 +00:00
0, /*tp_as_number*/
&listvalue_as_sequence, /*tp_as_sequence*/
2004-06-07 11:03:12 +00:00
&instance_as_mapping, /*tp_as_mapping*/
2002-10-12 11:37:38 +00:00
0, /*tp_hash*/
0, /*tp_call */
0,
NULL,
NULL,
0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
0,0,0,0,0,0,0,
Methods,
0,
0,
&CValue::Type,
0,0,0,0,0,0,
py_base_new
2002-10-12 11:37:38 +00:00
};
PyMethodDef CListValue::Methods[] = {
/* List style access */
{"append", (PyCFunction)CListValue::sPyappend,METH_O},
{"reverse", (PyCFunction)CListValue::sPyreverse,METH_NOARGS},
{"index", (PyCFunction)CListValue::sPyindex,METH_O},
{"count", (PyCFunction)CListValue::sPycount,METH_O},
/* Dict style access */
{"get", (PyCFunction)CListValue::sPyget,METH_VARARGS},
/* Own cvalue funcs */
{"from_id", (PyCFunction)CListValue::sPyfrom_id,METH_O},
2002-10-12 11:37:38 +00:00
{NULL,NULL} //Sentinel
};
PyAttributeDef CListValue::Attributes[] = {
{ NULL } //Sentinel
};
2002-10-12 11:37:38 +00:00
2012-09-16 04:58:18 +00:00
PyObject *CListValue::Pyappend(PyObject *value)
2002-10-12 11:37:38 +00:00
{
CValue *objval = ConvertPythonToValue(value, true, "CList.append(i): CValueList, ");
if (!objval) /* ConvertPythonToValue sets the error */
return NULL;
if (!BGE_PROXY_PYOWNS(m_proxy)) {
PyErr_SetString(PyExc_TypeError, "CList.append(i): this CValueList is used internally for the game engine and can't be modified");
return NULL;
}
Add(objval);
Py_RETURN_NONE;
2002-10-12 11:37:38 +00:00
}
2012-09-16 04:58:18 +00:00
PyObject *CListValue::Pyreverse()
2002-10-12 11:37:38 +00:00
{
std::reverse(m_pValueArray.begin(),m_pValueArray.end());
Py_RETURN_NONE;
2002-10-12 11:37:38 +00:00
}
2012-09-16 04:58:18 +00:00
PyObject *CListValue::Pyindex(PyObject *value)
2002-10-12 11:37:38 +00:00
{
2012-09-16 04:58:18 +00:00
PyObject *result = NULL;
2002-10-12 11:37:38 +00:00
CValue *checkobj = ConvertPythonToValue(value, true, "val = cList[i]: CValueList, ");
if (checkobj==NULL)
return NULL; /* ConvertPythonToValue sets the error */
int numelem = GetCount();
for (int i=0;i<numelem;i++)
2002-10-12 11:37:38 +00:00
{
CValue* elem = GetValue(i);
if (checkobj==elem || CheckEqual(checkobj,elem))
2002-10-12 11:37:38 +00:00
{
result = PyLong_FromLong(i);
break;
2002-10-12 11:37:38 +00:00
}
}
checkobj->Release();
2002-10-12 11:37:38 +00:00
if (result==NULL) {
PyErr_SetString(PyExc_ValueError, "CList.index(x): x not in CListValue");
}
2002-10-12 11:37:38 +00:00
return result;
2002-10-12 11:37:38 +00:00
}
2012-09-16 04:58:18 +00:00
PyObject *CListValue::Pycount(PyObject *value)
2002-10-12 11:37:38 +00:00
{
int numfound = 0;
CValue *checkobj = ConvertPythonToValue(value, false, ""); /* error ignored */
if (checkobj==NULL) { /* in this case just return that there are no items in the list */
PyErr_Clear();
return PyLong_FromLong(0);
}
int numelem = GetCount();
for (int i=0;i<numelem;i++)
2002-10-12 11:37:38 +00:00
{
CValue* elem = GetValue(i);
if (checkobj==elem || CheckEqual(checkobj,elem))
2002-10-12 11:37:38 +00:00
{
numfound ++;
2002-10-12 11:37:38 +00:00
}
}
checkobj->Release();
2002-10-12 11:37:38 +00:00
return PyLong_FromLong(numfound);
2002-10-12 11:37:38 +00:00
}
/* Matches python dict.get(key, [default]) */
2012-09-16 04:58:18 +00:00
PyObject *CListValue::Pyget(PyObject *args)
{
char *key;
2012-09-16 04:58:18 +00:00
PyObject *def = Py_None;
if (!PyArg_ParseTuple(args, "s|O:get", &key, &def))
return NULL;
CValue *item = FindValue((const char *)key);
if (item) {
2012-09-16 04:58:18 +00:00
PyObject *pyobj = item->ConvertValueToPython();
if (pyobj)
return pyobj;
else
return item->GetProxy();
}
Py_INCREF(def);
return def;
}
2002-10-12 11:37:38 +00:00
2012-09-16 04:58:18 +00:00
PyObject *CListValue::Pyfrom_id(PyObject *value)
{
2009-04-19 17:26:03 +00:00
uintptr_t id= (uintptr_t)PyLong_AsVoidPtr(value);
if (PyErr_Occurred())
return NULL;
int numelem = GetCount();
for (int i=0;i<numelem;i++)
{
2009-04-19 17:26:03 +00:00
if (reinterpret_cast<uintptr_t>(m_pValueArray[i]->m_proxy) == id)
return GetValue(i)->GetProxy();
}
PyErr_SetString(PyExc_IndexError, "from_id(#): id not found in CValueList");
2002-10-12 11:37:38 +00:00
return NULL;
}
#endif // WITH_PYTHON