{
std::string fromfile = original_file != "" ? original_file : inputfile;
+ if(selected_namespace != "") {
+ ns_prefix = selected_namespace;
+ ns_prefix += "::";
+ }
+
// hpp file
hppout
<< "/**\n"
<< "#define __" << modulename << "_WRAPPER_H__\n"
<< "\n"
<< "#include <squirrel.h>\n"
+ << "#include \"wrapper.interface.hpp\"\n"
<< "\n"
- << "void register_" << modulename << "_wrapper(HSQUIRRELVM v);\n"
- << "\n"
- << "#endif\n"
+ << "namespace SquirrelWrapper\n"
+ << "{\n"
<< "\n";
+
+ hppout << "void register_" << modulename << "_wrapper(HSQUIRRELVM v);\n"
+ << "\n";
+
+ for(std::vector<AtomicType*>::iterator i = ns->types.begin();
+ i != ns->types.end(); ++i) {
+ AtomicType* type = *i;
+ Class* _class = dynamic_cast<Class*> (type);
+ if(_class == 0)
+ continue;
+
+ hppout << "void create_squirrel_instance(HSQUIRRELVM v, "
+ << ns_prefix << _class->name
+ << "* object, bool setup_releasehook = false);\n";
+ }
+ hppout <<"\n"
+ << "}\n"
+ << "\n"
+ << "#endif\n"
+ << "\n";
// cpp header
out << "/**\n"
<< "#include <squirrel.h>\n"
<< "#include \"wrapper_util.hpp\"\n"
<< "#include \"wrapper.interface.hpp\"\n"
+ << "\n"
+ << "namespace SquirrelWrapper\n"
+ << "{\n"
<< "\n";
- if(selected_namespace != "") {
- out << "using namespace " << selected_namespace << ";\n";
- out << "\n";
- }
-
+
for(std::vector<AtomicType*>::iterator i = ns->types.begin();
i != ns->types.end(); ++i) {
AtomicType* type = *i;
create_function_wrapper(0, *i);
}
- out << "void register_" << modulename << "_wrapper(HSQUIRRELVM v)\n";
- out << "{\n";
- out << ind << "sq_pushroottable(v);\n";
+ out << "void register_" << modulename << "_wrapper(HSQUIRRELVM v)\n"
+ << "{\n"
+ << ind << "sq_pushroottable(v);\n";
create_register_constants_code(ns);
create_register_functions_code(ns);
create_register_classes_code(ns);
- out << ind << "sq_pop(v, 1);\n";
- out << "}\n";
+ out << ind << "sq_pop(v, 1);\n"
+ << "}\n"
+ << "\n"
+ << "}\n"
+ << "\n";
}
void
if(_class->super_classes.size() > 0) {
if(_class->super_classes.size() > 1) {
- std::stringstream msg;
+ std::ostringstream msg;
msg << "Multiple inheritance not supported (at class '"
<< _class->name << "')";
throw std::runtime_error(msg.str());
out << ind << "if(sq_newclass(v, "
<< (_class->super_classes.size() > 0 ? "SQTrue" : "SQFalse")
<< ") < 0) {\n";
- out << ind << ind << "std::stringstream msg;\n";
+ out << ind << ind << "std::ostringstream msg;\n";
out << ind << ind << "msg << \"Couldn't create new class '"
<< _class->name << "'\";\n";
out << ind << ind << "throw SquirrelError(v, msg.str());\n";
const std::string& name)
{
out << ind << "if(sq_createslot(v, -3) < 0) {\n";
- out << ind << ind << "std::stringstream msg;\n";
+ out << ind << ind << "std::ostringstream msg;\n";
out << ind << ind << "msg << \"Couldn't register " << what << "'"
<< name << "'\";\n";
out << ind << ind << "throw SquirrelError(v, msg.str());\n";
}
out << ind << "\n";
// push return value back on stack and return
- if(function->return_type.is_void()) {
- if(function->docu_comment.find("@SUSPEND@") != std::string::npos) {
- out << ind << "return sq_suspendvm(v);\n";
- } else {
- out << ind << "return 0;\n";
+ if(function->suspend) {
+ if(!function->return_type.is_void()) {
+ std::stringstream msg;
+ msg << "Function '" << function->name << "' declared as suspend"
+ << " but has a return value.";
+ throw std::runtime_error(msg.str());
}
+ out << ind << "return sq_suspendvm(v);\n";
+ } else if(function->return_type.is_void()) {
+ out << ind << "return 0;\n";
} else {
push_to_stack(function->return_type, "return_value");
out << ind << "return 1;\n";
void
WrapperCreator::create_class_wrapper(Class* _class)
{
- bool release_hook_created = false;
+ create_class_release_hook(_class);
+ create_squirrel_instance(_class);
for(std::vector<ClassMember*>::iterator i = _class->members.begin();
i != _class->members.end(); ++i) {
ClassMember* member = *i;
Function* function = dynamic_cast<Function*> (member);
if(!function)
continue;
- if(function->type == Function::CONSTRUCTOR
- && !release_hook_created) {
- create_class_release_hook(_class);
- release_hook_created = true;
- }
// don't wrap destructors
if(function->type == Function::DESTRUCTOR)
continue;
}
void
+WrapperCreator::create_squirrel_instance(Class* _class)
+{
+ out << "void create_squirrel_instance(HSQUIRRELVM v, "
+ << ns_prefix << _class->name
+ << "* object, bool setup_releasehook)\n"
+ << "{\n"
+ << ind << "sq_pushstring(v, \"" << _class->name << "\", -1);\n"
+ << ind << "if(sq_get(v, -2) < 0) {\n"
+ << ind << ind << "std::ostringstream msg;\n"
+ << ind << ind << "msg << \"Couldn't resolved squirrel type '"
+ << _class->name << "'\";\n"
+ << ind << ind << "throw SquirrelError(v, msg.str());\n"
+ << ind << "}\n"
+ << "\n"
+ << ind << "if(sq_createinstance(v, -1) < 0 || "
+ << "sq_setinstanceup(v, -1, object) < 0) {\n"
+ << ind << ind << "std::ostringstream msg;\n"
+ << ind << ind << "msg << \"Couldn't setup squirrel instance for "
+ << "object of type '" << _class->name << "'\";\n"
+ << ind << ind << "throw SquirrelError(v, msg.str());\n"
+ << ind << "}\n"
+ << ind << "sq_remove(v, -2);\n"
+ << "\n"
+ << ind << "if(setup_releasehook) {\n"
+ << ind << ind << "sq_setreleasehook(v, -1, "
+ << _class->name << "_release_hook);\n"
+ << ind << "}\n"
+ << "}\n";
+}
+
+void
WrapperCreator::create_class_release_hook(Class* _class)
{
out << "static int " << _class->name << "_release_hook(SQUserPointer ptr, int )\n"
<< "{\n"
- << ind << _class->name
- << "* _this = reinterpret_cast<" << _class->name << "*> (ptr);\n"
+ << ind << ns_prefix << _class->name
+ << "* _this = reinterpret_cast<" << ns_prefix << _class->name
+ << "*> (ptr);\n"
<< ind << "delete _this;\n"
<< ind << "return 0;\n"
<< "}\n"