home *** CD-ROM | disk | FTP | other *** search
/ Chip Special: HTML & Java / Chip-Special_1997-01_HTML-a-Java.bin / javasdk / sdk-java.exe / SDKJava.cab / Samples / Debugger / C++ Debugger / RefCount.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-10  |  1.9 KB  |  108 lines

  1. /*++
  2.  
  3. Copyright (c) 1996  Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.    refcount.hpp
  8.  
  9. Abstract:
  10.  
  11.    Reference counting class description.
  12.  
  13. --*/
  14.  
  15.  
  16. #ifndef __REFCOUNT_HPP__
  17. #define __REFCOUNT_HPP__
  18.  
  19.  
  20. /* Macros
  21.  *********/
  22.  
  23. //
  24. // Define generic method delegation from a derived class to a base class.
  25. //
  26. // E.g.,
  27. //
  28. // DEFINE_DELEGATED_METHOD(InprocServerClassFactory, ClassFactory, HRESULT, LockServer, (BOOL bLock), (bLock));
  29. //
  30. // defines a delegated method call from InprocServerClassFactory::LockServer()
  31. // to ClassFactory::LockServer().
  32. //
  33.  
  34. #define DEFINE_DELEGATED_METHOD(derived_cls, base_cls, result_type, method, derived_paren_args, base_paren_args) \
  35.    result_type STDMETHODCALLTYPE derived_cls::method derived_paren_args \
  36.    { \
  37.       return(base_cls::method base_paren_args); \
  38.    }
  39.  
  40. #define DEFINE_DELEGATED_REFCOUNT_ADDREF(cls) \
  41.    DEFINE_DELEGATED_METHOD(cls, RefCount, ULONG, AddRef, (void), ())
  42.  
  43. #define DEFINE_DELEGATED_REFCOUNT_RELEASE(cls) \
  44.    ULONG STDMETHODCALLTYPE cls::Release(void) \
  45.    { \
  46.       ULONG ulcRef; \
  47.    \
  48.       ulcRef = RefCount::DecUsage(); \
  49.    \
  50.       if (! ulcRef) \
  51.          delete(this); \
  52.    \
  53.       return(ulcRef); \
  54.    }
  55.  
  56.  
  57. /* Classes
  58.  **********/
  59.  
  60. // Generic reference counting virtual base class.
  61.  
  62. class RefCount
  63. {
  64. private:
  65.     /* Fields
  66.      *********/
  67.  
  68.    // reference count
  69.    ULONG m_ulcRef;
  70.  
  71. public:
  72.     /* Methods
  73.      **********/
  74.  
  75.    RefCount(void)
  76.    {
  77.       m_ulcRef = 1;
  78.  
  79.       return;
  80.    }
  81.  
  82.    ~RefCount(void)
  83.    {
  84.    }
  85.  
  86.    ULONG STDMETHODCALLTYPE AddRef(void)
  87.    {
  88.       ULONG ulcRef;
  89.  
  90.       ulcRef = (ULONG)(InterlockedIncrement((LPLONG)(&m_ulcRef)));
  91.  
  92.       return(ulcRef);
  93.    }
  94.  
  95.    ULONG STDMETHODCALLTYPE DecUsage(void)
  96.    {
  97.       ULONG ulcRef;
  98.  
  99.       ulcRef = (ULONG)(InterlockedDecrement((LPLONG)(&m_ulcRef)));
  100.  
  101.       return(ulcRef);
  102.    }
  103. };
  104.  
  105.  
  106. #endif  // ! __REFCOUNT_HPP__
  107.  
  108.