| 1 |
// |
| 2 |
// Copyright (c) 2004-2017 Benjamin Kaufmann |
| 3 |
// |
| 4 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
// of this software and associated documentation files (the "Software"), to |
| 6 |
// deal in the Software without restriction, including without limitation the |
| 7 |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 |
// sell copies of the Software, and to permit persons to whom the Software is |
| 9 |
// furnished to do so, subject to the following conditions: |
| 10 |
// |
| 11 |
// The above copyright notice and this permission notice shall be included in |
| 12 |
// all copies or substantial portions of the Software. |
| 13 |
// |
| 14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 |
// IN THE SOFTWARE. |
| 21 |
// |
| 22 |
// |
| 23 |
// NOTE: ProgramOptions is inspired by Boost.Program_options |
| 24 |
// see: www.boost.org/libs/program_options |
| 25 |
// |
| 26 |
#ifndef PROGRAM_OPTIONS_TYPED_VALUE_H_INCLUDED |
| 27 |
#define PROGRAM_OPTIONS_TYPED_VALUE_H_INCLUDED |
| 28 |
#ifdef _MSC_VER |
| 29 |
#pragma warning (disable : 4786) |
| 30 |
#pragma warning (disable : 4503) |
| 31 |
#pragma warning (disable : 4200) |
| 32 |
#endif |
| 33 |
#include <potassco/program_opts/value.h> |
| 34 |
#include <potassco/string_convert.h> |
| 35 |
#include <potassco/program_opts/detail/notifier.h> |
| 36 |
#include <potassco/program_opts/errors.h> |
| 37 |
namespace Potassco { namespace ProgramOptions { namespace detail { |
| 38 |
template <class T> |
| 39 |
struct Parser { typedef bool(*type)(const std::string&, T&); }; |
| 40 |
} // end namespace detail |
| 41 |
/////////////////////////////////////////////////////////////////////////////// |
| 42 |
// Enumeration Parser: string->int mapping |
| 43 |
/////////////////////////////////////////////////////////////////////////////// |
| 44 |
struct ValueMappingBase : private std::vector<std::pair<const char*, int> > { |
| 45 |
typedef std::vector<std::pair<const char*, int> > base_type; |
| 46 |
using base_type::size; |
| 47 |
using base_type::empty; |
| 48 |
void add(const char* strVal, int eVal) { push_back(value_type(strVal, eVal)); } |
| 49 |
const int* get(const char* strVal) const { |
| 50 |
for (const_iterator it = begin(), end = this->end(); it != end; ++it) { |
| 51 |
if (strcasecmp(strVal, it->first) == 0) { return &it->second; } |
| 52 |
} |
| 53 |
return 0; |
| 54 |
} |
| 55 |
}; |
| 56 |
template <class T> |
| 57 |
struct ValueMapping : ValueMappingBase { |
| 58 |
typedef ValueMapping this_type; |
| 59 |
typedef ValueMappingBase base_type; |
| 60 |
using base_type::get; |
| 61 |
this_type& operator()(const char* strVal, T eVal) { base_type::add(strVal, static_cast<int>(eVal)); return *this; } |
| 62 |
static ValueMapping& instance() { static ValueMapping m; return m; } |
| 63 |
operator typename detail::Parser<T>::type() { return &parse<T>; } |
| 64 |
operator typename detail::Parser<int>::type() { return &parse<int>; } |
| 65 |
template <class U> |
| 66 |
static bool parse(const std::string& value, U& out) { |
| 67 |
const ValueMappingBase& m = ValueMapping::instance(); |
| 68 |
if (const int* x = m.get(value.c_str())) { out = static_cast<U>(*x); return true; } |
| 69 |
return false; |
| 70 |
} |
| 71 |
private: |
| 72 |
ValueMapping() {} |
| 73 |
}; |
| 74 |
template <class T> ValueMapping<T>& values() { return ValueMapping<T>::instance(); } |
| 75 |
/////////////////////////////////////////////////////////////////////////////// |
| 76 |
// StoredValue - a typed value that writes to an existing variable |
| 77 |
/////////////////////////////////////////////////////////////////////////////// |
| 78 |
template <class T> |
| 79 |
class StoredValue : public Value { |
| 80 |
public: |
| 81 |
typedef typename detail::Parser<T>::type parser_type; |
| 82 |
StoredValue(T& var, parser_type p) |
| 83 |
: Value(0) |
| 84 |
, address_(&var) |
| 85 |
, parser_(p) { |
| 86 |
this->setProperty(Value::property_location); |
| 87 |
} |
| 88 |
bool doParse(const std::string&, const std::string& value) { |
| 89 |
return this->parser_(value, *address_); |
| 90 |
} |
| 91 |
protected: |
| 92 |
T* address_; // storage location of this value |
| 93 |
parser_type parser_; // str -> T |
| 94 |
}; |
| 95 |
//////////////////////////////////////////////////////////////////////////////////// |
| 96 |
// NotifiedValue - a typed value that is created on demand and passed to a callback |
| 97 |
//////////////////////////////////////////////////////////////////////////////////// |
| 98 |
template <class T> |
| 99 |
struct DefaultCreator { |
| 100 |
static T* create() { return new T(); } |
| 101 |
}; |
| 102 |
template <class T> |
| 103 |
class NotifiedValue : public Value { |
| 104 |
public: |
| 105 |
typedef typename detail::Parser<T>::type parser_type; |
| 106 |
typedef detail::Notifier<const T*> notifier_type; |
| 107 |
NotifiedValue(T* (*cf)(), const notifier_type& n, parser_type p) |
| 108 |
: Value(0) |
| 109 |
, parser_(p) |
| 110 |
, notify_(n) { |
| 111 |
value_.create = cf; |
| 112 |
} |
| 113 |
NotifiedValue<T>* storeTo(T& obj) { |
| 114 |
value_.address = &obj; |
| 115 |
this->setProperty(Value::property_location); |
| 116 |
return this; |
| 117 |
} |
| 118 |
bool doParse(const std::string& name, const std::string& value) { |
| 119 |
T* pv; |
| 120 |
detail::Owned<T> exit = {(pv = 0)}; |
| 121 |
if (this->hasProperty(Value::property_location)) { |
| 122 |
pv = value_.address; |
| 123 |
} |
| 124 |
else { |
| 125 |
exit.obj = (pv = value_.create()); |
| 126 |
} |
| 127 |
bool ret = this->parser_(value, *pv); |
| 128 |
if (ret && notify_.notify(name, pv)) { |
| 129 |
this->storeTo(*pv); |
| 130 |
exit.obj = 0; |
| 131 |
} |
| 132 |
return ret; |
| 133 |
} |
| 134 |
protected: |
| 135 |
union { |
| 136 |
T* address; |
| 137 |
T* (*create)(); |
| 138 |
} value_; |
| 139 |
parser_type parser_; |
| 140 |
notifier_type notify_; |
| 141 |
}; |
| 142 |
/////////////////////////////////////////////////////////////////////////////// |
| 143 |
// CustomValue - a value that must be parsed/interpreted by a custom context |
| 144 |
/////////////////////////////////////////////////////////////////////////////// |
| 145 |
class CustomValue : public Value { |
| 146 |
public: |
| 147 |
typedef detail::Notifier<const std::string&> notifier_type; |
| 148 |
CustomValue(const notifier_type& n) |
| 149 |
: Value(0) |
| 150 |
, notify_(n) { |
| 151 |
} |
| 152 |
bool doParse(const std::string& name, const std::string& value) { |
| 153 |
return notify_.notify(name, value); |
| 154 |
} |
| 155 |
protected: |
| 156 |
notifier_type notify_; |
| 157 |
}; |
| 158 |
/////////////////////////////////////////////////////////////////////////////// |
| 159 |
// value factories |
| 160 |
/////////////////////////////////////////////////////////////////////////////// |
| 161 |
#define LIT_TO_STRING_X(lit) #lit |
| 162 |
//! stringifies a literal like 1 or 23.0 |
| 163 |
#define LIT_TO_STRING(lit) LIT_TO_STRING_X(lit) |
| 164 |
struct FlagAction { |
| 165 |
typedef detail::Parser<bool>::type parser_t; |
| 166 |
static inline bool store_true(const std::string& v, bool& b) { |
| 167 |
if (v.empty()) { return (b = true); } |
| 168 |
return string_cast<bool>(v, b); |
| 169 |
} |
| 170 |
static inline bool store_false(const std::string& v, bool& b) { |
| 171 |
bool temp; |
| 172 |
return store_true(v, temp) && ((b = !temp), true); |
| 173 |
} |
| 174 |
enum Action { act_store_true, act_store_false } act; |
| 175 |
FlagAction(Action a) : act(a) {} |
| 176 |
parser_t parser() const { return act == act_store_true ? store_true : store_false; } |
| 177 |
}; |
| 178 |
static const FlagAction store_true((FlagAction::act_store_true)); |
| 179 |
static const FlagAction store_false((FlagAction::act_store_false)); |
| 180 |
|
| 181 |
/*! |
| 182 |
* Creates a value that is bound to an existing variable. |
| 183 |
* Assignments to the created value are directly stored in the |
| 184 |
* given variable. |
| 185 |
* |
| 186 |
* \param v The variable to which the new value object is bound |
| 187 |
* \param p The parser to use for parsing the value. If no parser is given, |
| 188 |
* type T must provide an operator>>(std::istream&, T&). |
| 189 |
*/ |
| 190 |
template <class T> |
| 191 |
inline StoredValue<T>* storeTo(T& v, typename detail::Parser<T>::type p = &string_cast<T>) { |
| 192 |
return new StoredValue<T>(v, p); |
| 193 |
} |
| 194 |
inline StoredValue<bool>* flag(bool& b, FlagAction x = store_true) { |
| 195 |
return static_cast<StoredValue<bool>*>(storeTo(b, x.parser())->flag()); |
| 196 |
} |
| 197 |
|
| 198 |
/*! |
| 199 |
* Creates a notified value, i.e. a value for which |
| 200 |
* a notification function is called once it was parsed. |
| 201 |
* The return value of that function determines whether the |
| 202 |
* value is kept (true) or deleted (false). In the former |
| 203 |
* case ownership of the value is transferred to the notified context. |
| 204 |
* |
| 205 |
* \param p0 A pointer to an object that should be passed |
| 206 |
* to the notification function once invoked. |
| 207 |
* \param nf The function to be invoked once a value is created. |
| 208 |
* On invocation, the first parameter will be p0. The second |
| 209 |
* parameter will be the value's option name and the third |
| 210 |
* the location of the newly created value. |
| 211 |
* |
| 212 |
* \param parser The parser to use for parsing the value |
| 213 |
* |
| 214 |
* \see OptionGroup::addOptions() |
| 215 |
*/ |
| 216 |
template <class T, class ParamT> |
| 217 |
inline NotifiedValue<T>* notify(ParamT* p0, typename detail::Notify<const T*, ParamT>::type nf, typename detail::Parser<T>::type parser = &string_cast<T>) { |
| 218 |
return new NotifiedValue<T>(&DefaultCreator<T>::create, detail::Notifier<const T*>(p0, nf), parser); |
| 219 |
} |
| 220 |
template <class T, class ParamT> |
| 221 |
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>) { |
| 222 |
return notify<T>(p0, nf, parser)->storeTo(obj); |
| 223 |
} |
| 224 |
template <class ParamT> |
| 225 |
inline NotifiedValue<bool>* flag(ParamT* p0, typename detail::Notify<const bool*, ParamT>::type nf, FlagAction a = store_true) { |
| 226 |
return static_cast<NotifiedValue<bool>*>(notify<bool>(p0, nf, a.parser())->flag()); |
| 227 |
} |
| 228 |
|
| 229 |
/*! |
| 230 |
* Creates a custom value, i.e. a value that is fully controlled |
| 231 |
* (parsed and created) by a notified context. |
| 232 |
* |
| 233 |
* During parsing of options, the notification function of a custom |
| 234 |
* value is called with its option name and the parsed value. |
| 235 |
* The return value of that function determines whether the |
| 236 |
* value is considered valid (true) or invalid (false). |
| 237 |
* |
| 238 |
* \param p0 A pointer to an object that should be passed |
| 239 |
* to the notification function once invoked. |
| 240 |
* \param nf The function to be invoked once a value is parsed. |
| 241 |
* On invocation, the first parameter will be p0. The second |
| 242 |
* parameter will be the value's option name and the third |
| 243 |
* a pointer to the parsed value string. |
| 244 |
* |
| 245 |
* \see OptionGroup::addOptions() |
| 246 |
*/ |
| 247 |
template <class ParamT> |
| 248 |
inline CustomValue* notify(ParamT* p0, typename detail::Notify<const std::string&, ParamT>::type nf) { |
| 249 |
return new CustomValue(detail::Notifier<const std::string&>(p0, nf)); |
| 250 |
} |
| 251 |
} // namespace ProgramOptions |
| 252 |
} // namespace Potassco |
| 253 |
#endif |