ASResult.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "ASResult.h"
  23. #include "Tuning.h"
  24. #include "Utils.h"
  25. #include <fstream>
  26. namespace ASBindingGenerator
  27. {
  28. void ASGeneratedFile_Base::AddHeader(const string& headerFile)
  29. {
  30. if (!CONTAINS(headers_, headerFile))
  31. headers_.push_back(headerFile);
  32. }
  33. void ASGeneratedFile_Base::AddIgnoredHeader(const string& headerFile)
  34. {
  35. if (!CONTAINS(ignoredHeaders_, headerFile))
  36. ignoredHeaders_.push_back(headerFile);
  37. }
  38. void ASGeneratedFile_Base::WriteHeaders(ofstream& out)
  39. {
  40. sort(ignoredHeaders_.begin(), ignoredHeaders_.end());
  41. sort(headers_.begin(), headers_.end());
  42. if (ignoredHeaders_.size() > 0)
  43. {
  44. out << "// Ignored headers\n";
  45. for (string header : ignoredHeaders_)
  46. {
  47. string insideDefine = InsideDefine(header);
  48. if (!insideDefine.empty())
  49. out << "//#ifdef " << insideDefine << "\n";
  50. out << "//#include \"" << header << "\"\n";
  51. if (!insideDefine.empty())
  52. out << "//#endif\n";
  53. }
  54. out << "\n";
  55. }
  56. for (string header : headers_)
  57. {
  58. string insideDefine = InsideDefine(header);
  59. if (!insideDefine.empty())
  60. out << "#ifdef " << insideDefine << "\n";
  61. out << "#include \"" << header << "\"\n";
  62. if (!insideDefine.empty())
  63. out << "#endif\n";
  64. }
  65. if (headers_.size() > 0)
  66. out << "\n";
  67. }
  68. // ============================================================================
  69. ASGeneratedFile_WithRegistrationFunction::ASGeneratedFile_WithRegistrationFunction(const string& outputFilePath, const string& functionName)
  70. {
  71. outputFilePath_ = outputFilePath;
  72. functionName_ = functionName;
  73. }
  74. // ============================================================================
  75. void ASGeneratedFile_Enums::Save()
  76. {
  77. ofstream out(outputFilePath_);
  78. out <<
  79. "// DO NOT EDIT. This file is generated\n"
  80. "\n"
  81. "// We need register all enums before registration of any members because members can use any enums\n"
  82. "\n"
  83. "#include \"../Precompiled.h\"\n"
  84. "#include \"../AngelScript/APITemplates.h\"\n"
  85. "\n";
  86. WriteHeaders(out);
  87. out <<
  88. "namespace Urho3D\n"
  89. "{\n"
  90. "\n"
  91. << glue_.str() <<
  92. "void " << functionName_ << "(asIScriptEngine* engine)\n"
  93. "{\n"
  94. << reg_.str() <<
  95. "}\n"
  96. "\n"
  97. "}\n";
  98. }
  99. // ============================================================================
  100. void ASGeneratedFile_Classes::Save()
  101. {
  102. ofstream out(outputFilePath_);
  103. out <<
  104. "// DO NOT EDIT. This file is generated\n"
  105. "\n"
  106. "// We need register all types before registration of any members because members can use any types\n"
  107. "\n"
  108. "#include \"../Precompiled.h\"\n"
  109. "#include \"../AngelScript/APITemplates.h\"\n"
  110. "\n";
  111. WriteHeaders(out);
  112. out <<
  113. "namespace Urho3D\n"
  114. "{\n"
  115. "\n"
  116. "void " << functionName_ << "(asIScriptEngine* engine)\n"
  117. "{\n"
  118. << reg_.str() <<
  119. "}\n"
  120. "\n"
  121. "}\n";
  122. }
  123. // ============================================================================
  124. void ASGeneratedFile_Members_HighPriority::Save()
  125. {
  126. ofstream out(outputFilePath_);
  127. out <<
  128. "// DO NOT EDIT. This file is generated\n"
  129. "\n"
  130. "// We need register default constructors before any members to allow using in Array<type>\n"
  131. "\n"
  132. "#include \"../Precompiled.h\"\n"
  133. "#include \"../AngelScript/APITemplates.h\"\n"
  134. "\n";
  135. WriteHeaders(out);
  136. out <<
  137. "namespace Urho3D\n"
  138. "{\n"
  139. "\n"
  140. "void FakeAddRef(void* ptr);\n"
  141. "void FakeReleaseRef(void* ptr);\n"
  142. "\n"
  143. << glue_.str() <<
  144. "void " << functionName_ << "(asIScriptEngine* engine)\n"
  145. "{\n"
  146. << reg_.str() <<
  147. "}\n"
  148. "\n"
  149. "}\n";
  150. }
  151. // ============================================================================
  152. void ASGeneratedFile_Members::Save()
  153. {
  154. ofstream out(outputFilePath_);
  155. out <<
  156. "// DO NOT EDIT. This file is generated\n"
  157. "\n"
  158. "#include \"../Precompiled.h\"\n"
  159. "#include \"../AngelScript/APITemplates.h\"\n"
  160. "\n";
  161. WriteHeaders(out);
  162. out <<
  163. "#include \"../AngelScript/Manual.h\"\n"
  164. "\n"
  165. "namespace Urho3D\n"
  166. "{\n"
  167. "\n"
  168. "void FakeAddRef(void* ptr);\n"
  169. "void FakeReleaseRef(void* ptr);\n"
  170. "\n"
  171. << glue_.str() <<
  172. "void " << functionName_ << "(asIScriptEngine* engine)\n"
  173. "{\n"
  174. << reg_.str() <<
  175. "}\n"
  176. "\n"
  177. "}\n";
  178. }
  179. // ============================================================================
  180. void ASGeneratedFile_GlobalVariables::Save()
  181. {
  182. ofstream out(outputFilePath_);
  183. out <<
  184. "// DO NOT EDIT. This file is generated\n"
  185. "\n"
  186. "#include \"../Precompiled.h\"\n"
  187. "#include \"../AngelScript/APITemplates.h\"\n"
  188. "\n";
  189. WriteHeaders(out);
  190. out <<
  191. "// Some headers could re-define M_PI, ensure that it's undefined\n"
  192. "#undef M_PI\n"
  193. "\n";
  194. out <<
  195. "namespace Urho3D\n"
  196. "{\n"
  197. "\n"
  198. << glue_.str() <<
  199. "void " << functionName_ << "(asIScriptEngine* engine)\n"
  200. "{\n"
  201. << reg_.str() <<
  202. "}\n"
  203. "\n"
  204. "}\n";
  205. }
  206. // ============================================================================
  207. void ASGeneratedFile_GlobalFunctions::Save()
  208. {
  209. ofstream out(outputFilePath_);
  210. out <<
  211. "// DO NOT EDIT. This file is generated\n"
  212. "\n"
  213. "#include \"../Precompiled.h\"\n"
  214. "#include \"../AngelScript/APITemplates.h\"\n"
  215. "\n";
  216. WriteHeaders(out);
  217. out <<
  218. "namespace Urho3D\n"
  219. "{\n"
  220. "\n"
  221. << glue_.str() <<
  222. "void " << functionName_ << "(asIScriptEngine* engine)\n"
  223. "{\n"
  224. << reg_.str() <<
  225. "}\n"
  226. "\n"
  227. "}\n";
  228. }
  229. // ============================================================================
  230. ASGeneratedFile_Templates::ASGeneratedFile_Templates(const string& outputFilePath)
  231. {
  232. outputFilePath_ = outputFilePath;
  233. }
  234. void ASGeneratedFile_Templates::Save()
  235. {
  236. ofstream out(outputFilePath_);
  237. out <<
  238. "// DO NOT EDIT. This file is generated\n"
  239. "\n"
  240. "#pragma once\n"
  241. "\n"
  242. "#include \"../Precompiled.h\"\n"
  243. "#include \"../AngelScript/APITemplates.h\"\n"
  244. "\n";
  245. WriteHeaders(out);
  246. out <<
  247. "#include \"../AngelScript/Manual.h\"\n"
  248. "\n"
  249. "namespace Urho3D\n"
  250. "{\n"
  251. "\n"
  252. "void FakeAddRef(void* ptr);\n"
  253. "void FakeReleaseRef(void* ptr);\n"
  254. "\n"
  255. << glue_.str()
  256. << reg_.str() <<
  257. "}\n";
  258. }
  259. }