Edinburgh Speech Tools  2.1-release
EST_Val.h
Go to the documentation of this file.
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 <cstdlib>
52 #include "EST_String.h"
53 #include "EST_error.h"
54 #include "EST_Contents.h"
55 #include "EST_Val_defs.h"
56 
57 typedef const char *val_type;
58 
59 extern val_type val_unset;
60 extern val_type val_int;
61 extern val_type val_float;
62 extern val_type val_string;
63 
64 /** @defgroup estlingclasses Linguistic classes
65  * List of all linguistic classes
66  */
67 
68 /** @class EST_Val
69  @ingroup estlingclasses
70  The EST_Val class is a container class, used to store a single
71  item which can be an int, float, string or other user-defined
72  class. It is often used as the base item in the EST_Features
73  class, to enable features to take on values of different types.
74 */
75 class EST_Val {
76  private:
77  val_type t;
78  union
80  float fval;
82  // * may have a string name as well as a value
83  EST_String sval;
84  ssize_t to_int() const;
85  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  #if (SSIZE_MAX != INT_MAX)
100  /** Copy constructor for an int*/
101  EST_Val(const int i)
102  {t=val_int; v.ival=i;}
103  #endif
104 
105  /** Copy constructor for an ssize_t*/
106  EST_Val(const ssize_t i)
107  {t=val_int; v.ival=i;}
108 
109  /** Copy constructor for a float*/
110  EST_Val(const float f)
111  {t=val_float; v.fval=f;}
112 
113  /** Copy constructor for a double*/
114  EST_Val(const double d) {t=val_float; v.fval=d;}
115 
116  /** Copy constructor for a string*/
117  // EST_Val(const EST_String &s) {t=val_string; sval = s;}
118  EST_Val(const EST_String &s) : t(val_string), sval(s) {};
119 
120  /** Copy constructor for a string literal*/
121  // EST_Val(const char *s) {t=val_string; sval = s;}
122  EST_Val(const char *s) : t(val_string), sval(s) {};
123 
124  EST_Val(val_type type,void *p, void (*f)(void *));
125 
126  /** Destructor */
127  ~EST_Val(void);
128 
129  ///@}
130 
131  /**@name Getting cast values
132  */
133 
134  ///@{
135 
136  /** returns the type that the val is currently holding */
137  val_type type(void) const
138  {return t;}
139 
140  /** returns the value, cast as an int */
141  int Int(void) const
142  {if (t==val_int) return v.ival; return to_int();}
143 
144  /** returns the value, cast as an int */
145  ssize_t I(void) const
146  { return Int(); }
147 
148  /** returns the value, cast as a float */
149  float Float(void) const
150  {if (t==val_float) return v.fval; return to_flt();}
151 
152  /** returns the value, cast as a float */
153  float F(void) const
154  { return Float(); }
155 
156  /** returns the value, cast as a string */
157  const EST_String &String(void) const
158  {if (t!=val_string) to_str(); return sval;}
159 
160  /** returns the value, cast as a string */
161  const EST_String &string(void) const
162  {return String();}
163 
164  /** returns the value, cast as a string */
165  const EST_String &S(void) const
166  {return String();}
167 
168  /** returns the value, cast as a string */
169  const EST_String &string_only(void) const {return sval;}
170 
171  ///@}
172 
173  // Humans should never call this only automatic functions
174  const void *internal_ptr(void) const
175  { return v.pval->get_contents(); }
176 
177  /**@name Setting values
178  */
179 
180  ///@{
181 
182  #if (SSIZE_MAX != INT_MAX)
183  /** Assignment of val to an int */
184  EST_Val &operator=(const int i) { t=val_int; v.ival=i; return *this;}
185  #endif
186 
187  /** Assignment of val to an ssize_t */
188  EST_Val &operator=(ssize_t i) { t=val_int; v.ival=i; return *this;}
189 
190  /** Assignment of val to a float */
191  EST_Val &operator=(const float f) { t=val_float; v.fval=f; return *this;}
192 
193  /** Assignment of val to a double */
194  EST_Val &operator=(const double d) { t=val_float; v.fval=d; return *this;}
195 
196  /** Assignment of val to a string */
197  EST_Val &operator=(const EST_String &s) { t=val_string; sval = s; return *this;}
198 
199  /** Assignment of val to a string literal*/
200  EST_Val &operator=(const char *s) { t=val_string; sval = s; return *this;}
201 
202  /** Assignment of val to another val*/
203  EST_Val &operator=(const EST_Val &c);
204 
205  ///@}
206 
207  /**@name Equivalence test
208  */
209 
210  ///@{
211 
212 
213  /** Test whether val is equal to a*/
214  int operator ==(const EST_Val &a) const
215  { if (t != a.t) return (1==0);
216  else if (t == val_string) return (sval == a.sval);
217  else if (t == val_int) return (v.ival == a.v.ival);
218  else if (t == val_float) return (v.fval == a.v.fval);
219  else return (internal_ptr() == a.internal_ptr()); }
220 
221  /** Test whether val is equal to the string a*/
222  int operator ==(const EST_String &a) const { return (string() == a); }
223  /** Test whether val is equal to the char * a*/
224  int operator ==(const char *a) const { return (string() == a); }
225  /** Test whether val is equal to the int a*/
226  int operator ==(const int &i) const { return (Int() == i); }
227  /** Test whether val is equal to the float a*/
228  int operator ==(const float &f) const { return (Float() == f); }
229  /** Test whether val is equal to the double a*/
230  int operator ==(const double &d) const { return (Float() == d); }
231 
232 
233  /** Test whether val is not equal to the val a*/
234  int operator !=(const EST_Val &a) const { return (!(*this == a)); }
235  /** Test whether val is not equal to the string a*/
236  int operator !=(const EST_String &a) const { return (string() != a); }
237  /** Test whether val is not equal to the char * a*/
238  int operator !=(const char *a) const { return (string() != a); }
239  /** Test whether val is not equal to the int a*/
240  int operator !=(const int &i) const { return (Int() != i);}
241  /** Test whether val is not equal to the float a*/
242  int operator !=(const float &f) const { return (Float() != f); }
243  /** Test whether val is not equal to the double float a*/
244  int operator !=(const double &d) const { return (Float() != d); }
245 
246  ///@{
247 
248  /**@name Automatic casting
249  */
250  ///@{
251  /** Automatically cast val as an ssize_t*/
252  operator ssize_t() const { return Int(); }
253  #if (SSIZE_MAX != INT_MAX)
254  /** Automatically cast val as an int*/
255  operator int() const { return Int(); }
256  #endif
257  /** Automatically cast val as an float*/
258  operator float() const { return Float(); }
259  /** Automatically cast val as an string*/
260  operator EST_String() const { return string(); }
261  ///@}
262  /** print val*/
263  friend ostream& operator << (ostream &s, const EST_Val &a)
264  { if (a.type() == val_unset) s << "[VAL unset]" ;
265  else if (a.type() == val_int) s << a.v.ival;
266  else if (a.type() == val_float) s << a.v.fval;
267  else if (a.type() == val_string) s << a.sval;
268  else s << "[PVAL " << a.type() << "]";
269  return s;
270  }
271 };
272 ///@}
273 
274 inline const char *error_name(const EST_Val val) { return (EST_String)val;}
275 
276 // For consistency with other (user-defined) types in val
277 inline EST_Val est_val(const EST_String s) { return EST_Val(s); }
278 inline EST_Val est_val(const char *s) { return EST_Val(s); }
279 inline int Int(const EST_Val &v) { return v.Int(); }
280 inline EST_Val est_val(const int i) { return EST_Val(i); }
281 inline float Float(const EST_Val &v) { return v.Float(); }
282 inline EST_Val est_val(const float f) { return EST_Val(f); }
283 
284 #endif
int Int(void) const
Definition: EST_Val.h:141
EST_Val & operator=(ssize_t i)
Definition: EST_Val.h:188
EST_Contents * pval
Definition: EST_Val.h:81
EST_Val & operator=(const EST_String &s)
Definition: EST_Val.h:197
val_type val_string
Definition: EST_Val.cc:46
EST_Val(const float f)
Definition: EST_Val.h:110
val_type val_unset
Definition: EST_Val.cc:43
const char * val_type
Definition: EST_Val.h:57
EST_Val(const EST_String &s)
Definition: EST_Val.h:118
int ssize_t
EST_Val(const double d)
Definition: EST_Val.h:114
friend ostream & operator<<(ostream &s, const EST_Val &a)
Definition: EST_Val.h:263
val_type val_float
Definition: EST_Val.cc:45
val_type type(void) const
Definition: EST_Val.h:137
EST_Val & operator=(const double d)
Definition: EST_Val.h:194
const void * internal_ptr(void) const
Definition: EST_Val.h:174
f
Definition: EST_item_aux.cc:48
int operator==(const EST_Val &a) const
Definition: EST_Val.h:214
const EST_String & string(void) const
Definition: EST_Val.h:161
getString int
Definition: EST_item_aux.cc:50
EST_Val & operator=(const char *s)
Definition: EST_Val.h:200
const EST_String & string_only(void) const
Definition: EST_Val.h:169
EST_Val()
Definition: EST_Val.h:93
const EST_String & String(void) const
Definition: EST_Val.h:157
float fval
Definition: EST_Val.h:80
const EST_String & S(void) const
Definition: EST_Val.h:165
val_type val_int
Definition: EST_Val.cc:44
EST_Val(const char *s)
Definition: EST_Val.h:122
EST_Val(const ssize_t i)
Definition: EST_Val.h:106
ssize_t ival
Definition: EST_Val.h:79
ssize_t I(void) const
Definition: EST_Val.h:145
EST_String
EST_Val est_val(const EST_String s)
Definition: EST_Val.h:277
EST_Val & operator=(const float f)
Definition: EST_Val.h:191
int operator!=(const EST_Val &a) const
Definition: EST_Val.h:234
~EST_Val(void)
Definition: EST_Val.cc:71
float F(void) const
Definition: EST_Val.h:153
float Float(void) const
Definition: EST_Val.h:149
const char * error_name(const EST_Val val)
Definition: EST_Val.h:274