using System;
using System.Runtime.InteropServices;
namespace SFML
{
////////////////////////////////////////////////////////////
///
/// The ObjectBase class is an abstract base for every
/// SFML object. It's meant for internal use only
///
////////////////////////////////////////////////////////////
public abstract class ObjectBase : IDisposable
{
////////////////////////////////////////////////////////////
///
/// Construct the object from a pointer to the C library object
///
/// Internal pointer to the object in the C libraries
////////////////////////////////////////////////////////////
public ObjectBase(IntPtr thisPtr)
{
myThis = thisPtr;
}
////////////////////////////////////////////////////////////
///
/// Dispose the object
///
////////////////////////////////////////////////////////////
~ObjectBase()
{
Dispose(false);
}
////////////////////////////////////////////////////////////
///
/// Access to the internal pointer of the object.
/// For internal use only
///
////////////////////////////////////////////////////////////
public IntPtr This
{
get {return myThis;}
}
////////////////////////////////////////////////////////////
///
/// Explicitely dispose the object
///
////////////////////////////////////////////////////////////
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
////////////////////////////////////////////////////////////
///
/// Destroy the object
///
/// Is the GC disposing the object, or is it an explicit call ?
////////////////////////////////////////////////////////////
private void Dispose(bool disposing)
{
if (myThis != IntPtr.Zero)
{
Destroy(disposing);
myThis = IntPtr.Zero;
}
}
////////////////////////////////////////////////////////////
///
/// Destroy the object (implementation is left to each derived class)
///
/// Is the GC disposing the object, or is it an explicit call ?
////////////////////////////////////////////////////////////
protected abstract void Destroy(bool disposing);
////////////////////////////////////////////////////////////
///
/// Set the pointer to the internal object. For internal use only
///
/// Pointer to the internal object in C library
////////////////////////////////////////////////////////////
protected void SetThis(IntPtr thisPtr)
{
myThis = thisPtr;
}
private IntPtr myThis = IntPtr.Zero;
}
}