Edinburgh Speech Tools  2.1-release
EST_Val.cc
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 /* Class to represent ints, floats and strings */
37 /* and other arbitrary objects */
38 /*=======================================================================*/
39 #include <cstdlib>
40 #include "EST_Val.h"
41 #include "EST_string_aux.h"
42 
43 val_type val_unset = "unset";
44 val_type val_int = "int";
45 val_type val_float = "float";
46 val_type val_string = "string";
47 
49 {
50  if (c.t == val_string)
51  sval = c.sval;
52  else if (c.t == val_int)
53  v.ival = c.v.ival;
54  else if (c.t == val_float)
55  v.fval = c.v.fval;
56  else if (c.t != val_unset)
57  { // does references not a real copy
58  v.pval = new EST_Contents;
59  *v.pval = *c.v.pval;
60  }
61  t=c.t;
62 }
63 
64 EST_Val::EST_Val(val_type type,void *p, void (*f)(void *))
65 {
66  t=type;
67  v.pval = new EST_Contents;
68  v.pval->set_contents(p,f);
69 }
70 
72 {
73  if ((t != val_int) &&
74  (t != val_float) &&
75  (t != val_unset) &&
76  (t != val_string))
77  delete v.pval;
78 }
79 
81 {
82  // Have to be careful with the case where they are different types
83  if ((t != val_int) &&
84  (t != val_float) &&
85  (t != val_unset) &&
86  (t != val_string))
87  delete v.pval;
88 
89  if (c.t == val_string)
90  sval = c.sval;
91  else if (c.t == val_int)
92  v.ival = c.v.ival;
93  else if (c.t == val_float)
94  v.fval = c.v.fval;
95  else if (c.t != val_unset)
96  { // does references not a real copy
97  v.pval = new EST_Contents;
98  *v.pval = *c.v.pval;
99  }
100  t=c.t;
101  return *this;
102 }
103 
104 ssize_t EST_Val::to_int(void) const
105 {
106  // coerce this to an int
107  if (t==val_float)
108  return (int)v.fval;
109  else if (t==val_string)
110  return atol(sval);
111  else
112  return v.ival; // just for completeness
113 }
114 
115 float EST_Val::to_flt(void) const
116 {
117  // coerce this to a float
118  if (t==val_int)
119  return (float)v.ival;
120  else if (t==val_string)
121  return atof(sval);
122  else
123  return v.fval; // just for completeness
124 }
125 
126 const EST_String &EST_Val::to_str(void) const
127 {
128  // coerce this to and save it for later
129  // This requires the following casting, so we can still tell the
130  // compiler this is a const function. If this was properly declared
131  // non-const vast amounts of the rest of this would also have to be
132  // non-const. So we do one nasty bit here for uniformity elsewhere.
133  // Not saving the result is also a possibility but probably too
134  // inefficient (maybe not with rjc's string class)
135  EST_String *n = (EST_String *)((void *)&sval);
136  if (t==val_int)
137  *n = itoString(v.ival);
138  else if (t==val_float)
139  {
140  if (v.fval == 0)
141  *n = "0"; // to be compatible with other's notion of fstrings
142  else
143  *n = ftoString(v.fval);
144  }
145  else if (t != val_string)
146  *n = EST_String("[Val ")+t+"]";
147 
148  return sval;
149 }
150 
EST_Val & operator=(ssize_t i)
Definition: EST_Val.h:188
EST_Contents * pval
Definition: EST_Val.h:81
val_type val_string
Definition: EST_Val.cc:46
const char * val_type
Definition: EST_Val.h:57
EST_String itoString(int n)
Make a EST_String object from an integer.
Definition: util_io.cc:141
int ssize_t
EST_String ftoString(float n, int pres=3, int width=0, int l=0)
Make a EST_String object from an float, with variable precision.
Definition: util_io.cc:149
val_type val_float
Definition: EST_Val.cc:45
val_type type(void) const
Definition: EST_Val.h:137
f
Definition: EST_item_aux.cc:48
val_type val_int
Definition: EST_Val.cc:44
EST_Val()
Definition: EST_Val.h:93
float fval
Definition: EST_Val.h:80
ssize_t ival
Definition: EST_Val.h:79
EST_String
val_type val_unset
Definition: EST_Val.cc:43
~EST_Val(void)
Definition: EST_Val.cc:71
Utility EST_String Functions header file.