Xpidl is the basis for developing XPCOM for the Mozilla platform. The following is a brief introduction here.
Journey to XP IDL
1. Interface
Xpidl is a language used to describe XPCOM. It can generate header files for C ++ and type libraries for other languages'
The simplest example:
Interface nsifoo {
};
If there is a parent interface, you can write it as follows:
Interface nsifoo: nsiparent {
};
According to the Conventions, the interface should have a uuid, so a more comprehensive version should be as follows:
[UUID (00000000-0000-0000-c000-000000000046)]
Interface nsifoo: nsiparent {
};
If you only want to declare an interface and do not want to define it, you can write it as follows:
Interface nsiforward;
2. Methods and attributes
There is an example of a method and an attribute:
[UUID (00000000-0000-0000-c000-000000000046)]
Interface nsifoo {
Attribute long ATTR;
Void fun ();
};
Methods can include any number of parameters with In, out, or inout modifiers. parameters can be of type. The following are optional types:Code:
Type C ++ mappingvoid voidboolean prbooloctet pruint8short print16long print32long long print64unsigned short too long pruint64float floatdouble doublechar charwchar prunicharstring char * wstring prunichar *
On the left is the built-in variable type of the IDL ing language, and on the right is the corresponding C ++.
You can also specify a method whose return value is not void. However, the generated C ++ code automatically adds the return value to the end of the method parameter.
Attribute can be modified with readonly.
You can use the native name (native_type); syntax to define your own type and use it in IDL, for example:
Native nsnativefileref (string );
Native nsnativefileptr (string );
Interface Foo {
Void openbyref (in nsnativefileref afilespecref );
Void openbyptr (in nsnativefileptr afilespecptr );
};
You can add a [Ref] or [PTR] modifier before native to indicate that the variable type is reference or pointer.
Note: If the parameters in the method are set to their own type, it usually means that the method cannot be called for the script.
You can use the # include keyword to include other IDL files.
If you use const to modify a constant, the ing to the c ++ code will become the enum type, so const can only be used to modify the integer type.
If you want to define a float constant, define a method and return a floating point value. [/Quote]
Attachment:
Keyword list Code:
Keyword example interface declaration interface FOO: Public nsisupports {}; Attribute declaration inteface Foo {attribute short bar;} readonly modifies the read-only attribute readonly attribute short myattribute; [UUID ()] define the interface UUID [UUID (00000000-0000-0000-c000-000000000046)] interface Foo {}; [Scriptable] sets the interface to be scripted [Scriptable] interface Foo {}; const declares a constant interface Foo {const short bar = 5 ;}in the interface; [const] declares a form parameter in the function call as the constant void Foo ([const] In voidstar bar ); native is used to declare the local type native myname (nsfilespec); [PTR] indicates that the type is Pointer [PTR] native nsfilespecptr (nsfilespec) in the local type declaration ); [Ref] indicates that the type is referenced in the local type declaration [Ref] native nsfilespecref (nsfilespec ); [retval] a parameter in the flag method is interface nsisupports {[iid_is]. In the method, a returned parameter is the uuid void QueryInterface (in nsiidref UUID, [iid_is (UUID ), retval] Out nsqiresult result );}; [NoScript] declaring that a method cannot be scripted. A parameter in the in method is a parameter in the out method of the input parameter. A parameter in the inout method of the output parameter is an input parameter and an output parameter % {C ++ code in the range is directly copied to the generated C ++ code. %}
All XPCOM interfaces inherit nsisupports. What is it, as follows:
[UUID (00000000-0000-0000-c000-000000000046)]
Interface nsisupports
{
Void QueryInterface (in nsiidref UUID, out nsqiresult result );
Nsrefcnt addref ();
Nsrefcnt release ();
};
Let's take a look at the example of Javascript in Firefox to learn more about the use of the XPCOM plug-in:
// Create a component
Var file = components. classes ["@ mozilla.org/file/local1_1"]. createinstance ();
@ Mozilla.org/file/local.pdf 1. This is a strange string that describes the general format of this article:
◎ <Internet Domain Name>/module [/submodule [...]; <version> [? <Name >=< value> [& <name >=< value> [...]
In fact, this string can also be replaced by a uuid that represents a class, but it is inconvenient to use, so we still choose to use the former.