variables_map.hpp 6.8 KB

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