here is what i mean
void *operator[](const T &key)
{
if (values.count(key)) // if exists, just return
return values[key];
else // if not, create
{
if (subTypeId & asTYPEID_OBJHANDLE) // handle
{
// equivalent to this, assertion at asASSERT( other.objType->DerivesFrom(objType) );
// values[key] = malloc(sizeof(size_t));
if (subTypeId & asTYPEID_OBJHANDLE) // handle
{
// equivalent to this, assertion at asASSERT( other.objType->DerivesFrom(objType) );
// values[key] = malloc(sizeof(size_t));
return values[key];
}
The values[key] needs to be populated with a null pointer, and then the address of that null pointer should be returned. I'm not too sure about what your code does, but I'd guess something like this would work:
if (subTypeId & asTYPEID_OBJHANDLE) // handle
{
// Allocate space to hold the pointer (handle)
values[key] = malloc(sizeof(void*));
// Clear the value of the pointer
*(void**)values[key] = 0;
return values[key];
}
The script also has a problem:
m[0] = t;
This script would do a value assignment, i.e. call the objects opAssign method. Since you're returning a null handle, it would result in a script exception.
To do a handle assignment, you need to prefix the left hand expression with @, i.e.
oh ok. so much time wasted trying to solve a nonexisting bug.
single @ causes so much grief.
i always think m[0] is already an handle, with @m[0] i am dereferencing it, and getting the value.
but it is the opposite.
in here we are returning an empty handle to the script, then this empty handle points to an actual ref counted object.
now c++ has an handle of the object without calling AddRef.
if this is so;
how should i even call AddRef on this object?
is VM wise enough to know it is dealing with an empty handle and increase its ref in our stead?
By the way, could overloading of the unary prefix * operator be added? It would allow for a more natural syntax of stl-like iterators exposed to the scripts.
The unary * operator is currently not part of the AngelScript syntax. Introducing the operator just to provide overloading for it doesn't feel like a good solution. It would probably just confuse the script writer.