ASResult.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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. ASGeneratedFile_WithRegistrationFunction::ASGeneratedFile_WithRegistrationFunction(const string& outputFilePath, const string& functionName)
  29. {
  30. outputFilePath_ = outputFilePath;
  31. functionName_ = functionName;
  32. }
  33. // ============================================================================
  34. void ASGeneratedFile_Members::Save()
  35. {
  36. ofstream out(outputFilePath_);
  37. out <<
  38. "// DO NOT EDIT. This file is generated\n"
  39. "\n"
  40. "#include \"../Precompiled.h\"\n"
  41. "#include \"../AngelScript/APITemplates.h\"\n"
  42. "\n"
  43. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  44. "#include \"../AngelScript/Manual.h\"\n"
  45. "\n"
  46. "namespace Urho3D\n"
  47. "{\n"
  48. "\n"
  49. "void FakeAddRef(void* ptr);\n"
  50. "void FakeReleaseRef(void* ptr);\n"
  51. "\n"
  52. << glue_.str() <<
  53. "void " << functionName_ << "(asIScriptEngine* engine)\n"
  54. "{\n"
  55. << reg_.str() <<
  56. "}\n"
  57. "\n"
  58. "}\n";
  59. }
  60. // ============================================================================
  61. ASGeneratedFile_Templates::ASGeneratedFile_Templates(const string& outputFilePath)
  62. {
  63. outputFilePath_ = outputFilePath;
  64. }
  65. void ASGeneratedFile_Templates::Save()
  66. {
  67. ofstream out(outputFilePath_);
  68. out <<
  69. "// DO NOT EDIT. This file is generated\n"
  70. "\n"
  71. "#pragma once\n"
  72. "\n"
  73. "#include \"../Precompiled.h\"\n"
  74. "#include \"../AngelScript/APITemplates.h\"\n"
  75. "\n"
  76. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  77. "\n"
  78. "#include \"../AngelScript/Manual.h\"\n"
  79. "\n"
  80. "namespace Urho3D\n"
  81. "{\n"
  82. "\n"
  83. "void FakeAddRef(void* ptr);\n"
  84. "void FakeReleaseRef(void* ptr);\n"
  85. "\n"
  86. << glue_.str()
  87. << reg_.str() <<
  88. "}\n";
  89. }
  90. bool ProcessedEnum::operator <(const ProcessedEnum& rhs) const
  91. {
  92. if (insideDefine_ != rhs.insideDefine_)
  93. return insideDefine_ < rhs.insideDefine_;
  94. return name_ < rhs.name_;
  95. }
  96. bool ProcessedGlobalFunction::operator <(const ProcessedGlobalFunction& rhs) const
  97. {
  98. if (insideDefine_ != rhs.insideDefine_)
  99. return insideDefine_ < rhs.insideDefine_;
  100. if (name_ != rhs.name_)
  101. return name_ < rhs.name_;
  102. // Overloads with the same name may exist
  103. if (comment_ != rhs.comment_)
  104. return comment_ < rhs.comment_;
  105. // Different specializations of the same template and aliases have the same comment
  106. return registration_ < rhs.registration_;
  107. }
  108. bool ProcessedGlobalVariable::operator <(const ProcessedGlobalVariable& rhs) const
  109. {
  110. if (insideDefine_ != rhs.insideDefine_)
  111. return insideDefine_ < rhs.insideDefine_;
  112. return name_ < rhs.name_;
  113. }
  114. bool ProcessedClass::operator <(const ProcessedClass& rhs) const
  115. {
  116. if (insideDefine_ != rhs.insideDefine_)
  117. return insideDefine_ < rhs.insideDefine_;
  118. return name_ < rhs.name_;
  119. }
  120. namespace Result
  121. {
  122. vector<ProcessedEnum> enums_;
  123. // Write result to GeneratedEnums.cpp
  124. static void SaveEnums(const string& outputBasePath)
  125. {
  126. sort(enums_.begin(), enums_.end());
  127. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedEnums.cpp");
  128. ofs <<
  129. "// DO NOT EDIT. This file is generated\n"
  130. "\n"
  131. "// We need register all enums before registration of any functions because functions can use any enums\n"
  132. "\n"
  133. "#include \"../Precompiled.h\"\n"
  134. "#include \"../AngelScript/APITemplates.h\"\n"
  135. "\n"
  136. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  137. "\n"
  138. "namespace Urho3D\n"
  139. "{\n"
  140. "\n";
  141. for (const ProcessedEnum& processedEnum : enums_)
  142. {
  143. if (!processedEnum.glue_.size())
  144. continue;
  145. if (!processedEnum.insideDefine_.empty())
  146. ofs << "#ifdef " << processedEnum.insideDefine_ << "\n";
  147. ofs << "// " << processedEnum.comment_ << "\n";
  148. for (const string& glue : processedEnum.glue_)
  149. ofs << glue << "\n";
  150. if (!processedEnum.insideDefine_.empty())
  151. ofs << "#endif\n";
  152. ofs << "\n";
  153. }
  154. ofs <<
  155. "void ASRegisterGeneratedEnums(asIScriptEngine* engine)\n"
  156. "{\n";
  157. bool isFirst = true;
  158. string openedDefine;
  159. for (const ProcessedEnum& processedEnum : enums_)
  160. {
  161. if (processedEnum.insideDefine_ != openedDefine && !openedDefine.empty())
  162. {
  163. ofs << "#endif\n";
  164. openedDefine.clear();
  165. }
  166. if (!isFirst)
  167. ofs << "\n";
  168. if (processedEnum.insideDefine_ != openedDefine && !processedEnum.insideDefine_.empty())
  169. {
  170. ofs << "#ifdef " << processedEnum.insideDefine_ << "\n";
  171. openedDefine = processedEnum.insideDefine_;
  172. }
  173. ofs << " // " << processedEnum.comment_ << "\n";
  174. for (const string& registration : processedEnum.registration_)
  175. ofs << " " << registration << "\n";
  176. isFirst = false;
  177. }
  178. if (!openedDefine.empty())
  179. ofs << "#endif\n";
  180. ofs <<
  181. "}\n"
  182. "\n"
  183. "}\n";
  184. }
  185. // ============================================================================
  186. vector<ProcessedGlobalFunction> globalFunctions_;
  187. // Write result to GlobalFunctions.cpp
  188. static void SaveGlobalFunctions(const string& outputBasePath)
  189. {
  190. sort(globalFunctions_.begin(), globalFunctions_.end());
  191. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedGlobalFunctions.cpp");
  192. ofs <<
  193. "// DO NOT EDIT. This file is generated\n"
  194. "\n"
  195. "#include \"../Precompiled.h\"\n"
  196. "#include \"../AngelScript/APITemplates.h\"\n"
  197. "\n"
  198. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  199. "\n"
  200. "namespace Urho3D\n"
  201. "{\n"
  202. "\n";
  203. for (const ProcessedGlobalFunction& globalFunction : globalFunctions_)
  204. {
  205. if (globalFunction.glue_.empty())
  206. continue;
  207. if (!globalFunction.insideDefine_.empty())
  208. ofs << "#ifdef " << globalFunction.insideDefine_ << "\n";
  209. ofs << "// " << globalFunction.comment_ << "\n";
  210. ofs << globalFunction.glue_ << "\n";
  211. if (!globalFunction.insideDefine_.empty())
  212. ofs << "#endif\n";
  213. ofs << "\n";
  214. }
  215. ofs <<
  216. "void ASRegisterGeneratedGlobalFunctions(asIScriptEngine* engine)\n"
  217. "{\n";
  218. bool isFirst = true;
  219. string openedDefine;
  220. string lastComment;
  221. for (const ProcessedGlobalFunction& globalFunction : globalFunctions_)
  222. {
  223. if (globalFunction.insideDefine_ != openedDefine && !openedDefine.empty())
  224. {
  225. ofs << "#endif\n";
  226. openedDefine.clear();
  227. }
  228. if (!isFirst && lastComment != globalFunction.comment_)
  229. ofs << "\n";
  230. if (globalFunction.insideDefine_ != openedDefine && !globalFunction.insideDefine_.empty())
  231. {
  232. ofs << "#ifdef " << globalFunction.insideDefine_ << "\n";
  233. openedDefine = globalFunction.insideDefine_;
  234. }
  235. if (lastComment != globalFunction.comment_)
  236. ofs << " // " << globalFunction.comment_ << "\n";
  237. ofs << " " << globalFunction.registration_ << "\n";
  238. isFirst = false;
  239. lastComment = globalFunction.comment_;
  240. }
  241. if (!openedDefine.empty())
  242. ofs << "#endif\n";
  243. ofs <<
  244. "}\n"
  245. "\n"
  246. "}\n";
  247. }
  248. // ============================================================================
  249. vector<ProcessedGlobalVariable> globalVariables_;
  250. // Write result to GlobalVariables.cpp
  251. static void SaveGlobalVariables(const string& outputBasePath)
  252. {
  253. sort(globalVariables_.begin(), globalVariables_.end());
  254. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedGlobalVariables.cpp");
  255. ofs <<
  256. "// DO NOT EDIT. This file is generated\n"
  257. "\n"
  258. "#include \"../Precompiled.h\"\n"
  259. "#include \"../AngelScript/APITemplates.h\"\n"
  260. "\n"
  261. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  262. "\n"
  263. "// Some headers could re-define M_PI, ensure that it's undefined\n"
  264. "#undef M_PI\n"
  265. "\n"
  266. "namespace Urho3D\n"
  267. "{\n"
  268. "\n"
  269. "void ASRegisterGeneratedGlobalVariables(asIScriptEngine* engine)\n"
  270. "{\n";
  271. bool isFirst = true;
  272. string openedDefine;
  273. string lastComment;
  274. for (const ProcessedGlobalVariable& globalVariable : globalVariables_)
  275. {
  276. if (globalVariable.insideDefine_ != openedDefine && !openedDefine.empty())
  277. {
  278. ofs << "#endif\n";
  279. openedDefine.clear();
  280. }
  281. if (!isFirst && lastComment != globalVariable.comment_)
  282. ofs << "\n";
  283. if (globalVariable.insideDefine_ != openedDefine && !globalVariable.insideDefine_.empty())
  284. {
  285. ofs << "#ifdef " << globalVariable.insideDefine_ << "\n";
  286. openedDefine = globalVariable.insideDefine_;
  287. }
  288. if (lastComment != globalVariable.comment_)
  289. ofs << " // " << globalVariable.comment_ << "\n";
  290. ofs << " " << globalVariable.registration_ << "\n";
  291. isFirst = false;
  292. lastComment = globalVariable.comment_;
  293. }
  294. if (!openedDefine.empty())
  295. ofs << "#endif\n";
  296. ofs <<
  297. "}\n"
  298. "\n"
  299. "}\n";
  300. }
  301. // ============================================================================
  302. vector<ProcessedClass> classes_;
  303. // Write result to GeneratedObjectTypes.cpp
  304. static void SaveObjectTypes(const string& outputBasePath)
  305. {
  306. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedObjectTypes.cpp");
  307. ofs <<
  308. "// DO NOT EDIT. This file is generated\n"
  309. "\n"
  310. "// We need register all types before registration of any functions because functions can use any types\n"
  311. "\n"
  312. "#include \"../Precompiled.h\"\n"
  313. "#include \"../AngelScript/APITemplates.h\"\n"
  314. "\n"
  315. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  316. "\n"
  317. "namespace Urho3D\n"
  318. "{\n"
  319. "\n"
  320. "void ASRegisterGeneratedObjectTypes(asIScriptEngine* engine)\n"
  321. "{\n";
  322. string openedDefine;
  323. bool isFirst = true;
  324. for (const ProcessedClass& processedClass : classes_)
  325. {
  326. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  327. {
  328. ofs << "#endif\n";
  329. openedDefine.clear();
  330. }
  331. if (!isFirst)
  332. ofs << "\n";
  333. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  334. {
  335. ofs << "#ifdef " << processedClass.insideDefine_ << "\n";
  336. openedDefine = processedClass.insideDefine_;
  337. }
  338. ofs
  339. << " // " << processedClass.comment_ << "\n"
  340. << " " << processedClass.objectTypeRegistration_ << "\n";
  341. isFirst = false;
  342. }
  343. if (!openedDefine.empty())
  344. ofs << "#endif\n";
  345. ofs <<
  346. "}\n"
  347. "\n"
  348. "}\n";
  349. }
  350. // Write result to GeneratedDefaultConstructors.cpp
  351. static void SaveDefaultConstructors(const string& outputBasePath)
  352. {
  353. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedDefaultConstructors.cpp");
  354. ofs <<
  355. "// DO NOT EDIT. This file is generated\n"
  356. "\n"
  357. "// We need register default constructors before any members to allow using in Array<type>\n"
  358. "\n"
  359. "#include \"../Precompiled.h\"\n"
  360. "#include \"../AngelScript/APITemplates.h\"\n"
  361. "\n"
  362. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  363. "\n"
  364. "namespace Urho3D\n"
  365. "{\n";
  366. string openedDefine;
  367. for (const ProcessedClass& processedClass : classes_)
  368. {
  369. if (!processedClass.defaultConstructor_)
  370. continue;
  371. if (processedClass.defaultConstructor_->glue_.empty())
  372. continue;
  373. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  374. {
  375. ofs << "\n#endif\n";
  376. openedDefine.clear();
  377. }
  378. ofs << "\n";
  379. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  380. {
  381. ofs << "#ifdef " << processedClass.insideDefine_ << "\n\n";
  382. openedDefine = processedClass.insideDefine_;
  383. }
  384. ofs <<
  385. "// " << processedClass.defaultConstructor_->comment_ << "\n" <<
  386. processedClass.defaultConstructor_->glue_;
  387. }
  388. if (!openedDefine.empty())
  389. {
  390. ofs << "\n#endif\n";
  391. openedDefine.clear();
  392. }
  393. ofs <<
  394. "\n"
  395. "void ASRegisterGeneratedDefaultConstructors(asIScriptEngine* engine)\n"
  396. "{\n";
  397. bool isFirst = true;
  398. for (const ProcessedClass& processedClass : classes_)
  399. {
  400. if (!processedClass.defaultConstructor_)
  401. continue;
  402. if (processedClass.noBind_)
  403. continue;
  404. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  405. {
  406. ofs << "#endif\n";
  407. openedDefine.clear();
  408. }
  409. if (!isFirst)
  410. ofs << "\n";
  411. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  412. {
  413. ofs << "#ifdef " << processedClass.insideDefine_ << "\n";
  414. openedDefine = processedClass.insideDefine_;
  415. }
  416. ofs <<
  417. " // " << processedClass.defaultConstructor_->comment_ << "\n" <<
  418. " " << processedClass.defaultConstructor_->registration_ << "\n";
  419. isFirst = false;
  420. }
  421. if (!openedDefine.empty())
  422. ofs << "#endif\n";
  423. ofs <<
  424. "}\n"
  425. "\n"
  426. "}\n";
  427. }
  428. // Write result to GeneratedClasses.cpp
  429. static void SaveGeneratedClasses(const string& outputBasePath)
  430. {
  431. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedClasses.cpp");
  432. ofs <<
  433. "// DO NOT EDIT. This file is generated\n"
  434. "\n"
  435. "#include \"../Precompiled.h\"\n"
  436. "#include \"../AngelScript/APITemplates.h\"\n"
  437. "\n"
  438. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  439. "#include \"../AngelScript/GeneratedClassMembers.h\"\n"
  440. "#include \"../AngelScript/Manual.h\"\n"
  441. "\n"
  442. "namespace Urho3D\n"
  443. "{\n"
  444. "\n"
  445. "void FakeAddRef(void* ptr);\n"
  446. "void FakeReleaseRef(void* ptr);\n";
  447. string openedDefine;
  448. for (const ProcessedClass& processedClass : classes_)
  449. {
  450. if (processedClass.noBind_)
  451. continue;
  452. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  453. {
  454. ofs << "\n#endif // def " << openedDefine << "\n";
  455. openedDefine.clear();
  456. }
  457. ofs << "\n";
  458. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  459. {
  460. ofs << "#ifdef " << processedClass.insideDefine_ << "\n\n";
  461. openedDefine = processedClass.insideDefine_;
  462. }
  463. if (processedClass.destructor_ && !processedClass.destructor_->glue_.empty())
  464. {
  465. ofs <<
  466. "// " << processedClass.destructor_->comment_ << "\n"
  467. << processedClass.destructor_->glue_ <<
  468. "\n";
  469. }
  470. ofs <<
  471. "// " << processedClass.comment_ << "\n"
  472. "static void Register_" << processedClass.name_ << "(asIScriptEngine* engine)\n"
  473. "{\n";
  474. bool needGap = false;
  475. for (const RegistrationError& regError : processedClass.unregisteredSpecialMethods_)
  476. {
  477. if (needGap)
  478. ofs << '\n';
  479. ofs <<
  480. " // " << regError.comment_ << "\n"
  481. " // " << regError.message_ << "\n";
  482. needGap = true;
  483. }
  484. /*
  485. for (string nonDefaultConstructor : processedClass.nonDefaultConstructors_)
  486. ofs << " // " << nonDefaultConstructor << "\n";
  487. */
  488. if (processedClass.destructor_)
  489. {
  490. if (needGap)
  491. ofs << '\n';
  492. ofs <<
  493. " // " << processedClass.destructor_->comment_ << "\n"
  494. " " << processedClass.destructor_->registration_ << "\n";
  495. needGap = true;
  496. }
  497. if (needGap)
  498. ofs << '\n';
  499. ofs <<
  500. " Vector<RegisterObjectMethodArgs> methods;\n"
  501. " CollectMembers_" << processedClass.name_ << "(methods);\n"
  502. " const char* asClassName = \"" << processedClass.name_ << "\";\n"
  503. " for (const RegisterObjectMethodArgs& method : methods)\n"
  504. " engine->RegisterObjectMethod(asClassName, method.declaration_.CString(), method.funcPointer_, method.callConv_);\n";
  505. ofs << "}\n";
  506. }
  507. if (!openedDefine.empty())
  508. {
  509. ofs << "\n#endif // def " << openedDefine << "\n";
  510. openedDefine.clear();
  511. }
  512. ofs <<
  513. "\n"
  514. "void ASRegisterGeneratedClasses(asIScriptEngine* engine)\n"
  515. "{\n";
  516. bool isFirst = true;
  517. for (const ProcessedClass& processedClass : classes_)
  518. {
  519. if (processedClass.noBind_)
  520. continue;
  521. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  522. {
  523. ofs << "#endif\n";
  524. openedDefine.clear();
  525. }
  526. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  527. {
  528. if (!isFirst)
  529. ofs << "\n";
  530. ofs << "#ifdef " << processedClass.insideDefine_ << "\n";
  531. openedDefine = processedClass.insideDefine_;
  532. }
  533. ofs << " Register_" << processedClass.name_ << "(engine);\n";
  534. isFirst = false;
  535. }
  536. if (!openedDefine.empty())
  537. ofs << "#endif\n";
  538. ofs <<
  539. "}\n"
  540. "\n"
  541. "}\n";
  542. }
  543. // Write result to GeneratedClassMembers.cpp and GeneratedClassMembers.h
  544. static void SaveClassMembers(const string& outputBasePath)
  545. {
  546. ofstream ofsCpp(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedClassMembers.cpp");
  547. ofsCpp <<
  548. "// DO NOT EDIT. This file is generated\n"
  549. "\n"
  550. "#include \"../Precompiled.h\"\n"
  551. "#include \"../AngelScript/APITemplates.h\"\n"
  552. "\n"
  553. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  554. "#include \"../AngelScript/GeneratedClassMembers.h\"\n"
  555. "#include \"../AngelScript/Manual.h\"\n"
  556. "\n"
  557. "namespace Urho3D\n"
  558. "{\n"
  559. "\n"
  560. "void FakeAddRef(void* ptr);\n"
  561. "void FakeReleaseRef(void* ptr);\n";
  562. string openedDefine;
  563. for (const ProcessedClass& processedClass : classes_)
  564. {
  565. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  566. {
  567. ofsCpp <<
  568. "\n"
  569. "#endif // def " << openedDefine << "\n";
  570. openedDefine.clear();
  571. }
  572. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  573. {
  574. ofsCpp <<
  575. "\n"
  576. "#ifdef " << processedClass.insideDefine_ << "\n";
  577. openedDefine = processedClass.insideDefine_;
  578. }
  579. /*for (const ClassMethodRegistration& method : processedClass.methods_)
  580. {
  581. if (!method.glue_.empty())
  582. {
  583. ofsCpp <<
  584. "\n"
  585. "// " << method.cppDeclaration_ << "\n"
  586. << method.glue_;
  587. }
  588. }*/
  589. ofsCpp <<
  590. "\n"
  591. "// " << processedClass.comment_ << "\n"
  592. "void CollectMembers_" << processedClass.name_ << "(Vector<RegisterObjectMethodArgs>& methods)\n"
  593. "{\n";
  594. bool needGap = false;
  595. /*for (const RegistrationError& unregisteredMethod : processedClass.unregisteredMethods_)
  596. {
  597. if (needGap)
  598. ofsCpp << '\n';
  599. ofsCpp <<
  600. " // " << unregisteredMethod.cppDeclaration_ << "\n"
  601. " // " << unregisteredMethod.message_ << "\n";
  602. needGap = true;
  603. }*/
  604. if (processedClass.baseClassNames_.size())
  605. {
  606. if (needGap)
  607. ofsCpp << '\n';
  608. for (const string& baseClassName : processedClass.baseClassNames_)
  609. ofsCpp << " CollectMembers_" << baseClassName << "(methods);\n";
  610. needGap = true;
  611. }
  612. /*for (const ClassMethodRegistration& method : processedClass.methods_)
  613. {
  614. if (needGap)
  615. ofsCpp << '\n';
  616. const RegisterObjectMethodArgs& args = method.registration_;
  617. ofsCpp <<
  618. " // " << method.cppDeclaration_ << "\n"
  619. " methods.Push(RegisterObjectMethodArgs(\"" << args.declaration_ << "\", " << args.funcPointer_ << ", " << args.callConv_ << "));\n";
  620. needGap = true;
  621. }*/
  622. ofsCpp << "}\n";
  623. }
  624. if (!openedDefine.empty())
  625. {
  626. ofsCpp <<
  627. "\n"
  628. "#endif // def " << openedDefine << "\n";
  629. openedDefine.clear();
  630. }
  631. ofsCpp <<
  632. "\n"
  633. "} // namespace Urho3D\n";
  634. ofstream ofsH(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedClassMembers.h");
  635. ofsH <<
  636. "// DO NOT EDIT. This file is generated\n"
  637. "\n"
  638. "#pragma once\n"
  639. "\n"
  640. "#include \"../AngelScript/APITemplates.h\"\n"
  641. "\n"
  642. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  643. "\n"
  644. "namespace Urho3D\n"
  645. "{\n";
  646. for (const ProcessedClass& processedClass : classes_)
  647. {
  648. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  649. {
  650. ofsH <<
  651. "\n"
  652. "#endif // def " << openedDefine << "\n";
  653. openedDefine.clear();
  654. }
  655. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  656. {
  657. ofsH <<
  658. "\n"
  659. "#ifdef " << processedClass.insideDefine_ << "\n";
  660. openedDefine = processedClass.insideDefine_;
  661. }
  662. ofsH <<
  663. "\n"
  664. "// " << processedClass.comment_ << "\n"
  665. "void CollectMembers_" << processedClass.name_ << "(Vector<RegisterObjectMethodArgs>& methods);\n";
  666. }
  667. if (!openedDefine.empty())
  668. {
  669. ofsH <<
  670. "\n"
  671. "#endif // def " << openedDefine << "\n";
  672. openedDefine.clear();
  673. }
  674. ofsH <<
  675. "\n"
  676. "} // namespace Urho3D\n";
  677. }
  678. static void SaveClasses(const string& outputBasePath)
  679. {
  680. sort(classes_.begin(), classes_.end());
  681. SaveObjectTypes(outputBasePath);
  682. SaveDefaultConstructors(outputBasePath);
  683. SaveGeneratedClasses(outputBasePath);
  684. SaveClassMembers(outputBasePath);
  685. }
  686. // ============================================================================
  687. // List of all required header files
  688. static vector<string> headers_;
  689. // Discarded header files for statistic
  690. static vector<string> ignoredHeaders_;
  691. // Add header to lists if not added yet
  692. void AddHeader(const string& headerFile)
  693. {
  694. if (IsIgnoredHeader(headerFile))
  695. {
  696. if (!CONTAINS(ignoredHeaders_, headerFile))
  697. ignoredHeaders_.push_back(headerFile);
  698. }
  699. else
  700. {
  701. if (!CONTAINS(headers_, headerFile))
  702. headers_.push_back(headerFile);
  703. }
  704. }
  705. // Write result to GeneratedIncludes.h
  706. static void SaveIncludes(const string& outputBasePath)
  707. {
  708. sort(headers_.begin(), headers_.end());
  709. sort(ignoredHeaders_.begin(), ignoredHeaders_.end());
  710. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedIncludes.h");
  711. ofs <<
  712. "// DO NOT EDIT. This file is generated\n"
  713. "\n"
  714. "#pragma once\n"
  715. "\n";
  716. string openedDefine;
  717. bool isFirst = true;
  718. for (const string& header : headers_)
  719. {
  720. string insideDefine = InsideDefine(header);
  721. if (insideDefine != openedDefine)
  722. {
  723. if (!openedDefine.empty())
  724. {
  725. ofs << "#endif\n";
  726. openedDefine.clear();
  727. }
  728. if (!isFirst) // First include can be guarded. Avoid print \n before it
  729. ofs << "\n";
  730. if (!insideDefine.empty())
  731. {
  732. ofs << "#ifdef " << insideDefine << "\n";
  733. openedDefine = insideDefine;
  734. }
  735. }
  736. ofs << "#include \"" << header << "\"\n";
  737. isFirst = false;
  738. }
  739. if (!openedDefine.empty())
  740. {
  741. ofs << "#endif\n";
  742. openedDefine.clear();
  743. }
  744. if (headers_.size() > 0)
  745. ofs << "\n";
  746. if (ignoredHeaders_.size() > 0)
  747. ofs << "// Ignored headers\n\n";
  748. isFirst = true;
  749. for (const string& header : ignoredHeaders_)
  750. {
  751. string insideDefine = InsideDefine(header);
  752. if (insideDefine != openedDefine)
  753. {
  754. if (!openedDefine.empty())
  755. {
  756. ofs << "//#endif\n";
  757. openedDefine.clear();
  758. }
  759. if (!isFirst) // First include can be guarded. Avoid print \n before it
  760. ofs << "\n";
  761. if (!insideDefine.empty())
  762. {
  763. ofs << "//#ifdef " << insideDefine << "\n";
  764. openedDefine = insideDefine;
  765. }
  766. }
  767. ofs << "//#include \"" << header << "\"\n";
  768. isFirst = false;
  769. }
  770. if (!openedDefine.empty())
  771. ofs << "//#endif\n";
  772. }
  773. }
  774. void SaveResult(const string& outputBasePath)
  775. {
  776. Result::SaveEnums(outputBasePath);
  777. Result::SaveGlobalFunctions(outputBasePath);
  778. Result::SaveGlobalVariables(outputBasePath);
  779. Result::SaveClasses(outputBasePath);
  780. Result::SaveIncludes(outputBasePath);
  781. }
  782. }