Danh mục

Creating Applications with Mozilla-Chapter 8. XPCOM- P5

Số trang: 16      Loại file: pdf      Dung lượng: 29.41 KB      Lượt xem: 2      Lượt tải: 0    
Thu Hiền

Hỗ trợ phí lưu trữ khi tải xuống: 10,000 VND Tải xuống file đầy đủ (16 trang) 0

Báo xấu

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- p5, 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- P5 Chapter 8. XPCOM- P5As you can see, the xpidl compiler can do a lot of work for you. The codegenerated in Example 8-11 is a C++ header file that declares the methods ofnsISimple. It provides the class definition, macros for using the interface,and a template for the class implementation, which contains stubbed-outdeclaratory code that you can paste into your implementation file to quicklyget started.8.2.5.3. Creating the implementation fileThe implementation file actually contains the C++ code that implements themember functions and properties declared in your interface. FornsISimple, these members are the yourName attribute and the write() and change( ) methods.First you need to generate a new UUID for the new implementation classyoull write. Every XPCOM implementation class must have its own UUID:$ uuidgen79e9424f-2c4d-4cae-a762-31b334079252As part of the generated file nsISimple.h, all the code stubs you need toget started are ready to be copied and pasted into the C++ source files. Youcan use those stubs as a guide to implement the component. In a text editor,create a new file called nsSimple.h and enter the code shown in Example8-12.To maintain clarity, the C++ implementation class is namednsSimpleImpl, where the default class name generated by the xpidlcompiler is nsSimple and the header file, nsSimple.h, is shown inExample 8-12.Example 8-12. The component header file nsSimple.h#include nsISimple.h// 79e9424f-2c4d-4cae-a762-31b334079252#define NS_SIMPLE_CID \{ 0x79e9424f, 0x2c4d, 0x4cae, { 0xa7, 0x62, 0x31,0xb3, 0x34, 0x07, 0x92, 0x52 } }#define NS_SIMPLE_CONTRACTID@mozilla.org/cpp_simple;1class nsSimpleImpl : public nsISimple{public: nsSimpleImpl( ); virtual ~nsSimpleImpl( ); // nsISupports interface NS_DECL_ISUPPORTS NS_DECL_NSISIMPLEprivate: char* mName;};Example 8-12 includes the ID-generated header file nsISimple.h, whichholds the C++ declarations for the interface class nsISimple. It then takesthe new UUID and breaks it into a class ID struct defined asNS_SIMPLE_CID. Next, it defines the contract ID for this implementationclass.The example uses a completely different class ID and contract ID than theone used for the JavaScript component because its a differentimplementation class and needs to have its own unique identification (eventhough it implements the same interface).Now the example makes the class declaration of the implementation, callednsSimpleImpl, which inherits from nsISimple, defining the classconstructor and virtual destructor. NS_DECL_ISUPPORTS is a macro thatholds the declaration of our required QueryInterface, AddRef, andRelease methods. NS_DECL_NSISIMPLE is created in the generatedheader file nsISimple.h. It expands to the used interface methoddeclarations. Finally Example 8-12 shows the addition of the char*member variable identified as mName. This variable is used to hold thevalue of the interface attribute yourName, just as it did earlier in theJavaScript class implementation.Once you have the header file, you are ready to start the implementationsource file. With a text editor, create a new file called nsSimple.cpp. Asin any C++ source file, you should add the header files required by theimplementation:#include plstr.h#include stdio.h#include nsCOMPtr.h#include nsMemory.h#include nsSimple.hStart by adding the implementation of our class constructor and destructor:// c++ constructornsSimpleImpl::nsSimpleImpl( ) : mName(nsnull){ NS_INIT_REFCNT( ); mName = PL_strdup(default value);}// c++ destructornsSimpleImpl::~nsSimpleImpl( ){ if (mName) PL_strfree(mName);}Then add the macro NS_IMPL_ISUPPORTS1_CI. As discussed earlier,this macro conveniently implements QueryInterface, AddRef, andRelease:NS_IMPL_ISUPPORTS1_CI(nsSimpleImpl, nsISimple);Next you are ready to implement the actual nsISimple interface methods:NS_IMETHODIMPnsSimpleImpl::GetYourName(char** aName){ NS_PRECONDITION(aName != nsnull, null ptr); if (!aName) return NS_ERROR_NULL_POINTER; if (mName) { *aName = (char*)nsMemory::Alloc(PL_strlen(mName) + 1); if (! *aName) return NS_ERROR_NULL_POINTER; PL_strcpy(*aName, mName); } else { *aName = nsnull; } return NS_OK;}A C++ implementation of an IDL method is declared as the typeNS_IMETHODIMP. The implementation starts with the getter methodGetYourName, which takes a char** parameter for the methods returnvalue. Return values in C++ XPCOM components are marshaled via methodarguments because interface implementations must always return anumerical nsresult, as described earlier. To ensure that the aNameparameter is a pointer, use the macro NS_PRECONDITION to warn if null,follow with a null test in the line below, and return the error result codeNS_ERROR_NULL_POINTER. Then test whether the member variablemName holds a value. If it does, allocate the necessary memory toaccommodate the size of the copy. Then by using PL_strcpy, you canassign the value to the parameter aName. Otherwise, mName is null and youcan assign null into aName and return:NS_IMETHODIMPnsSimpleImpl::SetYourName(const char* aName){ NS_PRECONDITION(aName != nsnull, null ptr); if (!aName) return NS_ERROR_NULL_POINTER; if (mName) { PL_strfree(mName); } mName = PL_strdup(aName); return NS_OK;}After implementing the getter, implement the setter. Again, useNS_PRECONDITION and then a null test on the aName. If that parameterholds data, you can free it by using PL_strfree and calling PL_strdup.Then assign the new value to class member mName:NS_IMETHODIMPnsSimpleImpl::Write( ){ ...

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