Sign in to follow this  
Jason Goepel

Reference Objects as Properties

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

"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?

Edited by BlackMoons

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this