ASTemplateGenerator.cpp 4.5 KB

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