Danh mục

Creating Applications with Mozilla-Chapter 8. XPCOM- P2

Số trang: 18      Loại file: pdf      Dung lượng: 41.21 KB      Lượt xem: 2      Lượt tải: 0    
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu creating applications with mozilla-chapter 8. xpcom- p2, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Creating Applications with Mozilla-Chapter 8. XPCOM- P2 Chapter 8. XPCOM- P28.1.3.2. Root interfacesQueryInterface, Addref, and Release are required methods that areimplemented by every component. QueryInterface matches a specificinterface with its implementation class module. Addref and Release aremethods used for reference counting. When an instance of a component iscreated, one or more pointers may reference that object. For each reference,a count is incremented by one. When a reference is no longer used,Release is called to decrement the count. You must hold a reference countto ensure that no pointers reference an object after it is deleted. Whenpointers try to access objects that are deleted, the application core dumps.Reference counting can get tricky, which is why smart pointers manageAddref and Release for you, as described in the later section Section8.2.4.Defining QueryInterface, Addref, and Release every time aninterface is created is clearly not very efficient. Instead, these methods aredefined in the base interface called nsISupports. All interfaces inherit fromthis mother of all interfaces and dont need to redefine these three basicfunctions.XPIDL supports the C style syntax preparser directive #include toinclude other IDL files -- not unlike MSCOM, which uses the importstatement. At the top of any IDL file that you create, you need to includensISupports:#include nsISupports.idlinterface nsISimple : nsISupports { readonly attribute string value;};Core IDL TypesThe core types used in IDL interface files are listed in the filexpcom/base/nsrootidl.idl. This file is included in nsISupports.(This means it is included in every interface file, since all interfaces inheritfrom nsISupports.) All interfaces used in a system are valid IDL types. Forexample, nsISimple is a valid type to use as a method parameter or a methodreturn type. Some of the main types listed in this interface are:typedef boolean PRBool;typedef octet PRUint8;typedef unsigned short PRUint16;typedef unsigned short PRUnichar;typedef unsigned long PRUint32;typedef unsigned long long PRUint64;typedef unsigned long long PRTime;typedef short PRInt16;typedef long PRInt32;typedef long long PRInt64;typedef unsigned long nsrefcnt;typedef unsigned long nsresult;typedef unsigned long size_t;8.1.3.3. The XPIDL compilerAn IDL compiler is a tool that creates a binary distribution file called a typelibrary from an interface description source file. Since support for manydifferent platforms is a requirement for Mozilla, a modified version of thelibIDL compiler from the Gnome project is used. This variant is called theXPIDL compiler and is primarily used to compile Mozillas own dialect ofIDL, conveniently called XPIDL. The XPIDL compiler generates XPCOMinterface information, headers for XPCOM objects, and XPT type librariesfrom which objects may be accessed dynamically through XPConnect. It canalso generate HTML files for documentation and Java class stubs. Anotherfeature of the XPIDL compiler is the option to generate C++ code stubs.This feature creates nearly all the declaratory C++ code you need when youstart a new project, which makes XPIDL useful as a coding wizard that helpsyou get started. Code generation is covered later in this chapter in the sectionSection 8.2.5.The XPIDL compiler is located in xpcom/typelib/xpidl/ in theMozilla sources. If you built Mozilla, you can add this directory to yourPATH:$ PATH=$PATH:/usr/src/mozilla/xpcom/typelib/xpidlUsing the compiler is fairly easy. If you use the help command, you can seethe usage syntax and other basic information about the compiler:$ ./xpidl --helpUsage: xpidl [-m mode] [-w] [-v] [-I path] [-obasename] filename.idl -a emit annotations to typelib -w turn on warnings (recommended) -v verbose mode (NYI) -I add entry to start of include path for``#include nsIThing.idl -o use basename (e.g. ``/tmp/nsIThing) foroutput -m specify output mode: header Generate C++ header(.h) typelib Generate XPConnect typelib(.xpt) doc Generate HTML documentation(.html) java Generate Java interface(.java)8.1.4. XPCOM Type LibrariesThe key to the component architecture of XPCOM is the presence of binary-independent interface files that are used uniformly across platforms,languages, and programming environments. These interface files arecompiled into .xpt files by the XPIDL compiler. The Mozillacomponents subdirectory is where type libraries and modules aretypically stored. If you create a cross-platform type library for yourcomponent, you must place it in this directory for it to be accessible toXPCOM.8.1.4.1. Creating a type library file from an IDL interfaceTo create a (.xpt) typelib file, use the flag -m typelib with warning (-w) and verbose (-v) modes turned on. -o is used for the name of the outputfile and -I is used to specify paths to other IDL files you want to include.To successfully compile your interface, you must always point to thedirectory where nsISupports is located.# include path to nsISupports.idl$ $XPIDL_INC = /usr/src/mozilla/xpcom/base#compile nsISimple.idl$ xpidl -m typelib -w -v -I $XPIDL_INC \> -o nsISimple nsISimple.idlThe file created after compilation is nsISimple.xpt. It provides thenecessary type information about your interface at runtime. Typelib filesenumerate the methods of ...

Tài liệu được xem nhiều: