Danh mục

Creating Applications with Mozilla-Chapter 8. XPCOM- P4

Số trang: 18      Loại file: pdf      Dung lượng: 32.71 KB      Lượt xem: 1      Lượt tải: 0    
thaipvcb

Hỗ trợ phí lưu trữ khi tải xuống: 16,000 VND Tải xuống file đầy đủ (18 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- p4, 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- P4 Chapter 8. XPCOM- P4As you can see, the macro is made up of other macros that implement basicmethods of the nsISupports interface. Unless you need to modify thesemacros, they should be left as is. This macro is used later on when we createour C++ component.Example 8-6 shows a reference implementation of the QueryInterfacemethod in C++.Example 8-6. Reference implementation of QueryInterfaceNS_IMETHODIMPnsMyImplementation::QueryInterface( REFNSIID aIID,void** aInstancePtr ){ NS_ASSERTION(aInstancePtr, QueryInterfacerequires a non-NULL destination!); if ( !aInstancePtr ) return NS_ERROR_NULL_POINTER; nsISupports* foundInterface; if ( aIID.Equals(nsCOMTypeInfo::GetIID( ))) foundInterface = NS_STATIC_CAST(nsIX*, this); else if (aIID.Equals(nsCOMTypeInfo::GetIID( )) ) foundInterface = NS_STATIC_CAST(nsIY*, this); else if (aIID.Equals(nsCOMTypeInfo::GetIID( ))) foundInterface = NS_STATIC_CAST(nsISupports*,NS_STATIC_CAST(nsIX*, this)); else foundInterface = 0; nsresult status; if ( !foundInterface ) { status = NS_NOINTERFACE; } else { NS_ADDREF(foundInterface); status = NS_OK; } *aInstancePtr = foundInterface; return status;}8.2.4.2. The results macrosSince all XPCOM methods return result codes called nsresults, anotheruseful macro is the NS_SUCCEEDED macro. This indicates whether anXPCOM accessor has returned a successful result. It is defined innsError.h: #define NS_SUCCEEDED(_nsresult) (!((_nsresult) &0x80000000))A related macro, NS_FAILED, is indicates whether an XPCOM accessorreturned a failure code result. It too is defined in nsError.h. Thefollowing code demonstrates the typical use of these two macros:nsresult rv;nsCOMPtrfile(do_CreateInstance(@mozilla.org/file/local;1,&rv)); if (NS_FAILED(rv)) { printf(FAILED\n); return rv; } if (NS_SUCCEEDED(rv)) { printf( SUCCEEDED \n); return rv; }You may have noticed that the declaration of the identifier rv as the typensresult. nsresult is a 32-bit unsigned integer declared innscore.h: typedef PRUint32 nsresult;We assign an nsCOMPtr or smart pointer named file to a newly createdinstance of the nsILocalFile component. Using the NS_FAILED andNS_SUCCEEDED macros, we test for the nsresult to see if our attemptto create an instance of the component failed. If it did, rv would be assignedan integer with a specific error return code. Return codes are defined innsError.h. Alternatively, you can test your results for the success code:nsresult rv =nsComponentManager::CreateInstance(@mozilla.org/file/local;1, nsnull,NS_GET_IID(nsILocalFile), (void**)&refp);If a result is successful, the value of rv returns NS_OK, which is 0.Return codes are used in XPCOM instead of exceptions. Exceptions are notallowed because of their inconsistent implementation across differentcompilers. All error code numbers equate to a specific type of error. Forexample NS_ERROR_FAILURE and NS_ERROR_NULL_POINTER arecommon types of error code return values used throughout the Mozilla codebase. If a value returned to rv was NS_ERROR_NULL_POINTER, the testfor failure would be true and the code would return the numerical result codefor NS_ERROR_NULL_POINTER.8.2.4.3. The nsnull typeAnother widely use type is nsnull, defined in nscore.h. Here is thedefinition:#define nsnull 0This definition, nsnull, is the most common way to use null. Thefollowing code shows how to use nsnull: nsresult rv; nsCOMPtr file = do_CreateInstance(@mozilla.org/file/local;1,&rv); if (NS_SUCCEEDED(rv)) { char* msg = we successfully created aninstance of file\n; *_retval = (char*)nsMemory::Alloc(PL_strlen(msg) + 1); if (!*_retval) return NS_ERROR_OUT_OF_MEMORY; PL_strcpy(*_retval, msg); } else { *_retval = nsnull; }8.2.4.4. The NS_IMETHODIMP macroIf you look in the Mozilla C++ source code, you will see the macroNS_IMETHODIMP used frequently. This macro identifies the type of yourinterface implementation method. It is also defined in nscore.h, asshown in Example 8-7.Example 8-7. Platform macros in xpcom/base/nscore.h#define NS_IMETHODIMP NS_IMETHODIMP_(nsresult)#ifdef NS_WIN32 #define NS_IMETHODIMP_(type) type _ _stdcall#elif defined(XP_MAC) #define NS_IMETHODIMP_(type) type#elif defined(XP_OS2) #define NS_IMETHODIMP_(type) type#else #define NS_IMETHODIMP_(type) type#endifExample 8-8 shows a typical use of the NS_IMETHODIMP macro. Allmethods that implement an interface are of the type NS_IMETHODIMP.Example 8-8. NS_IMETHOD macroNS_IMETHODIMPnsMyImpl::GetSomeString(char** _retval){ nsresult rv; nsCOMPtr file = do_CreateInstance(@mozilla.org/file/local;1,&rv); if (NS_SUCCEEDED(rv)) { char* msg = we successfully created aninstance of file\n; *_retval = (char*)nsMemory::Alloc(PL_strlen(msg) + 1); if (!*_retval) return NS_ERROR_OUT_OF_MEMORY; PL_strcpy(*_retval, msg); } else { *_retval = nsnull; } return NS_OK;}The macro in Example 8-8 declares the method GetSomeString as anXPCOM implementation.8.2.4.5. nsCOMPtr smart pointerAs described earlier, XPCOM provides a C++ tool called a smart pointer tomanage reference counting. A smart pointer is a template class that actssyntactically, just like an ordinary pointer in C or C++. You can apply * todereference the pointer, ->, or access what the pointer refers to. Unlike araw COM interface pointer, however, nsCOMPtr manages AddRef,Release, and QueryInterface for y ...

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