ASTemplateGenerator.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 <fstream>
  23. #include <stdio.h>
  24. #include <string>
  25. using namespace std;
  26. extern string _sourceDir;
  27. namespace ASBindingGenerator
  28. {
  29. // Generate templates for up to this number of function parameters
  30. const int MAX_PARAMS = 8;
  31. void WriteConstructors(ofstream& ofs)
  32. {
  33. ofs <<
  34. "// Constructors that don't require parameter conversion between C++ and AngelScript\n"
  35. "\n"
  36. "#ifdef AS_MAX_PORTABILITY\n"
  37. "\n";
  38. for (int numParams = 0; numParams <= MAX_PARAMS; numParams++)
  39. {
  40. ofs << "template <class C";
  41. for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
  42. ofs << ", typename P" << paramIndex;
  43. ofs <<
  44. ">\n"
  45. "void ASCompatibleConstructor(asIScriptGeneric* gen)\n"
  46. "{\n"
  47. " new (gen->GetObject()) C("; // Placement new
  48. for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
  49. {
  50. if (paramIndex != 0)
  51. ofs << ",\n ";
  52. ofs << "*reinterpret_cast<P" << paramIndex << "*>(gen->GetAddressOfArg(" << paramIndex << "))";
  53. }
  54. ofs <<
  55. ");\n"
  56. "}\n"
  57. "\n";
  58. }
  59. ofs <<
  60. "#else\n"
  61. "\n";
  62. for (int numParams = 0; numParams <= MAX_PARAMS; numParams++)
  63. {
  64. ofs << "template <class C";
  65. for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
  66. ofs << ", typename P" << paramIndex;
  67. ofs <<
  68. ">\n"
  69. "void ASCompatibleConstructor(C* ptr";
  70. for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
  71. ofs << ", P" << paramIndex << " p" << paramIndex;
  72. ofs <<
  73. ")\n"
  74. "{\n"
  75. " new (ptr) C("; // Placement new
  76. for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
  77. {
  78. if (paramIndex != 0)
  79. ofs << ", ";
  80. ofs << "p" << paramIndex;
  81. }
  82. ofs <<
  83. ");\n"
  84. "}\n"
  85. "\n";
  86. }
  87. ofs <<
  88. "#endif\n"
  89. "\n";
  90. }
  91. void WriteFactories(ofstream& ofs)
  92. {
  93. ofs <<
  94. "// Factories that don't require parameter conversion between C++ and AngelScript\n"
  95. "\n"
  96. "#ifdef AS_MAX_PORTABILITY\n"
  97. "\n";
  98. for (int numParams = 0; numParams <= MAX_PARAMS; numParams++)
  99. {
  100. ofs << "template <class C";
  101. for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
  102. ofs << ", typename P" << paramIndex;
  103. ofs <<
  104. ">\n"
  105. "void ASCompatibleFactory(asIScriptGeneric* gen)\n"
  106. "{\n"
  107. " gen->SetReturnAddress(new C(";
  108. for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
  109. {
  110. if (paramIndex != 0)
  111. ofs << ",\n ";
  112. ofs << "*reinterpret_cast<P" << paramIndex << "*>(gen->GetAddressOfArg(" << paramIndex << "))";
  113. }
  114. ofs <<
  115. "));\n"
  116. "}\n"
  117. "\n";
  118. }
  119. ofs <<
  120. "#else\n"
  121. "\n";
  122. for (int numParams = 0; numParams <= MAX_PARAMS; numParams++)
  123. {
  124. ofs << "template <class C";
  125. for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
  126. ofs << ", typename P" << paramIndex;
  127. ofs <<
  128. ">\n"
  129. "C* ASCompatibleFactory(";
  130. for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
  131. {
  132. if (paramIndex != 0)
  133. ofs << ", ";
  134. ofs << "P" << paramIndex << " p" << paramIndex;
  135. }
  136. ofs <<
  137. ")\n"
  138. "{\n"
  139. " return new C(";
  140. for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
  141. {
  142. if (paramIndex != 0)
  143. ofs << ", ";
  144. ofs << "p" << paramIndex;
  145. }
  146. ofs <<
  147. ");\n"
  148. "}\n"
  149. "\n";
  150. }
  151. ofs <<
  152. "#endif\n"
  153. "\n";
  154. }
  155. void GenerateTemplates()
  156. {
  157. ofstream ofs(_sourceDir + "/Source/Urho3D/AngelScript/Generated_Templates_New.h");
  158. ofs <<
  159. "// DO NOT EDIT. This file is generated\n"
  160. "\n"
  161. "#pragma once\n"
  162. "\n"
  163. "#include <AngelScript/angelscript.h>\n"
  164. "\n"
  165. "#include <new>\n"
  166. "\n"
  167. "namespace Urho3D\n"
  168. "{\n"
  169. "\n";
  170. WriteConstructors(ofs);
  171. WriteFactories(ofs);
  172. ofs << "} // namespace Urho3D\n";
  173. }
  174. } // ASBindingGenerator