Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
5.5 KiB

  1. //
  2. // smartptr.h
  3. //
  4. // This file contains stuff for implementing reference counting smart
  5. // pointers.
  6. //
  7. // Implementation Schedule for all classes defined by this file :
  8. //
  9. // 0.5 week.
  10. //
  11. // Unit Test Schedule for all classes defined by this file :
  12. //
  13. // 0.5 week.
  14. // Unit Testing will consist of the following :
  15. // Define a class derived from CRefCount. On one thread
  16. // create this object and place it into a smart pointer.
  17. // Pass a copy of the smart pointer to several other threads.
  18. // These threads should wait a random interval - print the contents
  19. // of the object and then destroy their smart pointer.
  20. //
  21. // The test should insure that :
  22. // All Memory is freed.
  23. // The objects are not prematurely destroyed by the smart pointers.
  24. //
  25. //
  26. #ifndef _SMARTPTR_H_
  27. #define _SMARTPTR_H_
  28. #ifndef Assert
  29. #define Assert _ASSERT
  30. #endif
  31. //------------------------------------------------------------------
  32. class CRefCount {
  33. //
  34. // This class contains a long which is used to count references to an
  35. // object. This class is designed to work with the CRefPtr template
  36. // that follows within this file.
  37. //
  38. // Users of this class should publicly derive from the class
  39. // (ie : 'class CNewsGroup : public CRefCount {'.
  40. // Once this is done, the derived object can be reference counted.
  41. // The functions AddRef() and RemoveRef() are used to add and remove
  42. // reference counts, using InterlockedIncrement and InterlockedDecrement.
  43. // If RemoveRef returns a negative value the object no longer has
  44. // any references.
  45. //
  46. // Objects derived from this should have Init() functions.
  47. // All initialization of the object should be done through these
  48. // Init() functions, the constructor should do minimal work.
  49. //
  50. // This allows the creation of a smart pointer to allocate the
  51. // memory for the object. The object is then initialized by calling
  52. // through the smart pointer into the Init() function.
  53. //
  54. public :
  55. LONG m_refs ;
  56. inline CRefCount( ) ;
  57. inline LONG AddRef( ) ;
  58. inline LONG RemoveRef( ) ;
  59. } ;
  60. //
  61. // refcount.inl contains all of the actual implementation of this class.
  62. //
  63. #include "refcount.inl"
  64. //------------------------------------------------------------------
  65. template< class Type >
  66. class CRefPtr {
  67. //
  68. // This template is used to build reference counting pointers to
  69. // an object. The object should be a class derived from CRefCount.
  70. // The cost of using these smart pointers is 1 DWORD for the smart pointer
  71. // itself, and 1 DWORD in the pointed to object to contain the reference
  72. // count.
  73. //
  74. // These Smart Pointers will do the following :
  75. // On Creation the smart pointer will add a reference to the pointed to object.
  76. // On Assignment 'operator=' the smart pointer will check for assignment
  77. // to self, if this is not the case it will remove a reference from the
  78. // pointed to object and destroy it if necessary. It will then point
  79. // itself to the assigned object and add a reference to it.
  80. // On Destruction the smart pointer will remove a reference from the
  81. // pointed to object, and if necessary delete it.
  82. //
  83. private:
  84. Type* m_p ;
  85. //CRefPtr( ) ;
  86. public :
  87. inline CRefPtr( const CRefPtr< Type >& ) ;
  88. inline CRefPtr( const Type *p = 0 ) ;
  89. inline ~CRefPtr( ) ;
  90. inline CRefPtr<Type>& operator=( const CRefPtr<Type>& ) ;
  91. inline CRefPtr<Type>& operator=( const Type * ) ;
  92. inline BOOL operator==( CRefPtr<Type>& ) ;
  93. inline BOOL operator!=( CRefPtr<Type>& ) ;
  94. inline BOOL operator==( Type* ) ;
  95. inline BOOL operator!=( Type* ) ;
  96. inline Type* operator->() const ;
  97. inline operator Type* () const ;
  98. inline BOOL operator!() const ;
  99. inline Type* Release() ;
  100. inline Type* Replace( Type * ) ;
  101. } ;
  102. //------------------------------------------------------------------
  103. template< class Type >
  104. class CSmartPtr {
  105. //
  106. // This template is used to build reference counting pointers to
  107. // an object. The object should be a class derived from CRefCount.
  108. // The cost of using these smart pointers is 1 DWORD for the smart pointer
  109. // itself, and 1 DWORD in the pointed to object to contain the reference
  110. // count.
  111. //
  112. // These Smart Pointers will do the following :
  113. // On Creation the smart pointer will add a reference to the pointed to object.
  114. // On Assignment 'operator=' the smart pointer will check for assignment
  115. // to self, if this is not the case it will remove a reference from the
  116. // pointed to object and destroy it if necessary. It will then point
  117. // itself to the assigned object and add a reference to it.
  118. // On Destruction the smart pointer will remove a reference from the
  119. // pointed to object, and if necessary delete it.
  120. //
  121. private:
  122. Type* m_p ;
  123. public :
  124. inline CSmartPtr( const CSmartPtr< Type >& ) ;
  125. inline CSmartPtr( const Type *p = 0 ) ;
  126. inline ~CSmartPtr( ) ;
  127. //inline CSmartPtr<Type>& operator=( CSmartPtr<Type>& ) ;
  128. inline CSmartPtr<Type>& operator=( const CSmartPtr<Type>& ) ;
  129. inline CSmartPtr<Type>& operator=( const Type * ) ;
  130. inline BOOL operator==( CSmartPtr<Type>& ) ;
  131. inline BOOL operator!=( CSmartPtr<Type>& ) ;
  132. inline BOOL operator==( Type* ) ;
  133. inline BOOL operator!=( Type* ) ;
  134. inline Type* operator->() const ;
  135. inline operator Type* () const ;
  136. inline BOOL operator!() const ;
  137. inline Type* Release() ;
  138. inline Type* Replace( Type * ) ;
  139. } ;
  140. #include "smartptr.inl"
  141. #endif // _SORTLIST_H_