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