Edinburgh Speech Tools  2.1-release
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EST_THandle.h
1  /************************************************************************/
2  /* */
3  /* Centre for Speech Technology Research */
4  /* University of Edinburgh, UK */
5  /* Copyright (c) 1996,1997 */
6  /* All Rights Reserved. */
7  /* */
8  /* Permission is hereby granted, free of charge, to use and distribute */
9  /* this software and its documentation without restriction, including */
10  /* without limitation the rights to use, copy, modify, merge, publish, */
11  /* distribute, sublicense, and/or sell copies of this work, and to */
12  /* permit persons to whom this work is furnished to do so, subject to */
13  /* the following conditions: */
14  /* 1. The code must retain the above copyright notice, this list of */
15  /* conditions and the following disclaimer. */
16  /* 2. Any modifications must be clearly marked as such. */
17  /* 3. Original authors' names are not deleted. */
18  /* 4. The authors' names are not used to endorse or promote products */
19  /* derived from this software without specific prior written */
20  /* permission. */
21  /* */
22  /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23  /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24  /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25  /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26  /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27  /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28  /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29  /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30  /* THIS SOFTWARE. */
31  /* */
32  /*************************************************************************/
33 
34 #ifndef __EST_THANDLE_H__
35 #define __EST_THANDLE_H__
36 
37 #include <iostream>
38 
39 using namespace std;
40 
41 #include "EST_bool.h"
42 
43 /** @class EST_THandle
44  * @ingroup supportclasses
45  *
46  * A `smart' pointer which does reference counting.
47  *
48  * Behaves almost like a pointer as far as naive code is concerned, but
49  * keeps count of how many handles are holding on to the contents
50  * and deletes it when there are none.
51  *
52  * You need to be careful there are no dumb C++ pointers to things which
53  * are being handled this way.
54  *
55  * Things to be handled should implement the same interface as EST_Handleable
56  * (either by taking that as a superclass or by reimplementing it) and in
57  * addition define `object_ptr()`. See EST_TBox for an example.
58  *
59  * There are two parameter types. In most cases the thing which contains the
60  * reference count and the data it represents will be the same object, but
61  * in the case of boxed values it may not be, so you can specify the type
62  * of both independently.
63  *
64  * @see EST_Handleable
65  * @see EST_TBox
66  * @see EST_THandle:example
67  *
68  * @author Richard Caley <rjc@cstr.ed.ac.uk>
69  * @version $Id: EST_THandle.h,v 1.5 2006/07/19 21:52:12 awb Exp $
70  */
71 template<class BoxT, class ObjectT>
72 class EST_THandle {
73 
74 private:
75  BoxT *ptr;
76 
77 public:
78 
79  EST_THandle(void) { ptr = (BoxT *)NULL; }
80 
81  EST_THandle(BoxT *p) { if ((ptr=p)) p->inc_refcount(); }
82 
83  EST_THandle(const EST_THandle &cp) {
84  ptr=cp.ptr;
85  if (ptr)
86  ptr->inc_refcount();
87  }
88 
89  ~EST_THandle(void) {
90  if (ptr)
91  ptr->dec_refcount();
92  if (ptr && ptr->is_unreferenced())
93  delete ptr;
94  }
95 
96  bool null() const { return ptr == NULL; }
97 
98  int shareing(void) const { return ptr?(ptr->refcount() > 1):0; }
99 
100  EST_THandle &operator = (EST_THandle h) {
101  // doing it in this order means self assignment is safe.
102  if (h.ptr)
103  (h.ptr)->inc_refcount();
104  if (ptr)
105  ptr->dec_refcount();
106  if (ptr && ptr->is_unreferenced())
107  delete ptr;
108  ptr=h.ptr;
109  return *this;
110  }
111 
112  // If they manage to get hold of one...
113  // Actually usually used to assign NULL and so (possibly) deallocate
114  // the object currently pointed to.
115  EST_THandle &operator = (BoxT *t_ptr) {
116  // doing it in this order means self assignment is safe.
117  if (t_ptr)
118  t_ptr->inc_refcount();
119  if (ptr)
120  ptr->dec_refcount();
121  if (ptr && ptr->is_unreferenced())
122  delete ptr;
123  ptr=t_ptr;
124  return *this;
125  }
126 
127  operator ObjectT *() {
128  return ptr?(ptr->object_ptr()):(ObjectT *)NULL;
129  }
130 
131  operator const ObjectT *() const {
132  return ptr?(ptr->object_ptr()):(const ObjectT *)NULL;
133  }
134 
135 
136  int operator == (const BoxT *p) const { return ptr == p; }
137  int operator != (const BoxT *p) const { return !(*this == p); }
138 
139  const ObjectT& operator *() const { return *(ptr->object_ptr()); }
140  ObjectT& operator *() { return *(ptr->object_ptr()); }
141  const ObjectT* operator ->() const { return (ptr->object_ptr()); }
142  ObjectT* operator ->() { return (ptr->object_ptr()); }
143 
144  friend int operator == (const EST_THandle< BoxT, ObjectT > &a, const EST_THandle< BoxT, ObjectT > & b)
145  {return a.ptr==b.ptr; }
146  friend int operator != (const EST_THandle< BoxT, ObjectT > &a, const EST_THandle< BoxT, ObjectT > & b)
147  { return !( a==b ); }
148 
149  friend ostream & operator << (ostream &s, const EST_THandle< BoxT, ObjectT > &a)
150  { return s << "HANDLE"; }
151 };
152 
153 #endif