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

Private GIT Repository
6dfe5c4b69e277d42a63b61a9ca9843948bc658a
[canny.git] / stc / exp / ml_stc_linux_make_v1.0 / include / boost / program_options / variables_map.hpp
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
5 \r
6 \r
7 #ifndef BOOST_VARIABLES_MAP_VP_2003_05_19\r
8 #define BOOST_VARIABLES_MAP_VP_2003_05_19\r
9 \r
10 #include <boost/program_options/config.hpp>\r
11 \r
12 #include <boost/any.hpp>\r
13 #include <boost/shared_ptr.hpp>\r
14 \r
15 #include <string>\r
16 #include <map>\r
17 #include <set>\r
18 \r
19 namespace boost { namespace program_options {\r
20 \r
21     template<class charT>\r
22     class basic_parsed_options;\r
23 \r
24     class value_semantic;\r
25     class variables_map;\r
26 \r
27     // forward declaration\r
28 \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
32     */\r
33     BOOST_PROGRAM_OPTIONS_DECL \r
34     void store(const basic_parsed_options<char>& options, variables_map& m,\r
35                     bool utf8 = false);\r
36 \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
41     */\r
42     BOOST_PROGRAM_OPTIONS_DECL \r
43     void store(const basic_parsed_options<wchar_t>& options, \r
44                     variables_map& m);\r
45 \r
46 \r
47     /** Runs all 'notify' function for options in 'm'. */\r
48     BOOST_PROGRAM_OPTIONS_DECL void notify(variables_map& m);\r
49 \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
52     */\r
53     class BOOST_PROGRAM_OPTIONS_DECL variable_value {\r
54     public:\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
58         {}\r
59 \r
60         /** If stored value if of type T, returns that value. Otherwise,\r
61             throws boost::bad_any_cast exception. */\r
62        template<class T>\r
63        const T& as() const {\r
64            return boost::any_cast<const T&>(v);\r
65        }\r
66        /** @overload */\r
67        template<class T>\r
68        T& as() {\r
69            return boost::any_cast<T&>(v);\r
70        }\r
71 \r
72         /// Returns true if no value is stored.\r
73         bool empty() const;\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
79 \r
80         /** Returns the contained value. */\r
81         boost::any& value();\r
82     private:\r
83         boost::any v;\r
84         bool m_defaulted;\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
91 \r
92         friend BOOST_PROGRAM_OPTIONS_DECL\r
93         void store(const basic_parsed_options<char>& options, \r
94               variables_map& m, bool);\r
95 \r
96         friend BOOST_PROGRAM_OPTIONS_DECL class variables_map;\r
97     };\r
98 \r
99     /** Implements string->string mapping with convenient value casting\r
100         facilities. */\r
101     class BOOST_PROGRAM_OPTIONS_DECL abstract_variables_map {\r
102     public:\r
103         abstract_variables_map();\r
104         abstract_variables_map(const abstract_variables_map* next);\r
105 \r
106         virtual ~abstract_variables_map() {}\r
107 \r
108         /** Obtains the value of variable 'name', from *this and\r
109             possibly from the chain of variable maps.\r
110 \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
114 \r
115             - if there's defaulted value\r
116                 - if there's next varaible map, which has a non-defauled\r
117                   value, return that\r
118                 - otherwise, return value from *this\r
119 \r
120             - if there's a non-defauled value, returns it.\r
121         */\r
122         const variable_value& operator[](const std::string& name) const;\r
123 \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
127 \r
128     private:\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
132 \r
133         const abstract_variables_map* m_next;\r
134     };\r
135 \r
136     /** Concrete variables map which store variables in real map. \r
137         \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
140     */\r
141     class BOOST_PROGRAM_OPTIONS_DECL variables_map : public abstract_variables_map,\r
142                                public std::map<std::string, variable_value>\r
143     {\r
144     public:\r
145         variables_map();\r
146         variables_map(const abstract_variables_map* next);\r
147 \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
151         \r
152         void notify();\r
153 \r
154     private:\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
158 \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
162 \r
163         friend BOOST_PROGRAM_OPTIONS_DECL\r
164         void store(const basic_parsed_options<char>& options, \r
165                           variables_map& xm,\r
166                           bool utf8);\r
167         \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
171     };\r
172 \r
173 \r
174     /*\r
175      * Templates/inlines\r
176      */\r
177 \r
178     inline bool\r
179     variable_value::empty() const\r
180     {\r
181         return v.empty();\r
182     }\r
183 \r
184     inline bool\r
185     variable_value::defaulted() const\r
186     {\r
187         return m_defaulted;\r
188     }\r
189 \r
190     inline\r
191     const boost::any&\r
192     variable_value::value() const\r
193     {\r
194         return v;\r
195     }\r
196 \r
197     inline\r
198     boost::any&\r
199     variable_value::value()\r
200     {\r
201         return v;\r
202     }\r
203 \r
204 }}\r
205 \r
206 #endif\r