CLAM-Development  1.4.0
Enum.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef _ENUM_H_
23 #define _ENUM_H_
24 #include <exception>
25 #include <iosfwd>
26 #include <string>
27 #include "Assert.hxx"
28 #include "Component.hxx"
29 
30 namespace CLAM {
31 
32 // TODO: Integrate this excepcion class with CLAM exception standards
33 class IllegalValue : public std::exception {
34  public:
35  IllegalValue(const IllegalValue & e): msg(e.msg) {};
36  IllegalValue(const std::string & s) : msg(s) {};
37  virtual ~IllegalValue() throw() {};
38  std::string msg;
39 };
40 
41 
93 class Enum : public Component {
94 
95 // Internal Types
96 public:
97  typedef int tValue;
98  typedef struct {tValue value; const char*name;} tEnumValue;
99 
100 // Attributes
101 private:
102  const tEnumValue * mEnumValues;
103  tValue mValue;
104 
107 
108 protected:
116  Enum (const tEnumValue * values, const tValue & value) /* throw IllegalValue */ {
117  mEnumValues = values;
118  SetValue(value);
119  }
127  Enum (const tEnumValue * values, const std::string & value) /* throw IllegalValue */ {
128  mEnumValues = values;
129  SetValue(value);
130  };
131 public:
133  virtual ~Enum ();
134  const char * GetClassName() const {return NULL;}
136 
137 // Operations
138 public:
142  const tEnumValue * GetSymbolMap() const {
143  return mEnumValues;
144  }
145 
150  void SetValue(const tValue v) {
152  for (int i = 0; mEnumValues[i].name; i++) {
153  if (v==mEnumValues[i].value) {
154  mValue = v;
155  return;
156  }
157  }
158  CLAM_ASSERT(false, "Invalid value for an Enum");
160  mValue = v;
161  }
171  void SetValueSafely(const tValue v) throw (IllegalValue) {
172  for (int i = 0; mEnumValues[i].name; i++) {
173  if (v==mEnumValues[i].value) {
174  mValue = v;
175  return;
176  }
177  }
178  throw IllegalValue("Invalid value for an Enum");
179  }
184  void SetValue(const std::string & s)
185  {
186  for (int i = 0; mEnumValues[i].name; i++) {
187  if (s.compare(mEnumValues[i].name)==0) {
188  mValue = mEnumValues[i].value;
189  return;
190  }
191  }
192  CLAM_ASSERT(false, "Illegal literal for an Enum");
193  }
194  /*
195  * Changes the value safely.
196  * That is it checks the value is ok for the enum and throws a
197  * catchable exception if not.
198  * @param s The new symbolic value
199  * @throw IllegalValue when the value is not valid for the enum
200  * @todo Fill IllegalValue with useful information to recover
201  * instead a insightfull string.
202  */
203  void SetValueSafely(const std::string & s) throw (IllegalValue)
204  {
205  for (int i = 0; mEnumValues[i].name; i++) {
206  if (s.compare(mEnumValues[i].name)==0) {
207  mValue = mEnumValues[i].value;
208  return;
209  }
210  }
211  throw IllegalValue("Illegal literal for an Enum.");
212  }
213 
218  tValue GetValue() const {
219  return mValue;
220  }
221 
226  std::string GetString() const throw (IllegalValue)
227  {
228  for (int i = 0; mEnumValues[i].name; i++) {
229  if (mValue==mEnumValues[i].value)
230  return mEnumValues[i].name;
231  }
232  CLAM_ASSERT(false, "Illegal numeric value for an Enum");
233  return "IllegalValue";
234  }
235 
236  Enum & operator = (const tValue & v) throw (IllegalValue) {
237  SetValue(v);
238  return *this;
239  }
240 
241  Enum & operator = (const std::string & v) throw (IllegalValue) {
242  SetValue(v);
243  return *this;
244  }
245 
246  Enum & operator = (const Enum & v) throw (IllegalValue) {
247  SetValue(tValue(v));
248  return *this;
249  }
250 
255  operator tValue() const {
256  return mValue;
257  }
258 
259 // Component Protocol
260 public:
267  virtual Component * Species () const = 0;
268 
272  virtual Component * DeepCopy () const {
273  Enum * pe = (Enum*)Species();
274  pe->SetValue(mValue);
275  return pe;
276  }
277 
281  virtual Component * ShallowCopy () const {
282  Enum * pe = (Enum*)Species();
283  pe->SetValue(mValue);
284  return pe;
285  }
286 
294  virtual void StoreOn (Storage & storage) const;
295 
303  virtual void LoadFrom (Storage & storage);
304 
305 
306 };
307 
314 std::ostream & operator << (std::ostream & os, const Enum & e) throw (IllegalValue);
315 
322 std::istream & operator >> (std::istream & os, Enum & e) throw (IllegalValue);
323 
324 }
325 #endif // _ENUM_H_
326