Operator+-*/, constructor, assignment and &in questions?

Started by
2 comments, last by WitchLord 15 years, 6 months ago
Here is the code for me to register a new type with its constructor, deconstructor, copy constructor. Assignment operator. Here are some parts of code. I did not include all the registration. If u need more information, let me know. Note: I write * operator return by reference is for testing purpose.

nRet = (asERetCodes) pScriptEngine->RegisterObjectType("UniType", sizeof(UniType),   asOBJ_VALUE | asOBJ_APP_CLASS_CDA /*asOBJ_APP_CLASS_CONSTRUCTOR | asOBJ_APP_CLASS_DESTRUCTOR | asOBJ_APP_CLASS_ASSIGNMENT*/);
assert( nRet >= 0 );
nRet = (asERetCodes) pScriptEngine->RegisterObjectBehaviour("UniType", asBEHAVE_CONSTRUCT,    "void f()",                 asFUNCTION(uniTypeDefaultConstructor), asCALL_CDECL_OBJLAST);
assert( nRet >= 0 );
	
//nRet = (asERetCodes) pScriptEngine->RegisterObjectBehaviour("_String", asBEHAVE_CONSTRUCT,    "void f()(const _String &in)", asFUNCTION(stringCopyConstructor), asCALL_CDECL_OBJLAST);
//nRet = (asERetCodes) pScriptEngine->RegisterObjectBehaviour("_String", asBEHAVE_DESTRUCT,    "void f(int)", asFUNCTION(stringINTConstructor), asCALL_CDECL_OBJLAST);
nRet = (asERetCodes) pScriptEngine->RegisterObjectBehaviour("UniType", asBEHAVE_CONSTRUCT,    "void f(const UniType &in )",		asFUNCTION(uniTypeCopyConstructor), asCALL_CDECL_OBJLAST); 
assert( nRet >= 0 );
nRet = (asERetCodes) pScriptEngine->RegisterObjectBehaviour("UniType", asBEHAVE_CONSTRUCT,    "void f(longlong )",		asFUNCTION(uniTypeLongConstructor), asCALL_CDECL_OBJLAST); 
assert( nRet >= 0 );
nRet = (asERetCodes) pScriptEngine->RegisterObjectBehaviour("UniType", asBEHAVE_CONSTRUCT,    "void f(double )",		asFUNCTION(uniTypeDoubleConstructor), asCALL_CDECL_OBJLAST); 
assert( nRet >= 0 );
nRet = (asERetCodes) pScriptEngine->RegisterObjectBehaviour("UniType", asBEHAVE_CONSTRUCT,    "void f(const _String &in )",		asFUNCTION(uniType_StringConstructor), asCALL_CDECL_OBJLAST); 
assert( nRet >= 0 );
nRet = (asERetCodes) pScriptEngine->RegisterObjectBehaviour("UniType", asBEHAVE_CONSTRUCT,    "void f(const DisplayType )",		asFUNCTION(uniType_DisplayTypeConstructor), asCALL_CDECL_OBJLAST); 
assert( nRet >= 0 );

..................................

nRet = (asERetCodes) pScriptEngine->RegisterGlobalBehaviour(asBEHAVE_DIVIDE , "UniType f(const UniType &in, int64)", asFUNCTIONPR( globalOperatorDiv, ( UniType &, long long), UniType), asCALL_CDECL);
assert( nRet >= 0 );

nRet = (asERetCodes) pScriptEngine->RegisterGlobalBehaviour(asBEHAVE_MULTIPLY  , "UniType &f(const UniType &in, int64)", asFUNCTIONPR( globalOperatorMult, ( UniType &, long long), UniType&), asCALL_CDECL);
assert( nRet >= 0 );

nRet = (asERetCodes) pScriptEngine->RegisterGlobalBehaviour(asBEHAVE_SUBTRACT , "UniType f(const UniType &in, int64)", asFUNCTIONPR( globalOperatorMinus, ( UniType &, long long), UniType), asCALL_CDECL);
assert( nRet >= 0 );

nRet = (asERetCodes) pScriptEngine->RegisterGlobalBehaviour(asBEHAVE_ADD , "UniType f(const UniType &in, int64)", asFUNCTIONPR( globalOperatorPlus, ( UniType &, long long), UniType), asCALL_CDECL);
assert( nRet >= 0 );
I think, from my reading, const + &in, will not create a copy of object. Is it? But in my case, it is always created a new object, either by assignment operator or copy constructor. So, when will angel script call copy constructor, when did angel script call = operator? Here is some experiences from coding:
 UniType a(0); UniType b(3); a = a + 1; 
I got an error: ExecuteString (1, 35) : ERR : No conversion from 'UniType&' to match type avaiable.
 UniType a(0); UniType b(3); a = a - 1; 
I will go to operator - first, then I go into the copy constructor. is that means I assign the a(a-1)?
 UniType a(0); UniType b(3); a = a * 1; 
Note: the return of operator * is by reference. I go to operator * firstly, and then go to operator = twice. is that mean: UniType temp = a * 1; a = temp;
 UniType a(0); UniType b(3); a = a/1; 
I got the same result as - operator.
 UniType buffer(DISPLAY_DOUBLE_VARYDECISION_POINT);
bool neg() {
        // Just to get a handle, this can make sure that we have a handle. 
        // The result.Value is not changed.
	_Save@ result = current['RESULT'];
        // result.Value is a attribute of _Save handle.
	buffer = result.Value;
	result.Value = buffer * -1;
	return true;
}
This is the section to be build in the script engine. Then in the program I called:
 neg(); 
It get unpredicatable results. it first coming to operator =, which I believe is this sentence:
 buffer ] result.Value
, then it use a default constructor to create a temp object, and then called operator = to assign value to temp. and then use temp to * -1; Then it did not assign value back to result.Value, since not break on the operator. Another kind of sequence would be: result.Value = buffer; result.Value = result.Value * -1; But the result.Value suppose not changed, so, it should never come to default constructor in the mid edge. Can you make this clear for me plz? By the same. The copy constructor and assignment operator are working differently, I need to know when the script will call which, otherwise, my results are totally wrong.
Advertisement
Whenever AngelScript needs to create a copy of an object it will create a new object with the default constructor then call the assignment behaviour. I haven't had the time to implement use of the copy constructor for this yet.

I'll answer the other questions when I get more time to investigate 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

Quote:Original post by WitchLord
Whenever AngelScript needs to create a copy of an object it will create a new object with the default constructor then call the assignment behaviour. I haven't had the time to implement use of the copy constructor for this yet.

I'll answer the other questions when I get more time to investigate it.


const &in supports to stop the script copying the object. is it?
Or sometimes u must make a copy of object since u need to make a local temp variable for simplying the logic or parsing the code?

I found another interesting issue.
void neg() {        _Save@ result = current['RESULT'];	print(buffer); // Note print operation here.	buffer = result.Value;	print(buffer); // note print operation here.	UniType temp(DISPLAY_DOUBLE_VARYDECISION_POINT);	temp = -1;	result.Value = temp * result.Value;	return true;}


I register the print as passing by const &in. It working all fine when u simply calling the print. But in the code above. It will use operator = to make a copy of object and passing into print function for both print function.
Any comment on this?
AngelScript can only avoid copying the object for const&in when the expression is a local variable (including temporary). Global variables and non-variables (such as class properties and array elements) will always be copied into a temporary variable first, in order to guarantee that the reference passed in to the function remains valid throughout the function call.

Note, for reference types AngelScript shouldn't have to make these copies, as it the reference counter is enough to guarantee that the object isn't destroyed too soon.


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

This topic is closed to new replies.

Advertisement