truelight@9388: /* $Id$ */ truelight@9388: truelight@9388: /** @file squirrel_class.cpp defines templates for converting C++ classes to Squirrel classes */ truelight@9388: truelight@9387: #ifndef SQUIRREL_CLASS_HPP truelight@9387: #define SQUIRREL_CLASS_HPP truelight@9387: truelight@9602: #if (__GNUC__ == 2) truelight@9602: /* GCC 2.95 doesn't like to have SQConvert::DefSQStaticCallback inside a truelight@9602: * template (it gives an internal error 373). Above that, it doesn't listen truelight@9602: * to 'using namespace' inside a function of a template. So for GCC 2.95 we truelight@9602: * do it in the global space to avoid compiler errors. */ truelight@9602: using namespace SQConvert; truelight@9602: #endif /* __GNUC__ == 2 */ truelight@9602: truelight@9387: /** truelight@9387: * The template to define classes in Squirrel. It takes care of the creation truelight@9387: * and calling of such classes, to make the AI Layer cleaner while having a truelight@9387: * powerful script as possible AI language. truelight@9387: */ truelight@9387: template truelight@9387: class DefSQClass { truelight@9387: private: truelight@9387: const char *classname; truelight@9387: truelight@9387: public: truelight@9387: DefSQClass(const char *_classname) : truelight@9387: classname(_classname) truelight@9387: {} truelight@9387: truelight@9387: /** truelight@9387: * This defines a method inside a class for Squirrel. truelight@9387: */ truelight@9387: template truelight@9530: void DefSQMethod(Squirrel *engine, Func function_proc, const char *function_name) truelight@9387: { truelight@9602: using namespace SQConvert; truelight@9602: engine->AddMethod(function_name, DefSQNonStaticCallback, 0, NULL, &function_proc, sizeof(function_proc)); truelight@9539: } truelight@9539: truelight@9539: /** truebrain@9813: * This defines a method inside a class for Squirrel, which has access to the 'engine' (experts only!). truebrain@9813: */ truebrain@9813: template truebrain@9813: void DefSQAdvancedMethod(Squirrel *engine, Func function_proc, const char *function_name) truebrain@9813: { truebrain@9813: using namespace SQConvert; truebrain@9813: engine->AddMethod(function_name, DefSQAdvancedNonStaticCallback, 0, NULL, &function_proc, sizeof(function_proc)); truebrain@9813: } truebrain@9813: truebrain@9813: /** truebrain@10290: * This defines a method inside a class for Squirrel with defined params. truelight@9540: * @note If you define nparam, make sure that he first param is always 'x', truelight@9540: * which is the 'this' inside the function. This is hidden from the rest truelight@9540: * of the code, but without it calling your function will fail! truelight@9539: */ truelight@9539: template truelight@9539: void DefSQMethod(Squirrel *engine, Func function_proc, const char *function_name, int nparam, const char *params) truelight@9539: { truelight@9602: using namespace SQConvert; truelight@9602: engine->AddMethod(function_name, DefSQNonStaticCallback, nparam, params, &function_proc, sizeof(function_proc)); truelight@9530: } truelight@9530: truelight@9530: /** truelight@9530: * This defines a static method inside a class for Squirrel. truelight@9530: */ truelight@9530: template truelight@9530: void DefSQStaticMethod(Squirrel *engine, Func function_proc, const char *function_name) truelight@9530: { truelight@9602: using namespace SQConvert; truelight@9602: engine->AddMethod(function_name, DefSQStaticCallback, 0, NULL, &function_proc, sizeof(function_proc)); truelight@9539: } truelight@9539: truelight@9539: /** truebrain@10290: * This defines a static method inside a class for Squirrel with defined params. truelight@9540: * @note If you define nparam, make sure that he first param is always 'x', truelight@9540: * which is the 'this' inside the function. This is hidden from the rest truelight@9540: * of the code, but without it calling your function will fail! truelight@9539: */ truelight@9539: template truelight@9539: void DefSQStaticMethod(Squirrel *engine, Func function_proc, const char *function_name, int nparam, const char *params) truelight@9539: { truelight@9602: using namespace SQConvert; truelight@9602: engine->AddMethod(function_name, DefSQStaticCallback, nparam, params, &function_proc, sizeof(function_proc)); truelight@9387: } truelight@9387: truelight@9525: template truelight@9525: void DefSQConst(Squirrel *engine, Var value, const char *var_name) truelight@9525: { truelight@9525: engine->AddConst(var_name, value); truelight@9525: } truelight@9525: truelight@9422: void PreRegister(Squirrel *engine) truelight@9387: { truelight@9396: engine->AddClassBegin(this->classname); truelight@9404: } truelight@9404: truelight@9588: void PreRegister(Squirrel *engine, const char *parent_class) truelight@9588: { truelight@9588: engine->AddClassBegin(this->classname, parent_class); truelight@9588: } truelight@9588: truelight@9635: template truelight@9635: void AddConstructor(Squirrel *engine, const char *params) truelight@9404: { truelight@9609: using namespace SQConvert; truelight@9635: engine->AddMethod("constructor", DefSQConstructorCallback, Tnparam, params); truelight@9387: } truelight@9387: truelight@9422: void PostRegister(Squirrel *engine) truelight@9387: { truelight@9396: engine->AddClassEnd(); truelight@9387: } truelight@9387: }; truelight@9387: truelight@9387: #endif /* SQUIRREL_CLASS_HPP */