1 // Copyright Vladimir Prus 2002-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
7 #ifndef BOOST_VARIABLES_MAP_VP_2003_05_19
\r
8 #define BOOST_VARIABLES_MAP_VP_2003_05_19
\r
10 #include <boost/program_options/config.hpp>
\r
12 #include <boost/any.hpp>
\r
13 #include <boost/shared_ptr.hpp>
\r
19 namespace boost { namespace program_options {
\r
21 template<class charT>
\r
22 class basic_parsed_options;
\r
24 class value_semantic;
\r
25 class variables_map;
\r
27 // forward declaration
\r
29 /** Stores in 'm' all options that are defined in 'options'.
\r
30 If 'm' already has a non-defaulted value of an option, that value
\r
31 is not changed, even if 'options' specify some value.
\r
33 BOOST_PROGRAM_OPTIONS_DECL
\r
34 void store(const basic_parsed_options<char>& options, variables_map& m,
\r
37 /** Stores in 'm' all options that are defined in 'options'.
\r
38 If 'm' already has a non-defaulted value of an option, that value
\r
39 is not changed, even if 'options' specify some value.
\r
40 This is wide character variant.
\r
42 BOOST_PROGRAM_OPTIONS_DECL
\r
43 void store(const basic_parsed_options<wchar_t>& options,
\r
47 /** Runs all 'notify' function for options in 'm'. */
\r
48 BOOST_PROGRAM_OPTIONS_DECL void notify(variables_map& m);
\r
50 /** Class holding value of option. Contains details about how the
\r
51 value is set and allows to conveniently obtain the value.
\r
53 class BOOST_PROGRAM_OPTIONS_DECL variable_value {
\r
55 variable_value() : m_defaulted(false) {}
\r
56 variable_value(const boost::any& v, bool defaulted)
\r
57 : v(v), m_defaulted(defaulted)
\r
60 /** If stored value if of type T, returns that value. Otherwise,
\r
61 throws boost::bad_any_cast exception. */
\r
63 const T& as() const {
\r
64 return boost::any_cast<const T&>(v);
\r
69 return boost::any_cast<T&>(v);
\r
72 /// Returns true if no value is stored.
\r
74 /** Returns true if the value was not explicitly
\r
75 given, but has default value. */
\r
76 bool defaulted() const;
\r
77 /** Returns the contained value. */
\r
78 const boost::any& value() const;
\r
80 /** Returns the contained value. */
\r
81 boost::any& value();
\r
85 // Internal reference to value semantic. We need to run
\r
86 // notifications when *final* values of options are known, and
\r
87 // they are known only after all sources are stored. By that
\r
88 // time options_description for the first source might not
\r
89 // be easily accessible, so we need to store semantic here.
\r
90 shared_ptr<const value_semantic> m_value_semantic;
\r
92 friend BOOST_PROGRAM_OPTIONS_DECL
\r
93 void store(const basic_parsed_options<char>& options,
\r
94 variables_map& m, bool);
\r
96 friend BOOST_PROGRAM_OPTIONS_DECL class variables_map;
\r
99 /** Implements string->string mapping with convenient value casting
\r
101 class BOOST_PROGRAM_OPTIONS_DECL abstract_variables_map {
\r
103 abstract_variables_map();
\r
104 abstract_variables_map(const abstract_variables_map* next);
\r
106 virtual ~abstract_variables_map() {}
\r
108 /** Obtains the value of variable 'name', from *this and
\r
109 possibly from the chain of variable maps.
\r
111 - if there's no value in *this.
\r
112 - if there's next variable map, returns value from it
\r
113 - otherwise, returns empty value
\r
115 - if there's defaulted value
\r
116 - if there's next varaible map, which has a non-defauled
\r
118 - otherwise, return value from *this
\r
120 - if there's a non-defauled value, returns it.
\r
122 const variable_value& operator[](const std::string& name) const;
\r
124 /** Sets next variable map, which will be used to find
\r
125 variables not found in *this. */
\r
126 void next(abstract_variables_map* next);
\r
129 /** Returns value of variable 'name' stored in *this, or
\r
130 empty value otherwise. */
\r
131 virtual const variable_value& get(const std::string& name) const = 0;
\r
133 const abstract_variables_map* m_next;
\r
136 /** Concrete variables map which store variables in real map.
\r
138 This class is derived from std::map<std::string, variable_value>,
\r
139 so you can use all map operators to examine its content.
\r
141 class BOOST_PROGRAM_OPTIONS_DECL variables_map : public abstract_variables_map,
\r
142 public std::map<std::string, variable_value>
\r
146 variables_map(const abstract_variables_map* next);
\r
148 // Resolve conflict between inherited operators.
\r
149 const variable_value& operator[](const std::string& name) const
\r
150 { return abstract_variables_map::operator[](name); }
\r
155 /** Implementation of abstract_variables_map::get
\r
156 which does 'find' in *this. */
\r
157 const variable_value& get(const std::string& name) const;
\r
159 /** Names of option with 'final' values -- which should not
\r
160 be changed by subsequence assignments. */
\r
161 std::set<std::string> m_final;
\r
163 friend BOOST_PROGRAM_OPTIONS_DECL
\r
164 void store(const basic_parsed_options<char>& options,
\r
168 /** Names of required options, filled by parser which has
\r
169 access to options_description. */
\r
170 std::set<std::string> m_required;
\r
175 * Templates/inlines
\r
179 variable_value::empty() const
\r
185 variable_value::defaulted() const
\r
187 return m_defaulted;
\r
192 variable_value::value() const
\r
199 variable_value::value()
\r