00001 #pragma once
00002
00003 #include <assert.h>
00004 #include <vector>
00005 #include "angelscript.h"
00006
00007 template <typename T>
00008 class vectorRegisterHelper
00009 {
00010 public:
00011 static void Construct(std::vector<T>* in)
00012 {
00013 new (in) std::vector<T>();
00014 }
00015
00016 static void Destruct(std::vector<T>* in)
00017 {
00018 using namespace std;
00019 in->~vector<T>();
00020 }
00021
00022 static void CopyConstruct(const std::vector<T>& rhs, std::vector<T>* in)
00023 {
00024 new (in) std::vector<T>(rhs);
00025 }
00026
00027 static void NumConstruct(int size, std::vector<T>* in)
00028 {
00029 new (in) std::vector<T>(size);
00030 }
00031
00032 static std::vector<T>& Assign(const std::vector<T>& rhs, std::vector<T>* lhs)
00033 {
00034 *lhs = rhs;
00035 return *lhs;
00036 }
00037
00038 static T* Index(int i, std::vector<T>* lhs)
00039 {
00040
00041 #ifdef AS_VECTOR_ASSERTBOUNDS
00042 assert(i >= 0 && i < lhs->size() && "Array index out of bounds.");
00043 #endif
00044
00045 #ifdef AS_VECTOR_CHECKBOUNDS
00046 if (i < 0 || i >= (signed)lhs->size())
00047 {
00048 asIScriptContext* context = asGetActiveContext();
00049 if( context )
00050 context->SetException("Array Index Out of Bounds.");
00051 return 0;
00052 }
00053 #endif
00054
00055 return &(*lhs)[i];
00056 }
00057
00058 static int Size(std::vector<T>* lhs)
00059 {
00060 return (int)lhs->size();
00061 }
00062
00063 static void Resize(int size, std::vector<T>* lhs)
00064 {
00065 lhs->resize(size);
00066 }
00067
00068 static void PushBack(const T& in, std::vector<T> *lhs)
00069 {
00070 lhs->push_back(in);
00071 }
00072
00073 static void PopBack(std::vector<T>* lhs)
00074 {
00075 lhs->pop_back();
00076 }
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 };
00089
00090
00091 template <typename T>
00092 bool RegisterVector(const std::string V_AS,
00093 const std::string T_AS,
00094 asIScriptEngine* engine)
00095 {
00096 assert(engine && "Passed NULL engine pointer to registerVector");
00097
00098 int error_code = 0;
00099 error_code = engine->RegisterObjectType(V_AS.c_str(), sizeof(std::vector<T>), asOBJ_CLASS_CDA);
00100
00101 if (error_code < 0)
00102 return false;
00103
00104
00105 error_code = engine->RegisterObjectBehaviour(V_AS.c_str(),
00106 asBEHAVE_CONSTRUCT,
00107 "void f()",
00108 asFUNCTION(vectorRegisterHelper<T>::Construct),
00109 asCALL_CDECL_OBJLAST);
00110 if (error_code < 0)
00111 return false;
00112
00113 error_code = engine->RegisterObjectBehaviour(V_AS.c_str(),
00114 asBEHAVE_DESTRUCT,
00115 "void f()",
00116 asFUNCTION(vectorRegisterHelper<T>::Destruct),
00117 asCALL_CDECL_OBJLAST);
00118 if (error_code < 0)
00119 return false;
00120
00121 error_code = engine->RegisterObjectBehaviour(V_AS.c_str(),
00122 asBEHAVE_CONSTRUCT,
00123 (std::string("void f(")+V_AS+"&in)").c_str(),
00124 asFUNCTION(vectorRegisterHelper<T>::CopyConstruct),
00125 asCALL_CDECL_OBJLAST);
00126 if (error_code < 0)
00127 return false;
00128
00129 error_code = engine->RegisterObjectBehaviour(V_AS.c_str(),
00130 asBEHAVE_CONSTRUCT,
00131 "void f(int)",
00132 asFUNCTION(vectorRegisterHelper<T>::NumConstruct),
00133 asCALL_CDECL_OBJLAST);
00134 if (error_code < 0)
00135 return false;
00136
00137 error_code = engine->RegisterObjectBehaviour(V_AS.c_str(),
00138 asBEHAVE_INDEX,
00139 (T_AS+"& f(int)").c_str(),
00140 asFUNCTION(vectorRegisterHelper<T>::Index),
00141 asCALL_CDECL_OBJLAST);
00142 if (error_code < 0)
00143 return false;
00144
00145 error_code = engine->RegisterObjectBehaviour(V_AS.c_str(),
00146 asBEHAVE_INDEX,
00147 ("const "+T_AS+"& f(int) const").c_str(),
00148 asFUNCTION(vectorRegisterHelper<T>::Index),
00149 asCALL_CDECL_OBJLAST);
00150 if (error_code < 0)
00151 return false;
00152
00153 error_code = engine->RegisterObjectBehaviour(V_AS.c_str(),
00154 asBEHAVE_ASSIGNMENT,
00155 (V_AS+"& f(const "+V_AS+"&in)").c_str(),
00156 asFUNCTION(vectorRegisterHelper<T>::Assign),
00157 asCALL_CDECL_OBJLAST);
00158 if (error_code < 0)
00159 return false;
00160
00161 error_code = engine->RegisterObjectMethod(V_AS.c_str(),
00162 "int size() const",
00163 asFUNCTION(vectorRegisterHelper<T>::Size),
00164 asCALL_CDECL_OBJLAST);
00165 if (error_code < 0)
00166 return false;
00167
00168 error_code = engine->RegisterObjectMethod(V_AS.c_str(),
00169 "void resize(int)",
00170 asFUNCTION(vectorRegisterHelper<T>::Resize),
00171 asCALL_CDECL_OBJLAST);
00172 if (error_code < 0)
00173 return false;
00174
00175 error_code = engine->RegisterObjectMethod(V_AS.c_str(),
00176 (std::string("void push_back(")+T_AS+"&in)").c_str(),
00177 asFUNCTION(vectorRegisterHelper<T>::PushBack),
00178 asCALL_CDECL_OBJLAST);
00179 if (error_code < 0)
00180 return false;
00181
00182 error_code = engine->RegisterObjectMethod(V_AS.c_str(),
00183 "void pop_back()",
00184 asFUNCTION(vectorRegisterHelper<T>::PopBack),
00185 asCALL_CDECL_OBJLAST);
00186 if (error_code < 0)
00187 return false;
00188
00189 return true;
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 }