Education/Learning/UnderstandingXpcomFiles: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 136: Line 136:
=== Getters and Setters ===
=== Getters and Setters ===


It's also important to notice how IDL attributes get rewritten in C++.
It's also important to notice how IDL attributes get rewritten in C++. Recall the following from [http://hg.mozilla.org/mozilla-central/file/0cd41f599080/xpcom/io/nsILocalFile.idl nsILocalFile.idl]:
 
102 attribute PRBool followLinks; 
...
122 readonly attribute PRInt64 diskSpaceAvailable;
 
In the first case a readable and writable attribute, and in the second a readonly attribute.  Here's how the actual getters and setter look in the generated C++:
 
<pre>
/* attribute PRBool followLinks; */
NS_IMETHODIMP nsLocalFile::GetFollowLinks(PRBool *aFollowLinks)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP nsLocalFile::SetFollowLinks(PRBool aFollowLinks)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}
 
...
 
/* readonly attribute PRInt64 diskSpaceAvailable; */
NS_IMETHODIMP nsLocalFile::GetDiskSpaceAvailable(PRInt64 *aDiskSpaceAvailable)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}
<pre>
 
The followLinks attribute becomes ::GetFollowLinks and ::SetFollowLinks, and diskSpaceAvailable becomes ::GetDiskSpaceAvailable.  Simple when you know, and used everywhere in the source.
 
== Interface Implementations ==
 
Thus far we've looked at how the [http://hg.mozilla.org/mozilla-central/file/0cd41f599080/xpcom/io/nsILocalFile.idl nsILocalFile.idl] interface is defined, and seen what happens when it gets translated into [[Education/Learning/UnderstandingXpcomFiles/nsILocalFile.h|nsILocalFile.h]].
 
Now it's time to look for an implementation of this interface.  The relevant files are [http://hg.mozilla.org/mozilla-central/file/0cd41f599080/xpcom/io/nsLocalFile.h nsLocalFile.h] and [http://hg.mozilla.org/mozilla-central/file/0cd41f599080/xpcom/io/nsLocalFileCommon.cpp nsLocalFileCommon.cpp].
 
[http://hg.mozilla.org/mozilla-central/file/0cd41f599080/xpcom/io/nsLocalFile.h nsLocalFile.h] takes care of include our interface (i.e., the generated header file), and also figures out which platform specific header to include for the implementation:
 
71 #include "nsILocalFile.h"
72
73 #ifdef XP_WIN
74 #include "nsLocalFileWin.h"
75 #elif defined(XP_MACOSX)
76 #include "nsLocalFileOSX.h"
77 #elif defined(XP_UNIX) || defined(XP_BEOS)
78 #include "nsLocalFileUnix.h"
79 #elif defined(XP_OS2)
80 #include "nsLocalFileOS2.h"
81 #else
82 #error NOT_IMPLEMENTED
83 #endif
 
[http://hg.mozilla.org/mozilla-central/file/0cd41f599080/xpcom/io/nsLocalFileCommon.cpp nsLocalFileCommon.cpp] includes [http://hg.mozilla.org/mozilla-central/file/0cd41f599080/xpcom/io/nsLocalFile.h nsLocalFile.h], and therefore all the platform specific headers we added above.  It implements some common elements of the interface, leaving specifics to platform-specific implementations.  If we take Windows and [http://hg.mozilla.org/mozilla-central/file/0cd41f599080/xpcom/io/nsLocalFileWin.h nsLocalFileWin.h] as an example, we see where the NS_DECL_NSILOCALFILE macro gets used:
 
82    // nsILocalFile interface
83    NS_DECL_NSILOCALFILE
 
We also see how
 
 
 
 
 
 
 
* nsILocalFileWin
* nsILocalFileUnix
* nsILocalFileOS2
* nsILocalFileOSX
 
Depending on the operating system we are on, we need to implement this interface differently (the 'XP' in XPCOM stands for cross-platform, after all).  The build system once again does the heavy lifting and figures out which implementation to use at compile time (see [http://hg.mozilla.org/mozilla-central/file/0cd41f599080/xpcom/io/Makefile.in Makefile.in]):
 
91  ifeq ($(MOZ_WIDGET_TOOLKIT),os2)
92  CPPSRCS += nsLocalFileOS2.cpp
93  else
94  ifeq ($(MOZ_WIDGET_TOOLKIT),cocoa)
95  CMMSRCS = nsLocalFileOSX.mm
96  else
97  ifeq ($(MOZ_WIDGET_TOOLKIT),windows)
98  CPPSRCS += nsLocalFileWin.cpp
99  else
100 CPPSRCS += nsLocalFileUnix.cpp
101 endif # windows
102 endif # mac
103 endif # OS2
 
=== nsI vs. ns ===
 
The interface is named '''nsILocalFile''' and our four implementations are all named '''nsLocalFile*''', dropping the ''I''.  This is another obvious thing once you know to look for it, but can be confusing when you start and your eye skims over '''nsIFoo''' and '''nsFoo''' as though they were the same thing.  Incidentally, the ''ns'' prefix stands for Netscape, and you'll also sometimes encounter ''mozI'' and ''moz'' as a naming prefix. 
 


still editing...
still editing...
Confirmed users
656

edits

Navigation menu