5 #include "create_wrapper.h"
9 WrapperCreator::create_wrapper(Namespace* ns)
14 << " * WARNING: This file is automatically generated from '"
15 << inputfile << "' - do not change\n"
17 << "#ifndef __" << modulename << "_WRAPPER_H__\n"
18 << "#define __" << modulename << "_WRAPPER_H__\n"
20 << "#include \"wrapper_util.h\"\n"
22 << "extern WrappedFunction " << modulename << "_global_functions[];\n"
23 << "extern WrappedClass " << modulename << "_classes[];\n"
30 << " * WARNING: This file is automatically generated from '"
31 << inputfile << "' - do not change\n"
34 << "#include <config.h>\n"
36 << "#include <assert.h>\n"
37 << "#include <string>\n"
38 << "#include <squirrel.h>\n"
39 << "#include \"wrapper_util.h\"\n"
40 << "#include \"wrapper.interface.h\"\n"
42 if(selected_namespace != "") {
43 out << "using namespace " << selected_namespace << ";\n";
47 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
48 i != ns->types.end(); ++i) {
49 AtomicType* type = *i;
50 Class* _class = dynamic_cast<Class*> (type);
52 create_class_wrapper(_class);
54 for(std::vector<Function*>::iterator i = ns->functions.begin();
55 i != ns->functions.end(); ++i) {
56 create_function_wrapper(0, *i);
59 // create function list...
60 out << "WrappedFunction " << modulename << "_global_functions[] = {\n";
61 for(std::vector<Function*>::iterator i = ns->functions.begin();
62 i != ns->functions.end(); ++i) {
63 Function* function = *i;
64 out << ind << "{ \"" << function->name << "\", &"
65 << function->name << "_wrapper },\n";
67 out << ind << "{ 0, 0 }\n"
71 // create class list...
72 std::ostringstream classlist;
73 classlist << "WrappedClass " << modulename << "_classes[] = {\n";
75 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
76 i != ns->types.end(); ++i) {
77 AtomicType* type = *i;
78 Class* _class = dynamic_cast<Class*> (type);
82 classlist << ind << "{ \"" << _class->name << "\", "
83 << modulename << "_" << _class->name
86 out << "static WrappedFunction " << modulename << "_"
87 << _class->name << "_methods[] = {\n";
88 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
89 i != _class->members.end(); ++i) {
90 ClassMember* member = *i;
91 if(member->visibility != ClassMember::PUBLIC)
93 Function* function = dynamic_cast<Function*> (member);
94 if(!function || function->type == Function::DESTRUCTOR)
97 out << ind << "{ \"" << function->name << "\", &"
98 << _class->name << "_" << function->name << "_wrapper },\n";
103 classlist << ind << "{ 0, 0 }\n";
105 out << classlist.str();
110 WrapperCreator::create_function_wrapper(Class* _class, Function* function)
112 if(function->type == Function::DESTRUCTOR)
115 std::string ns_prefix;
116 if(selected_namespace != "")
117 ns_prefix = selected_namespace + "::";
118 if(function->type == Function::CONSTRUCTOR)
119 function->name = "constructor";
121 out << "static int ";
123 out << _class->name << "_";
125 out << function->name << "_wrapper(HSQUIRRELVM v)\n"
128 if(_class == 0 && function->parameters.empty()
129 && function->return_type.is_void()
130 && function->type != Function::CONSTRUCTOR) {
131 out << ind << "(void) v;\n";
134 // eventually retrieve pointer to class instance
135 if(_class != 0 && function->type != Function::CONSTRUCTOR) {
136 out << ind << ns_prefix << _class->name << "* _this;\n";
137 out << ind << "sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);\n";
140 // declare and retrieve arguments
142 for(std::vector<Parameter>::iterator p = function->parameters.begin();
143 p != function->parameters.end(); ++p) {
145 snprintf(argname, sizeof(argname), "arg%u", i);
146 prepare_argument(p->type, i + 2, argname);
154 if(!function->return_type.is_void()) {
155 function->return_type.write_c_type(out);
156 out << " return_value = ";
159 if(function->type == Function::CONSTRUCTOR) {
160 out << ns_prefix << _class->name << "* _this = new " << ns_prefix;
167 if(function->type == Function::CONSTRUCTOR) {
168 out << _class->name << "(";
170 out << function->name << "(";
172 for(size_t i = 0; i < function->parameters.size(); ++i) {
178 if(function->type == Function::CONSTRUCTOR) {
179 out << ind << "sq_setinstanceup(v, 1, _this);\n";
180 out << ind << "sq_setreleasehook(v, 1, "
181 << _class->name << "_release_hook);\n";
184 // push return value back on stack and return
185 if(function->return_type.is_void()) {
186 out << ind << "return 0;\n";
188 push_to_stack(function->return_type, "return_value");
189 out << ind << "return 1;\n";
196 WrapperCreator::prepare_argument(const Type& type, size_t index,
197 const std::string& var)
199 if(type.ref > 0 && type.atomic_type != StringType::instance())
200 throw std::runtime_error("References not handled yet");
202 throw std::runtime_error("Pointers not handled yet");
203 if(type.atomic_type == &BasicType::INT) {
204 out << ind << "int " << var << ";\n";
205 out << ind << "sq_getinteger(v, " << index << ", &" << var << ");\n";
206 } else if(type.atomic_type == &BasicType::FLOAT) {
207 out << ind << "float " << var << ";\n";
208 out << ind << "sq_getfloat(v, " << index << ", &" << var << ");\n";
209 } else if(type.atomic_type == &BasicType::BOOL) {
210 out << ind << "SQBool " << var << ";\n";
211 out << ind << "sq_getbool(v, " << index << ", &" << var << ");\n";
212 } else if(type.atomic_type == StringType::instance()) {
213 out << ind << "const char* " << var << ";\n";
214 out << ind << "sq_getstring(v, " << index << ", &" << var << ");\n";
216 std::ostringstream msg;
217 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
218 throw std::runtime_error(msg.str());
223 WrapperCreator::push_to_stack(const Type& type, const std::string& var)
225 if(type.ref > 0 && type.atomic_type != StringType::instance())
226 throw std::runtime_error("References not handled yet");
228 throw std::runtime_error("Pointers not handled yet");
230 if(type.atomic_type == &BasicType::INT) {
231 out << "sq_pushinteger(v, " << var << ");\n";
232 } else if(type.atomic_type == &BasicType::FLOAT) {
233 out << "sq_pushfloat(v, " << var << ");\n";
234 } else if(type.atomic_type == &BasicType::BOOL) {
235 out << "sq_pushbool(v, " << var << ");\n";
236 } else if(type.atomic_type == StringType::instance()) {
237 out << "sq_pushstring(v, " << var << ".c_str(), "
238 << var << ".size());\n";
240 std::ostringstream msg;
241 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
242 throw std::runtime_error(msg.str());
247 WrapperCreator::create_class_wrapper(Class* _class)
249 bool release_hook_created = false;
250 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
251 i != _class->members.end(); ++i) {
252 ClassMember* member = *i;
253 if(member->visibility != ClassMember::PUBLIC)
255 Function* function = dynamic_cast<Function*> (member);
258 if(function->type == Function::CONSTRUCTOR
259 && !release_hook_created) {
260 create_class_release_hook(_class);
261 release_hook_created = true;
263 // don't wrap destructors
264 if(function->type == Function::DESTRUCTOR)
266 create_function_wrapper(_class, function);
271 WrapperCreator::create_class_release_hook(Class* _class)
273 out << "static int " << _class->name << "_release_hook(SQUserPointer ptr, int )\n"
275 << ind << _class->name
276 << "* _this = reinterpret_cast<" << _class->name << "*> (ptr);\n"
277 << ind << "delete _this;\n"
278 << ind << "return 0;\n"