1 2 #ifndef WK_ERROR_FORMATTER 3 #define WK_ERROR_FORMATTER 4 5 // https://stackoverflow.com/questions/12261915/how-to-throw-stdexceptions-with-variable-messages 6 #include <stdexcept> 7 #include <sstream> 8 #include <stdexcept> 9 10 class ErrorFormatter { 11 public: ErrorFormatter()12 ErrorFormatter() {} ~ErrorFormatter()13 ~ErrorFormatter() {} 14 15 template <typename Type> operator <<(const Type & value)16 ErrorFormatter & operator << (const Type & value) { 17 stream_ << value; 18 return *this; 19 } 20 str() const21 std::string str() const { return stream_.str(); } operator std::string() const22 operator std::string () const { return stream_.str(); } 23 24 enum ConvertToString { 25 to_str 26 }; operator >>(ConvertToString)27 std::string operator >> (ConvertToString) { return stream_.str(); } 28 29 private: 30 std::stringstream stream_; 31 32 ErrorFormatter(const ErrorFormatter &); 33 ErrorFormatter & operator = (ErrorFormatter &); 34 }; 35 36 # endif 37