Angelscript addon Template Containers  1.0
STL container powerr for Angelscript
Registering tempspecs

Before you can register a template specialization for a container, you must first register the content class itself.

You can only register template specializations for these containers:

  • vector
  • list
  • set
  • unordered_set

map and unordered_map are not available because they take 2 template parameters and that would require partial template specialization which angelscript probably doesnt support.


Some of the containers might require your class to have these operators implemented:

  • operator equals is required for
    • vector and list :: count and erase_value
    • the unordered_set container to work
  • operator less is required for
    • vector and list :: sort
    • the set container to work
  • hash is required for
    • the unordered_set container to work

Hash is not an operator, but a functor by name of aatc_functor_hash


The tempspec registration functions work like this:

#include "aatc_vector.hpp"
/*
Syntax:
template parameters
c++ typename
bool does_the_type_have_function_operator_equals
bool does_the_type_have_function_operator_less
bool does_the_type_have_function_hash
function parameters
as engine*
name of class in script
*/
aatc_register_container_tempspec_vector<myVec3, 1, 0, 0>(engine, "myVec3");


You can register a tempspec even if your class doesnt have all the required operators. The container's functions that require the missing operators will simply not be registered to the script and your c++ compiler probably wont cry about it (because the troublesome functions are hidden by template magics). If you try to register a tempspec for a container that totally requires an operator (for example set requires operator less) it probably wont work at all.


Examples

Registering a template specialization of the class "myVec3" for the containers vector and list:

//include the container headers that you will be needing
#include "aatc_vector.hpp"
#include "aatc_list.hpp"
class myVec3{
public:
float x,y,z;
myVec3(float x,float y,float z);
bool operator==(const myVec3& other);//we have an operator equals, but no operator less
};
void your_aatc_tempspec_registration(asIScriptEngine* engine){
aatc_register_container_tempspec_vector<myVec3, 1, 0, 0>(engine, "myVec3");
aatc_register_container_tempspec_list<myVec3, 1, 0, 0>(engine, "myVec3");
}

Because myVec3 does not have an operator<
vector<myVec3>::sort and list<myVec3>::sort will not be available, but you can still register a tempspec of it.


Registering a hash function for the class "myVec3", so that we can use it in unordered_set:

template<> class aatc_functor_hash<myVec3>{
public:
std::size_t operator()(const myVec3& a) const{
//terribad hash function, do not try this at home
return a.x + a.y + a.z;
}
};