Chris@16
|
1 // Copyright Vladimir Prus 2002-2004.
|
Chris@16
|
2 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
3 // (See accompanying file LICENSE_1_0.txt
|
Chris@16
|
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6
|
Chris@16
|
7 #ifndef BOOST_VARIABLES_MAP_VP_2003_05_19
|
Chris@16
|
8 #define BOOST_VARIABLES_MAP_VP_2003_05_19
|
Chris@16
|
9
|
Chris@16
|
10 #include <boost/program_options/config.hpp>
|
Chris@16
|
11
|
Chris@16
|
12 #include <boost/any.hpp>
|
Chris@16
|
13 #include <boost/shared_ptr.hpp>
|
Chris@16
|
14
|
Chris@16
|
15 #include <string>
|
Chris@16
|
16 #include <map>
|
Chris@16
|
17 #include <set>
|
Chris@16
|
18
|
Chris@16
|
19 #if defined(BOOST_MSVC)
|
Chris@16
|
20 # pragma warning (push)
|
Chris@16
|
21 # pragma warning (disable:4251) // 'boost::program_options::variable_value::v' : class 'boost::any' needs to have dll-interface to be used by clients of class 'boost::program_options::variable_value
|
Chris@16
|
22 #endif
|
Chris@16
|
23
|
Chris@16
|
24 namespace boost { namespace program_options {
|
Chris@16
|
25
|
Chris@16
|
26 template<class charT>
|
Chris@16
|
27 class basic_parsed_options;
|
Chris@16
|
28
|
Chris@16
|
29 class value_semantic;
|
Chris@16
|
30 class variables_map;
|
Chris@16
|
31
|
Chris@16
|
32 // forward declaration
|
Chris@16
|
33
|
Chris@16
|
34 /** Stores in 'm' all options that are defined in 'options'.
|
Chris@16
|
35 If 'm' already has a non-defaulted value of an option, that value
|
Chris@16
|
36 is not changed, even if 'options' specify some value.
|
Chris@16
|
37 */
|
Chris@16
|
38 BOOST_PROGRAM_OPTIONS_DECL
|
Chris@16
|
39 void store(const basic_parsed_options<char>& options, variables_map& m,
|
Chris@16
|
40 bool utf8 = false);
|
Chris@16
|
41
|
Chris@16
|
42 /** Stores in 'm' all options that are defined in 'options'.
|
Chris@16
|
43 If 'm' already has a non-defaulted value of an option, that value
|
Chris@16
|
44 is not changed, even if 'options' specify some value.
|
Chris@16
|
45 This is wide character variant.
|
Chris@16
|
46 */
|
Chris@16
|
47 BOOST_PROGRAM_OPTIONS_DECL
|
Chris@16
|
48 void store(const basic_parsed_options<wchar_t>& options,
|
Chris@16
|
49 variables_map& m);
|
Chris@16
|
50
|
Chris@16
|
51
|
Chris@16
|
52 /** Runs all 'notify' function for options in 'm'. */
|
Chris@16
|
53 BOOST_PROGRAM_OPTIONS_DECL void notify(variables_map& m);
|
Chris@16
|
54
|
Chris@16
|
55 /** Class holding value of option. Contains details about how the
|
Chris@16
|
56 value is set and allows to conveniently obtain the value.
|
Chris@16
|
57 */
|
Chris@16
|
58 class BOOST_PROGRAM_OPTIONS_DECL variable_value {
|
Chris@16
|
59 public:
|
Chris@16
|
60 variable_value() : m_defaulted(false) {}
|
Chris@16
|
61 variable_value(const boost::any& xv, bool xdefaulted)
|
Chris@16
|
62 : v(xv), m_defaulted(xdefaulted)
|
Chris@16
|
63 {}
|
Chris@16
|
64
|
Chris@16
|
65 /** If stored value if of type T, returns that value. Otherwise,
|
Chris@16
|
66 throws boost::bad_any_cast exception. */
|
Chris@16
|
67 template<class T>
|
Chris@16
|
68 const T& as() const {
|
Chris@16
|
69 return boost::any_cast<const T&>(v);
|
Chris@16
|
70 }
|
Chris@16
|
71 /** @overload */
|
Chris@16
|
72 template<class T>
|
Chris@16
|
73 T& as() {
|
Chris@16
|
74 return boost::any_cast<T&>(v);
|
Chris@16
|
75 }
|
Chris@16
|
76
|
Chris@16
|
77 /// Returns true if no value is stored.
|
Chris@16
|
78 bool empty() const;
|
Chris@16
|
79 /** Returns true if the value was not explicitly
|
Chris@16
|
80 given, but has default value. */
|
Chris@16
|
81 bool defaulted() const;
|
Chris@16
|
82 /** Returns the contained value. */
|
Chris@16
|
83 const boost::any& value() const;
|
Chris@16
|
84
|
Chris@16
|
85 /** Returns the contained value. */
|
Chris@16
|
86 boost::any& value();
|
Chris@16
|
87 private:
|
Chris@16
|
88 boost::any v;
|
Chris@16
|
89 bool m_defaulted;
|
Chris@16
|
90 // Internal reference to value semantic. We need to run
|
Chris@16
|
91 // notifications when *final* values of options are known, and
|
Chris@16
|
92 // they are known only after all sources are stored. By that
|
Chris@16
|
93 // time options_description for the first source might not
|
Chris@16
|
94 // be easily accessible, so we need to store semantic here.
|
Chris@16
|
95 shared_ptr<const value_semantic> m_value_semantic;
|
Chris@16
|
96
|
Chris@16
|
97 friend BOOST_PROGRAM_OPTIONS_DECL
|
Chris@16
|
98 void store(const basic_parsed_options<char>& options,
|
Chris@16
|
99 variables_map& m, bool);
|
Chris@16
|
100
|
Chris@101
|
101 friend class BOOST_PROGRAM_OPTIONS_DECL variables_map;
|
Chris@16
|
102 };
|
Chris@16
|
103
|
Chris@16
|
104 /** Implements string->string mapping with convenient value casting
|
Chris@16
|
105 facilities. */
|
Chris@16
|
106 class BOOST_PROGRAM_OPTIONS_DECL abstract_variables_map {
|
Chris@16
|
107 public:
|
Chris@16
|
108 abstract_variables_map();
|
Chris@16
|
109 abstract_variables_map(const abstract_variables_map* next);
|
Chris@16
|
110
|
Chris@16
|
111 virtual ~abstract_variables_map() {}
|
Chris@16
|
112
|
Chris@16
|
113 /** Obtains the value of variable 'name', from *this and
|
Chris@16
|
114 possibly from the chain of variable maps.
|
Chris@16
|
115
|
Chris@16
|
116 - if there's no value in *this.
|
Chris@16
|
117 - if there's next variable map, returns value from it
|
Chris@16
|
118 - otherwise, returns empty value
|
Chris@16
|
119
|
Chris@16
|
120 - if there's defaulted value
|
Chris@16
|
121 - if there's next varaible map, which has a non-defauled
|
Chris@16
|
122 value, return that
|
Chris@16
|
123 - otherwise, return value from *this
|
Chris@16
|
124
|
Chris@16
|
125 - if there's a non-defauled value, returns it.
|
Chris@16
|
126 */
|
Chris@16
|
127 const variable_value& operator[](const std::string& name) const;
|
Chris@16
|
128
|
Chris@16
|
129 /** Sets next variable map, which will be used to find
|
Chris@16
|
130 variables not found in *this. */
|
Chris@16
|
131 void next(abstract_variables_map* next);
|
Chris@16
|
132
|
Chris@16
|
133 private:
|
Chris@16
|
134 /** Returns value of variable 'name' stored in *this, or
|
Chris@16
|
135 empty value otherwise. */
|
Chris@16
|
136 virtual const variable_value& get(const std::string& name) const = 0;
|
Chris@16
|
137
|
Chris@16
|
138 const abstract_variables_map* m_next;
|
Chris@16
|
139 };
|
Chris@16
|
140
|
Chris@16
|
141 /** Concrete variables map which store variables in real map.
|
Chris@16
|
142
|
Chris@16
|
143 This class is derived from std::map<std::string, variable_value>,
|
Chris@16
|
144 so you can use all map operators to examine its content.
|
Chris@16
|
145 */
|
Chris@16
|
146 class BOOST_PROGRAM_OPTIONS_DECL variables_map : public abstract_variables_map,
|
Chris@16
|
147 public std::map<std::string, variable_value>
|
Chris@16
|
148 {
|
Chris@16
|
149 public:
|
Chris@16
|
150 variables_map();
|
Chris@16
|
151 variables_map(const abstract_variables_map* next);
|
Chris@16
|
152
|
Chris@16
|
153 // Resolve conflict between inherited operators.
|
Chris@16
|
154 const variable_value& operator[](const std::string& name) const
|
Chris@16
|
155 { return abstract_variables_map::operator[](name); }
|
Chris@16
|
156
|
Chris@16
|
157 // Override to clear some extra fields.
|
Chris@16
|
158 void clear();
|
Chris@16
|
159
|
Chris@16
|
160 void notify();
|
Chris@16
|
161
|
Chris@16
|
162 private:
|
Chris@16
|
163 /** Implementation of abstract_variables_map::get
|
Chris@16
|
164 which does 'find' in *this. */
|
Chris@16
|
165 const variable_value& get(const std::string& name) const;
|
Chris@16
|
166
|
Chris@16
|
167 /** Names of option with 'final' values -- which should not
|
Chris@16
|
168 be changed by subsequence assignments. */
|
Chris@16
|
169 std::set<std::string> m_final;
|
Chris@16
|
170
|
Chris@16
|
171 friend BOOST_PROGRAM_OPTIONS_DECL
|
Chris@16
|
172 void store(const basic_parsed_options<char>& options,
|
Chris@16
|
173 variables_map& xm,
|
Chris@16
|
174 bool utf8);
|
Chris@16
|
175
|
Chris@16
|
176 /** Names of required options, filled by parser which has
|
Chris@16
|
177 access to options_description.
|
Chris@16
|
178 The map values are the "canonical" names for each corresponding option.
|
Chris@16
|
179 This is useful in creating diagnostic messages when the option is absent. */
|
Chris@16
|
180 std::map<std::string, std::string> m_required;
|
Chris@16
|
181 };
|
Chris@16
|
182
|
Chris@16
|
183
|
Chris@16
|
184 /*
|
Chris@16
|
185 * Templates/inlines
|
Chris@16
|
186 */
|
Chris@16
|
187
|
Chris@16
|
188 inline bool
|
Chris@16
|
189 variable_value::empty() const
|
Chris@16
|
190 {
|
Chris@16
|
191 return v.empty();
|
Chris@16
|
192 }
|
Chris@16
|
193
|
Chris@16
|
194 inline bool
|
Chris@16
|
195 variable_value::defaulted() const
|
Chris@16
|
196 {
|
Chris@16
|
197 return m_defaulted;
|
Chris@16
|
198 }
|
Chris@16
|
199
|
Chris@16
|
200 inline
|
Chris@16
|
201 const boost::any&
|
Chris@16
|
202 variable_value::value() const
|
Chris@16
|
203 {
|
Chris@16
|
204 return v;
|
Chris@16
|
205 }
|
Chris@16
|
206
|
Chris@16
|
207 inline
|
Chris@16
|
208 boost::any&
|
Chris@16
|
209 variable_value::value()
|
Chris@16
|
210 {
|
Chris@16
|
211 return v;
|
Chris@16
|
212 }
|
Chris@16
|
213
|
Chris@16
|
214 }}
|
Chris@16
|
215
|
Chris@16
|
216 #if defined(BOOST_MSVC)
|
Chris@16
|
217 # pragma warning (pop)
|
Chris@16
|
218 #endif
|
Chris@16
|
219
|
Chris@16
|
220 #endif
|