Sign in to follow this  
Little Coding Fox

Creating a reference object without modifying base class - Possible?

Recommended Posts

I'm working on some AngelScript bindings for a framework I'm working on, and I have created a generic reference countable class that works fine with AngelScript, but I have some problems when I want to interface the properties of the contained class with the scripts.

 

To give you an idea, let's say I have a class Texture. I have something like this in my Reference class:

 

class ScriptReferenceTexture
{
public:
	SuperSmartPointer<Texture> Object;

	uint32 ReferenceCount;

	ScriptReferenceTexture() : ReferenceCount(1) {};

	void AddReference()
	{
		ReferenceCount++;
	};

	void RemoveReference()
	{
		ReferenceCount--;

		if(ReferenceCount == 0)
		{
			delete this;
		};
	};

	static ScriptReferenceTexture *Factory()
	{
		return new ScriptReferenceTexture();
	};
};

So this works fine for containing the Texture object, but let's say I want to connect a regular Texture to a ScriptTexture stored in another script class, or want to add methods that access the Texture's own methods. I can't do that since the reference counting bit isn't in the Texture class, and I'm using a smart pointer to store the texture class so modifying the Texture class is not going to work.

 

I really don't want to change my resource handling scheme since SuperSmartPointers are too important for the native side (since my framework should be used both with and without scripting), and they also allow for the implicit destruction of their contained object (which goes against the AngelScript reference style).

 

So does anyone have any idea of how I could solve this?

Share this post


Link to post
Share on other sites

I found a way to solve this. Simply create a new class that inherits from ScriptReferenceTexture and add the new methods there, and if I need to use properties I simply create the Get/Set methods as described by the AS documentation.

Share this post


Link to post
Share on other sites
AgentC    2352

I know you said you don't want to modify your existing resource handling, but if you made your smart pointer mechanism intrusively reference-counted, ie. Texture would have its own AddRef() and RemoveRef() functions, then using a smart pointer and an AngelScript handle would be essentially equivalent, and there would be no need for the Reference class at all.

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