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.

137 lines
1.9 KiB

  1. //-----------------------------------------------------------------------------
  2. //
  3. // File: smartptr.inl
  4. // Copyright (C) 1994-1997 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. //
  8. //
  9. //-----------------------------------------------------------------------------
  10. template <class T>
  11. SmartPtr< T >::SmartPtr()
  12. {
  13. m_pObject = NULL;
  14. }
  15. template <class T>
  16. SmartPtr< T >::SmartPtr(
  17. T *pObject)
  18. {
  19. m_pObject = pObject;
  20. }
  21. template <class T>
  22. T &
  23. SmartPtr< T >::operator*(void)
  24. const
  25. {
  26. LTASSERT(m_pObject != NULL);
  27. return *m_pObject;
  28. }
  29. template <class T>
  30. T *
  31. SmartPtr< T >::operator->(void)
  32. const
  33. {
  34. LTASSERT(m_pObject != NULL);
  35. return m_pObject;
  36. }
  37. template <class T>
  38. T *
  39. SmartPtr< T >::Extract(void)
  40. {
  41. T *pObj = m_pObject;
  42. m_pObject = NULL;
  43. return pObj;
  44. }
  45. template <class T>
  46. T*
  47. SmartPtr< T >::GetPointer(void)
  48. {
  49. return m_pObject;
  50. }
  51. template <class T>
  52. const T*
  53. SmartPtr< T >::GetPointer(void) const
  54. {
  55. return m_pObject;
  56. }
  57. template <class T>
  58. BOOL
  59. SmartPtr< T >::IsNull(void)
  60. const
  61. {
  62. return m_pObject == NULL;
  63. }
  64. template <class T>
  65. void
  66. SmartPtr< T >::operator=(
  67. T *pObject)
  68. {
  69. LTASSERT(m_pObject == NULL);
  70. if (m_pObject != NULL)
  71. {
  72. delete m_pObject;
  73. }
  74. m_pObject = pObject;
  75. }
  76. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  77. //
  78. // This should only be used to init a smart pointer.
  79. //
  80. //-----------------------------------------------------------------------------
  81. template <class T>
  82. SmartPtr< T >::operator T * & (void)
  83. {
  84. LTASSERT(m_pObject == NULL);
  85. return m_pObject;
  86. }
  87. template <class T>
  88. void
  89. SmartPtr< T >::operator delete(
  90. void *)
  91. {
  92. LTASSERT(m_pObject != NULL);
  93. delete m_pObject;
  94. m_pObject = NULL;
  95. }
  96. template <class T>
  97. SmartPtr< T >::~SmartPtr()
  98. {
  99. if (m_pObject != NULL)
  100. {
  101. delete m_pObject;
  102. }
  103. }