Edinburgh Speech Tools  2.1-release
EST_THandle.h
Go to the documentation of this file.
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 #include "EST_bool.h"
40 
41 /** @class EST_THandle
42  * @ingroup supportclasses
43  *
44  * A `smart' pointer which does reference counting.
45  *
46  * Behaves almost like a pointer as far as naive code is concerned, but
47  * keeps count of how many handles are holding on to the contents
48  * and deletes it when there are none.
49  *
50  * You need to be careful there are no dumb C++ pointers to things which
51  * are being handled this way.
52  *
53  * Things to be handled should implement the same interface as EST_Handleable
54  * (either by taking that as a superclass or by reimplementing it) and in
55  * addition define `object_ptr()`. See EST_TBox for an example.
56  *
57  * There are two parameter types. In most cases the thing which contains the
58  * reference count and the data it represents will be the same object, but
59  * in the case of boxed values it may not be, so you can specify the type
60  * of both independently.
61  *
62  * @see EST_Handleable
63  * @see EST_TBox
64  * @see EST_THandle:example
65  *
66  * @author Richard Caley <rjc@cstr.ed.ac.uk>
67  * @version $Id: EST_THandle.h,v 1.5 2006/07/19 21:52:12 awb Exp $
68  */
69 template<class BoxT, class ObjectT>
70 class EST_THandle {
71 
72 private:
73  BoxT *ptr;
74 
75 public:
76 
77  EST_THandle(void) { ptr = (BoxT *)NULL; }
78 
79  EST_THandle(BoxT *p) { if ((ptr=p)) p->inc_refcount(); }
80 
81  EST_THandle(const EST_THandle &cp) {
82  ptr=cp.ptr;
83  if (ptr)
84  ptr->inc_refcount();
85  }
86 
87  ~EST_THandle(void) {
88  if (ptr)
89  ptr->dec_refcount();
90  if (ptr && ptr->is_unreferenced())
91  delete ptr;
92  }
93 
94  bool null() const { return ptr == NULL; }
95 
96  int shareing(void) const { return ptr?(ptr->refcount() > 1):0; }
97 
99  // doing it in this order means self assignment is safe.
100  if (h.ptr)
101  (h.ptr)->inc_refcount();
102  if (ptr)
103  ptr->dec_refcount();
104  if (ptr && ptr->is_unreferenced())
105  delete ptr;
106  ptr=h.ptr;
107  return *this;
108  }
109 
110  // If they manage to get hold of one...
111  // Actually usually used to assign NULL and so (possibly) deallocate
112  // the object currently pointed to.
113  EST_THandle &operator = (BoxT *t_ptr) {
114  // doing it in this order means self assignment is safe.
115  if (t_ptr)
116  t_ptr->inc_refcount();
117  if (ptr)
118  ptr->dec_refcount();
119  if (ptr && ptr->is_unreferenced())
120  delete ptr;
121  ptr=t_ptr;
122  return *this;
123  }
124 
125  operator ObjectT *() {
126  return ptr?(ptr->object_ptr()):(ObjectT *)NULL;
127  }
128 
129  operator const ObjectT *() const {
130  return ptr?(ptr->object_ptr()):(const ObjectT *)NULL;
131  }
132 
133 
134  int operator == (const BoxT *p) const { return ptr == p; }
135  int operator != (const BoxT *p) const { return !(*this == p); }
136 
137  const ObjectT& operator *() const { return *(ptr->object_ptr()); }
138  ObjectT& operator *() { return *(ptr->object_ptr()); }
139  const ObjectT* operator ->() const { return (ptr->object_ptr()); }
140  ObjectT* operator ->() { return (ptr->object_ptr()); }
141 
143  {return a.ptr==b.ptr; }
145  { return !( a==b ); }
146 
147  friend std::ostream & operator << (std::ostream &s, const EST_THandle< BoxT, ObjectT > &a)
148  { (void)a; return s << "HANDLE"; }
149 };
150 
151 #endif
const ObjectT * operator->() const
Definition: EST_THandle.h:139
EST_THandle(void)
Definition: EST_THandle.h:77
EST_THandle & operator=(EST_THandle h)
Definition: EST_THandle.h:98
const ObjectT & operator*() const
Definition: EST_THandle.h:137
~EST_THandle(void)
Definition: EST_THandle.h:87
bool null() const
Definition: EST_THandle.h:94
NULL
Definition: EST_WFST.cc:55
int operator==(const BoxT *p) const
Definition: EST_THandle.h:134
EST_THandle(BoxT *p)
Definition: EST_THandle.h:79
int shareing(void) const
Definition: EST_THandle.h:96
int operator!=(const BoxT *p) const
Definition: EST_THandle.h:135
EST_THandle(const EST_THandle &cp)
Definition: EST_THandle.h:81