asVectorHelper.h

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 /*      static void Erase(int i, std::vector<T>* lhs)
00079         {
00080                 lhs->erase(Index(i,lhs));
00081         }
00082 
00083         static void Insert(int i, const T& e, std::vector<T>* lhs)
00084         {
00085                 lhs->insert(Index(i,lhs), e);
00086         }
00087 */
00088 };
00089 
00090 
00091 template <typename T>
00092 bool RegisterVector(const std::string V_AS,  //The typename of the vector inside AS
00093                                 const std::string T_AS,  //Template parameter typename in AS - must already be
00094                                 asIScriptEngine* engine) //registered (or be primitive type)!!
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 /*      error_code = engine->RegisterObjectMethod(V_AS.c_str(),
00191                 "void erase(int)",
00192                 asFUNCTION(vectorRegisterHelper<T>::Erase),
00193                 asCALL_CDECL_OBJLAST);
00194         assert(error_code == 0 && "Failed to register erase");
00195 
00196         error_code = engine->RegisterObjectMethod(V_AS.c_str(),
00197                 (std::string("void insert(int, const ")+T_AS+"&)").c_str(),
00198                 asFUNCTION(vectorRegisterHelper<T>::Insert),
00199                 asCALL_CDECL_OBJLAST);
00200         assert(error_code == 0 && "Failed to register insert");
00201 */
00202 }

Generado el Tue May 29 14:46:14 2007 para Zak Engine v1.1.0 por  doxygen 1.5.1-p1