Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
ValueOption.h
1 /*
2  * $Id$
3  * ValueOption 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_VALUEOPTION_H_
23 #define ELM_OPTION_VALUEOPTION_H_
24 
25 #include <elm/option/Option.h>
26 #include <elm/type_info.h>
27 
28 namespace elm { namespace option {
29 
30 // AbstractValueOption class
31 class AbstractValueOption: public Option {
32 public:
33  class Make: public Option::Make {
34  friend class AbstractValueOption;
35  public:
36  inline Make(Manager *man): Option::Make(man), _usage(arg_required) { }
37  inline Make(Manager& man): Option::Make(man), _usage(arg_required) { }
38  inline Make& argDescription(cstring s) { arg_desc = s; return *this; }
39  inline Make& arg(cstring s) { arg_desc = s; return *this; }
40  inline Make& usage(usage_t u) { _usage = u; return *this; }
41  inline Make& cmd(string c) { Option::Make::cmd(c); return *this; }
42  inline Make& description(cstring d) { Option::Make::description(d); return *this; }
43  inline Make& help(cstring d) { Option::Make::description(d); return *this; }
44  private:
45  cstring arg_desc;
46  usage_t _usage;
47  };
48 
49  AbstractValueOption(const Make& make);
51 
52  // Option overload
53  cstring description(void) override;
54  usage_t usage(void) override;
55  cstring argDescription(void) override;
56 
57 private:
58  cstring arg_desc;
59  usage_t use;
60 };
61 
62 
63 // useful inline
64 template <class T>
65 inline T read(string arg) { T val; arg >> val; return val; }
66 
67 
68 // ValueOption<T> class
69 template <class T>
71 public:
73  friend class ValueOption<T>;
74  public:
78  inline Make& arg(cstring s) { AbstractValueOption::Make::argDescription(s); return *this; }
79  inline Make& usage(usage_t u) { AbstractValueOption::Make::usage(u); return *this; }
80  inline Make& cmd(string c) { Option::Make::cmd(c); return *this; }
81  inline Make& description(cstring d) { Option::Make::description(d); return *this; }
82  inline Make& help(cstring d) { Option::Make::description(d); return *this; }
83  inline Make& def(const T& d) { _def = d; return *this; }
84  private:
85  T _def;
86  };
87 
88  inline ValueOption(void) { }
89  inline ValueOption(const Make& make): AbstractValueOption(make), val(make._def) { }
90 
91  inline const T& get(void) const { return val; };
92  inline void set(const T& value) { val = value; };
93 
94  // Operators
95  inline operator const T&(void) const { return get(); };
96  inline ValueOption<T>& operator=(const T& value) { set(value); return *this; };
97  inline const T& operator*(void) const { return get(); }
98  inline operator bool(void) const { return get(); }
99 
100  // Option overload
101  virtual void process(String arg) { val = read<T>(arg); }
102 
103  // deprecated
104  inline const T& value(void) const { return val; };
105 
106 private:
107  T val;
108  inline T get(VarArg& args) { return args.next<T>(); }
109 };
110 
111 
112 // alias
113 template <class T> class Value: public ValueOption<T> {
114 public:
115  inline Value(const typename ValueOption<T>::Make& make): ValueOption<T>(make) { }
116 };
117 
118 // read specializations
119 template <> string read<string>(string arg);
120 template <> cstring read<cstring>(string arg);
121 
122 } } // elm::option
123 
124 #endif /* ELM_OPTION_VALUEOPTION_H_ */
elm::option::AbstractValueOption::Make::help
Make & help(cstring d)
Definition: ValueOption.h:43
elm::option::AbstractValueOption::Make::argDescription
Make & argDescription(cstring s)
Definition: ValueOption.h:38
elm::option::Value::Value
Value(const typename ValueOption< T >::Make &make)
Definition: ValueOption.h:115
elm::option::ValueOption::ValueOption
ValueOption(void)
Definition: ValueOption.h:88
elm::option::ValueOption::Make::description
Make & description(cstring d)
Definition: ValueOption.h:81
elm::option::ValueOption::Make::cmd
Make & cmd(string c)
Definition: ValueOption.h:80
elm::option::AbstractValueOption
Definition: ValueOption.h:31
elm::option::AbstractValueOption::Make::description
Make & description(cstring d)
Definition: ValueOption.h:42
Make
elm::option::ValueOption::Make::argDescription
Make & argDescription(cstring s)
Definition: ValueOption.h:77
elm::option::ValueOption::Make::usage
Make & usage(usage_t u)
Definition: ValueOption.h:79
elm::option::ValueOption::Make
Definition: ValueOption.h:72
elm::option::Option::Make::man
Manager & man
Definition: Option.h:61
elm::option::ValueOption::Make::Make
Make(Manager *man)
Definition: ValueOption.h:75
elm::option::AbstractValueOption::usage
usage_t usage(void) override
Definition: option_ValueOption.cpp:166
elm::option::AbstractValueOption::Make::Make
Make(Manager *man)
Definition: ValueOption.h:36
elm::option::ValueOption::operator*
const T & operator*(void) const
Definition: ValueOption.h:97
elm::option::ValueOption::Make::arg
Make & arg(cstring s)
Definition: ValueOption.h:78
elm::option::Option::Make::description
Make & description(cstring desc)
Definition: Option.h:59
elm::option::usage_t
usage_t
Definition: Option.h:41
elm::option::Manager
Definition: Manager.h:37
elm::option::AbstractValueOption::Make::arg
Make & arg(cstring s)
Definition: ValueOption.h:39
value
void
elm::option::ValueOption::Make::help
Make & help(cstring d)
Definition: ValueOption.h:82
bool
elm::CString
Definition: CString.h:17
elm::option::ValueOption::value
const T & value(void) const
Definition: ValueOption.h:104
elm::option::read< string >
string read< string >(string arg)
Definition: option_ValueOption.cpp:180
elm::option::Option::Make
Definition: Option.h:52
elm::option::AbstractValueOption::Make::cmd
Make & cmd(string c)
Definition: ValueOption.h:41
elm::option::read< cstring >
cstring read< cstring >(string arg)
Definition: option_ValueOption.cpp:186
elm::option::ValueOption::set
void set(const T &value)
Definition: ValueOption.h:92
elm::option::AbstractValueOption::Make
Definition: ValueOption.h:33
elm::option::ValueOption::ValueOption
ValueOption(const Make &make)
Definition: ValueOption.h:89
elm
Definition: adapter.h:26
elm::option::ValueOption
Definition: ValueOption.h:70
elm::option::Option
Definition: Option.h:49
elm::option::AbstractValueOption::description
cstring description(void) override
Definition: option_ValueOption.cpp:159
elm::option::AbstractValueOption::AbstractValueOption
AbstractValueOption(const Make &make)
Definition: option_ValueOption.cpp:152
elm::VarArg
Definition: VarArg.h:25
elm::option::ValueOption::get
const T & get(void) const
Definition: ValueOption.h:91
elm::option::Option::Make::cmd
Make & cmd(string cmd)
Definition: Option.h:57
elm::option::read
T read(string arg)
Definition: ValueOption.h:65
elm::VarArg::next
T next(void)
Definition: VarArg.h:42
elm::option::ValueOption::process
virtual void process(String arg)
Definition: ValueOption.h:101
elm::String
Definition: String.h:30
elm::option::AbstractValueOption::Make::usage
Make & usage(usage_t u)
Definition: ValueOption.h:40
elm::option::Value
Definition: ValueOption.h:113
elm::option::arg_required
@ arg_required
Definition: Option.h:44
elm::option::ValueOption::Make::Make
Make(Manager &man)
Definition: ValueOption.h:76
elm::option::AbstractValueOption::argDescription
cstring argDescription(void) override
Definition: option_ValueOption.cpp:173
elm::option::AbstractValueOption::Make::Make
Make(Manager &man)
Definition: ValueOption.h:37
elm::option::ValueOption::Make::def
Make & def(const T &d)
Definition: ValueOption.h:83
elm::option::ValueOption::operator=
ValueOption< T > & operator=(const T &value)
Definition: ValueOption.h:96