clingo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
typed_value.h
Go to the documentation of this file.
1 //
2 // Copyright (c) Benjamin Kaufmann 2004
3 //
4 // This is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This file is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this file; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 //
19 // NOTE: ProgramOptions is inspired by Boost.Program_options
20 // see: www.boost.org/libs/program_options
21 //
22 #ifndef PROGRAM_OPTIONS_TYPED_VALUE_H_INCLUDED
23 #define PROGRAM_OPTIONS_TYPED_VALUE_H_INCLUDED
24 #ifdef _MSC_VER
25 #pragma warning (disable : 4786)
26 #pragma warning (disable : 4503)
27 #pragma warning (disable : 4200)
28 #endif
29 #include "value.h"
30 #include "string_convert.h"
31 #include "detail/notifier.h"
32 #include "errors.h"
33 #include <memory>
34 namespace ProgramOptions { namespace detail {
35 template <class T>
36 struct Parser { typedef bool (*type)(const std::string&, T&); };
37 } // end namespace detail
40 // Enumeration Parser: string->int mapping
42 struct ValueMappingBase : private std::vector<std::pair<const char*, int> > {
43  typedef std::vector<std::pair<const char*, int> > base_type;
44  using base_type::size;
45  using base_type::empty;
46  void add(const char* strVal, int eVal){ push_back(value_type(strVal, eVal)); }
47  const int* get(const char* strVal) const {
48  for (const_iterator it = begin(), end = this->end(); it != end; ++it) {
49  if (strcasecmp(strVal, it->first) == 0) { return &it->second; }
50  }
51  return 0;
52  }
53 };
54 template <class T>
58  using base_type::get;
59  this_type& operator()(const char* strVal, T eVal){ base_type::add(strVal, static_cast<int>(eVal)); return *this; }
60  static ValueMapping& instance() { static ValueMapping m; return m; }
61  operator typename detail::Parser<T>::type() { return &parse<T>; }
62  operator typename detail::Parser<int>::type() { return &parse<int>; }
63  template <class U>
64  static bool parse(const std::string& value, U& out){
66  if (const int* x = m.get(value.c_str())) { out = static_cast<U>(*x); return true; }
67  return false;
68  }
69 private:
70  ValueMapping() {}
71 };
72 template <class T> ValueMapping<T>& values() { return ValueMapping<T>::instance(); }
74 // StoredValue - a typed value that writes to an existing variable
76 template <class T>
77 class StoredValue : public Value {
78 public:
81  : Value(0)
82  , address_(&var)
83  , parser_(p) {
85  }
86  bool doParse(const std::string&, const std::string& value) {
87  return this->parser_(value, *address_);
88  }
89 protected:
90  T* address_; // storage location of this value
91  parser_type parser_; // str -> T
92 };
94 // NotifiedValue - a typed value that is created on demand and passed to a callback
96 template <class T>
98  static T* create() { return new T(); }
99 };
100 template <class T>
101 class NotifiedValue : public Value {
102 public:
105  NotifiedValue(T* (*cf)(), const notifier_type& n, parser_type p)
106  : Value(0)
107  , parser_(p)
108  , notify_(n) {
109  value_.create = cf;
110  }
112  value_.address = &obj;
114  return this;
115  }
116  bool doParse(const std::string& name, const std::string& value) {
117  bool ret;
118  T* pv = 0;
119  std::auto_ptr<T> holder;
121  pv = value_.address;
122  }
123  else {
124  holder.reset(value_.create());
125  pv = holder.get();
126  }
127  ret = this->parser_(value, *pv);
128  if (ret && notify_.notify(name, pv)) {
129  this->storeTo(*pv);
130  holder.release();
131  }
132  return ret;
133  }
134 protected:
135  union {
137  T* (*create)();
138  } value_;
141 };
143 // CustomValue - a value that must be parsed/interpreted by a custom context
145 class CustomValue : public Value {
146 public:
149  : Value(0)
150  , notify_(n) { }
151  bool doParse(const std::string& name, const std::string& value) {
152  return notify_.notify(name, value);
153  }
154 protected:
156 };
158 // value factories
160 #define LIT_TO_STRING_X(lit) #lit
161 #define LIT_TO_STRING(lit) LIT_TO_STRING_X(lit)
163 struct FlagAction {
165  static inline bool store_true(const std::string& v, bool& b) {
166  if (v.empty()) { return (b = true); }
167  return string_cast<bool>(v, b);
168  }
169  static inline bool store_false(const std::string& v, bool& b) {
170  bool temp;
171  return store_true(v, temp) && ((b = !temp), true);
172  }
176 };
177 static const FlagAction store_true( (FlagAction::act_store_true) );
178 static const FlagAction store_false( (FlagAction::act_store_false));
179 
189 template <class T>
190 inline StoredValue<T>* storeTo(T& v, typename detail::Parser<T>::type p = &string_cast<T>) {
191  return new StoredValue<T>(v, p);
192 }
193 inline StoredValue<bool>* flag(bool& b, FlagAction x = store_true) {
194  return static_cast<StoredValue<bool>*>(storeTo(b, x.parser())->flag());
195 }
196 
215 template <class T, class ParamT>
216 inline NotifiedValue<T>* notify(ParamT* p0, typename detail::Notify<const T*, ParamT>::type nf, typename detail::Parser<T>::type parser = &string_cast<T>) {
218 }
219 template <class T, class ParamT>
220 inline NotifiedValue<T>* storeNotify(T& obj, ParamT* p0, typename detail::Notify<const T*, ParamT>::type nf, typename detail::Parser<T>::type parser = &string_cast<T>) {
221  return notify<T>(p0, nf, parser)->storeTo(obj);
222 }
223 template <class ParamT>
224 inline NotifiedValue<bool>* flag(ParamT* p0, typename detail::Notify<const bool*, ParamT>::type nf, FlagAction a = store_true) {
225  return static_cast<NotifiedValue<bool>*>(notify<bool>(p0, nf, a.parser())->flag());
226 }
227 
246 template <class ParamT>
249 }
250 }
251 #endif
bool notify(const std::string &name, ParamT val) const
Definition: notifier.h:47
parser_t parser() const
Definition: typed_value.h:175
StoredValue(T &var, parser_type p)
Definition: typed_value.h:80
detail::Parser< T >::type parser_type
Definition: typed_value.h:103
parser_type parser_
Definition: typed_value.h:91
CustomValue(const notifier_type &n)
Definition: typed_value.h:148
this_type & operator()(const char *strVal, T eVal)
Definition: typed_value.h:59
ValueMapping< T > & values()
Definition: typed_value.h:72
NotifiedValue< T > * notify(ParamT *p0, typename detail::Notify< const T *, ParamT >::type nf, typename detail::Parser< T >::type parser=&string_cast< T >)
Definition: typed_value.h:216
bool doParse(const std::string &name, const std::string &value)
Definition: typed_value.h:151
detail::Notifier< const std::string & > notifier_type
Definition: typed_value.h:147
union ProgramOptions::NotifiedValue::@50 value_
FlagAction(Action a)
Definition: typed_value.h:174
NotifiedValue< T > * storeNotify(T &obj, ParamT *p0, typename detail::Notify< const T *, ParamT >::type nf, typename detail::Parser< T >::type parser=&string_cast< T >)
Definition: typed_value.h:220
static ValueMapping & instance()
Definition: typed_value.h:60
Manages the value of an option and defines how it is parsed from a string.
Definition: value.h:53
std::vector< std::pair< const char *, int > > base_type
Definition: typed_value.h:43
Definition: typed_value.h:42
ValueMapping this_type
Definition: typed_value.h:56
tuple a
Definition: pyclingo.py:6
detail::Parser< T >::type parser_type
Definition: typed_value.h:79
bool(* type)(const std::string &, T &)
Definition: typed_value.h:36
bool doParse(const std::string &name, const std::string &value)
Definition: typed_value.h:116
Definition: typed_value.h:173
Definition: typed_value.h:173
notifier_type notify_
Definition: typed_value.h:155
Definition: typed_value.h:163
StoredValue< T > * storeTo(T &v, typename detail::Parser< T >::type p=&string_cast< T >)
Definition: typed_value.h:190
Action
Definition: typed_value.h:173
T * address_
Definition: typed_value.h:90
bool doParse(const std::string &, const std::string &value)
Definition: typed_value.h:86
tuple b
Definition: pyclingo.py:47
parser_type parser_
Definition: typed_value.h:139
bool hasProperty(Property f) const
Definition: value.h:191
static bool parse(const std::string &value, U &out)
Definition: typed_value.h:64
detail::Notifier< const T * > notifier_type
Definition: typed_value.h:104
Definition: typed_value.h:97
detail::Parser< bool >::type parser_t
Definition: typed_value.h:164
tuple p
Definition: server.py:49
static bool store_false(const std::string &v, bool &b)
Definition: typed_value.h:169
notifier_type notify_
Definition: typed_value.h:140
T * address
Definition: typed_value.h:136
Definition: typed_value.h:145
void add(const char *strVal, int eVal)
Definition: typed_value.h:46
enum ProgramOptions::FlagAction::Action act
int x
Definition: utility.cc:65
Gringo::IntervalSet< T >::const_iterator begin(Gringo::IntervalSet< T > const &x)
Definition: intervals.hh:311
Definition: notifier.h:54
Definition: typed_value.h:55
NotifiedValue< T > * storeTo(T &obj)
Definition: typed_value.h:111
Definition: typed_value.h:101
Value var
Definition: output.cc:70
const int * get(const char *strVal) const
Definition: typed_value.h:47
bool string_cast(const char *arg, T &to)
Definition: string_convert.h:203
Definition: typed_value.h:36
NotifiedValue< bool > * flag(ValueMap &map, FlagAction a=store_true)
Definition: mapped_value.h:88
NotifiedValue(T *(*cf)(), const notifier_type &n, parser_type p)
Definition: typed_value.h:105
Definition: typed_value.h:77
void setProperty(Property f)
Definition: value.h:189
LparseOutputter & out
Definition: output.cc:685
ValueMappingBase base_type
Definition: typed_value.h:57
static bool store_true(const std::string &v, bool &b)
Definition: typed_value.h:165
static T * create()
Definition: typed_value.h:98
int end
Definition: literals.cc:62