Creating a reference object without modifying base class - Possible?

Started by
1 comment, last by AgentC 11 years ago

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?

Advertisement

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.

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.

This topic is closed to new replies.

Advertisement