1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4  
     5  /**
     6   * Mtasc compileable Delegate class.
     7   */
     8  class org.aswing.utils.Delegate{
     9  
    10  
    11  	
    12  	private var func:Function;
    13  	/**
    14  	 *Creates a functions wrapper for the original function so that it runs 
    15  	 *in the provided context.
    16  	 *@param obj Context in which to run the function.
    17  	 *@param func Function to run. you can add custom parameters after this to be the 
    18  	 *additional parameters when called the created function.
    19  	 */
    20  	static function create(obj:Object, func:Function):Function
    21  	{
    22  		
    23  		  var params:Array = new Array();
    24  		  var count:Number=2;
    25  		  while(count<arguments.length){
    26  		  	params[ count-2] = arguments[count];
    27  		  	count++;
    28  		  }
    29  		  
    30  		
    31  		var f:Function = function()
    32  		{
    33  			var target:Object = arguments.callee.target;
    34  			var func0:Function = arguments.callee.func;
    35              var parameters:Array = arguments.concat(params);
    36  			return func0.apply(target, parameters);
    37  		};
    38  
    39  		f.target = obj;
    40  		f.func = func;
    41  
    42  		return f;
    43  	}
    44  
    45  	function Delegate(f:Function)
    46  	{
    47  		func = f;
    48  	}
    49  
    50  	
    51  
    52  	public function createDelegate(obj:Object):Function
    53  	{
    54  		return create(obj, func);
    55  	}	
    56  }
    57