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