Edinburgh Speech Tools  2.1-release
handle_example.cc
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  /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */
35  /* Date: Wed Mar 18 1998 */
36  /* */
37  /* -------------------------------------------------------------------- */
38  /* Example of using the THandle reference counted pointer type. */
39  /* */
40  /*************************************************************************/
41 
42 
43 #include <cstdlib>
44 #include <fstream>
45 #include <iostream>
46 #include "EST_Handleable.h"
47 #include "EST_THandle.h"
48 #include "EST_TBox.h"
49 #include "EST_String.h"
50 
51 using namespace std;
52 
53 
54 /**@name EST_THandle:example
55  *
56  * Example of using the THandle reference counted pointer type.
57  *
58  * @see EST_THandle
59  */
60 //@{
61 
62 /** A simple object which can be handled and reference counted.
63  */
64 
65 class HandleableThing : public EST_Handleable
66 {
67 private:
68  EST_String p_name;
69 
70 public:
71  HandleableThing(EST_String name)
72  {
73  p_name=name;
74  start_refcounting();
75  cout << "[create-" << name << "]\n";
76  }
77 
78  ~HandleableThing(void)
79  { cout << "[destroy-" << p_name << "]\n"; }
80 
81  EST_String name(void) const { return p_name; }
82 
83  friend ostream& operator << (ostream &st, const HandleableThing &t);
84 
85  HandleableThing *object_ptr() { return this; }
86  const HandleableThing *object_ptr() const { return this; }
87 };
88 
89 ostream &operator << (ostream &st, const HandleableThing &t)
90 {
91  return st << "<<" << (const char *)t.name() << "/" << t.refcount() << ">>";
92 }
93 
95 
96 /** A simple object which doesn't understand reference counting.
97  */
98 
99 class Thing
100 {
101 private:
102  EST_String p_name;
103 
104 public:
105  Thing(EST_String name)
106  {
107  p_name=name;
108  cout << "[create-" << name << "]\n";
109  }
110 
111  ~Thing(void)
112  { cout << "[destroy-" << p_name << "]\n"; }
113 
114  EST_String name(void) const { return p_name; }
115 
116  friend ostream& operator << (ostream &st, const EST_TBox<Thing> &t);
117  friend ostream& operator << (ostream &st, const Thing &t);
118 
119  Thing *object_ptr() { return this; }
120  const Thing *object_ptr() const { return this; }
121 };
122 
123 ostream &operator << (ostream &st, const EST_TBox<Thing> &t)
124 {
125  return st << "<<[[" << t.c()->name() << "/" << t.refcount() << "]]>>";
126 }
127 
128 ostream &operator << (ostream &st, const Thing &t)
129 {
130  return st << "{" << t.name() << "}";
131 }
132 
133 typedef EST_TBox<Thing> BoxedThing; // decl
135 
136 void unboxed(void)
137 {
138  cout << "\n\nUnboxed Examples\n";
139  HandleableThingP pa;
140  HandleableThingP pb;
141 
142  pa = new HandleableThing("apple");
143  pb = new HandleableThing("banana");
144  HandleableThingP pc = new HandleableThing("cherry");
145 
146  cout << *pa
147  << " " << *pb
148  << "\n";
149 
150  pc=pa;
151 
152  cout << *pa
153  << " " << *pb
154  << "\n";
155 
156  pc = pb;
157 
158  cout << *pa
159  << " " << *pb
160  << "\n";
161 
162  pa = NULL;
163 
164  cout << "NULL"
165  << " " << *pb
166  << "\n";
167 
168  pa = new HandleableThing("pie");
169  cout << *pa
170  << " " << *pb
171  << "\n";
172 
173  pb = new HandleableThing("split");
174  pc = new HandleableThing("cheesecake");
175  cout << *pa
176  << " " << *pb
177  << "\n";
178 
179 
180 }
181 
182 void boxed(void)
183 {
184  cout << "\n\nBoxed Examples\n";
185  BoxedThingP pa;
186  BoxedThingP pb;
187 
188  pa = new BoxedThing(new Thing("aubergene"));
189  pb = new BoxedThing(new Thing("brocoli"));
190  BoxedThingP pc = new BoxedThing(new Thing("cauliflower"));
191 
192  cout << *pa
193  << " " << *pb
194  << "\n";
195 
196  pc=pa;
197 
198  cout << *pa
199  << " " << *pb
200  << "\n";
201 
202  pc = pb;
203 
204  cout << *pa
205  << " " << *pb
206  << "\n";
207 
208  pa = NULL;
209 
210  cout << "NULL"
211  << " " << *pb
212  << "\n";
213 
214  pa = new BoxedThing(new Thing("pate"));
215  cout << *pa
216  << " " << *pb
217  << "\n";
218 
219  pb = new BoxedThing(new Thing("quiche"));
220  pc = new BoxedThing(new Thing("cheese"));
221  cout << *pa
222  << " " << *pb
223  << "\n";
224 
225 
226 }
227 
228 int main(void)
229 {
230  unboxed();
231  boxed();
232  exit(0);
233 }
234 
235 #ifdef INSTANTIATE_TEMPLATES
237 template class EST_THandle<BoxedThing,Thing>;
238 template class EST_TBox<Thing>;
239 #endif
240 
241 //@}
void unboxed(void)
Reference Counting Interface.
EST_THandle< HandleableThing, HandleableThing > HandleableThingP
NULL
Definition: EST_WFST.cc:55
EST_TBox< Thing > BoxedThing
int main(void)
ostream & operator<<(ostream &st, const HandleableThing &t)
void boxed(void)
EST_THandle< BoxedThing, Thing > BoxedThingP