Ptr.C 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: Ptr.C /main/1 1996/07/29 17:02:08 cde-hp $ */
  24. // Copyright (c) 1994 James Clark
  25. // See the file COPYING for copying permission.
  26. #ifndef Ptr_DEF_INCLUDED
  27. #define Ptr_DEF_INCLUDED 1
  28. #ifdef SP_NAMESPACE
  29. namespace SP_NAMESPACE {
  30. #endif
  31. template<class T>
  32. Ptr<T>::Ptr(T *ptr) : ptr_(ptr)
  33. {
  34. if (ptr_)
  35. ptr_->ref();
  36. }
  37. template<class T>
  38. Ptr<T>::~Ptr()
  39. {
  40. if (ptr_) {
  41. if (ptr_->unref())
  42. delete ptr_;
  43. ptr_ = 0;
  44. }
  45. }
  46. template<class T>
  47. Ptr<T>::Ptr(const Ptr<T> &p)
  48. : ptr_(p.ptr_)
  49. {
  50. if (p.ptr_)
  51. p.ptr_->ref();
  52. }
  53. template<class T>
  54. Ptr<T> &Ptr<T>::operator=(const Ptr<T> &p)
  55. {
  56. if (p.ptr_)
  57. p.ptr_->ref();
  58. if (ptr_ && ptr_->unref())
  59. delete ptr_;
  60. ptr_ = p.ptr_;
  61. return *this;
  62. }
  63. template<class T>
  64. Ptr<T> &Ptr<T>::operator=(T *p)
  65. {
  66. if (p)
  67. p->ref();
  68. if (ptr_ && ptr_->unref())
  69. delete ptr_;
  70. ptr_ = p;
  71. return *this;
  72. }
  73. template<class T>
  74. void Ptr<T>::clear()
  75. {
  76. if (ptr_) {
  77. if (ptr_->unref())
  78. delete ptr_;
  79. ptr_ = 0;
  80. }
  81. }
  82. #ifdef SP_NAMESPACE
  83. }
  84. #endif
  85. #endif /* not Ptr_DEF_INCLUDED */