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.

126 lines
3.2 KiB

  1. // scuArrayP.h -- implementation of AutoArrayPtr template
  2. // (c) Copyright Schlumberger Technology Corp., unpublished work, created
  3. // 2000. This computer program includes Confidential, Proprietary
  4. // Information and is a Trade Secret of Schlumberger Technology Corp. All
  5. // use, disclosure, and/or reproduction is prohibited unless authorized
  6. // in writing. All Rights Reserved.
  7. #if !defined(SLBSCU_ARRAYP_H)
  8. #define SLBSCU_ARRAYP_H
  9. namespace scu
  10. {
  11. // AutoArrayPtr is like std::auto_ptr but for arrays. It will
  12. // automatically release the array resource it owns.
  13. template<class T>
  14. class AutoArrayPtr
  15. {
  16. public:
  17. // Types
  18. typedef T ElementType;
  19. // C'tors/D'tors
  20. explicit
  21. AutoArrayPtr(T *p = 0) throw()
  22. : m_fOwns(p != 0),
  23. m_p(p)
  24. {}
  25. AutoArrayPtr(AutoArrayPtr<T> const &raap) throw()
  26. : m_fOwns(raap.m_fOwns),
  27. m_p(raap.Release())
  28. {}
  29. ~AutoArrayPtr() throw()
  30. {
  31. if (m_fOwns)
  32. delete [] m_p;
  33. }
  34. // Operators
  35. AutoArrayPtr<T> &
  36. operator=(AutoArrayPtr<T> const &rhs) throw()
  37. {
  38. if (&rhs != this)
  39. {
  40. if (rhs.Get() != m_p)
  41. {
  42. if (m_fOwns)
  43. delete [] m_p;
  44. m_fOwns = rhs.m_fOwns;
  45. }
  46. else
  47. if (rhs.m_fOwns)
  48. m_fOwns = true;
  49. m_p = rhs.Release();
  50. }
  51. return *this;
  52. }
  53. T &
  54. operator*()
  55. {
  56. return *Get();
  57. }
  58. T const &
  59. operator*() const
  60. {
  61. return *Get();
  62. }
  63. T &
  64. operator[](size_t index)
  65. {
  66. return m_p[index];
  67. }
  68. T const &
  69. operator[](size_t index) const
  70. {
  71. return m_p[index];
  72. }
  73. // Operations
  74. T *
  75. Get() const throw()
  76. {
  77. return m_p;
  78. }
  79. T *
  80. Release() const throw()
  81. {
  82. // workaround function const.
  83. m_fOwns = false;
  84. return m_p;
  85. }
  86. // Predicates
  87. protected:
  88. // Types
  89. // C'tors/D'tors
  90. // Operators
  91. // Operations
  92. // Access
  93. // Predicates
  94. // Variables
  95. private:
  96. // Types
  97. // C'tors/D'tors
  98. // Operators
  99. // Operations
  100. // Access
  101. // Predicates
  102. // Variables
  103. bool mutable m_fOwns;
  104. T *m_p;
  105. };
  106. } // namespace scu
  107. #endif // SLBSCU_ARRAYP_H