casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ptr.h
Go to the documentation of this file.
1 //# ptr.h: pointer classes (currently only counted pointer) used within casadbus
2 //#
3 //# Copyright (C) 2011
4 //# Associated Universities, Inc. Washington DC, USA.
5 //#
6 //# This library is free software; you can redistribute it and/or modify it
7 //# under the terms of the GNU Library General Public License as published by
8 //# the Free Software Foundation; either version 2 of the License, or (at your
9 //# option) any later version.
10 //#
11 //# This library is distributed in the hope that it will be useful, but WITHOUT
12 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 //# License for more details.
15 //#
16 //# You should have received a copy of the GNU Library General Public License
17 //# along with this library; if not, write to the Free Software Foundation,
18 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
19 //#
20 //# Correspondence concerning AIPS++ should be addressed as follows:
21 //# Internet email: aips2-request@nrao.edu.
22 //# Postal address: AIPS++ Project Office
23 //# National Radio Astronomy Observatory
24 //# 520 Edgemont Road
25 //# Charlottesville, VA 22903-2475 USA
26 //#
27 //# $Id$
28 
29 #ifndef __casadbus_ptr_h__
30 #define __casadbus_ptr_h__
31 #include <string>
32 #include <stdio.h>
33 
34 namespace casa {
35 
36  namespace memory {
37  template <class T> class cptr {
38  public:
39  cptr( ) : ptr((T*)0) { }
40  cptr(T *p) : ptr(p) { }
41  cptr<T>(const cptr<T> &other) : ptr(other.ptr) { }
42  cptr<T>(cptr<T> *other) : ptr(other->ptr) { }
43  T *operator->( ) { return ptr.val; }
44  T &operator*( ) { return *ptr.val; }
45 
46  const T *operator->( ) const { return ptr.val; }
47  const T &operator*( ) const { return *ptr.val; }
48 
49  bool isNull( ) const { return ptr.isNull( ); }
50  const cptr<T> &operator=( const cptr<T> &other ) { ptr = other.ptr; return *this; }
51  const cptr<T> &operator=( T *&optr ) { ptr = optr; optr = 0; return *this; }
52  std::string state( ) const {
53  char buf[128];
54  sprintf( buf, "0x%lx (%d)", (unsigned long) ptr.val, *ptr.count );
55  return std::string(buf);
56  }
57  void clear( ) { ptr.clear( ); }
58  unsigned int count( ) const { return *ptr.count; }
59 
60  private:
61  template <class X> friend bool operator==(const cptr<X>&, X*);
62  template <class X> friend bool operator==(X*, const cptr<X>&);
63 
64  struct kernel {
65  T *val;
66  unsigned int *count;
67  kernel(T *v) : val(v), count(new unsigned int) { *count = 1u; }
68  kernel( const kernel &other ) : val(other.val), count(other.count) { *count += 1u; }
69  ~kernel( ) { release( ); }
70  void operator=( const kernel &other ) { release( ); val = other.val; count = other.count; *count += 1u; }
71  void operator=( T *oval ) { release( ); val = oval; count = new unsigned int; *count = 1u; }
72  void release( ) { if ( --*count == 0u ) { delete val; delete count; } }
73  bool isNull( ) const { return val == 0 ? true : false; }
74  void clear( ) { release( ); val = 0; count = new unsigned int; *count = 1u; }
75  bool eq( T *x ) const { return val == x; }
76  };
77  kernel ptr;
78  };
79 
80  template<class T> bool operator==( const cptr<T> &l, T *r ) { return l.ptr.eq(r); }
81  template<class T> bool operator==( T *l, const cptr<T> &r ) { return r.ptr.eq(l); }
82 
83  // considered introducing something like:
84  //
85  // template <class D, class B> class cptr_ref {
86  // public:
87  // cptr_ref(cptr<D> &);
88  // cptr_ref( const cptr_ref<D,B> &other);
89  // B *operator->( ) { return ref.operator->( ); }
90  // B &operator*( ) { return ref.operator*( ); }
91  // };
92  //
93  // to represent a base class (B) reference to a counted pointer based on
94  // a derived class (D). However, the derived class is still intertwined
95  // with the type so it falls short for a number of derived classes.
96  }
97 
98  using memory::operator==;
99 
100 }
101 
102 #endif
103 
104 
const T * operator->() const
Definition: ptr.h:46
friend bool operator==(const cptr< X > &, X *)
kernel(const kernel &other)
Definition: ptr.h:68
void operator=(T *oval)
Definition: ptr.h:71
std::string state() const
Definition: ptr.h:52
kernel ptr
Definition: ptr.h:77
T & operator*()
Definition: ptr.h:44
cptr(T *p)
Definition: ptr.h:40
void operator=(const kernel &other)
Definition: ptr.h:70
bool operator==(const cptr< T > &l, T *r)
Definition: ptr.h:80
unsigned int count() const
Definition: ptr.h:58
const cptr< T > & operator=(T *&optr)
Definition: ptr.h:51
T * operator->()
Definition: ptr.h:43
bool isNull() const
Definition: ptr.h:49
bool isNull() const
Definition: ptr.h:73
const cptr< T > & operator=(const cptr< T > &other)
Definition: ptr.h:50
void clear()
Definition: ptr.h:57
unsigned int * count
Definition: ptr.h:66
bool eq(T *x) const
Definition: ptr.h:75
const T & operator*() const
Definition: ptr.h:47