Advertisement

bstr vs. int8*

Started by April 01, 2004 02:41 PM
1 comment, last by leblebi 20 years, 8 months ago
Consider the following script:

t_img *tga;
bstr origname = "reflect.tga";
tga = img_new_tga ( bstr_to_int8(origname) );
and the functions:

t_img* img_new_tga (char *fname)
{
//blah blah

}
char* bstr_to_int8 ( asBSTR& str )
{
return (char*)str;
}
This works fine. But it doesnt work if I do this:

tga = img_new_tga ( "reflect.tga" );
or this

tga = img_new_tga ( bstr_to_int8("reflect.tga") );
Is this the expected behaviour? and if it is, is there a way to eliminate the need for the "bstr_to_int8" function. Typecasting in script doesnt work, it says that "it cant implicitly typecast from bstr to int8*". Thanks for any help.
Try registering

t_img* img_new_tga (char *fname){  //blah blah} 


like this:

engine->RegisterGlobalFunction("t_img *img_new_tga(bstr)", asFUNCTION(img_new_tga), asCALL_CDECL); 


This should let you write your script like this:

t_img *tga;tga = img_new_tga("reflect.tga"); 


BSTR is typedefed as (unsigned char *) so that it can be sent to functions taking a (char *) directly.

---

As to your other question. The behaviour is expected, since your bstr_to_int8() function takes a non const reference. Simply declare the parameter as const and you should be able to call it with a constant directly.

---

Currently the bstr needs to be handled with care. This is because AngelScript has to manage the memory allocated for the strings. Thus there are a few rules that you ought to follow or you''ll get memory leaks:

- When receiving an asBSTR by value, you must free the memory before returning from the function. Call asBStrFree()
- When returning an new asBSTR to the script engine it must be allocated with asBStrAlloc()
- When receiving an asBSTR & or an asBSTR * you don''t have to free the memory since the script engine is still holding a reference to it
- If you receive an asBSTR *, and need to change the string it points to call asBStrAlloc() for the new string, and asBStrFree() on the old string.

In a future version I will break out the implementation of asBSTR from the script engine so that each application can decide how they are handled. Of course I will make the functions needed to make the asBSTR work as normal available to the public for free.


__________________________________________________________
www.AngelCode.com - game development and more...
AngelScript - free scripting library

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
Ahhh, ok. This,
AS_API asBSTR AS_CALL asBStrAlloc(asUINT length){	unsigned char *str = new unsigned char[length+4+1];	*((asUINT*)str)  = length; // Length of string	str[length+4] = 0;      // Null terminated	return str + 4;}

explains it. Thanks a lot.

This topic is closed to new replies.

Advertisement