clingo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
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_VALUE_H_INCLUDED
23 #define PROGRAM_OPTIONS_VALUE_H_INCLUDED
24 #ifdef _MSC_VER
25 #pragma warning (disable : 4786)
26 #pragma warning (disable : 4503)
27 #endif
28 #include <string>
29 #include <typeinfo>
30 #include <cstddef>
31 #if defined(_MSC_VER) && _MSC_VER <= 1200
32 namespace std { using ::size_t; }
33 #endif
34 
35 namespace ProgramOptions {
36 
37 
45 };
46 
48 
53 class Value {
54 public:
56  enum State {
60  };
62  enum DescType {
66  };
67  virtual ~Value();
68 
70  State state() const { return static_cast<State>(state_); }
71 
76  state(true, s);
77  return this;
78  }
79 
81 
85  const char* arg() const;
86  Value* arg(const char* n) { return desc(desc_name, n); }
87 
89  Value* alias(char c) { optAlias_ = c; return this; }
90  char alias() const { return optAlias_; }
92 
97  unsigned x = (lev * level_shift);
98  flags_ = static_cast<byte_t>(x | (flags_&(level_shift-1)));
99  return this;
100  }
102  DescriptionLevel level() const { return DescriptionLevel(flags_ / level_shift); }
103 
105 
109  bool isNegatable() const { return hasProperty(property_negatable); }
111 
112 
114 
123  bool isImplicit() const { return hasProperty(property_implicit); }
124 
126 
132  bool isFlag() const { return hasProperty(property_flag); }
133 
138  Value* flag() { setProperty(property_flag); return this; }
139 
140 
142  bool isComposing() const { return hasProperty(property_composing); }
148 
152  Value* defaultsTo(const char* v){ return desc(desc_default, v); }
154  const char* defaultsTo() const { return desc(desc_default); }
161  Value* implicit(const char* str) { return desc(desc_implicit, str); }
163  const char* implicit() const;
164 
166 
177  bool parse(const std::string& name, const std::string& value, State st = value_fixed);
178 protected:
179  typedef unsigned char byte_t;
180  enum Property {
181  property_implicit = 1 // implicit value?
182  , property_flag = 3 // implicit and type bool?
183  , property_composing = 4 // multiple values allowed?
184  , property_negatable = 8 // negatable form allowed?
185  , property_location =16 // fixed storage location?
187  };
188  Value(byte_t flagSet, State initial = value_unassigned);
189  void setProperty(Property f) { flags_ |= byte_t(f); }
190  void clearProperty(Property f) { flags_ &= ~byte_t(f);}
191  bool hasProperty(Property f)const{ return (flags_ & byte_t(f)) == f; }
192  bool state(bool b, State s) { if (b) { state_ = static_cast<byte_t>(s); } return b; }
193  virtual bool doParse(const std::string& name, const std::string& value) = 0;
194  const char* desc(DescType t) const;
195  Value* desc(DescType t, const char* d);
196 private:
197  enum { desc_pack = 8, level_shift = not_a_property, levels = 255/level_shift };
198  byte_t state_; // state: one of State
199  byte_t flags_; // flag set holding properties
200  byte_t descFlag_; // either desc_pack or one of DescType
201  byte_t optAlias_; // alias name of option
202  union ValueDesc { // optional value descriptions either
203  const char* value;// a single value or
204  const char** pack; // a pointer to a full pack
205  } desc_;
206 };
207 }
208 #endif
char alias() const
Definition: value.h:90
void clearProperty(Property f)
Definition: value.h:190
unsigned char byte_t
Definition: value.h:179
DescriptionLevel
Definition: value.h:38
Definition: value.h:42
State state() const
Returns the current state of this value.
Definition: value.h:70
virtual ~Value()
Definition: program_options.cpp:128
bool state(bool b, State s)
Definition: value.h:192
Value * state(Value::State s)
Definition: value.h:75
Value * negatable()
Definition: value.h:110
tuple s
Definition: server.py:45
bool parse(const std::string &name, const std::string &value, State st=value_fixed)
Parses the given string and updates the value's state.
Definition: program_options.cpp:177
Manages the value of an option and defines how it is parsed from a string.
Definition: value.h:53
Value * flag()
Definition: value.h:138
auto f
Definition: statements.cc:122
bool isNegatable() const
Returns true if this is the value of an negatable option.
Definition: value.h:109
const char * desc(DescType t) const
Definition: program_options.cpp:162
Definition: value.h:63
virtual bool doParse(const std::string &name, const std::string &value)=0
const char * arg() const
Returns the name of this value.
Definition: program_options.cpp:134
Value * level(DescriptionLevel lev)
Sets a description level for the corresponding option.
Definition: value.h:96
Property
Definition: value.h:180
Value * composing()
Definition: value.h:147
tuple b
Definition: pyclingo.py:47
const char * defaultsTo() const
Returns the default value of this or 0 none exists.
Definition: value.h:154
Definition: value.h:41
bool hasProperty(Property f) const
Definition: value.h:191
Value * arg(const char *n)
Definition: value.h:86
bool isImplicit() const
Returns true if value can be implicitly created from an empty string.
Definition: value.h:123
State
Possible (tentative) states of an option value.
Definition: value.h:56
DescType
Possible value descriptions.
Definition: value.h:62
Value(byte_t flagSet, State initial=value_unassigned)
Definition: program_options.cpp:120
bool isFlag() const
Returns true if this is the value of an option flag.
Definition: value.h:132
StringSet str
Definition: dependency.cc:92
tuple c
Definition: visualize.py:132
Definition: value.h:44
DescriptionLevel level() const
Returns the description level of the corresponding option.
Definition: value.h:102
int x
Definition: utility.cc:65
tuple d
Definition: pyclingo.py:49
Value * alias(char c)
Sets an alias name for the corresponding option.
Definition: value.h:89
Definition: value.h:40
const char * implicit() const
Returns the implicit value of this or 0 if isImplicit() == false.
Definition: program_options.cpp:171
Definition: value.h:43
void setProperty(Property f)
Definition: value.h:189
bool isComposing() const
Returns true if the value of this option can be composed from multiple source.
Definition: value.h:142
Value * implicit(const char *str)
Definition: value.h:161
Value * defaultsTo(const char *v)
Definition: value.h:152