ClassBinding.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #include "ClassBinding.h"
  2. #include "Generator.h"
  3. static void generateInstanceGetter(ostream& o, const string& classname, const string& uniquename)
  4. {
  5. o << "static " << classname << "* getInstance(lua_State* state)\n";
  6. o << "{\n";
  7. o << " void* userdata = luaL_checkudata(state, 1, \"" << uniquename << "\");\n";
  8. o << " luaL_argcheck(state, userdata != NULL, 1, \"\'" << uniquename << "\' expected.\");\n";
  9. o << " return (" << classname << "*)((" << LUA_OBJECT << "*)userdata)->instance;\n";
  10. o << "}\n\n";
  11. }
  12. ClassBinding::ClassBinding(string classname, string refId) : classname(classname),
  13. refId(refId), inaccessibleConstructor(false), inaccessibleDestructor(false)
  14. {
  15. // If specified, remove the namespace from the class name.
  16. if (REMOVE_NAMESPACE)
  17. {
  18. this->classname = Generator::getInstance()->getClassNameWithoutNamespace(classname);
  19. }
  20. // Calculate the unique name for the class.
  21. uniquename = Generator::getUniqueName(this->classname);
  22. }
  23. void ClassBinding::write(string dir, const set<string>& includes, string* bindingNS)
  24. {
  25. // Calculate the constructor string.
  26. size_t index = uniquename.rfind(SCOPE_REPLACEMENT);
  27. string constructorString = "";
  28. string* constructorUniqueName = NULL;
  29. if (index != uniquename.npos && index != uniquename.length())
  30. constructorString += uniquename.substr(index + SCOPE_REPLACEMENT_SIZE);
  31. else
  32. constructorString += uniquename;
  33. // Generate a constructor function if there isn't one
  34. // and the class doesn't have an inaccessible constructor.
  35. FunctionBinding b(classname, uniquename);
  36. b.name = constructorString;
  37. b.returnParam = FunctionBinding::Param(FunctionBinding::Param::TYPE_CONSTRUCTOR, FunctionBinding::Param::KIND_POINTER, refId);
  38. b.type = FunctionBinding::MEMBER_FUNCTION;
  39. b.own = true;
  40. map<string, vector<FunctionBinding> >::iterator iter = bindings.find(b.getFunctionName());
  41. if (iter == bindings.end())
  42. {
  43. if (!inaccessibleConstructor)
  44. {
  45. bindings[b.getFunctionName()].push_back(b);
  46. constructorUniqueName = new string(b.getFunctionName());
  47. }
  48. }
  49. else
  50. {
  51. constructorUniqueName = new string(iter->second[0].getFunctionName());
  52. }
  53. // Calculate the destructor string.
  54. index = uniquename.rfind(SCOPE_REPLACEMENT);
  55. string destructorString = "~";
  56. string* destructorUniqueName = NULL;
  57. if (index != uniquename.npos && index != uniquename.length())
  58. destructorString += uniquename.substr(index + SCOPE_REPLACEMENT_SIZE);
  59. else
  60. destructorString += uniquename;
  61. // Generate a destructor function if there isn't one
  62. // and the class doesn't have an inaccessible destructor
  63. // or the class is derived from Ref.
  64. b = FunctionBinding(classname, uniquename);
  65. b.name = destructorString;
  66. b.returnParam = FunctionBinding::Param(FunctionBinding::Param::TYPE_DESTRUCTOR);
  67. b.type = FunctionBinding::MEMBER_FUNCTION;
  68. iter = bindings.find(b.getFunctionName());
  69. if (iter == bindings.end())
  70. {
  71. if (!inaccessibleDestructor || Generator::getInstance()->isRef(classname))
  72. {
  73. bindings[b.getFunctionName()].push_back(b);
  74. destructorUniqueName = new string(b.getFunctionName());
  75. }
  76. }
  77. else
  78. {
  79. destructorUniqueName = new string(iter->second[0].getFunctionName());
  80. }
  81. // Write out the header.
  82. {
  83. string path = dir + string("lua_") + uniquename + string(".h");
  84. ofstream o(path.c_str());
  85. if (!o)
  86. {
  87. GP_ERROR("Failed to open file '%s' for generating Lua bindings.", path.c_str());
  88. return;
  89. }
  90. string includeGuard = string("lua_") + uniquename + string("_H_");
  91. transform(includeGuard.begin(), includeGuard.end(), includeGuard.begin(), ::toupper);
  92. o << "#ifndef " << includeGuard << "\n";
  93. o << "#define " << includeGuard << "\n\n";
  94. if (bindingNS)
  95. {
  96. o << "namespace " << *bindingNS << "\n";
  97. o << "{\n\n";
  98. }
  99. o << "// Lua bindings for " << classname << ".\n";
  100. // Write out the binding functions declarations.
  101. iter = bindings.begin();
  102. for (; iter != bindings.end(); iter++)
  103. {
  104. o << "int " << iter->second[0].getFunctionName() << "(lua_State* state);\n";
  105. }
  106. o << "\n";
  107. // Write out the signature of the function used to register the class with Lua.
  108. o << "void luaRegister_" << uniquename << "();\n\n";
  109. if (bindingNS)
  110. o << "}\n\n";
  111. o << "#endif\n";
  112. o.close();
  113. }
  114. // Write out the implementation.
  115. {
  116. string path = dir + string("lua_") + uniquename + string(".cpp");
  117. ofstream o(path.c_str());
  118. if (!o)
  119. {
  120. GP_ERROR("Failed to open file '%s' for generating Lua bindings.", path.c_str());
  121. return;
  122. }
  123. o << "#include \"Base.h\"\n";
  124. o << "#include \"ScriptController.h\"\n";
  125. o << "#include \"lua_" << uniquename << ".h\"\n";
  126. // Ensure we include the original class header, even
  127. // if the list of includes doesn't have it.
  128. if (includes.find(include) == includes.end())
  129. o << "#include \"" << include << "\"\n";
  130. for (set<string>::iterator includeIter = includes.begin(); includeIter != includes.end(); includeIter++)
  131. {
  132. o << "#include \"" << *includeIter << "\"\n";
  133. }
  134. o << "\n";
  135. // If the original class is part of a namespace and we aren't generating into that namespace,
  136. // include its member with a 'using' statement.
  137. if (ns.length() > 0 && (!bindingNS || (*bindingNS != ns)))
  138. {
  139. o << "using " << ns << ";\n\n";
  140. }
  141. if (bindingNS)
  142. {
  143. o << "namespace " << *bindingNS << "\n";
  144. o << "{\n\n";
  145. }
  146. // Write out the function used to register the class with Lua.
  147. o << "void luaRegister_" << uniquename << "()\n";
  148. o << "{\n";
  149. // Get the member functions ready to bind.
  150. iter = bindings.begin();
  151. bool hasMembers = false;
  152. for (; iter != bindings.end(); iter++)
  153. {
  154. for (unsigned int i = 0, count = iter->second.size(); i < count; i++)
  155. {
  156. if (iter->second[i].type == FunctionBinding::MEMBER_FUNCTION ||
  157. iter->second[i].type == FunctionBinding::MEMBER_CONSTANT ||
  158. iter->second[i].type == FunctionBinding::MEMBER_VARIABLE)
  159. {
  160. hasMembers = true;
  161. break;
  162. }
  163. }
  164. if (hasMembers)
  165. break;
  166. }
  167. if (hasMembers)
  168. {
  169. o << " const luaL_Reg lua_members[] = \n";
  170. o << " {\n";
  171. iter = bindings.begin();
  172. for (; iter != bindings.end(); iter++)
  173. {
  174. for (unsigned int i = 0, count = iter->second.size(); i < count; i++)
  175. {
  176. if ((iter->second[i].type == FunctionBinding::MEMBER_FUNCTION ||
  177. iter->second[i].type == FunctionBinding::MEMBER_CONSTANT ||
  178. iter->second[i].type == FunctionBinding::MEMBER_VARIABLE) &&
  179. iter->second[i].returnParam.type != FunctionBinding::Param::TYPE_CONSTRUCTOR &&
  180. iter->second[i].returnParam.type != FunctionBinding::Param::TYPE_DESTRUCTOR)
  181. {
  182. o << " {\"" << iter->second[i].name << "\", " << iter->second[i].getFunctionName() << "},\n";
  183. break;
  184. }
  185. }
  186. }
  187. o << " {NULL, NULL}\n";
  188. o << " };\n";
  189. }
  190. else
  191. {
  192. o << " const luaL_Reg* lua_members = NULL;\n";
  193. }
  194. // Get the static functions ready to bind.
  195. iter = bindings.begin();
  196. bool hasStatics = false;
  197. for (; iter != bindings.end(); iter++)
  198. {
  199. for (unsigned int i = 0, count = iter->second.size(); i < count; i++)
  200. {
  201. if (iter->second[i].type == FunctionBinding::STATIC_FUNCTION ||
  202. iter->second[i].type == FunctionBinding::STATIC_CONSTANT ||
  203. iter->second[i].type == FunctionBinding::STATIC_VARIABLE)
  204. {
  205. hasStatics = true;
  206. break;
  207. }
  208. }
  209. if (hasStatics)
  210. break;
  211. }
  212. if (hasStatics)
  213. {
  214. o << " const luaL_Reg lua_statics[] = \n";
  215. o << " {\n";
  216. iter = bindings.begin();
  217. for (; iter != bindings.end(); iter++)
  218. {
  219. for (unsigned int i = 0, count = iter->second.size(); i < count; i++)
  220. {
  221. if (iter->second[i].type == FunctionBinding::STATIC_FUNCTION ||
  222. iter->second[i].type == FunctionBinding::STATIC_CONSTANT ||
  223. iter->second[i].type == FunctionBinding::STATIC_VARIABLE)
  224. {
  225. o << " {\"" << iter->second[i].name << "\", " << iter->second[i].getFunctionName() << "},\n";
  226. break;
  227. }
  228. }
  229. }
  230. o << " {NULL, NULL}\n";
  231. o << " };\n";
  232. }
  233. else
  234. {
  235. o << " const luaL_Reg* lua_statics = NULL;\n";
  236. }
  237. // Output the scope path for the class (used for inner classes).
  238. vector<string> scopePath = Generator::getScopePath(classname, ns);
  239. o << " std::vector<std::string> scopePath;\n";
  240. for (vector<string>::iterator scopeIter = scopePath.begin(); scopeIter != scopePath.end(); scopeIter++)
  241. {
  242. o << " scopePath.push_back(\"" << *scopeIter << "\");\n";
  243. }
  244. // Register the class (its member and static functions and constructor and destructor).
  245. o << "\n ScriptUtil::registerClass(\"" << uniquename << "\", lua_members, ";
  246. o << ((constructorUniqueName) ? *constructorUniqueName : "NULL") << ", ";
  247. o << ((destructorUniqueName) ? *destructorUniqueName : "NULL") << ", ";
  248. o << "lua_statics, scopePath);\n";
  249. o << "}\n\n";
  250. // Write out the function used to get the instance for
  251. // calling member functions and variables.
  252. generateInstanceGetter(o, classname, uniquename);
  253. // Write out the binding functions.
  254. iter = bindings.begin();
  255. for (; iter != bindings.end(); iter++)
  256. {
  257. FunctionBinding::write(o, iter->second);
  258. }
  259. if (bindingNS)
  260. o << "}\n";
  261. o.close();
  262. }
  263. SAFE_DELETE(constructorUniqueName);
  264. SAFE_DELETE(destructorUniqueName);
  265. }