Edinburgh Speech Tools  2.1-release
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
EST_Val.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 /* */
37 /* A generic container class, originally for ints floats and string now */
38 /* extended for some others, eventually allow run addition of new types */
39 /* "built-in" types (i.e. ones explicitly mentioned in this file) may */
40 /* be accessed by member functions, objects added at run time may only */
41 /* be accessed by functions */
42 /* */
43 /* This is so similar to the LISP class in SIOD it could be viewed as a */
44 /* little embarrassing, but this is done without a cons cell heap or gc */
45 /* which may or may not be a good thing. */
46 /* */
47 /*=======================================================================*/
48 #ifndef __EST_VAL_H__
49 #define __EST_VAL_H__
50 
51 #include "EST_String.h"
52 #include "EST_error.h"
53 #include "EST_Contents.h"
54 #include "EST_Val_defs.h"
55 
56 typedef const char *val_type;
57 
58 extern val_type val_unset;
59 extern val_type val_int;
60 extern val_type val_float;
61 extern val_type val_string;
62 
63 /** @defgroup estlingclasses Linguistic classes
64  * List of all linguistic classes
65  */
66 
67 /** @class EST_Val
68  @ingroup estlingclasses
69  The EST_Val class is a container class, used to store a single
70  item which can be an int, float, string or other user-defined
71  class. It is often used as the base item in the EST_Features
72  class, to enable features
73  to take on values of different types.
74 */
75 class EST_Val {
76  private:
77  val_type t;
78  union
79  { int ival;
80  float fval;
81  EST_Contents *pval;} v;
82  // * may have a string name as well as a value
83  EST_String sval;
84  const int to_int() const;
85  const float to_flt() const;
86  const EST_String &to_str() const;
87  public:
88  /** @name Constructor and Destructor functions
89  */
90 
91  ///@{
92  /** Default constructor */
94  {t=val_unset;}
95 
96  /** Copy constructor for another EST_Val*/
97  EST_Val(const EST_Val &val);
98 
99  /** Copy constructor for an int*/
100  EST_Val(const int i)
101  {t=val_int; v.ival=i;}
102 
103  /** Copy constructor for a float*/
104  EST_Val(const float f)
105  {t=val_float; v.fval=f;}
106 
107  /** Copy constructor for a double*/
108  EST_Val(const double d) {t=val_float; v.fval=d;}
109 
110  /** Copy constructor for a string*/
111  // EST_Val(const EST_String &s) {t=val_string; sval = s;}
112  EST_Val(const EST_String &s) : t(val_string), sval(s) {};
113 
114  /** Copy constructor for a string literal*/
115  // EST_Val(const char *s) {t=val_string; sval = s;}
116  EST_Val(const char *s) : t(val_string), sval(s) {};
117 
118  EST_Val(val_type type,void *p, void (*f)(void *));
119 
120  /** Destructor */
121  ~EST_Val(void);
122 
123  ///@}
124 
125  /**@name Getting cast values
126  */
127 
128  ///@{
129 
130  /** returns the type that the val is currently holding */
131  const val_type type(void) const
132  {return t;}
133 
134  /** returns the value, cast as an int */
135  const int Int(void) const
136  {if (t==val_int) return v.ival; return to_int();}
137 
138  /** returns the value, cast as an int */
139  const int I(void) const
140  { return Int(); }
141 
142  /** returns the value, cast as a float */
143  const float Float(void) const
144  {if (t==val_float) return v.fval; return to_flt();}
145 
146  /** returns the value, cast as a float */
147  const float F(void) const
148  { return Float(); }
149 
150  /** returns the value, cast as a string */
151  const EST_String &String(void) const
152  {if (t!=val_string) to_str(); return sval;}
153 
154  /** returns the value, cast as a string */
155  const EST_String &string(void) const
156  {return String();}
157 
158  /** returns the value, cast as a string */
159  const EST_String &S(void) const
160  {return String();}
161 
162  /** returns the value, cast as a string */
163  const EST_String &string_only(void) const {return sval;}
164 
165  ///@}
166 
167  // Humans should never call this only automatic functions
168  const void *internal_ptr(void) const
169  { return v.pval->get_contents(); }
170 
171  /**@name Setting values
172  */
173 
174  ///@{
175 
176  /** Assignment of val to an int */
177  EST_Val &operator=(const int i) { t=val_int; v.ival=i; return *this;}
178 
179  /** Assignment of val to a float */
180  EST_Val &operator=(const float f) { t=val_float; v.fval=f; return *this;}
181 
182  /** Assignment of val to a double */
183  EST_Val &operator=(const double d) { t=val_float; v.fval=d; return *this;}
184 
185  /** Assignment of val to a string */
186  EST_Val &operator=(const EST_String &s) { t=val_string; sval = s; return *this;}
187 
188  /** Assignment of val to a string literal*/
189  EST_Val &operator=(const char *s) { t=val_string; sval = s; return *this;}
190 
191  /** Assignment of val to another val*/
192  EST_Val &operator=(const EST_Val &c);
193 
194  ///@}
195 
196  /**@name Equivalence test
197  */
198 
199  ///@{
200 
201 
202  /** Test whether val is equal to a*/
203  int operator ==(const EST_Val &a) const
204  { if (t != a.t) return (1==0);
205  else if (t == val_string) return (sval == a.sval);
206  else if (t == val_int) return (v.ival == a.v.ival);
207  else if (t == val_float) return (v.fval == a.v.fval);
208  else return (internal_ptr() == a.internal_ptr()); }
209 
210  /** Test whether val is equal to the string a*/
211  int operator ==(const EST_String &a) const { return (string() == a); }
212  /** Test whether val is equal to the char * a*/
213  int operator ==(const char *a) const { return (string() == a); }
214  /** Test whether val is equal to the int a*/
215  int operator ==(const int &i) const { return (Int() == i); }
216  /** Test whether val is equal to the float a*/
217  int operator ==(const float &f) const { return (Float() == f); }
218  /** Test whether val is equal to the double a*/
219  int operator ==(const double &d) const { return (Float() == d); }
220 
221 
222  /** Test whether val is not equal to the val a*/
223  int operator !=(const EST_Val &a) const { return (!(*this == a)); }
224  /** Test whether val is not equal to the string a*/
225  int operator !=(const EST_String &a) const { return (string() != a); }
226  /** Test whether val is not equal to the char * a*/
227  int operator !=(const char *a) const { return (string() != a); }
228  /** Test whether val is not equal to the int a*/
229  int operator !=(const int &i) const { return (Int() != i);}
230  /** Test whether val is not equal to the float a*/
231  int operator !=(const float &f) const { return (Float() != f); }
232  /** Test whether val is not equal to the double float a*/
233  int operator !=(const double &d) const { return (Float() != d); }
234 
235  ///@{
236 
237  /**@name Automatic casting
238  */
239  ///@{
240 
241  /** Automatically cast val as an int*/
242  operator int() const { return Int(); }
243  /** Automatically cast val as an float*/
244  operator float() const { return Float(); }
245  /** Automatically cast val as an string*/
246  operator EST_String() const { return string(); }
247  ///@}
248  /** print val*/
249  friend ostream& operator << (ostream &s, const EST_Val &a)
250  { if (a.type() == val_unset) s << "[VAL unset]" ;
251  else if (a.type() == val_int) s << a.v.ival;
252  else if (a.type() == val_float) s << a.v.fval;
253  else if (a.type() == val_string) s << a.sval;
254  else s << "[PVAL " << a.type() << "]";
255  return s;
256  }
257 };
258 ///@}
259 
260 inline const char *error_name(const EST_Val val) { return (EST_String)val;}
261 
262 // For consistency with other (user-defined) types in val
263 inline EST_Val est_val(const EST_String s) { return EST_Val(s); }
264 inline EST_Val est_val(const char *s) { return EST_Val(s); }
265 inline int Int(const EST_Val &v) { return v.Int(); }
266 inline EST_Val est_val(const int i) { return EST_Val(i); }
267 inline float Float(const EST_Val &v) { return v.Float(); }
268 inline EST_Val est_val(const float f) { return EST_Val(f); }
269 
270 #endif
const int I(void) const
Definition: EST_Val.h:139
EST_Val & operator=(const EST_String &s)
Definition: EST_Val.h:186
EST_Val(const float f)
Definition: EST_Val.h:104
const int Int(void) const
Definition: EST_Val.h:135
EST_Val(const int i)
Definition: EST_Val.h:100
EST_Val(const EST_String &s)
Definition: EST_Val.h:112
const float Float(void) const
Definition: EST_Val.h:143
const val_type type(void) const
Definition: EST_Val.h:131
EST_Val(const double d)
Definition: EST_Val.h:108
friend ostream & operator<<(ostream &s, const EST_Val &a)
Definition: EST_Val.h:249
EST_Val & operator=(const double d)
Definition: EST_Val.h:183
int operator==(const EST_Val &a) const
Definition: EST_Val.h:203
const EST_String & string(void) const
Definition: EST_Val.h:155
EST_Val & operator=(const char *s)
Definition: EST_Val.h:189
const EST_String & string_only(void) const
Definition: EST_Val.h:163
EST_Val()
Definition: EST_Val.h:93
const EST_String & String(void) const
Definition: EST_Val.h:151
const EST_String & S(void) const
Definition: EST_Val.h:159
const float F(void) const
Definition: EST_Val.h:147
EST_Val(const char *s)
Definition: EST_Val.h:116
EST_Val & operator=(const int i)
Definition: EST_Val.h:177
EST_Val & operator=(const float f)
Definition: EST_Val.h:180
int operator!=(const EST_Val &a) const
Definition: EST_Val.h:223
~EST_Val(void)
Definition: EST_Val.cc:71