Delegates provide the underlying mechanism for events in the NGWS Component Model.
Delegates are implemented as abstract classes within the NGWS frameworks library. There are two types of delegates: single cast and multicast. The single-cast delegate (implemented by System::Delegate) enables binding a function pointer to only one method. The multicast delegate (implemented by System::MultiCastDelegate) may bind a function pointer to a single method or many methods. Delegates differ from C++ function pointers in that they may only point to members of managed classes and may bind to an object to support calling methods of that type of instance.
Note For simplicity, the term "delegate" will refer to both the single-cast and multicast versions. If only one of these classes is intended, it will be explicitly stated.
Under Managed Extensions for C++, delegates are supported by the __delegate keyword. The __delegate keyword defines a single-cast delegate type with a specific method signature. A variant of the __delegate keyword, __delegate(multicast), declares a multicast delegate type with a specific method signature.
The single-cast delegate only binds the delegate to one method. The following example declares a single-cast delegate GetDayOfWeek
.
__delegate int GetDayOfWeek();
Single-cast delegate objects are implemented by the NGWS frameworks base class Delegate.
Another version of a delegate is the multicast delegate. This delegate is able to bind to multiple methods. Multicast delegates are declared by using a variant of the __delegate keyword, __delegate(multicast). The following example declares a multicast delegate DayOfWeekChanged
.
__delegate(multicast) void DayOfWeekChanged();
Multicast delegate objects are implemented by the NGWS frameworks base class MultiCastDelegate.
Note Multicast delegates are not permitted to return a value or out parameters. This limitation is due to the properties of return values and out parameters being supplied by one callee and a multicast delegate having many callees.
Although the syntax for the __delegate keyword is similar to the syntax of the standard C++ typedef keyword, the compiler actually generates a definition of a managed class. The managed class has the following characteristics:
Invoke
whose signature matches the declared signature of the delegate. This method makes the actual call to the bound method to all delegates (clients).The following example declares a single-cast delegate and illustrates that the delegate is actually implemented by the NGWS frameworks base class Delegate:
__delegate int GetDayOfWeek(); __gc class MyCalendar { public: int MyGetDayOfWeek() { //Function implementation } }; void UsesDelegate () { MyCalendar* pCal = new MyCalendar; GetDayOfWeek* pGetDayOfWeek = new GetDayOfWeek(pCal, &MyCalendar::MyGetDayOfWeek); int dayOfWeek = pGetDayOfWeek->Invoke(); }
Managed Extensions support a special case for a delegate that uses a single argument constructor where the syntax can be automatically constructed to include both the object reference and method. The previous statement, creating a GetDayOfWeek
delegate, could be written as:
GetDayOfWeek* pGetDayOfWeek = new GetDayOfWeek(pcal->MyGetDayOfWeek);