123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- #ifndef BOOST_VALUE_SEMANTIC_HPP_VP_2004_02_24
- #define BOOST_VALUE_SEMANTIC_HPP_VP_2004_02_24
- #include <boost/program_options/config.hpp>
- #include <boost/program_options/errors.hpp>
- #include <boost/any.hpp>
- #include <boost/function/function1.hpp>
- #include <boost/lexical_cast.hpp>
- #include <string>
- #include <vector>
- #include <typeinfo>
- #include <limits>
- namespace boost { namespace program_options {
-
- class BOOST_PROGRAM_OPTIONS_DECL value_semantic {
- public:
-
- virtual std::string name() const = 0;
-
- virtual unsigned min_tokens() const = 0;
-
- virtual unsigned max_tokens() const = 0;
-
- virtual bool is_composing() const = 0;
-
- virtual bool is_required() const = 0;
-
-
- virtual void parse(boost::any& value_store,
- const std::vector<std::string>& new_tokens,
- bool utf8) const
- = 0;
-
- virtual bool apply_default(boost::any& value_store) const = 0;
-
-
- virtual void notify(const boost::any& value_store) const = 0;
-
- virtual ~value_semantic() {}
- };
-
- template<class charT>
- class value_semantic_codecvt_helper {
-
- };
-
- template<>
- class BOOST_PROGRAM_OPTIONS_DECL
- value_semantic_codecvt_helper<char> : public value_semantic {
- private:
- void parse(boost::any& value_store,
- const std::vector<std::string>& new_tokens,
- bool utf8) const;
- protected:
- virtual void xparse(boost::any& value_store,
- const std::vector<std::string>& new_tokens)
- const = 0;
- };
-
- template<>
- class BOOST_PROGRAM_OPTIONS_DECL
- value_semantic_codecvt_helper<wchar_t> : public value_semantic {
- private:
- void parse(boost::any& value_store,
- const std::vector<std::string>& new_tokens,
- bool utf8) const;
- protected:
- #if !defined(BOOST_NO_STD_WSTRING)
- virtual void xparse(boost::any& value_store,
- const std::vector<std::wstring>& new_tokens)
- const = 0;
- #endif
- };
-
-
- class BOOST_PROGRAM_OPTIONS_DECL
- untyped_value : public value_semantic_codecvt_helper<char> {
- public:
- untyped_value(bool zero_tokens = false)
- : m_zero_tokens(zero_tokens)
- {}
- std::string name() const;
- unsigned min_tokens() const;
- unsigned max_tokens() const;
- bool is_composing() const { return false; }
- bool is_required() const { return false; }
-
-
- void xparse(boost::any& value_store,
- const std::vector<std::string>& new_tokens) const;
-
- bool apply_default(boost::any&) const { return false; }
-
- void notify(const boost::any&) const {}
- private:
- bool m_zero_tokens;
- };
- #ifndef BOOST_NO_RTTI
-
- class typed_value_base
- {
- public:
-
-
- virtual const std::type_info& value_type() const = 0;
-
-
- virtual ~typed_value_base() {}
- };
- #endif
-
- template<class T, class charT = char>
- class typed_value : public value_semantic_codecvt_helper<charT>
- #ifndef BOOST_NO_RTTI
- , public typed_value_base
- #endif
- {
- public:
-
- typed_value(T* store_to)
- : m_store_to(store_to), m_composing(false),
- m_implicit(false), m_multitoken(false),
- m_zero_tokens(false), m_required(false)
- {}
-
- typed_value* default_value(const T& v)
- {
- m_default_value = boost::any(v);
- m_default_value_as_text = boost::lexical_cast<std::string>(v);
- return this;
- }
-
- typed_value* default_value(const T& v, const std::string& textual)
- {
- m_default_value = boost::any(v);
- m_default_value_as_text = textual;
- return this;
- }
-
- typed_value* implicit_value(const T &v)
- {
- m_implicit_value = boost::any(v);
- m_implicit_value_as_text =
- boost::lexical_cast<std::string>(v);
- return this;
- }
-
- typed_value* value_name(const std::string& name)
- {
- m_value_name = name;
- return this;
- }
-
- typed_value* implicit_value(const T &v, const std::string& textual)
- {
- m_implicit_value = boost::any(v);
- m_implicit_value_as_text = textual;
- return this;
- }
-
- typed_value* notifier(function1<void, const T&> f)
- {
- m_notifier = f;
- return this;
- }
-
- typed_value* composing()
- {
- m_composing = true;
- return this;
- }
-
- typed_value* multitoken()
- {
- m_multitoken = true;
- return this;
- }
-
- typed_value* zero_tokens()
- {
- m_zero_tokens = true;
- return this;
- }
-
-
- typed_value* required()
- {
- m_required = true;
- return this;
- }
- public:
- std::string name() const;
- bool is_composing() const { return m_composing; }
- unsigned min_tokens() const
- {
- if (m_zero_tokens || !m_implicit_value.empty()) {
- return 0;
- } else {
- return 1;
- }
- }
- unsigned max_tokens() const {
- if (m_multitoken) {
- return std::numeric_limits<unsigned>::max BOOST_PREVENT_MACRO_SUBSTITUTION();
- } else if (m_zero_tokens) {
- return 0;
- } else {
- return 1;
- }
- }
- bool is_required() const { return m_required; }
-
- void xparse(boost::any& value_store,
- const std::vector< std::basic_string<charT> >& new_tokens)
- const;
-
- virtual bool apply_default(boost::any& value_store) const
- {
- if (m_default_value.empty()) {
- return false;
- } else {
- value_store = m_default_value;
- return true;
- }
- }
-
- void notify(const boost::any& value_store) const;
- public:
-
- #ifndef BOOST_NO_RTTI
- const std::type_info& value_type() const
- {
- return typeid(T);
- }
- #endif
-
- private:
- T* m_store_to;
-
-
-
- std::string m_value_name;
- boost::any m_default_value;
- std::string m_default_value_as_text;
- boost::any m_implicit_value;
- std::string m_implicit_value_as_text;
- bool m_composing, m_implicit, m_multitoken, m_zero_tokens, m_required;
- boost::function1<void, const T&> m_notifier;
- };
-
- template<class T>
- typed_value<T>*
- value();
-
- template<class T>
- typed_value<T>*
- value(T* v);
-
- template<class T>
- typed_value<T, wchar_t>*
- wvalue();
-
- template<class T>
- typed_value<T, wchar_t>*
- wvalue(T* v);
-
- BOOST_PROGRAM_OPTIONS_DECL typed_value<bool>*
- bool_switch();
-
- BOOST_PROGRAM_OPTIONS_DECL typed_value<bool>*
- bool_switch(bool* v);
- }}
- #include "boost/program_options/detail/value_semantic.hpp"
- #endif
|