]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/src/Ptr.hh
Initial revision
[helm.git] / helm / DEVEL / mathml_editor / src / Ptr.hh
1 // Copyright (C) 2000-2002, Luca Padovani <luca.padovani@cs.unibo.it>.
2 //
3 // This file is part of GtkMathView, a Gtk widget for MathML.
4 // 
5 // GtkMathView is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // GtkMathView is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with GtkMathView; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 // 
19 // For details, see the GtkMathView World-Wide-Web page,
20 // http://www.cs.unibo.it/helm/mml-widget, or send a mail to
21 // <luca.padovani@cs.unibo.it>
22
23 #ifndef Ptr_hh
24 #define Ptr_hh
25
26 template <class P>
27 class Ptr
28 {
29 public:
30   Ptr(P* p = 0) : ptr(p) { if (ptr != 0) ptr->ref(); }
31   Ptr(const Ptr& p) : ptr(p.ptr) { if (ptr != 0) ptr->ref(); }
32   ~Ptr() { if (ptr != 0) ptr->unref(); }
33
34   P* operator->() const { assert(ptr != 0); return ptr; }
35   Ptr& operator=(const Ptr& p)
36   { 
37     if (ptr == p.ptr) return *this;
38     if (p.ptr != 0) p.ptr->ref();
39     if (ptr != 0) ptr->unref();
40     ptr = p.ptr;
41     return *this;
42   }
43
44   operator P*() const { return ptr; }
45   template <class Q> friend Ptr<Q> smart_cast(const Ptr& p) { return Ptr<Q>(dynamic_cast<Q*>(p.ptr)); }  
46   template <class Q> friend bool is_a(const Ptr& p) { return dynamic_cast<Q*>(p.ptr) != 0; }
47   template <class Q> operator Ptr<Q>() const { return Ptr<Q>(ptr); }
48
49 private:
50   P* ptr;
51 };
52
53 #endif // Ptr_hh