Link with C++ linker
[oweals/cde.git] / cde / programs / nsgmls / Ptr.h
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 librararies 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.h /main/2 1996/08/12 16:00:24 mgreess $ */
24 // Copyright (c) 1994 James Clark
25 // See the file COPYING for copying permission.
26
27 #ifndef Ptr_INCLUDED
28 #define Ptr_INCLUDED 1
29
30 #include "Boolean.h"
31
32 // T must have Resource as a public base class
33 // T may be an incomplete type
34
35 #ifdef SP_NAMESPACE
36 namespace SP_NAMESPACE {
37 #endif
38
39 template<class T>
40 class Ptr {
41 public:
42   Ptr() : ptr_(0) { }
43   Ptr(T *ptr);
44   ~Ptr();
45   Ptr(const Ptr<T> &);
46   Ptr<T> &operator=(const Ptr<T> &);
47   Ptr<T> &operator=(T *);
48   T *pointer() const { return ptr_; }
49   T *operator->() const { return ptr_; }
50   T &operator*() const { return *ptr_; }
51   void swap(Ptr<T> &p) {
52     T *tem = p.ptr_;
53     p.ptr_ = ptr_;
54     ptr_ = tem;
55   }
56   Boolean isNull() const { return ptr_ == 0; }
57   // operator const void *() const { return ptr_; }
58   void clear();
59   Boolean operator==(const Ptr<T> &p) const {
60     return ptr_ == p.ptr_;
61   }
62   Boolean operator!=(const Ptr<T> &p) const {
63     return ptr_ != p.ptr_;
64   }
65   Boolean operator==(const T *p) const {
66     return ptr_ == p;
67   }
68   Boolean operator!=(const T *p) const {
69     return ptr_ != p;
70   }
71 private:
72   T *ptr_;
73 };
74
75 template<class T>
76 #if defined (USL)
77 class ConstPtr : public Ptr<T> {
78 #else
79 class ConstPtr : private Ptr<T> {
80 #endif
81 public:
82   ConstPtr() { }
83   ConstPtr(T *ptr) : Ptr<T>(ptr) { }
84   ConstPtr(const Ptr<T> &p) : Ptr<T>(p) { }
85   ConstPtr(const ConstPtr<T> &p) : Ptr<T>(p) { }
86 #if defined (USL)
87 #else
88   ConstPtr<T> &operator=(const Ptr<T> &p) {
89     Ptr<T>::operator=(p); return *this;
90   }
91 #endif
92   ConstPtr<T> &operator=(const ConstPtr<T> &p) {
93     Ptr<T>::operator=(p); return *this;
94   }
95   ConstPtr<T> &operator=(T *p) {
96     Ptr<T>::operator=(p); return *this;
97   }
98   const T *pointer() const { return Ptr<T>::pointer(); }
99   const T *operator->() const { return Ptr<T>::pointer(); }
100   const T &operator*() const { return *Ptr<T>::pointer(); }
101   void swap(ConstPtr<T> &p) { Ptr<T>::swap(p); }
102   Ptr<T>::isNull;
103   Ptr<T>::clear;
104   Boolean operator==(const Ptr<T> &p) const { return Ptr<T>::operator==(p); }
105   Boolean operator!=(const Ptr<T> &p) const { return Ptr<T>::operator!=(p); }
106   Boolean operator==(const ConstPtr<T> &p) const {
107     return Ptr<T>::operator==(p);
108   }
109   Boolean operator!=(const ConstPtr<T> &p) const {
110     return Ptr<T>::operator!=(p);
111   }
112 };
113
114 #ifdef SP_NAMESPACE
115 }
116 #endif
117
118 #endif /* not Ptr_INCLUDED */
119
120 #ifdef SP_DEFINE_TEMPLATES
121 #include "Ptr.C"
122 #endif