Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
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/Option.h>
26 
27 namespace elm { namespace option {
28 
29 // BoolOption class
30 template <class T>
31 class EnumOption: public Option {
32 public:
34 
35  class Make: public Option::Make {
36  friend class EnumOption;
37  public:
38  Make(Manager *m): Option::Make(m), _def(), _def_set(false) { }
39  Make(Manager& m): Option::Make(m), _def(), _def_set(false) { }
40  inline Make& cmd(string cmd) { Option::Make::cmd(cmd); return *this; }
42  inline Make& val(cstring n, T v) { _values.add(pair(n, v)); return *this; }
43  inline Make& def(T v) { _def = v; _def_set = true; return *this; }
44  inline Make& arg(cstring d) { _arg = d; return *this; }
45  private:
46  Vector<value_t> _values;
47  T _def;
48  bool _def_set;
49  cstring _arg;
50  };
51 
52  inline EnumOption(const Make& m): Option(m),
53  val(m._def_set ? m._def : m._values[0].snd), vals(m._values), arg(m._arg) { }
54 
55  inline T value(void) const { return val; }
56  inline void set(const T& value) { val = value; }
57 
58  // Option overload
59  usage_t usage(void) override { return arg_required; }
60 
61  void process(String arg) override {
62  for(auto v: vals) if(arg == v.fst) { val = v.snd; return; }
63  throw OptionException(_ << "bad value \"" << arg << "\"");
64  }
65 
66  cstring argDescription(void) override { return arg; }
67 
68  // Operators
69  inline operator T(void) const { return value(); }
70  inline EnumOption& operator=(const T& value) { set(value); return *this; }
71 
72 private:
73  T val;
74  Vector<value_t> vals;
75  cstring arg;
76 };
77 
78 } } // elm::option
79 
80 #endif // ELM_OPTION_ENUM_OPTION_H
elm::option::EnumOption::Make::def
Make & def(T v)
Definition: EnumOption.h:43
elm::pair
Pair< T1, T2 > pair(const T1 &v1, const T2 &v2)
Definition: Pair.h:63
elm::option::Option::Make::description
Make & description(cstring desc)
Definition: Option.h:59
elm::Pair
Definition: Pair.h:33
elm::option::usage_t
usage_t
Definition: Option.h:41
elm::option::Manager
Definition: Manager.h:37
elm::option::EnumOption::set
void set(const T &value)
Definition: EnumOption.h:56
value
elm::CString
Definition: CString.h:17
elm::option::EnumOption
Definition: EnumOption.h:31
elm::option::Option::Make
Definition: Option.h:52
elm::option::EnumOption::Make::val
Make & val(cstring n, T v)
Definition: EnumOption.h:42
elm::option::EnumOption::argDescription
cstring argDescription(void) override
Definition: EnumOption.h:66
elm::option::EnumOption::value
T value(void) const
Definition: EnumOption.h:55
elm::_
AutoStringStartup & _
Definition: debug_CrashHandler.cpp:232
elm::option::EnumOption::Make
Definition: EnumOption.h:35
elm
Definition: adapter.h:26
elm::Vector
Definition: Vector.h:34
elm::option::Option
Definition: Option.h:49
elm::option::EnumOption::Make::arg
Make & arg(cstring d)
Definition: EnumOption.h:44
elm::option::EnumOption::Make::Make
Make(Manager &m)
Definition: EnumOption.h:39
elm::option::EnumOption::EnumOption
EnumOption(const Make &m)
Definition: EnumOption.h:52
elm::option::Option::Make::cmd
Make & cmd(string cmd)
Definition: Option.h:57
elm::option::OptionException
Definition: Option.h:34
elm::String
Definition: String.h:30
elm::option::EnumOption::Make::description
Make & description(cstring desc)
Definition: EnumOption.h:41
elm::option::EnumOption::Make::cmd
Make & cmd(string cmd)
Definition: EnumOption.h:40
elm::option::EnumOption::Make::Make
Make(Manager *m)
Definition: EnumOption.h:38
elm::option::arg_required
@ arg_required
Definition: Option.h:44
elm::option::EnumOption::usage
usage_t usage(void) override
Definition: EnumOption.h:59
elm::option::EnumOption::value_t
Pair< cstring, T > value_t
Definition: EnumOption.h:33
elm::option::EnumOption::process
void process(String arg) override
Definition: EnumOption.h:61
elm::option::EnumOption::operator=
EnumOption & operator=(const T &value)
Definition: EnumOption.h:70
elm::option::Option::desc
cstring desc
Definition: Option.h:75