view dstep/objc/bridge/Wrapper.d @ 27:57371c29ef73 default tip

ObjcWrap is now automatically mixed in. Added support for building as a dylib with DMD.
author Jacob Carlborg <doob@me.com>
date Fri, 09 Apr 2010 23:00:22 +0200
parents b9de51448c6b
children
line wrap: on
line source

/**
 * Copyright: Copyright (c) 2009 Jacob Carlborg.
 * Authors: Jacob Carlborg
 * Version: Initial created: Feb 1, 2009
 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
 */
module dstep.objc.bridge.Wrapper;

import dstep.internal.String;
import dstep.objc.bridge.Bridge;
import dstep.objc.bridge.Capsule;
import dstep.objc.message;
import dstep.objc.objc;
import dstep.objc.runtime;

/// Wrapper class for an Objective-C object. 
abstract class ObjcWrapper
{	
	static private Class objcSuperClass_;
	
	private id objcObject_;
	private objc_super* objcSuper_;
	
	this () {}
		
	/// Initializes object from an Objective-C object instance to wrap.
	this (id object)
	{
		objcObject = object;
		dObject = this;
	}
	
	/// Initializes object from another wrapper.
	this (ObjcWrapper wrapper)
	{
		if (wrapper)
			this(wrapper.objcObject);
		
		else
			this(cast(id) null);
	}
	
	~this ()
	{
		Bridge.deregisterObjcInstance(objcObject_);
	}
	
	/// Returns the Objective-C class for this wrapper
	static Class objcClass ()
	{
		return capsuleClass;
	}
	
	/// Returns the Objective-C superclass for this wrapper
	static Class objcSuperClass ()
	{
		if (!objcSuperClass_)
			return objcSuperClass_ = objcClass.getSuperclass;
		
		return objcSuperClass_;
	}
	
	/// Gets the Objective-C object instance pointer. 
	final id objcObject ()
	{
		return objcObject_;
	}
	
	/// Sets the Objective-C object instance pointer. 
	protected final id objcObject (id object)
	{
		return objcObject_ = object;
	}
	
	/// Sets the D object in the receiver's Objective-C instance
	protected final void dObject (Object dObject)
	{
		Bridge.setDObject(dObject, objcObject);
	}
	
	/// Gets the objc_super instance of the receiver
	protected final objc_super* objcSuper ()
	{
		if (!objcSuper_)
		{
			objcSuper_ = new objc_super;
			objcSuper_.receiver = objcObject;
			objcSuper_.cls = objcSuperClass;
		}
		
		return objcSuper_;
	}
}