#pragma once

#include <exception>
#include <cstdio>
#include <cstdarg>
#include <string>

///Allows to throw an error message formatted from a printf-like argument list.
class StrException: public std::exception
{
	std::string msg;

public:
	StrException(const char *format, ... ) 
	{
		va_list ap;
		va_start( ap, format );
		// measure message length
		size_t msgLen = _vscprintf(format, ap); 
		// allocate memory for message
		char *tmpMsg = new char[msgLen+1]; 
		// format the message
		_vsnprintf_s( tmpMsg, msgLen+1, _TRUNCATE, format, ap );
		va_end( ap );
		// copy to member
		msg.assign(tmpMsg);
		delete [] tmpMsg;
	}

	virtual const char* what() const throw() { return msg.c_str(); }
};
