]> AND Private Git Repository - canny.git/blob - stc/exp/ml_stc_linux_make_v1.0/include/boost/program_options/detail/value_semantic.hpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
137094a8d6fc3281574c3ec051a88552bc61c2e8
[canny.git] / stc / exp / ml_stc_linux_make_v1.0 / include / boost / program_options / detail / value_semantic.hpp
1 // Copyright Vladimir Prus 2004.\r
2 // Distributed under the Boost Software License, Version 1.0.\r
3 // (See accompanying file LICENSE_1_0.txt\r
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)\r
5 \r
6 // This file defines template functions that are declared in\r
7 // ../value_semantic.hpp.\r
8 \r
9 #include <boost/throw_exception.hpp>\r
10 \r
11 namespace boost { namespace program_options { \r
12 \r
13     extern BOOST_PROGRAM_OPTIONS_DECL std::string arg;\r
14     \r
15     template<class T, class charT>\r
16     std::string\r
17     typed_value<T, charT>::name() const\r
18     {\r
19         if (!m_implicit_value.empty() && !m_implicit_value_as_text.empty()) {\r
20             std::string msg = "[=arg(=" + m_implicit_value_as_text + ")]";\r
21             if (!m_default_value.empty() && !m_default_value_as_text.empty())\r
22                 msg += " (=" + m_default_value_as_text + ")";\r
23             return msg;\r
24         }\r
25         else if (!m_default_value.empty() && !m_default_value_as_text.empty()) {\r
26             return arg + " (=" + m_default_value_as_text + ")";\r
27         } else {\r
28             return arg;\r
29         }\r
30     }\r
31 \r
32     template<class T, class charT>\r
33     void \r
34     typed_value<T, charT>::notify(const boost::any& value_store) const\r
35     {\r
36         const T* value = boost::any_cast<T>(&value_store);\r
37         if (m_store_to) {\r
38             *m_store_to = *value;\r
39         }\r
40         if (m_notifier) {\r
41             m_notifier(*value);\r
42         }\r
43     }\r
44 \r
45     namespace validators {\r
46         /* If v.size() > 1, throw validation_error. \r
47            If v.size() == 1, return v.front()\r
48            Otherwise, returns a reference to a statically allocated\r
49            empty string if 'allow_empty' and throws validation_error\r
50            otherwise. */\r
51         template<class charT>\r
52         const std::basic_string<charT>& get_single_string(\r
53             const std::vector<std::basic_string<charT> >& v, \r
54             bool allow_empty = false)\r
55         {\r
56             static std::basic_string<charT> empty;\r
57             if (v.size() > 1)\r
58                 boost::throw_exception(validation_error(validation_error::multiple_values_not_allowed));\r
59             else if (v.size() == 1)\r
60                 return v.front();\r
61             else if (!allow_empty)\r
62                 boost::throw_exception(validation_error(validation_error::at_least_one_value_required));\r
63             return empty;\r
64         }\r
65 \r
66         /* Throws multiple_occurrences if 'value' is not empty. */\r
67         BOOST_PROGRAM_OPTIONS_DECL void \r
68         check_first_occurrence(const boost::any& value);\r
69     }\r
70 \r
71     using namespace validators;\r
72 \r
73     /** Validates 's' and updates 'v'.\r
74         @pre 'v' is either empty or in the state assigned by the previous\r
75         invocation of 'validate'.\r
76         The target type is specified via a parameter which has the type of \r
77         pointer to the desired type. This is workaround for compilers without\r
78         partial template ordering, just like the last 'long/int' parameter.\r
79     */\r
80     template<class T, class charT>\r
81     void validate(boost::any& v, \r
82                   const std::vector< std::basic_string<charT> >& xs, \r
83                   T*, long)\r
84     {\r
85         validators::check_first_occurrence(v);\r
86         std::basic_string<charT> s(validators::get_single_string(xs));\r
87         try {\r
88             v = any(lexical_cast<T>(s));\r
89         }\r
90         catch(const bad_lexical_cast&) {\r
91             boost::throw_exception(invalid_option_value(s));\r
92         }\r
93     }\r
94 \r
95     BOOST_PROGRAM_OPTIONS_DECL void validate(boost::any& v, \r
96                        const std::vector<std::string>& xs, \r
97                        bool*,\r
98                        int);\r
99 \r
100 #if !defined(BOOST_NO_STD_WSTRING)\r
101     BOOST_PROGRAM_OPTIONS_DECL void validate(boost::any& v, \r
102                        const std::vector<std::wstring>& xs, \r
103                        bool*,\r
104                        int);\r
105 #endif\r
106     // For some reason, this declaration, which is require by the standard,\r
107     // cause gcc 3.2 to not generate code to specialization defined in\r
108     // value_semantic.cpp\r
109 #if ! ( ( BOOST_WORKAROUND(__GNUC__, <= 3) &&\\r
110           BOOST_WORKAROUND(__GNUC_MINOR__, < 3) ) || \\r
111         ( BOOST_WORKAROUND(BOOST_MSVC, == 1310) ) \\r
112       ) \r
113     BOOST_PROGRAM_OPTIONS_DECL void validate(boost::any& v, \r
114                        const std::vector<std::string>& xs,\r
115                        std::string*,\r
116                        int);\r
117 \r
118 #if !defined(BOOST_NO_STD_WSTRING)\r
119     BOOST_PROGRAM_OPTIONS_DECL void validate(boost::any& v, \r
120                        const std::vector<std::wstring>& xs,\r
121                        std::string*,\r
122                        int);\r
123 #endif\r
124 #endif\r
125 \r
126     /** Validates sequences. Allows multiple values per option occurrence\r
127        and multiple occurrences. */\r
128     template<class T, class charT>\r
129     void validate(boost::any& v, \r
130                   const std::vector<std::basic_string<charT> >& s, \r
131                   std::vector<T>*,\r
132                   int)\r
133     {\r
134         if (v.empty()) {\r
135             v = boost::any(std::vector<T>());\r
136         }\r
137         std::vector<T>* tv = boost::any_cast< std::vector<T> >(&v);\r
138         assert(NULL != tv);\r
139         for (unsigned i = 0; i < s.size(); ++i)\r
140         {\r
141             try {\r
142                 /* We call validate so that if user provided\r
143                    a validator for class T, we use it even\r
144                    when parsing vector<T>.  */\r
145                 boost::any a;\r
146                 std::vector<std::basic_string<charT> > v;\r
147                 v.push_back(s[i]);\r
148                 validate(a, v, (T*)0, 0);                \r
149                 tv->push_back(boost::any_cast<T>(a));\r
150             }\r
151             catch(const bad_lexical_cast& /*e*/) {\r
152                 boost::throw_exception(invalid_option_value(s[i]));\r
153             }\r
154         }\r
155     }\r
156 \r
157     template<class T, class charT>\r
158     void \r
159     typed_value<T, charT>::\r
160     xparse(boost::any& value_store, \r
161            const std::vector<std::basic_string<charT> >& new_tokens) const\r
162     {\r
163         // If no tokens were given, and the option accepts an implicit\r
164         // value, then assign the implicit value as the stored value;\r
165         // otherwise, validate the user-provided token(s).\r
166         if (new_tokens.empty() && !m_implicit_value.empty())\r
167             value_store = m_implicit_value;\r
168         else\r
169             validate(value_store, new_tokens, (T*)0, 0);\r
170     }\r
171 \r
172     template<class T>\r
173     typed_value<T>*\r
174     value()\r
175     {\r
176         // Explicit qualification is vc6 workaround.\r
177         return boost::program_options::value<T>(0);\r
178     }\r
179 \r
180     template<class T>\r
181     typed_value<T>*\r
182     value(T* v)\r
183     {\r
184         typed_value<T>* r = new typed_value<T>(v);\r
185 \r
186         return r;        \r
187     }\r
188 \r
189     template<class T>\r
190     typed_value<T, wchar_t>*\r
191     wvalue()\r
192     {\r
193         return wvalue<T>(0);\r
194     }\r
195 \r
196     template<class T>\r
197     typed_value<T, wchar_t>*\r
198     wvalue(T* v)\r
199     {\r
200         typed_value<T, wchar_t>* r = new typed_value<T, wchar_t>(v);\r
201 \r
202         return r;        \r
203     }\r
204 \r
205 \r
206 \r
207 }}\r