CANCapture Scripting
Type behaviours

Math operators

All of these, with exception to asBEHAVE_NEGATE, should be registered with two parameters using asIScriptEngine::RegisterGlobalBehaviour. asBEHAVE_NEGATE should be registered with no parameters using asIScriptEngine::RegisterObjectBehaviour.

// Example ADD behaviour for a value type
value operator+(const value &a, const value &b)
{
// Perform the add operation and return the new value
value result;
result.attr = a.attr + b.attr;
return result;
}
// Example registration of the behaviour
r = engine->RegisterGlobalBehaviour(asBEHAVE_ADD, "value f(const value &in, const value &in)", asFUNCTIONPR(operator+, (const value&,const value&), value), asCALL_CDECL); assert( r >= 0 );

Comparison operators

These should be registered with two parameters using asIScriptEngine::RegisterGlobalBehaviour. All of them should return a bool type.

Bitwise operators

These should be registered with two parameters using asIScriptEngine::RegisterGlobalBehaviour.

Assignment operators

All of these should be registered with one parameter using asIScriptEngine::RegisterObjectBehaviour. Preferably the functions should return a reference to the object itself.

// Example ASSIGNMENT behaviour for a reference type
object &object::operator=(const object &other)
{
// Copy only the buffer, not the reference counter
attr = other.attr;
// Return a reference to this object
return *this;
}
// Example registration of the behaviour
r = engine->RegisterObjectBehaviour("object", asBEHAVE_ASSIGNMENT, "object &f(const object &in)", asMETHODPR(object, operator =, (const object&), object&), asCALL_THISCALL); assert( r >= 0 );

Index operator

This behaviour should be registered with one parameter using asIScriptEngine::RegisterObjectBehaviour.

Cast operators

asBEHAVE_VALUE_CAST and asBEHAVE_IMPLICIT_VALUE_CAST must be registered without parameters using asIScriptEngine::RegisterObjectBehaviour. The return type can be any type, except bool and void. The value cast allows explicit casts through construct calls, whereas the implicit value cast also allow the compiler to implicitly use the behaviour to convert expressions as necessary. The value cast behaviours are meant to create a new value so the function must not return a reference or handle to the original value.

See Also
asBEHAVE_CONSTRUCT and asBEHAVE_FACTORY in Memory management as well for an alternative value cast operator.

asBEHAVE_REF_CAST and asBEHAVE_IMPLICIT_REF_CAST must be registered with one parameter using asIScriptEngine::RegisterGlobalBehaviour. The parameter must be an object handle, as must the return type. The only difference between the two is that the script compiler may use the later for implicit casts, while the former can only be used by explicitly calling the cast operator. This distinction is very useful when registering class hierarchies, where cast to a base class usually is registered with an implicit cast, whereas a cast to a derived class usually is registered with an explicit cast.

See Also
Registering class hierarchies

Memory management

These must be registered using asIScriptEngine::RegisterObjectBehaviour. asBEHAVE_CONSTRUCT and asBEHAVE_FACTORY may take parameters for object initialization, but the others shouldn't use parameters.

See Also
Registering an object type for more information on how to register types.

Garbage collection

These behaviours are exclusive for objects that have been registered with the flag asOBJ_GC. All of them should be registered using asIScriptEngine::RegisterObjectBehaviour.

See Also
Implementing a garbage collected object for more information on using these.