comparison C++/Parser.h @ 209:706f861c4c6d

Little cleanup and rename getValue -> getOption
author David Demelier <markand@malikania.fr>
date Fri, 24 Jan 2014 09:17:06 +0100
parents 1ffe6d4937b7
children 3b0e276f0866
comparison
equal deleted inserted replaced
203:1ffe6d4937b7 209:706f861c4c6d
35 * options are allowed (default behavior), the root 35 * options are allowed (default behavior), the root
36 * section is "". 36 * section is "".
37 */ 37 */
38 class Section { 38 class Section {
39 public: 39 public:
40 friend class Parser;
41
40 using Map = std::unordered_map<std::string, std::string>; 42 using Map = std::unordered_map<std::string, std::string>;
41 43
42 friend class Parser; 44 template <typename T>
45 struct Converter {
46 static const bool supported = false;
47 };
43 48
44 private: 49 private:
45 std::string m_name; /*! name of section */ 50 std::string m_name; /*! name of section */
46 Map m_options; /*! list of options inside */ 51 Map m_options; /*! list of options inside */
47 bool m_allowed; /*! is authorized to push */ 52 bool m_allowed; /*! is authorized to push */
48 53
49 const std::string findOption(const std::string &name) const; 54 const std::string findOption(const std::string &name) const;
55
50 public: 56 public:
51 template <typename T>
52 struct Converter {
53 static const bool supported = false;
54 };
55
56 /** 57 /**
57 * Default constructor. 58 * Default constructor.
58 */ 59 */
59 Section(); 60 Section();
60 61
61 /** 62 /**
62 * Named constructor. 63 * Named constructor.
63 * 64 *
64 * @param name the section name 65 * @param name the section name
65 */ 66 * @param allowed is allowed to push
66 Section(const std::string &name); 67 */
68 Section(const std::string &name, bool allowed = true);
67 69
68 /** 70 /**
69 * Tells if that section has the specified option name. 71 * Tells if that section has the specified option name.
70 * 72 *
71 * @param name the option name 73 * @param name the option name
113 * 115 *
114 * @param name the option name 116 * @param name the option name
115 * @return the value if found 117 * @return the value if found
116 */ 118 */
117 template <typename T> 119 template <typename T>
118 T getValue(const std::string &name) const 120 T getOption(const std::string &name) const
119 { 121 {
120 try { 122 try {
121 return requireValue<T>(name); 123 return requireOption<T>(name);
122 } catch (...) { 124 } catch (...) {
123 // Catch any conversion error. 125 // Catch any conversion error.
124 } 126 }
125 127
126 return T(); 128 return T();
135 * @return the value 137 * @return the value
136 * @throw std::out_of_range if not found 138 * @throw std::out_of_range if not found
137 * @throw std::invalid_argument on conversion failures 139 * @throw std::invalid_argument on conversion failures
138 */ 140 */
139 template <typename T> 141 template <typename T>
140 T requireValue(const std::string &name) const 142 T requireOption(const std::string &name) const
141 { 143 {
142 static_assert(Converter<T>::supported, "invalid type requested"); 144 static_assert(Converter<T>::supported, "invalid type requested");
143 145
144 return Converter<T>::convert(m_options.at(name)); 146 return Converter<T>::convert(m_options.at(name));
145 } 147 }