Advertisement

Cast to string

Started by May 25, 2007 02:07 PM
3 comments, last by _Sigma 17 years, 6 months ago
Well progress is being made in my knowledge of AS, yet I've run into a road block! This might be related to my still no 100% complete knowledge of C++ though, so I apologize if so! I want to create a helper method only availble to the scripts called String. This method will take any type and atempt to cast it to a std::string using boost::lexical_cast(...); My question is: 1. What type do I need when registering with AS? RegisterGlbFn works and just registers a global function with AS.

m_script.RegisterGblFn("string String(any inStr)",asFUNCTION(::ToString));
Is that correct? So what does the prototype for ToString need to be? I'm thinking taking in a void* ...but I have a feeling templates might work? I'm currently using this, but clearly that only works for ints, and I would like to no have to overload this function 10times!

std::string ToString( int in )
{

	std::string out;
	try
	{
		out = boost::lexical_cast<std::string>(in);
	}
	catch (...)
	{
		out = "Bad cast.";
	}

	return out;
}
Any guidance would be great. Cheers _S
I'm afraid you're out of luck. You'll have to register a function for each type you wish to support the conversion for. By using the templates, you don't have to write the function each time, but you cannot take the address of the template function without specifying which type it is working on (which is when the code is generated for it).

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
Ah. Damn!
Quote: but you cannot take the address of the template function without specifying which type it is working on

Do you have an example of what this looks like? (I'm useless at times!! sorry!!)

Quote:
m_script.RegisterGblFn("string String(any inStr)",asFUNCTION(::ToString));
Is that correct then?
registerfunc("string String(int)",ASFUNCTION((boost::lexical_cast<int,string>)),ASCALL_CDECL);

Repeat for each type. Might need to switch the int,string. 50/50 chance of that.
What happens if lexical cast throws a bad cast? Although .. since we are dealing with known types, its not a big deal correct?

This topic is closed to new replies.

Advertisement