Sign in to follow this  

Cast to string

This topic is 3855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
What happens if lexical cast throws a bad cast? Although .. since we are dealing with known types, its not a big deal correct?

Share this post


Link to post
Share on other sites

This topic is 3855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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