Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
EST_TNamedEnum.h
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  /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */
34  /* Date: Fri Feb 28 1997 */
35  /************************************************************************/
36  /* */
37  /* A template class which allows names (const char *s) to be */
38  /* associated with enums, providing conversion. */
39  /* */
40  /* EST_TValuesEnum is the obvious generalisation to associating */
41  /* things other than const char * with each value. */
42  /* */
43  /* EST_T{Named/Valued}EnumI can include extra information for each */
44  /* enum element. */
45  /* */
46  /* This should be rewritten as something other than linear search. At */
47  /* least sort them. */
48  /* */
49  /************************************************************************/
50 
51 #ifndef __EST_TNAMEDENUM_H__
52 #define __EST_TNAMEDENUM_H__
53 
54 #include <cstring>
55 
56 using namespace std;
57 
58 #include "EST_String.h"
59 #include "EST_rw_status.h"
60 
61 #define NAMED_ENUM_MAX_SYNONYMS (10)
62 
63 // Used in the type of tables with no info field.
64 
65 typedef char NO_INFO;
66 
67 // struct used to define the mapping.
68 
69 template<class ENUM, class VAL, class INFO>
71 public:
72  ENUM token;
73  VAL values[NAMED_ENUM_MAX_SYNONYMS];
74  INFO info;
75 } ;
76 
77 // This is the most general case, a mapping from enum to some other type
78 // with extra info.
79 
80 template<class ENUM, class VAL, class INFO> class EST_TValuedEnumI {
81 
82 protected:
83  int ndefinitions;
84  ENUM p_unknown_enum;
85  VAL p_unknown_value;
87 
88  virtual int eq_vals(VAL v1, VAL v2) const {return v1 == v2; };
89  // This is only a void * because INFO can`t manage to get the
90  // parameter declaration in the definition past gcc with the actual type.
91  void initialise(const void *defs);
92  void initialise(const void *defs, ENUM (*conv)(const char *));
93  void initialise(void) {ndefinitions=0; definitions=NULL;};
94  void initialise(ENUM unknown_e, VAL unknown_v) {initialise(); p_unknown_enum=unknown_e; p_unknown_value = unknown_v;};
95 
96 protected:
97  EST_TValuedEnumI(void) {initialise();};
98 
99 public:
101  {initialise((const void *)defs); };
102  EST_TValuedEnumI(EST_TValuedEnumDefinition<const char *,VAL,INFO> defs[], ENUM (*conv)(const char *))
103  {initialise((const void *)defs, conv); };
104  virtual ~EST_TValuedEnumI(void);
105 
106  int n(void) const;
107 
108  ENUM token(VAL value) const;
109  ENUM token(int n) const { return nth_token(n); }
110  ENUM nth_token(int n) const;
111  VAL value(ENUM token, int n=0) const;
112  INFO &info(ENUM token) const;
113 
114  ENUM unknown_enum(void) const {return p_unknown_enum;};
115  VAL unknown_value(void) const {return p_unknown_value;};
116  int valid(ENUM token) const { return !eq_vals(value(token),p_unknown_value); };
117 };
118 
119 // This is a special case for names. This saves typing and also
120 // takes care of the fact that strings need their own compare function.
121 
122 template<class ENUM, class INFO> class EST_TNamedEnumI : public EST_TValuedEnumI<ENUM, const char *, INFO> {
123 
124 protected:
126  int eq_vals(const char *v1, const char *v2) const {return strcmp(v1,v2) ==0; };
127 public:
128 
130  {this->initialise((const void *)defs); };
132  {this->initialise((const void *)defs, conv); };
133  const char *name(ENUM tok, int n=0) const {return this->value(tok,n); };
134 
135 };
136 
137 // Now the simple cases with no extra information
138 
139 template<class ENUM, class VAL> class EST_TValuedEnum : public EST_TValuedEnumI<ENUM,VAL,NO_INFO> {
140 public:
142  {this->initialise((const void *)defs);};
143  EST_TValuedEnum(EST_TValuedEnumDefinition<const char *,VAL,NO_INFO> defs[], ENUM (*conv)(const char *))
144  {this->initialise((const void *)defs, conv);};
145 };
146 
147 
148 template<class ENUM> class EST_TNamedEnum : public EST_TNamedEnumI<ENUM,NO_INFO> {
149 private:
150  EST_read_status priv_load(EST_String name, EST_TNamedEnum *definitive);
151  EST_write_status priv_save(EST_String name, EST_TNamedEnum *definitive, char quote) const;
152 public:
153  EST_TNamedEnum(ENUM undef_e, const char *undef_n = NULL)
154  {this->initialise(undef_e, undef_n);};
156  {this->initialise((const void *)defs);};
158  {this->initialise((const void *)defs, conv);};
159 
160  EST_read_status load(EST_String name) { return priv_load(name, NULL); };
161  EST_read_status load(EST_String name, EST_TNamedEnum &definitive) { return priv_load(name, &definitive); };
162  EST_write_status save(EST_String name, char quote='"') const { return priv_save(name, NULL, quote); };
163  EST_write_status save(EST_String name, EST_TNamedEnum &definitive, char quote='"') const { return priv_save(name, &definitive, quote); };
164 
165 };
166 
167 #include "instantiate/EST_TNamedEnumI.h"
168 
169 #endif