Click or drag to resize

Delegates

A delegate is a user-defined reference type, similar to a class. However while a class represents a collection of data, a delegate keeps track of one or more methods. As with classes, a delegate type must be declared before you can use it to create variables and objects of the delegate type.

gradient

 

The delegate type declaration is very similar to a method declaration - it has both a return type and a signature, specifying  the methods that the delegate will accept, however it doesn't have an implementation block.

An instance of a particular delegate type can be used as a reference to any method that has the particular signature defined by the delegate.

The following example declares a delegate type.

 

public delegate short DelegateComputeFn(void);

The delegate type declaration differs from a method declaration in two ways:

 

After a delegate type is declared, you can declare variables and create objects of the type:

MyDel delVar;

 

To create and use a delegate in SIMPL# you would:

  1. Declare a new delegate type with a particular signature and return type.

  2. Declare a delegate variable of the new delegate type.

  3. Create an object of the delegate type, and assign it to the delegate variable. The new delegate object will includes a reference to a method with the same signature as defined in step 1.

  4. Invoke the newly created delegate, just as you would invoke a method. When you call the delegate, the method it contains is executed.

 

Delegates in SIMPL# translate to function pointers in SIMPL+.

To create a delegate in SIMPL#:

  1. Define a signature for a function. For example:

    public delegate short DelegateComputeFn();

  2. Create a property of that delegate type:

    public DelegateComputeFn ComputeFn {get;set;}

  3. Assign a SIMPL+ function with a matching signature to the property, created in step 2.

    RegisterDelegate (myClass, ComputeFn, ComputeCallbackFn);

    Now a function within your SIMPL+ module can be called from SIMPL#, for example the SIMPL#’s ComputeFn will be able to call ComputeCallbackFn within the SIMPL+ module.

 

Use UnregisterDelegate if at some point you need to clear the delegate within the SIMPL+ module. This will stop SIMPL# from calling the previously registered function in your SIMPL+ module.

 

NOTE: Delegate functions are synchronous calls!

 

The following code is an example of how to define a delegate within SIMPL# and create and call a callback function in SIMPL+:

 

SIMPL#:

C#
	namespace SIMPLSharpLibrary1
	{
		// Define the delegate:
		public delegate short DelegateComputeFn();

		public delegate SimplSharpString DelegateFn(uint id);

		//Create a property in a class:

		public class DelegateTest
		{

			//non-static property
			public DelegateComputeFn ComputeFn { get; set; }

			//static property
			static public DelegateFn StaticFn { get; set; }

			public short Compute()
			{

				short ret = 0;

				//Call the mapped function in SIMPL+
				//* remember to check for null in case *
				//*   the function was never mapped!   *
				if (ComputeFn != null)
					ret = ComputeFn();

				return ret;
			}
		}
	}

 

SIMPL+:

C++
	DelegateTest delTest;
	function Init()
	{
		// register non-static property
		RegisterDelegate (delTest, ComputeFn, ComputeCallbackFn);

		// register static property 
		RegisterDelegate (DelegateTest,StaticFn, StaticCallbackFn);
	}
	function Close()
	{
		//unregister non-static property
		UnregisterDelegate (delTest, ComputeFn);

		//unregister static property
		UnregisterDelegate (DelegateTest, StaticFn);
	}
	callback signed_integer_function ComputeCallbackFn()
	{
		return (0);
	}
	callback simplsharpstring_function StaticCallbackFn(long_integer id)
	{
		return ("");
	}

Delegate function arguments

Remember that the SIMPL+ callback function must match the SIMPL# delegate’s signature exactly.In other words, the callback function’s return type and all of its arguments must be the same data type as the type defined within SIMPL#. If any argument type or return type differ, the callback will not be matched and will not be called during runtime.

The table below shows how to map SIMPL# data types to SIMPL+ data types:

 

SIMPL# Datatype

SIMPL+ Datatype

(Argument)

SIMPL+ Datatype

(Function)

string

STRING

STRING_FUNCTION

SimplSharpString

STRING

STRING_FUNCTION

int

SIGNED_LONG_INTEGER

SIGNED_LONG_INTEGER_FUNCTION

uint

LONG_INTEGER

LONG_INTEGER_FUNCTION

short

SIGNED_INTEGER

SIGNED_INTEGER_FUNCTION

ushort

INTEGER

INTEGER_FUNCTION

<user_class>

<user_class>

 

 

footer
Crestron Blue 400 x 100

Copyright (C) 2013 to the present, Crestron Electronics, Inc. All rights reserved. No part of this work may be reproduced in any form, machine or natural, without the express written consent of Crestron Electronics.