ASTemplateGenerator.cpp 5.6 KB

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