FAUST compiler  0.9.9.6b8
smartpointer.hh
Go to the documentation of this file.
1 /************************************************************************
2  ************************************************************************
3  FAUST compiler
4  Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
5  ---------------------------------------------------------------------
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  ************************************************************************
20  ************************************************************************/
21 
22 
23 
24 #ifndef _SMARTPOINTER_H
25 #define _SMARTPOINTER_H
26 
27 //#include <iostream>
28 #include <cstdlib>
29 #include <cstdio>
30 
31 
32 
33 template<class T>
34 class P {
35  private:
36  T* p;
37  public:
38  P() : p(0) { }
39  P(T* rawptr) : p(rawptr) { }
40  P(const P& ptr) : p((T*)ptr) { }
41 
42  template<class T2>
43  P(const P<T2>& ptr) : p((T*)ptr) { }
44 
45  ~P() { }
46 
47  operator T*() const { return p; }
48  T& operator*() const {
49  if (p == 0) {
50  //throw std::runtime_error("Null dereference in P<?>::operator*() const ");
51  fprintf(stderr, "Null dereference in P<?>::operator*() const \n ");
52  exit(1);
53  }
54  return *p;
55  }
56  T* operator->() const {
57  if (p == 0) {
58  fprintf(stderr, "Null dereference in P<?>::operator->() const \n ");
59  //throw std::runtime_error("Null dereference in P<?>::operator->() const ");
60  //std::cerr << "Null dereference in P<?>::operator->() const " << std::endl;
61  exit(1);
62  }
63  return p;
64  }
65  T* pointee() const { return p;}
66  P& operator=(T* p_) { p = p_; return *this;}
67 
68  P& operator=(const P<T>& p_) { return operator=((T *) p_); }
69  template<class T2> P& cast(T2* p_) { return operator=(dynamic_cast<T*>(p_)); }
70  template<class T2> P& cast(const P<T2>& p_) { return operator=(dynamic_cast<T*>(p_)); }
71 };
72 
73 
74 #endif
P(const P< T2 > &ptr)
Definition: smartpointer.hh:43
T * p
Definition: smartpointer.hh:36
P & cast(T2 *p_)
Definition: smartpointer.hh:69
P & operator=(T *p_)
Definition: smartpointer.hh:66
P(const P &ptr)
Definition: smartpointer.hh:40
P & cast(const P< T2 > &p_)
Definition: smartpointer.hh:70
T * operator->() const
Definition: smartpointer.hh:56
string T(char *c)
Definition: Text.cpp:158
P(T *rawptr)
Definition: smartpointer.hh:39
P & operator=(const P< T > &p_)
Definition: smartpointer.hh:68
~P()
Definition: smartpointer.hh:45
T * pointee() const
Definition: smartpointer.hh:65
T & operator*() const
Definition: smartpointer.hh:48
P()
Definition: smartpointer.hh:38