Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / nsgmls / Owner.h
1 /* $XConsortium: Owner.h /main/3 1996/08/13 14:30:51 mgreess $ */
2 // Copyright (c) 1994 James Clark
3 // See the file COPYING for copying permission.
4
5 #ifndef Owner_INCLUDED
6 #define Owner_INCLUDED 1
7
8 // A pointer that owns the object pointed to.
9 // T must be of class type.
10 // This is coded so that T need not yet have been defined.
11
12 #ifdef SP_NAMESPACE
13 namespace SP_NAMESPACE {
14 #endif
15
16 template<class T>
17 class Owner {
18 public:
19   Owner() : p_(0) { }
20   Owner(T *p) : p_(p) { }
21   ~Owner();
22   void operator=(T *p) {
23     if (p_) del();
24     p_ = p;
25   }
26   operator int() const { return p_ != 0; }
27   T *pointer() const { return p_; }
28   T *operator->() const { return p_; }
29   T &operator*() const { return *p_; }
30   void swap(Owner<T> &x) {
31     T *tem = p_;
32     p_ = x.p_;
33     x.p_ = tem;
34   }
35   T *extract() {
36     T *tem = p_;
37     p_ = 0;
38     return tem;
39   }
40   void clear() {
41     if (p_) {
42       del();
43       p_ = 0;
44     }
45   }
46 private:
47   Owner(const Owner<T> &) {}
48   void operator=(const Owner<T> &) {}
49   void del();
50   T *p_;
51 };
52
53 #ifdef SP_NAMESPACE
54 }
55 #endif
56
57 #endif /* not Owner_INCLUDED */
58
59 #ifdef SP_DEFINE_TEMPLATES
60 #include "Owner.C"
61 #endif