Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
EnumOption.h
1 /*
2  * $Id$
3  * EnumOption class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2006-07, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef ELM_OPTION_ENUM_OPTION_H
23 #define ELM_OPTION_ENUM_OPTION_H
24 
25 #include <elm/option/StandardOption.h>
26 
27 namespace elm { namespace option {
28 
29 // BoolOption class
30 template <class T>
31 class EnumOption: public StandardOption {
32 public:
33  typedef struct value_t {
35  T value;
36  } value_t;
37 private:
38  int val;
39  value_t *vals;
40  String arg;
41 public:
42  inline EnumOption(Manager& manager, char short_name, CString description,
43  value_t values[]);
44  inline EnumOption(Manager& manager, CString long_name, CString description,
45  value_t values[]);
46  inline EnumOption(Manager& manager, char short_name, CString long_name,
47  CString description, value_t values[]);
48  inline const T& value(void) const;
49  inline void set(const T& value);
50 
51  // Option overload
52  virtual usage_t usage(void);
53  virtual CString argDescription(void);
54  virtual void process(String arg);
55 
56  // Operators
57  inline operator T(void) const;
58  inline EnumOption& operator=(const T& value);
59 };
60 
61 
62 // EnumOption inlines
63 template <class T>
65 value_t values[]): StandardOption(manager, short_name, description),
66 val(values[0].value), vals(values) {
67 }
68 
69 template <class T>
71 CString description, value_t values[]): StandardOption(manager, long_name,
72 description), val(values[0].value), vals(values) {
73 }
74 
75 template <class T>
76 EnumOption<T>::EnumOption(Manager& manager, char short_name, CString long_name,
77 CString description, value_t values[]): StandardOption(manager, short_name,
78 long_name, description), val(values[0].value), vals(values) {
79 }
80 
81 template <class T>
82 inline const T& EnumOption<T>::value(void) const {
83  return val;
84 }
85 
86 template <class T>
87 inline void EnumOption<T>::set(const T& value) {
88  val = value;
89 }
90 
91 template <class T>
93  return arg_required;
94 }
95 
96 template <class T>
98  return vals[0].name;
99 }
100 
101 template <class T>
103  for(int i = 1; vals[i].name; i++)
104  if(arg == vals[i].name) {
105  val = vals[i].value;
106  return;
107  }
108  throw OptionException(_ << "bad value \"" << arg << "\"");
109 }
110 
111 template <class T>
112 EnumOption<T>::operator T(void) const {
113  return val;
114 }
115 
116 template <class T>
118  val = value;
119  return *this;
120 }
121 
122 } } // elm::option
123 
124 #endif // ELM_OPTION_ENUM_OPTION_H
Definition: CString.h:17
const T & value(void) const
Definition: EnumOption.h:82
Definition: Manager.h:35
Definition: Manager.h:59
AutoStringStartup & _
Definition: debug_CrashHandler.cpp:221
usage_t
Definition: Option.h:35
Definition: Option.h:38
value_t value(CString name, int value)
Definition: rtti.h:40
Definition: EnumOption.h:31
Definition: StandardOption.h:31
elm::CString name
Definition: EnumOption.h:34
void set(const T &value)
Definition: EnumOption.h:87
virtual usage_t usage(void)
Definition: EnumOption.h:92
virtual CString argDescription(void)
Definition: EnumOption.h:97
Definition: String.h:31
EnumOption(Manager &manager, char short_name, CString description, value_t values[])
Definition: EnumOption.h:64
T value
Definition: EnumOption.h:35
const int description
Definition: Manager.h:46
Definition: EnumOption.h:33
virtual void process(String arg)
Definition: EnumOption.h:102
virtual cstring description(void)
Definition: option_StandardOption.cpp:85
struct elm::option::EnumOption::value_t value_t
EnumOption & operator=(const T &value)
Definition: EnumOption.h:117