Misc. RAII API

Minor issues with the Ogre API that can be trivial to fix
cadabra
Gnoblar
Posts: 22
Joined: Thu Sep 23, 2010 8:32 am
Location: sfbay
x 3

Misc. RAII API

Post by cadabra »

What the title says. E.g. for HardwareBuffer locking:

Code: Select all

{
    scoped_lock... lock = buf.lock(...);
    // do stuff with lock.data (or lock.pixelBox for HardwarePixelBuffer, etc)
}
Instead of today's:

Code: Select all

{
    void *data = buf.lock(...);
    try{
        // do stuff with data
    }
    catch(...){
        buf.unlock()
        throw;
    }
    buf.unlock();
}
It would be nice to have RAII API for SceneManager's createFoo/destroyFoo, SceneNode's attachObject/detachObject, and Node's addChild/removeChild, but that would be quite a big API change, and looking at this thread http://www.ogre3d.org/forums/viewtopic.php?f=3&t=41039, I suppose there's a lot to hammer out first.
CABAListic
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2903
Joined: Thu Jan 18, 2007 2:48 pm
x 58

Re: Misc. RAII API

Post by CABAListic »

I agree with the proposed change (or addition) to Buffer locking.

Not so much with createFoo/destroyFoo, though. Technically, due to the way they are managed by Ogre, they are already protected not to leave memory leaks, since Ogre will clean them up on shutdown. Also, often you don't need to explicitly delete them.
If you do, however, and you want it to be done by RAII, it's already possible. Boost's shared_ptr, for instance, provides the means to pass it a different destruction method, so you could wrap an Ogre pointer in it quite fine.
User avatar
Eugene
OGRE Team Member
OGRE Team Member
Posts: 185
Joined: Mon Mar 24, 2008 4:54 pm
Location: Kraków, Poland
x 41

Re: Misc. RAII API

Post by Eugene »

There are such guards in OgreProgressiveMesh.h

Code: Select all


template <typename T> struct HardwareBufferLockGuard
	{
		HardwareBufferLockGuard(const T& p, HardwareBuffer::LockOptions options)
		: pBuf(p)
		{
			pData = pBuf->lock(options);
		}
		HardwareBufferLockGuard(const T& p, size_t offset, size_t length, HardwareBuffer::LockOptions options)
		: pBuf(p)
		{
			pData = pBuf->lock(offset, length, options);
		}		
		~HardwareBufferLockGuard()
		{
			pBuf->unlock();
		}
		const T& pBuf;
		void* pData;
	};
	
	typedef HardwareBufferLockGuard<HardwareVertexBufferSharedPtr> VertexBufferLockGuard;
	typedef HardwareBufferLockGuard<HardwareIndexBufferSharedPtr> IndexBufferLockGuard;
Probably, they needs to be moved into OgreHardwareBuffer.h, and this solves the issue.