|
|
Songbird Wiki > Developer Center > Articles > Style Manual > C/C++ Headers and Object Definition
C/C++ Headers and Object DefinitionFrom $1Table of contents
C/C++ Header FileAll headers should contain the following elements.
See an example. The Include GuardTo prevent files from being compiled more than once you should always wrap the contents of the file in an include guard. Our convention will be to add a double underscore before and after the filename. A complete include guard looks like this: #ifndef __SB_FILENAME_H__ #define __SB_FILENAME_H__ // Contents of file #endif /* __SB_FILENAME_H__ */ Note: You should not use Including FilesNote: These are guidelines, not strict rules. No code police will chase after you if you decide not to follow these guidelines.
According to The C++ Portability Guide Forward DeclarationsWhenever possible please forward-declare classes and structs if you only need to use pointers in your header file. This speeds up compilation. Here is an example: // Include nsIChannel because we use it as a base class
#include <nsIChannel.h>
// Forward-declare nsIURI because we only use a pointer below
class nsIURI;
class sbTestChannel : public nsIChannel
{
nsIURI* GetURI();
};
Example HeaderBelow is an example C++ header file that meets all the requirements above as well as the documentation requirements for header files. /* // // BEGIN SONGBIRD GPL // // This file is part of the Songbird web player. // // Copyright(c) 2006 POTI, Inc. // http://songbirdnest.com // // This file may be licensed under the terms of of the // GNU General Public License Version 2 (the "GPL"). // // Software distributed under the License is distributed // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either // express or implied. See the GPL for the specific language // governing rights and limitations. // // You should have received a copy of the GPL along with this // program. If not, go to http://www.gnu.org/licenses/gpl.html // or write to the Free Software Foundation, Inc., // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // // END SONGBIRD GPL // */ /** * \file SampleHeader.h * \brief Songbird Sample Definition. */ #ifndef __SB_SAMPLEHEADER_H__ #define __SB_SAMPLEHEADER_H__ #include <sbISample.h> #include <nsCOMArray.h> #include <nsIObserver.h> #include <nsString.h> #include <prlock.h> class nsIURI; #define SONGBIRD_SAMPLE_DESCRIPTION \ "Songbird Sample" #define SONGBIRD_SAMPLE_CONTRACTID \ "@songbirdnest.com/Songbird/Sample;1" #define SONGBIRD_SAMPLE_CLASSNAME \ "Songbird Sample" #define SONGBIRD_SAMPLE_CID \ { /* 31268a56-f732-4140-838e-c68ac69c8c51 */ \ 0x31268a56, \ 0xf732, \ 0x4140, \ { 0x83, 0x8e, 0xc6, 0x8a, 0xc6, 0x9c, 0x8c, 0x51 } \ } class sbSample : public sbISample { public: NS_DECL_ISUPPORTS NS_DECL_NSIOBSERVER sbSample(); protected: ~sbSample(); // The lock that protects some data PRLock* mLock; // A URI nsCOMArray<nsIURI> mURI; }; #endif /* __SB_SAMPLEHEADER_H__ */
Tags:
|
Powered by MindTouch Deki Wiki |