Jump to content
  • Advertisement
Sign in to follow this  
_Sigma

Cast to string

This topic is 4075 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!