libmoost
|
00001 /* vim:set ts=3 sw=3 sts=3 et: */ 00028 #ifndef MOOST_UTILS_YESNO_HPP__ 00029 #define MOOST_UTILS_YESNO_HPP__ 00030 00031 #include <stdexcept> 00032 #include <string> 00033 #include <iostream> 00034 00035 namespace moost { namespace utils { 00036 00037 class YesNo 00038 { 00039 public: 00040 YesNo(std::string const & sMsg, bool bImplicitNo = false) : bYes(false) 00041 { 00042 std::string s; 00043 00044 for(;;) 00045 { 00046 std::cout << sMsg << " <yes|no> " << std::flush; 00047 if(!std::getline(std::cin, s)) 00048 { 00049 throw std::runtime_error("Invalid input stream"); 00050 } 00051 00052 if(s == "yes") 00053 { 00054 bYes = true; 00055 break; 00056 } 00057 else 00058 if(bImplicitNo || s == "no") 00059 { 00060 break; 00061 } 00062 } 00063 } 00064 00065 bool IsYes() const { return bYes; } 00066 bool IsNo() const { return !bYes; } 00067 00068 private: 00069 bool bYes; 00070 }; 00071 00072 }} 00073 00074 #endif // MOOST_UTILS_YESNO_HPP__