The usage of __event and __raise keywords in C + + Event processing _c language

Source: Internet
Author: User
Tags uuid

__event
declare the event.
Grammar

   __event 
   method-declarator
   ;
__event __interface Interface-specifier;
__event Member-declarator;

Note
The keyword __event can be applied to a method declaration, an interface declaration, or a data member declaration. However, you cannot qualify a member of a nested class with the __event keyword.
Depending on whether your event source and receiver are native C + +, COM, or managed (. NET Framework), you can use the following constructs as events:

Use __hook in an event receiver to associate a handler method with an event method. Note that after an event is created using the __event keyword, all event handlers that are subsequently hooked to it are invoked when this event is invoked.
The __event method declaration cannot have a definition; the definition is implicitly generated, so you can call the event method as any normal method.
System_caps_note Note
A template class or struct cannot contain events.
Native events
Native events are methods. The return type is usually HRESULT or void, but can be any integral type (including an enum). When an event uses an integer to return a type, an error condition is defined if the event handler returns a value other than 0, in which case the event that is raised invokes the other delegate.

Examples of native C + + events:
__event void OnDblClick ();
__event HRESULT OnClick (int* B, char* s);

For code examples, see event handling in native C + +.
COM Events
COM events are interfaces. The parameters of the method in the event source interface should be in parameters (but this is not mandatory) because out parameters are not useful for multicast. If the out parameter is used, a Level 1 warning is issued.
The return type is usually HRESULT or void, but can be any integral type (including an enum). When an event uses an integer return type and the event handler returns a value other than 0, this is an error condition, and the event that is raised aborts the invocation of the other delegates. Note that the compiler automatically marks an event source interface as the source in the generated IDL.
The __interface keyword is always required after the __event of the COM event source.

Example of a COM event:
__event __interface IEvent1;

For code examples, see event handling in COM.
Managed Events
For information about encoded events in the new syntax, see event (C + + component extensions).
A managed event is a data member or method. When used with events, the return type of the delegate must conform to the common language specification. The return type of the event handler must match the return type of the delegate. For more information about delegates, see __delegate. If a managed event is a data member, its type must be a pointer to a delegate.
In the. NET Framework, you can treat data members as the method itself (that is, the Invoke method that corresponds to the delegate). You must have predefined delegate types that declare managed event data members. Conversely, if the corresponding managed delegate has not been defined, the managed event method implicitly defines it. For example, you can declare an event value, such as OnClick, as the event shown below:

Examples of managed events:
__event clickeventhandler* OnClick;//data member as event
__event void OnClick (St ring* s); method as Event

When you implicitly declare a managed event, you can specify add and remove accessors that will be invoked when you add or remove an event handler. You can also define methods that invoke (raise) events from outside the class.
Example: Native Event

Eventhandling_native_event.cpp
//compile with:/C
[Event_source (Native)]
class Csource {public
:
  __event void MyEvent (int nvalue);

Example: COM Event

Eventhandling_com_event.cpp
//compile with:/C
#define _atl_attributes 1
#include <atlbase.h>
#include <atlcom.h>

[Module (DLL, name= "EventSource", uuid= "6e46b59e-89c3-4c15-a6d8-b8a1cec98830")];

[Dual, UUID ("00000000-0000-0000-0000-000000000002")]
__interface IEventSource {
  [ID (1)] HRESULT myevent ();
};
 [CoClass, uuid ("00000000-0000-0000-0000-000000000003"), event_source (COM)]
Class Csource:public IEventSource {public
:
  __event __interface IEventSource;
  HRESULT FireEvent () {
   __raise myevent ();
   return S_OK;
  }
;

Example: Managed events

Eventhandling_managed_event.cpp
//compile with:/clr:oldsyntax/c
using namespace System;
[Event_source (Managed)]
Public __gc class Cpsource {public

:
  __event void MyEvent (Int16 nvalue);


When you apply an attribute to an event, you can specify whether the attribute applies to the generated method or to the Invoke method of the generated delegate. Default value (event:) Used to apply an attribute to an event.

Eventhandling_managed_event_2.cpp
//compile with:/clr:oldsyntax/c
using namespace System;
[Attribute (all, allowmultiple=true)]
Public __gc class Attr {};

Public __delegate void D ();

Public __gc class X {public
:
  [method:attr] __event d* E;
  [returnvalue:attr] __event void NoE ();



__raise
The call site that emphasizes an event.

__raise Method-declarator;

Note
In managed code, an event can only be raised from the class in which it is defined.
Description
A template class or struct cannot contain events.
Example

 Eventhandlingref_raise.cpp
struct E {
  __event void func1 ();
  void func1 (int) {}

  void Func2 () {}

  void B () {
   __raise func1 ();
   __raise func1 (1); C3745: ' int event::bar (int) ': 
             //Only a Event can be ' raised '
   __raise func2 ();  C3745
  }
};

int main () {
  e e;
  __raise e.func1 ();
  __raise e.func1 (1); C3745
  __raise e.func2 ();  C3745
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.