home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / adsi / sampprov / formtrck.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-29  |  7.1 KB  |  205 lines

  1. //+---------------------------------------------------------------------------
  2. //
  3. //  Microsoft Windows
  4. //  Copyright (C) 1992, Microsoft Corporation.
  5. //
  6. //  File:       formtrck.hxx
  7. //
  8. //  Contents:   This file defines facilities for a standard implementation
  9. //              of reference-counted objects, incorporating mechanisms for
  10. //              tracking the usage history of the objects.
  11. //
  12. //  Classes:    ObjectTracker
  13. //
  14. //  History:    6-Apr-92       MikeSe  Created
  15. //              08-Aug-94      DonCl   copied from cinc, and renamed to formtrck.hxx
  16. //
  17. //----------------------------------------------------------------------------
  18.  
  19. #ifndef __FORMTRCK_HXX__
  20. #define __FORMTRCK_HXX__
  21.  
  22. #include <windows.h>
  23.  
  24. //+-------------------------------------------------------------------------
  25. //
  26. //  Class:      ObjectTracker (otr)
  27. //
  28. //  Purpose:    Provides basis for tracking (OLE) objects (aka interface handles)
  29. //
  30. //  History:    6-Apr-92 MikeSe         Created
  31. //
  32. //  Notes:      Access to this class is only indirect, through the macros
  33. //              defined later.
  34. //
  35. //--------------------------------------------------------------------------
  36.  
  37. class ObjectTracker
  38. {
  39. protected:
  40.  
  41.                      ObjectTracker ();
  42.  
  43. //# if DBG == 1
  44. #if 0
  45.                     ~ObjectTracker();
  46.     ULONG            StdAddRef ( void );
  47.     ULONG            StdRelease ( void );
  48.     void             TrackClassName ( char * pszName );
  49.     unsigned long    GetRefCount ( void ) {return _ulRefs;};
  50.  
  51. private:
  52.  
  53.     BOOL             IsClassTracking( char * pszName );
  54.  
  55.     struct TrackLink *  _tl;
  56.     static  BOOL        _TrackAll;
  57.  
  58. public:
  59.         // dumps information on all outstanding objects
  60.     static void        DumpTrackingInfo ( int fDeleteNode = 0 );
  61.     static void        TrackClass(BOOL, char * pszName );
  62.  
  63. # endif // DBG == 1
  64.  
  65. protected:
  66.     unsigned long      _ulRefs;
  67. };
  68.  
  69. //+-------------------------------------------------------------------------
  70. //
  71. //  The following macros encapsulate use of the above
  72. //
  73. //  INHERIT_TRACKING:
  74. //
  75. //      For any class which implements a Cairo interface, add this macro
  76. //      in the class declaration, eg:
  77. //
  78. //              class CMyFoo: INHERIT_TRACKING, IFoo
  79. //
  80. //      Do not repeat this in any subclass. If both INHERIT_UNWIND and
  81. //      INHERIT_TRACKING are used, the former should appear first.
  82.  
  83. # define INHERIT_TRACKING       protected ObjectTracker
  84.  
  85.         // The following declarations are for non-retail builds
  86.  
  87. //# if DBG == 1
  88. #if 0
  89. //
  90. // DECLARE_STD_REFCOUNTING:
  91. //
  92. //      To make use of standard refcounting code, place the above
  93. //      in the class declaration, in the public method section. This
  94. //      macro defines the AddRef and Release methods for the class.
  95. //
  96.  
  97. #  define DECLARE_STD_REFCOUNTING                                       \
  98.         STDMETHOD_(ULONG, AddRef) ()                                    \
  99.                 {                                                       \
  100.                     return StdAddRef();                                 \
  101.                 };                                                      \
  102.         STDMETHOD_(ULONG, Release) ()                                   \
  103.                 {                                                       \
  104.                     ULONG ul = StdRelease();                            \
  105.                     if ( ul==0 ) delete this;                           \
  106.                     return ul;                                          \
  107.                 };
  108.  
  109. //
  110. //  ENLIST_TRACKING(class name)
  111. //
  112. //      Place an invocation of this in each constructor, at any appropriate
  113. //      point (generally immediately before returning from the constructor).
  114. //
  115. //              ENLIST_TRACKING(CMyFoo)
  116. //
  117.  
  118. #  define ENLIST_TRACKING(cls)  TrackClassName( #cls )
  119.  
  120. //
  121. // NB:  In a subclass of a class which has INHERIT_TRACKING, do not
  122. //      use INHERIT_TRACKING again. However, do use ENLIST_TRACKING in
  123. //      in the constructor of the derived class, and use TRACK_ADDREF
  124. //      and TRACK_RELEASE if the subclass overrides the AddRef and Release
  125. //      methods.
  126.  
  127. //
  128. //  TRACK_CLASS(fTrack, pszClassName)
  129. //
  130. //      Use this to add or remove a class in the list of tracked classes.
  131. //      You can turn tracking of all classes on or off by using a NULL
  132. //      pszClassName.  If fTrack is TRUE, the class will be tracked, if FALSE,
  133. //      the class will no longer be tracked.  The default configuration is
  134. //      that all classes are tracked.
  135. //
  136. //      NOTE: this affects only objects created after this macro is executed.
  137. //
  138.  
  139. #  define TRACK_CLASS(fTrack, cls) \
  140.                       ObjectTracker::TrackClass( fTrack, cls )
  141.  
  142. //
  143. //  DUMP_TRACKING_INFO()
  144. //
  145. //      Place this anywhere it would be useful to dump out the object
  146. //      tracking database. By default, this is always issued at program
  147. //      termination.
  148. //
  149.  
  150. #  define DUMP_TRACKING_INFO()        DUMP_TRACKING_INFO_KEEP()
  151. #  define DUMP_TRACKING_INFO_KEEP()   ObjectTracker::DumpTrackingInfo(0)
  152. #  define DUMP_TRACKING_INFO_DELETE() ObjectTracker::DumpTrackingInfo(1)
  153.  
  154. //      Output from this is controlled by setting the following mask values
  155. //      in OtInfoLevel
  156.  
  157. #  define DEB_OT_OBJECTS        0x00000001L
  158.         // display object addresses, reference count and name (Default)
  159.  
  160. #  define DEB_OT_CALLERS        0x80000000L
  161.         // display call history
  162.  
  163. // In addition, set the following values to cause debug output during
  164. // operation:
  165.  
  166. #  define DEB_OT_ERRORS         0x00000002L
  167.         // report errors during tracking operations (Default)
  168.  
  169. #  define DEB_OT_ACTIONS        0x40000000L
  170.         // report each create, addref and release
  171.  
  172. #  define DEB_OT_DELETE         0x20000000L
  173.         // display call history at object delete
  174.  
  175. # else // DBG == 0
  176.  
  177. inline ObjectTracker::ObjectTracker():_ulRefs(1) {};
  178.  
  179. #  define DECLARE_STD_REFCOUNTING                                       \
  180.         STDMETHOD_(ULONG, AddRef) ()                                    \
  181.                 {                                                       \
  182.                     InterlockedIncrement((long*)&_ulRefs);         \
  183.                     return _ulRefs;                                \
  184.                 };                                                      \
  185.         STDMETHOD_(ULONG, Release) ()                                   \
  186.                 {                                                          \
  187.                     if ( InterlockedDecrement((long*)&_ulRefs) == 0 ) \
  188.                     {                                                      \
  189.                         delete this;                                       \
  190.                         return 0;                                          \
  191.                     }                                                      \
  192.                     else                                                   \
  193.                         return  _ulRefs;                              \
  194.                 };
  195.  
  196. #  define ENLIST_TRACKING(cls)
  197.  
  198. #  define DUMP_TRACKING_INFO()
  199.  
  200. #  define TRACK_CLASS(fTrack, cls)
  201.  
  202. # endif // DBG == 0
  203.  
  204. #endif  // of ifndef __OTRACK_HXX__
  205.