Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
EST_Contents.h
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1995,1996 */
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 /* Author : Alan W Black */
34 /* Date : May 1996 */
35 /*-----------------------------------------------------------------------*/
36 /* A class for representing ints floats and strings */
37 /* */
38 /*=======================================================================*/
39 #ifndef __EST_CONTENTS_H__
40 #define __EST_CONTENTS_H__
41 
42 /** A class for containing some other (arbitrary) class
43  Not general enough to call itself a run-time type system
44  Is designed to solve the problem of holding user
45  specified information.
46  Keeps reference count to know when to delete contents
47 
48  This is done on two levels EST_Contents and Contents_Data
49 */
51  private:
52  int refs;
53  void *data;
54  void (*free_func)(void *data);
55  public:
56  EST_Content_Data(void *d,void (*f)(void *d)) {free_func=f; data=d; refs=1;}
57  ~EST_Content_Data() { free_func(data); }
58  ///
59  int unref() { return --refs; }
60  ///
61  int ref() { return ++refs; }
62  ///
63  int the_refs() { return refs; }
64  void *contents() { return data; }
65  EST_Content_Data &operator=(const EST_Content_Data &c)
66  {refs = c.refs; data = c.data; free_func = c.free_func; return *this;}
67 };
68 
69 /** More contents */
70 
72 private:
73  EST_Content_Data *content_data;
74  void unref_contents(void)
75  { if ((content_data != 0) &&
76  (content_data->unref() == 0))
77  delete content_data;}
78 public:
79  EST_Contents() { content_data = 0; }
80  EST_Contents(void *p,void (*free_func)(void *p))
81  { content_data = new EST_Content_Data(p,free_func); }
82  ~EST_Contents() { unref_contents(); }
83  void set_contents(void *p,void (*free_func)(void *p))
84  { unref_contents(); content_data = new EST_Content_Data(p,free_func);}
85  void *get_contents() const
86  {return (content_data ? content_data->contents() : 0);}
87  ///
88  int refs() const { return ((content_data == 0) ? 0 :
89  content_data->the_refs());}
90  EST_Contents &operator=(const EST_Contents &c)
91  { unref_contents();
92  content_data = c.content_data;
93  if (content_data != 0) content_data->ref();
94  return *this; }
95 };
96 
97 #endif
98