@@ -273,6 +273,8 @@ class INIReader
273273
274274 template <typename T>
275275 T Converter (std::string s) const ;
276+
277+ const bool BoolConverter (std::string s) const ;
276278};
277279
278280#endif // __INIREADER_H__
@@ -354,21 +356,10 @@ inline T INIReader::Get(std::string section, std::string name) const {
354356 if constexpr (std::is_same<T, std::string>()) {
355357 return value;
356358 } else if constexpr (std::is_same<T, bool >()) {
357- std::string s{_values.at (key)};
358- std::transform (s.begin (), s.end (), s.begin (), ::tolower);
359-
360- const std::unordered_map<std::string, bool > s2b{
361- {" 1" , true }, {" true" , true }, {" yes" , true }, {" on" , true },
362- {" 0" , false }, {" false" , false }, {" no" , false }, {" off" , false },
363- };
364- return s2b.find (s)->second ;
359+ return BoolConverter (value);
365360 } else {
366- try {
367- return Converter<T>(_values.at (key));
368- } catch (std::exception& e) {
369- throw std::runtime_error (" cannot parse value in " + key + " to type<T>." );
370- }
371- }
361+ return Converter<T>(value);
362+ };
372363}
373364
374365template <typename T>
@@ -409,21 +400,30 @@ inline std::vector<T> INIReader::GetVector(std::string section, std::string name
409400 };
410401}
411402
412-
413403template <typename T>
414404inline T INIReader::Converter (std::string s) const {
415- if constexpr (std::is_same<T, std::string>()) {
416- return s;
417- }
418-
419- T v{};
420- std::istringstream _{s};
421- _.exceptions (std::ios::failbit);
422-
423- _ >> v;
424- return v;
405+ try {
406+ T v{};
407+ std::istringstream _{s};
408+ _.exceptions (std::ios::failbit);
409+ _ >> v;
410+ return v;
411+ } catch (std::exception& e) {
412+ throw std::runtime_error (" cannot parse value '" + s + " ' to type<T>." );
413+ };
425414}
426415
416+ inline const bool INIReader::BoolConverter (std::string s) const {
417+ std::transform (s.begin (), s.end (), s.begin (), ::tolower);
418+ const std::unordered_map<std::string, bool > s2b{
419+ {" 1" , true }, {" true" , true }, {" yes" , true }, {" on" , true },
420+ {" 0" , false }, {" false" , false }, {" no" , false }, {" off" , false },
421+ };
422+ auto const value = s2b.find (s);
423+ if (value == s2b.end ()) {
424+ throw std::runtime_error (" '" + s + " ' is not a valid boolean value." );
425+ }
426+ return value->second ;
427427}
428428
429429inline int INIReader::ValueHandler (void * user, const char * section, const char * name, const char * value)
0 commit comments