xref: /onnv-gate/usr/src/cmd/man/src/util/nsgmls.src/include/Ptr.cxx (revision 0:68f95e015346)
1 // Copyright (c) 1994 James Clark
2 // See the file COPYING for copying permission.
3 #pragma ident	"%Z%%M%	%I%	%E% SMI"
4 
5 #ifndef Ptr_DEF_INCLUDED
6 #define Ptr_DEF_INCLUDED 1
7 
8 #ifdef SP_NAMESPACE
9 namespace SP_NAMESPACE {
10 #endif
11 
12 template<class T>
Ptr(T * ptr)13 Ptr<T>::Ptr(T *ptr) : ptr_(ptr)
14 {
15   if (ptr_)
16     ptr_->ref();
17 }
18 
19 template<class T>
~Ptr()20 Ptr<T>::~Ptr()
21 {
22   if (ptr_) {
23     if (ptr_->unref())
24       delete ptr_;
25     ptr_ = 0;
26   }
27 }
28 
29 template<class T>
Ptr(const Ptr<T> & p)30 Ptr<T>::Ptr(const Ptr<T> &p)
31 : ptr_(p.ptr_)
32 {
33   if (p.ptr_)
34     p.ptr_->ref();
35 }
36 
37 template<class T>
operator =(const Ptr<T> & p)38 Ptr<T> &Ptr<T>::operator=(const Ptr<T> &p)
39 {
40   if (p.ptr_)
41     p.ptr_->ref();
42   if (ptr_ && ptr_->unref())
43     delete ptr_;
44   ptr_ = p.ptr_;
45   return *this;
46 }
47 
48 template<class T>
operator =(T * p)49 Ptr<T> &Ptr<T>::operator=(T *p)
50 {
51   if (p)
52     p->ref();
53   if (ptr_ && ptr_->unref())
54     delete ptr_;
55   ptr_ = p;
56   return *this;
57 }
58 
59 template<class T>
clear()60 void Ptr<T>::clear()
61 {
62   if (ptr_) {
63     if (ptr_->unref())
64       delete ptr_;
65     ptr_ = 0;
66   }
67 }
68 
69 #ifdef SP_NAMESPACE
70 }
71 #endif
72 
73 #endif /* not Ptr_DEF_INCLUDED */
74