1 // Copyright Vladimir Prus 2004.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
6 // This file defines template functions that are declared in
7 // ../value_semantic.hpp.
9 #include <boost/throw_exception.hpp>
11 namespace boost { namespace program_options {
13 extern BOOST_PROGRAM_OPTIONS_DECL std::string arg;
15 template<class T, class charT>
17 typed_value<T, charT>::name() const
19 std::string const& var = (m_value_name.empty() ? arg : m_value_name);
20 if (!m_implicit_value.empty() && !m_implicit_value_as_text.empty()) {
21 std::string msg = "[=" + var + "(=" + m_implicit_value_as_text + ")]";
22 if (!m_default_value.empty() && !m_default_value_as_text.empty())
23 msg += " (=" + m_default_value_as_text + ")";
26 else if (!m_default_value.empty() && !m_default_value_as_text.empty()) {
27 return var + " (=" + m_default_value_as_text + ")";
33 template<class T, class charT>
35 typed_value<T, charT>::notify(const boost::any& value_store) const
37 const T* value = boost::any_cast<T>(&value_store);
46 namespace validators {
47 /* If v.size() > 1, throw validation_error.
48 If v.size() == 1, return v.front()
49 Otherwise, returns a reference to a statically allocated
50 empty string if 'allow_empty' and throws validation_error
53 const std::basic_string<charT>& get_single_string(
54 const std::vector<std::basic_string<charT> >& v,
55 bool allow_empty = false)
57 static std::basic_string<charT> empty;
59 boost::throw_exception(validation_error(validation_error::multiple_values_not_allowed));
60 else if (v.size() == 1)
62 else if (!allow_empty)
63 boost::throw_exception(validation_error(validation_error::at_least_one_value_required));
67 /* Throws multiple_occurrences if 'value' is not empty. */
68 BOOST_PROGRAM_OPTIONS_DECL void
69 check_first_occurrence(const boost::any& value);
72 using namespace validators;
74 /** Validates 's' and updates 'v'.
75 @pre 'v' is either empty or in the state assigned by the previous
76 invocation of 'validate'.
77 The target type is specified via a parameter which has the type of
78 pointer to the desired type. This is workaround for compilers without
79 partial template ordering, just like the last 'long/int' parameter.
81 template<class T, class charT>
82 void validate(boost::any& v,
83 const std::vector< std::basic_string<charT> >& xs,
86 validators::check_first_occurrence(v);
87 std::basic_string<charT> s(validators::get_single_string(xs));
89 v = any(lexical_cast<T>(s));
91 catch(const bad_lexical_cast&) {
92 boost::throw_exception(invalid_option_value(s));
96 BOOST_PROGRAM_OPTIONS_DECL void validate(boost::any& v,
97 const std::vector<std::string>& xs,
101 #if !defined(BOOST_NO_STD_WSTRING)
102 BOOST_PROGRAM_OPTIONS_DECL void validate(boost::any& v,
103 const std::vector<std::wstring>& xs,
107 // For some reason, this declaration, which is require by the standard,
108 // cause msvc 7.1 to not generate code to specialization defined in
109 // value_semantic.cpp
110 #if ! ( BOOST_WORKAROUND(BOOST_MSVC, == 1310) )
111 BOOST_PROGRAM_OPTIONS_DECL void validate(boost::any& v,
112 const std::vector<std::string>& xs,
116 #if !defined(BOOST_NO_STD_WSTRING)
117 BOOST_PROGRAM_OPTIONS_DECL void validate(boost::any& v,
118 const std::vector<std::wstring>& xs,
124 /** Validates sequences. Allows multiple values per option occurrence
125 and multiple occurrences. */
126 template<class T, class charT>
127 void validate(boost::any& v,
128 const std::vector<std::basic_string<charT> >& s,
133 v = boost::any(std::vector<T>());
135 std::vector<T>* tv = boost::any_cast< std::vector<T> >(&v);
137 for (unsigned i = 0; i < s.size(); ++i)
140 /* We call validate so that if user provided
141 a validator for class T, we use it even
142 when parsing vector<T>. */
144 std::vector<std::basic_string<charT> > cv;
146 validate(a, cv, (T*)0, 0);
147 tv->push_back(boost::any_cast<T>(a));
149 catch(const bad_lexical_cast& /*e*/) {
150 boost::throw_exception(invalid_option_value(s[i]));
155 template<class T, class charT>
157 typed_value<T, charT>::
158 xparse(boost::any& value_store,
159 const std::vector<std::basic_string<charT> >& new_tokens) const
161 // If no tokens were given, and the option accepts an implicit
162 // value, then assign the implicit value as the stored value;
163 // otherwise, validate the user-provided token(s).
164 if (new_tokens.empty() && !m_implicit_value.empty())
165 value_store = m_implicit_value;
167 validate(value_store, new_tokens, (T*)0, 0);
174 // Explicit qualification is vc6 workaround.
175 return boost::program_options::value<T>(0);
182 typed_value<T>* r = new typed_value<T>(v);
188 typed_value<T, wchar_t>*
195 typed_value<T, wchar_t>*
198 typed_value<T, wchar_t>* r = new typed_value<T, wchar_t>(v);