home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / xpcom / src / nsISupports.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.1 KB  |  173 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef nsISupports_h___
  20. #define nsISupports_h___
  21.  
  22. #include "nsDebug.h"
  23. #include "nsID.h"
  24.  
  25. // An "interface id" which can be used to uniquely identify a given
  26. // interface. Primarily used as an argument to nsISupports.QueryInterface
  27. // method.
  28.  
  29. typedef nsID nsIID;
  30.  
  31. // Define an IID
  32. #define NS_DEFINE_IID(_name, _iidspec) \
  33.   const nsIID _name = _iidspec
  34.  
  35. //----------------------------------------------------------------------
  36.  
  37. // IID for the nsISupports interface
  38. // {00000000-0000-0000-c000-000000000046}
  39. #define NS_ISUPPORTS_IID      \
  40. { 0x00000000, 0x0000, 0x0000, \
  41.   {0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46} }
  42.  
  43. #define NS_FAILED(_nsresult) ((_nsresult) & 0x80000000)
  44. #define NS_SUCCEEDED(_nsresult) (!((_nsresult) & 0x80000000))
  45.  
  46. // Standard "it worked" return value
  47. #define NS_OK 0
  48.  
  49. #define NS_ERROR_BASE ((nsresult) 0xC1F30000)
  50.  
  51. // Some standard error codes we use
  52. #define NS_ERROR_OUT_OF_MEMORY (NS_ERROR_BASE + 0)
  53. #define NS_ERROR_NO_AGGREGATION (NS_ERROR_BASE + 1)
  54. #define NS_ERROR_NULL_POINTER (NS_ERROR_BASE + 2)
  55. #define NS_ERROR_ILLEGAL_VALUE (NS_ERROR_BASE + 3)
  56. #define NS_ERROR_NOT_INITIALIZED (NS_ERROR_BASE + 4)
  57. #define NS_ERROR_ALREADY_INITIALIZED (NS_ERROR_BASE + 5)
  58. #define NS_ERROR_NOT_IMPLEMENTED (NS_ERROR_BASE + 6)
  59.  
  60. // Generic result data type
  61. typedef PRUint32 nsresult;
  62.  
  63. // This is returned by QueryInterface when a given interface is not
  64. // supported.
  65. #define NS_NOINTERFACE ((nsresult) 0x80004002L)
  66.  
  67. // Reference count values
  68. typedef PRUint32 nsrefcnt;
  69.  
  70. // Basic component object model interface. Objects which implement
  71. // this interface support runtime interface discovery (QueryInterface)
  72. // and a reference counted memory model (AddRef/Release). This is
  73. // modelled after the win32 IUnknown API.
  74. class nsISupports {
  75. public:
  76.   NS_IMETHOD QueryInterface(const nsIID& aIID,
  77.                             void** aInstancePtr) = 0;
  78.   NS_IMETHOD_(nsrefcnt) AddRef(void) = 0;
  79.   NS_IMETHOD_(nsrefcnt) Release(void) = 0;
  80. };
  81.  
  82. //----------------------------------------------------------------------
  83.  
  84. // Some convenience macros for implementing AddRef and Release
  85.  
  86. // Declare the reference count variable and the implementations of the
  87. // AddRef and QueryInterface methods.
  88. #define NS_DECL_ISUPPORTS                                                   \
  89. public:                                                                     \
  90.   NS_IMETHOD QueryInterface(const nsIID& aIID,                              \
  91.                             void** aInstancePtr);                           \
  92.   NS_IMETHOD_(nsrefcnt) AddRef(void);                                       \
  93.   NS_IMETHOD_(nsrefcnt) Release(void);                                      \
  94. protected:                                                                  \
  95.   nsrefcnt mRefCnt;                                                         \
  96. public:
  97.  
  98. // Initialize the reference count variable. Add this to each and every
  99. // constructor you implement.
  100. #define NS_INIT_REFCNT() mRefCnt = 0
  101.  
  102. // Use this macro to implement the AddRef method for a given _class
  103. #define NS_IMPL_ADDREF(_class)                               \
  104. nsrefcnt _class::AddRef(void)                                \
  105. {                                                            \
  106.   return ++mRefCnt;                                          \
  107. }
  108.  
  109. #define NS_ADDREF(_ptr) \
  110.  (_ptr)->AddRef()
  111.  
  112. #define NS_IF_ADDREF(_ptr)  \
  113. ((0 != (_ptr)) ? (_ptr)->AddRef() : 0);
  114.  
  115. #define NS_RELEASE(_ptr) \
  116.  (_ptr)->Release();      \
  117.  (_ptr) = NULL
  118.  
  119. #define NS_IF_RELEASE(_ptr)             \
  120.  ((0 != (_ptr)) ? (_ptr)->Release() : 0); \
  121.  (_ptr) = NULL
  122.  
  123. // Use this macro to implement the Release method for a given _class
  124. #define NS_IMPL_RELEASE(_class)                        \
  125. nsrefcnt _class::Release(void)                         \
  126. {                                                      \
  127.   if (--mRefCnt == 0) {                                \
  128.     delete this;                                       \
  129.     return 0;                                          \
  130.   }                                                    \
  131.   return mRefCnt;                                      \
  132. }
  133.  
  134. //----------------------------------------------------------------------
  135.  
  136. // Some convenience macros for implementing QueryInterface
  137.  
  138. // This implements query interface with two assumptions: First, the
  139. // class in question implements nsISupports and it's own interface and
  140. // nothing else. Second, the implementation of the class's primary
  141. // inheritance chain leads to it's own interface.
  142. //
  143. // _class is the name of the class implementing the method
  144. // _classiiddef is the name of the #define symbol that defines the IID
  145. // for the class (e.g. NS_ISUPPORTS_IID)
  146. #define NS_IMPL_QUERY_INTERFACE(_class,_classiiddef)                     \
  147. nsresult _class::QueryInterface(const nsIID& aIID, void** aInstancePtr)  \
  148. {                                                                        \
  149.   if (NULL == aInstancePtr) {                                            \
  150.     return NS_ERROR_NULL_POINTER;                                        \
  151.   }                                                                      \
  152.   static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);                 \
  153.   static NS_DEFINE_IID(kClassIID, _classiiddef);                         \
  154.   if (aIID.Equals(kClassIID)) {                                          \
  155.     *aInstancePtr = (void*) this;                                        \
  156.     AddRef();                                                            \
  157.     return NS_OK;                                                        \
  158.   }                                                                      \
  159.   if (aIID.Equals(kISupportsIID)) {                                      \
  160.     *aInstancePtr = (void*) ((nsISupports*)this);                        \
  161.     AddRef();                                                            \
  162.     return NS_OK;                                                        \
  163.   }                                                                      \
  164.   return NS_NOINTERFACE;                                                 \
  165. }
  166.  
  167. #define NS_IMPL_ISUPPORTS(_class,_classiiddef) \
  168.   NS_IMPL_ADDREF(_class)                       \
  169.   NS_IMPL_RELEASE(_class)                      \
  170.   NS_IMPL_QUERY_INTERFACE(_class,_classiiddef)
  171.  
  172. #endif /* nsISupports_h___ */
  173.