xc
puntero.h
1 // -*-c++-*-
2 //----------------------------------------------------------------------------
3 // xc utils library; general purpose classes and functions.
4 //
5 // Copyright (C) Luis C. PĂ©rez Tato
6 //
7 // XC utils is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // This software is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program.
19 // If not, see <http://www.gnu.org/licenses/>.
20 //----------------------------------------------------------------------------
21 //puntero.h
22 #ifndef PUNTERO_H
23 #define PUNTERO_H
24 
25 #include <cstdlib>
26 #include <cassert>
27 #include <functional>
28 
29 template <class N>
30 class puntero
31  {
32  private:
33  N *ptr;
34  public:
35  puntero(void): ptr(NULL) {}
36  puntero(N *const p): ptr(p) {}
37  puntero(const puntero<N> &otro): ptr(otro.ptr) {}
38  puntero<N> &operator =(N *const p)
39  {
40  ptr= p;
41  return *this;
42  }
43  puntero<N> &operator =(const puntero<N> &otro)
44  {
45  ptr= otro.ptr;
46  return *this;
47  }
48  ~puntero(void) { ptr= NULL; }
49  inline bool nulo(void) const
50  { return (ptr==NULL); }
51  N &operator *(void) const
52  {
53  assert(!nulo());
54  return *ptr;
55  }
56  inline operator const N *(void) const
57  { return ptr; }
58  inline operator N *const(void) const
59  { return ptr; }
60  inline operator N*(void)
61  { return ptr; }
62  inline operator const bool(void) const
63  { return ptr; }
64  inline void put(N *const p)
65  { ptr= p; }
66  inline N* operator->(void)
67  { return ptr; }
68  inline const N* operator->(void) const
69  { return ptr; }
70  inline N *get_ptr(void) const
71  { return ptr; }
72  inline N *const get_const_ptr(void) const
73  { return ptr; }
74  inline bool operator==(const puntero<N> &p2) const
75  { return (ptr == p2.ptr); }
76  inline bool operator==(const N *const p2) const
77  { return (ptr == p2); }
78  bool operator!=(const N *const p2) const
79  { return (ptr != p2); }
80  friend bool operator ==(const N *const p1,const puntero<N> &p2)
81  { return (p1 == p2.ptr); }
82  friend bool operator !=(const N *const p1,const puntero<N> &p2)
83  { return (p1 == p2.ptr); }
84  };
85 
86 #endif
Definition: puntero.h:30