home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / xpcom / src / nsAgg.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.2 KB  |  126 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 nsAgg_h___
  20. #define nsAgg_h___
  21.  
  22. #include "nsISupports.h"
  23.  
  24. // Put this in your class's declaration:
  25. #define NS_DECL_AGGREGATED                                                  \
  26.     NS_DECL_ISUPPORTS                                                       \
  27.                                                                             \
  28. protected:                                                                  \
  29.                                                                             \
  30.     /* You must implement this operation instead of the nsISupports */      \
  31.     /* methods if you inherit from nsAggregated. */                         \
  32.     NS_IMETHOD AggregatedQueryInterface(const nsIID& aIID,                  \
  33.                                         void** aInstancePtr);               \
  34.                                                                             \
  35.     class Internal : public nsISupports {                                   \
  36.     public:                                                                 \
  37.                                                                             \
  38.         Internal() {}                                                       \
  39.                                                                             \
  40.         NS_IMETHOD QueryInterface(const nsIID& aIID,                        \
  41.                                   void** aInstancePtr);                     \
  42.         NS_IMETHOD_(nsrefcnt) AddRef(void);                                 \
  43.         NS_IMETHOD_(nsrefcnt) Release(void);                                \
  44.                                                                             \
  45.     };                                                                      \
  46.                                                                             \
  47.     friend class Internal;                                                  \
  48.                                                                             \
  49.     nsISupports*        fOuter;                                             \
  50.     Internal            fAggregated;                                        \
  51.                                                                             \
  52. public:                                                                     \
  53.  
  54.  
  55. // Put this in your class's constructor:
  56. #define NS_INIT_AGGREGATED(outer)                                           \
  57.     NS_INIT_REFCNT();                                                       \
  58.     fOuter = outer;                                                         \
  59.  
  60.  
  61. // Put this in your class's implementation file:
  62. #define NS_IMPL_AGGREGATED(_class)                                          \
  63. NS_METHOD                                                                   \
  64. _class::QueryInterface(const nsIID& aIID, void** aInstancePtr)              \
  65. {                                                                           \
  66.     static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);                  \
  67.     if (aIID.Equals(kISupportsIID)) {                                       \
  68.         *aInstancePtr = &fAggregated;                                       \
  69.         return NS_OK;                                                       \
  70.     }                                                                       \
  71.     else if (fOuter)                                                        \
  72.         return fOuter->QueryInterface(aIID, aInstancePtr);                  \
  73.     else                                                                    \
  74.         return AggregatedQueryInterface(aIID, aInstancePtr);                \
  75. }                                                                           \
  76.                                                                             \
  77. NS_METHOD_(nsrefcnt)                                                        \
  78. _class::AddRef(void)                                                        \
  79. {                                                                           \
  80.     if (fOuter)                                                             \
  81.         return fOuter->AddRef();                                            \
  82.     else                                                                    \
  83.         return ++mRefCnt;                                                   \
  84. }                                                                           \
  85.                                                                             \
  86. NS_METHOD_(nsrefcnt)                                                        \
  87. _class::Release(void)                                                       \
  88. {                                                                           \
  89.     if (fOuter)                                                             \
  90.         return fOuter->Release();                                           \
  91.     else {                                                                  \
  92.         if (--mRefCnt == 0) {                                               \
  93.             delete this;                                                    \
  94.             return 0;                                                       \
  95.         }                                                                   \
  96.         return mRefCnt;                                                     \
  97.     }                                                                       \
  98. }                                                                           \
  99.                                                                             \
  100. NS_METHOD                                                                   \
  101. _class::Internal::QueryInterface(const nsIID& aIID, void** aInstancePtr)    \
  102. {                                                                           \
  103.     _class* agg = (_class*)((char*)(this) - offsetof(_class, fAggregated)); \
  104.     return agg->AggregatedQueryInterface(aIID, aInstancePtr);               \
  105. }                                                                           \
  106.                                                                             \
  107. NS_METHOD                                                                   \
  108. _class::Internal::AddRef(void)                                              \
  109. {                                                                           \
  110.     _class* agg = (_class*)((char*)(this) - offsetof(_class, fAggregated)); \
  111.     return ++agg->mRefCnt;                                                  \
  112. }                                                                           \
  113.                                                                             \
  114. NS_METHOD                                                                   \
  115. _class::Internal::Release(void)                                             \
  116. {                                                                           \
  117.     _class* agg = (_class*)((char*)(this) - offsetof(_class, fAggregated)); \
  118.     if (--agg->mRefCnt == 0) {                                              \
  119.         delete agg;                                                         \
  120.         return 0;                                                           \
  121.     }                                                                       \
  122.     return agg->mRefCnt;                                                    \
  123. }                                                                           \
  124.  
  125. #endif /* nsAgg_h___ */
  126.