Songbird Wiki > Developer Center > Articles > Style Manual > C/C++ Headers and Object Definition

C/C++ Headers and Object Definition

From $1

C/C++ Header File

All headers should contain the following elements.

  1. Songbird GPL License
  2. Doxygen File and Object Documentation
  3. Include Guard
  4. Header File Sections

See an example.

The Include Guard

To 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 #pramga once as gcc doesn't understand that directive and will actually spit out a compiler warning if it is used. Not to mention that the file can be included more than once :-(

Including Files

Note: These are guidelines, not strict rules. No code police will chase after you if you decide not to follow these guidelines.

  • All files included from the Mozilla SDK should have angle brackets to contain the filename.
  • All Songbird files should use quotes to contain the filename.
  • All operating system files should use angle brackets to contain the filename.
  • Alphabetizing headers is kinda nice.

According to The C++ Portability Guide #include statements should contain filenames only (no directories).

Forward Declarations

Whenever 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();
 };

Defines

Definitions should have comments indicating their usage.

Class Definitions

Class definitions should follow this pattern:

 class sbTestClass : public sbITestInterface,
                     public sbITestInterface2
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_SOMEOTHERBASEINTERFACE

   sbTestClass();
   ~sbTestClass();

   // Other public methods

 protected:
   // Member data
 };

Example Header

Below 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:
 
Images (0)
 
Comments (0)
You must login to post a comment.