File indexing completed on 2025-01-18 09:50:15
0001
0002
0003
0004
0005
0006 #ifndef BOOST_VALUE_SEMANTIC_HPP_VP_2004_02_24
0007 #define BOOST_VALUE_SEMANTIC_HPP_VP_2004_02_24
0008
0009 #include <boost/program_options/config.hpp>
0010 #include <boost/program_options/errors.hpp>
0011
0012 #include <boost/any.hpp>
0013 #include <boost/function/function1.hpp>
0014 #include <boost/lexical_cast.hpp>
0015
0016 #include <string>
0017 #include <vector>
0018 #include <typeinfo>
0019 #include <limits>
0020
0021 namespace boost { namespace program_options {
0022
0023
0024
0025
0026 class BOOST_PROGRAM_OPTIONS_DECL value_semantic {
0027 public:
0028
0029
0030
0031 virtual std::string name() const = 0;
0032
0033
0034
0035 virtual unsigned min_tokens() const = 0;
0036
0037
0038
0039 virtual unsigned max_tokens() const = 0;
0040
0041
0042
0043
0044
0045 virtual bool is_composing() const = 0;
0046
0047
0048
0049
0050 virtual bool is_required() const = 0;
0051
0052
0053
0054
0055
0056
0057 virtual void parse(boost::any& value_store,
0058 const std::vector<std::string>& new_tokens,
0059 bool utf8) const
0060 = 0;
0061
0062
0063
0064
0065 virtual bool apply_default(boost::any& value_store) const = 0;
0066
0067
0068
0069 virtual void notify(const boost::any& value_store) const = 0;
0070
0071 virtual ~value_semantic() {}
0072 };
0073
0074
0075
0076
0077 template<class charT>
0078 class value_semantic_codecvt_helper {
0079
0080 };
0081
0082
0083
0084
0085
0086
0087
0088
0089 template<>
0090 class BOOST_PROGRAM_OPTIONS_DECL
0091 value_semantic_codecvt_helper<char> : public value_semantic {
0092 private:
0093 void parse(boost::any& value_store,
0094 const std::vector<std::string>& new_tokens,
0095 bool utf8) const;
0096 protected:
0097 virtual void xparse(boost::any& value_store,
0098 const std::vector<std::string>& new_tokens)
0099 const = 0;
0100 };
0101
0102
0103
0104
0105
0106
0107
0108
0109 template<>
0110 class BOOST_PROGRAM_OPTIONS_DECL
0111 value_semantic_codecvt_helper<wchar_t> : public value_semantic {
0112 private:
0113 void parse(boost::any& value_store,
0114 const std::vector<std::string>& new_tokens,
0115 bool utf8) const;
0116 protected:
0117 #if !defined(BOOST_NO_STD_WSTRING)
0118 virtual void xparse(boost::any& value_store,
0119 const std::vector<std::wstring>& new_tokens)
0120 const = 0;
0121 #endif
0122 };
0123
0124
0125
0126 class BOOST_PROGRAM_OPTIONS_DECL
0127 untyped_value : public value_semantic_codecvt_helper<char> {
0128 public:
0129 untyped_value(bool zero_tokens = false)
0130 : m_zero_tokens(zero_tokens)
0131 {}
0132
0133 std::string name() const;
0134
0135 unsigned min_tokens() const;
0136 unsigned max_tokens() const;
0137
0138 bool is_composing() const { return false; }
0139
0140 bool is_required() const { return false; }
0141
0142
0143
0144
0145
0146
0147 void xparse(boost::any& value_store,
0148 const std::vector<std::string>& new_tokens) const;
0149
0150
0151 bool apply_default(boost::any&) const { return false; }
0152
0153
0154 void notify(const boost::any&) const {}
0155 private:
0156 bool m_zero_tokens;
0157 };
0158
0159 #ifndef BOOST_NO_RTTI
0160
0161
0162
0163
0164
0165
0166 class typed_value_base
0167 {
0168 public:
0169
0170
0171 virtual const std::type_info& value_type() const = 0;
0172
0173
0174 virtual ~typed_value_base() {}
0175 };
0176 #endif
0177
0178
0179
0180 template<class T, class charT = char>
0181 class typed_value : public value_semantic_codecvt_helper<charT>
0182 #ifndef BOOST_NO_RTTI
0183 , public typed_value_base
0184 #endif
0185 {
0186 public:
0187
0188
0189 typed_value(T* store_to)
0190 : m_store_to(store_to), m_composing(false),
0191 m_implicit(false), m_multitoken(false),
0192 m_zero_tokens(false), m_required(false)
0193 {}
0194
0195
0196
0197
0198
0199 typed_value* default_value(const T& v)
0200 {
0201 m_default_value = boost::any(v);
0202 m_default_value_as_text = boost::lexical_cast<std::string>(v);
0203 return this;
0204 }
0205
0206
0207
0208
0209
0210
0211
0212 typed_value* default_value(const T& v, const std::string& textual)
0213 {
0214 m_default_value = boost::any(v);
0215 m_default_value_as_text = textual;
0216 return this;
0217 }
0218
0219
0220
0221
0222
0223 typed_value* implicit_value(const T &v)
0224 {
0225 m_implicit_value = boost::any(v);
0226 m_implicit_value_as_text =
0227 boost::lexical_cast<std::string>(v);
0228 return this;
0229 }
0230
0231
0232 typed_value* value_name(const std::string& name)
0233 {
0234 m_value_name = name;
0235 return this;
0236 }
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248 typed_value* implicit_value(const T &v, const std::string& textual)
0249 {
0250 m_implicit_value = boost::any(v);
0251 m_implicit_value_as_text = textual;
0252 return this;
0253 }
0254
0255
0256
0257 typed_value* notifier(function1<void, const T&> f)
0258 {
0259 m_notifier = f;
0260 return this;
0261 }
0262
0263
0264
0265
0266 typed_value* composing()
0267 {
0268 m_composing = true;
0269 return this;
0270 }
0271
0272
0273
0274 typed_value* multitoken()
0275 {
0276 m_multitoken = true;
0277 return this;
0278 }
0279
0280
0281
0282
0283
0284
0285
0286
0287 typed_value* zero_tokens()
0288 {
0289 m_zero_tokens = true;
0290 return this;
0291 }
0292
0293
0294 typed_value* required()
0295 {
0296 m_required = true;
0297 return this;
0298 }
0299
0300 public:
0301
0302 std::string name() const;
0303
0304 bool is_composing() const { return m_composing; }
0305
0306 unsigned min_tokens() const
0307 {
0308 if (m_zero_tokens || !m_implicit_value.empty()) {
0309 return 0;
0310 } else {
0311 return 1;
0312 }
0313 }
0314
0315 unsigned max_tokens() const {
0316 if (m_multitoken) {
0317 return std::numeric_limits<unsigned>::max BOOST_PREVENT_MACRO_SUBSTITUTION();
0318 } else if (m_zero_tokens) {
0319 return 0;
0320 } else {
0321 return 1;
0322 }
0323 }
0324
0325 bool is_required() const { return m_required; }
0326
0327
0328
0329 void xparse(boost::any& value_store,
0330 const std::vector< std::basic_string<charT> >& new_tokens)
0331 const;
0332
0333
0334
0335
0336
0337 virtual bool apply_default(boost::any& value_store) const
0338 {
0339 if (m_default_value.empty()) {
0340 return false;
0341 } else {
0342 value_store = m_default_value;
0343 return true;
0344 }
0345 }
0346
0347
0348
0349
0350 void notify(const boost::any& value_store) const;
0351
0352 public:
0353
0354 #ifndef BOOST_NO_RTTI
0355 const std::type_info& value_type() const
0356 {
0357 return typeid(T);
0358 }
0359 #endif
0360
0361
0362 private:
0363 T* m_store_to;
0364
0365
0366
0367 std::string m_value_name;
0368 boost::any m_default_value;
0369 std::string m_default_value_as_text;
0370 boost::any m_implicit_value;
0371 std::string m_implicit_value_as_text;
0372 bool m_composing, m_implicit, m_multitoken, m_zero_tokens, m_required;
0373 boost::function1<void, const T&> m_notifier;
0374 };
0375
0376
0377
0378
0379
0380
0381
0382
0383 template<class T>
0384 typed_value<T>*
0385 value();
0386
0387
0388
0389 template<class T>
0390 typed_value<T>*
0391 value(T* v);
0392
0393
0394
0395
0396
0397 template<class T>
0398 typed_value<T, wchar_t>*
0399 wvalue();
0400
0401
0402
0403 template<class T>
0404 typed_value<T, wchar_t>*
0405 wvalue(T* v);
0406
0407
0408
0409
0410
0411 BOOST_PROGRAM_OPTIONS_DECL typed_value<bool>*
0412 bool_switch();
0413
0414
0415
0416 BOOST_PROGRAM_OPTIONS_DECL typed_value<bool>*
0417 bool_switch(bool* v);
0418
0419 }}
0420
0421 #include "boost/program_options/detail/value_semantic.hpp"
0422
0423 #endif
0424