Reference Objects as Properties

Started by
5 comments, last by Jason Goepel 10 years, 6 months ago

What is the best way to expose a Reference Type as an Object Property?


class TypeA  // asOBJ_REF
{
};

class TypeB // asOBJ_REF
{
    TypeA m_A;  // ... RegisterObjectProperty("TypeB", "TypeA A", asOFFSET(TypeB, m_A), ...
};

class TypeC // asOBJ_REF | asOBJ_GC
{
    TypeA* m_pA;  // ... RegisterObjectProperty("TypeC", "TypeA@ A", asOFFSET(TypeC, m_pA), ...
};


If a script gets a handle to TypeB::m_A and the TypeB object goes out of scope, it seems like m_A would be prematurely destroyed. How does one inform the garbage collector to not destroy the TypeB object until all references to it's member m_A have been released?

The TypeC scenario looks more straight forward, but there is no garuntee that m_pA points to a valid object since the script could assign to it an empty handle.

Thanks for the help.

Advertisement

TypeB is complicated. TypeA is in this case used as a value type in C++ even though it is a reference type. Unless you implement the control to make sure TypeB stays alive as long as there are references to the member you'd do best not to expose this to the scripts at all. Instead I suggest that you use property accessors to allow the script to manipulate the TypeA member without directly accessing it.

For TypeC you're correct to use @. If you want to prevent that the pointer is reassigned, then ideally AngelScript should allow the member as &, but that is currently not supported. For now you'll need to use property accessors to make sure the script cannot reassign the handle. In this case you just need to expose a get accessor that returns the handle to the member.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

I'm not sure I entirely understand when the asOBJ_GC flag is needed. Does the simple situation in my original post justify the asOBJ_GC flag? Would the asOBJ_GC flag only be necessary if TypeA could reference TypeC?

asOBJ_GC is necessary if the object can form circular references that will not be resolved by the application.

The classes TypeA, TypeB, and TypeC in your example can't form any circular references,

If TypeA could hold a pointer to TypeC too, then a circular reference could be formed between TypeA and TypeC. If the application doesn't keep track of these and resolve them itself then the asOBJ_GC flag and related behaviours are necessary to avoid memory leaks.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

"Unless you implement the control to make sure TypeB stays alive as long as there are references to the member"

I wonder how one might do that.

maybe have all children add/subtract there refcount above 1 from there parent object? I had a few other ideas but this seems the simplest:


class RefCount
{
RefCount * mParent;
int mCount;
RefCount(){mCount = 1; mParent = NULL;}
Add()
{
mCount++; 
if (mParent) // We know count is above 1 or the object should of been deleted already, Add is not called for the initial 1 mCount value.
  {
  mParent->Add();
  }
}
Release()
{
if (mParent && (mCount > 1))
  {
  mParent->Release()
  }
mCount--;
if (mCount == 0) delete this;
}
};

Of course, One has to manualy set the mParent of each child 'RefType as a Value type' member in the constructor of the refTypes(your TypeB) object.

While I guess one could have RefCount derive from your existing ref counting class and make Add/Release virtual, I suspect the virtual call overhead would cost more then checking mParent and mCount again every release/addition. And one additional pointer per ref object is not a huge deal in most cases.

You would then have..


class TypeA : public RefCount // asOBJ_REF
{
};

class TypeB : public RefCount // asOBJ_REF
{
TypeA m_A; // ... RegisterObjectProperty("TypeB", "TypeA A", asOFFSET(TypeB, m_A), ...
TypeB()
  {
  m_A.mParent = this;
  }
};

Thoughts? Any problems with this solution?

Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com

On a subnote, the below Release() might preform a little better although might not be as clear.


Release()
{
mCount--;
if (mCount > 0)
  {
  if (mParent)
    mParent->Release()
  }
else 
  delete this;
}

Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com

BlackMoons:

In principle that looks like it should work. I chose to go with my "TypeC" implementation and property accessors, because the underlying C++ code would better mirror the use of the object in a script. Your solution would avoid the extra function call involved in accessing the property, which in some scenarios would be valuable. Thank you for your input.

This topic is closed to new replies.

Advertisement