Sign in to follow this  
crellbar

Help, I'm going mad!!

Recommended Posts

Hi I've been working on my custom assert for the past few hours and it isn't working. Well it is, however when compiling for windows in debug mode (#undef NDEBUG, #define WIN32) it spits the dumbest error at me (in VS.NET 2005). It appears to try to replace the TEXT(stream_...) call in Assert.h with the letter L thus leaving Lstream_.... I then get an error saying Lstream_ does not exits etc. If any one has time could you try to compile it and tell me what you get. If you have any idea why it is doing this I would also be extremely grateful (but not in a gay way... sorry been watchin Peter Kay). http://crellbar.com/crap/BastardCode.zip Cheers in advance to anyone who can help.

Share this post


Link to post
Share on other sites
You're running up against a Microsoft 'feature' that makes it easier to use different string encodings. Unfortunately it uses the a macro called TEXT() to do this. This will be defined in one of the MS header files somewhere, and might have instructions for how you can disable the behaviour in your program, typically with a #define of your own. Obviously you can use a differently named call to TEXT instead, which will work around the problem.

Share this post


Link to post
Share on other sites
Firstly, Thanks for the quick reply.

Sorry - I must have left that bit out - I'm trying to use the microsoft TEXT() macro - I'm playing with a assert in a windows MessageBox and for the content it only accepts LpOmgMs (or something) which TEXT() will convert my char* to.

TEXT() just seems to be spazzing out on me. My old assert setup is very similar and works, but this one (even when I copy the code directly from my old one) just keeps screaming 'L' at me as an error message.

Share this post


Link to post
Share on other sites

if(MessageBox(NULL,TEXT((stream_.str().c_str())), TEXT("Assertion Failed."), 0))

the macro is used for string literals, like the caption parameter that you passed there. it merely prepends L to the macro parameter. i'm not sure what the best idea here would be since i never use win32 or unicode. i'd probably use ifdefs and hackish stuff, so somebody else should answer this. [grin]

Share this post


Link to post
Share on other sites
Quote:
Original post by crellbar
Firstly, Thanks for the quick reply.

Sorry - I must have left that bit out - I'm trying to use the microsoft TEXT() macro - I'm playing with a assert in a windows MessageBox and for the content it only accepts LpOmgMs (or something) which TEXT() will convert my char* to.

TEXT() just seems to be spazzing out on me. My old assert setup is very similar and works, but this one (even when I copy the code directly from my old one) just keeps screaming 'L' at me as an error message.


TEXT() doesn't convert a char * variable to a wide char string - it can only work with string litterals - that's why it puts a L in front of your string.

To convert a char* to a wchar_t*, you have to use mbstowcs().

Regards,

Share this post


Link to post
Share on other sites
The TEXT macro is only for string literals ("something"), not actual objects. The reason it's complaining about L is that wide string literals indeed look like this:
L"something"
And all TEXT does is to add L. Your actual problem is that you are using std::stringstream, the actual type you should use depend on the character set you are using, if you use standard characters (char), then std::stringstream is ok. If you need wide characters (wchar_t), then you should use std::wstringstream. Since your envirornment supports TEXT (it isn't standard), then it most likely also have a TCHAR to represent the current character type. Therefore if you have a TCHAR type then you could just use std::basic_stringstream<TCHAR> (you could use a typedef to shorten the name).

Share this post


Link to post
Share on other sites
Use TEXT() or _T() only on string literals, never on variables.

Try starting with the compiler libraries' supplied assert macro (which works), and start modifying it from there.

Share this post


Link to post
Share on other sites
Sure,


/* Make the message up here, not included to make main part easier to read */

/* Widen chars from char to wchar_t(2 bytes) */
std::string message_ = stream_.str();
// Got help from gamedev.net chat room for this
std::wstring w_message;
std::locale loc_;
const std::ctype< wchar_t > &ct = std::use_facet< std::ctype< wchar_t > >(loc_);

for(size_t i = 0; i < message_.size(); ++i)
w_message += ct.widen(message_[i]);


/* Create a message box containing the error text and wait until user presses OK*/
if(MessageBox(NULL, w_message.c_str(), TEXT("Assertion Failed."), 0))
_asm {int 3};


It's probably not the most efficient way - but at the end of the day it's an assert that is called once as the app is about to die. If anyone wants to add a better way to the end of this thread that would be ace.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this