Angelscript addon Template Containers  2.31.0
STL container powerr for Angelscript
aatc_funcpointer

aatc_funcpointer is used to pass a function name from script to host. It is a ref object. Right now its only used by vector::sort and list::sort, but you can use it for whatever you like.

aatc_funcpointer was created in ancient times to make sorting by angelscript functions possible. Nowadays angelscript has anonymous functions and everyone likes to use those for sorting instead of global functions. Anonymous function support for sorting is available, but only for tempspec'd containers for now, templated containers would probably require angelscript language support for templated funcdefs.

aatc_funcpointer script interface:

class aatc_funcpointer{
//default constructor
aatc_funcpointer();
bool ready;
bool is_thiscall;
string funcname;
//set to global function
bool Set(string funcname);
//set to method of an object, object must be passed in a handle, not a value
bool Set(string funcname, ?&in any_object);
//used to call the function from script, you cant set any parameters from script side yet.
void Call();
};


Examples

Example of sorting a vector with a global function:

class Material{
string name;
Material(string _name){
name = _name;
}
};
int global_comp(Material &in lhs,Material &in rhs){
if(lhs.name < rhs.name){return -1;}
if(lhs.name > rhs.name){return 1;}
return 0;
}
void main(){
vector<Material> cont;
cont.push_back(Material("stone"));
cont.push_back(Material("wood"));
cont.push_back(Material("aluminium"));
aatc_funcpointer func;
func.Set("global_comp");
cont.sort(func,true);
for(auto it = cont.begin();it++;){
Print("content = "+it.value.name);
}
}

Prints:

content = aluminium
content = stone
content = wood


Example of sorting a list with a method:

class Material{
string name;
Material(string _name){
name = _name;
}
};
class Comparizorr{
int method_comp(Material &in lhs,Material &in rhs){
if(lhs.name < rhs.name){return -1;}
if(lhs.name > rhs.name){return 1;}
return 0;
}
}
void main(){
list<Material> cont;
cont.push_back(Material("hard rock"));
cont.push_back(Material("heavy metal"));
cont.push_back(Material("black metal"));
Comparizorr compa;
aatc_funcpointer func;
func.Set("method_comp",@compa);//remember that @@@
cont.sort(func,true);
for(auto it = cont.begin();it++;){
Print("content = "+it.value.name);
}
}

Prints:

content = black metal
content = hard rock
content = heavy metal