| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- // Copyright (c) 2008-2023 the Urho3D project
- // License: MIT
- #include <fstream>
- #include <stdio.h>
- #include <string>
- using namespace std;
- extern string _sourceDir;
- namespace ASBindingGenerator
- {
- // Generate templates for up to this number of function parameters
- const int MAX_PARAMS = 8;
- void WriteConstructors(ofstream& ofs)
- {
- ofs <<
- "// Constructors that don't require parameter conversion between C++ and AngelScript\n"
- "\n"
- "#ifdef AS_MAX_PORTABILITY\n"
- "\n";
- for (int numParams = 0; numParams <= MAX_PARAMS; numParams++)
- {
- ofs << "template <class C";
- for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
- ofs << ", typename P" << paramIndex;
- ofs <<
- ">\n"
- "void ASCompatibleConstructor(asIScriptGeneric* gen)\n"
- "{\n"
- " new (gen->GetObject()) C("; // Placement new
- for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
- {
- if (paramIndex != 0)
- ofs << ",\n ";
- ofs << "*reinterpret_cast<P" << paramIndex << "*>(gen->GetAddressOfArg(" << paramIndex << "))";
- }
- ofs <<
- ");\n"
- "}\n"
- "\n";
- }
- ofs <<
- "#else\n"
- "\n";
- for (int numParams = 0; numParams <= MAX_PARAMS; numParams++)
- {
- ofs << "template <class C";
- for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
- ofs << ", typename P" << paramIndex;
- ofs <<
- ">\n"
- "void ASCompatibleConstructor(C* ptr";
- for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
- ofs << ", P" << paramIndex << " p" << paramIndex;
- ofs <<
- ")\n"
- "{\n"
- " new (ptr) C("; // Placement new
- for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
- {
- if (paramIndex != 0)
- ofs << ", ";
- ofs << "p" << paramIndex;
- }
- ofs <<
- ");\n"
- "}\n"
- "\n";
- }
- ofs <<
- "#endif\n"
- "\n";
- }
- void WriteFactories(ofstream& ofs)
- {
- ofs <<
- "// Factories that don't require parameter conversion between C++ and AngelScript\n"
- "\n"
- "#ifdef AS_MAX_PORTABILITY\n"
- "\n";
- for (int numParams = 0; numParams <= MAX_PARAMS; numParams++)
- {
- ofs << "template <class C";
- for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
- ofs << ", typename P" << paramIndex;
- ofs <<
- ">\n"
- "void ASCompatibleFactory(asIScriptGeneric* gen)\n"
- "{\n"
- " gen->SetReturnAddress(new C(";
- for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
- {
- if (paramIndex != 0)
- ofs << ",\n ";
- ofs << "*reinterpret_cast<P" << paramIndex << "*>(gen->GetAddressOfArg(" << paramIndex << "))";
- }
- ofs <<
- "));\n"
- "}\n"
- "\n";
- }
- ofs <<
- "#else\n"
- "\n";
- for (int numParams = 0; numParams <= MAX_PARAMS; numParams++)
- {
- ofs << "template <class C";
- for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
- ofs << ", typename P" << paramIndex;
- ofs <<
- ">\n"
- "C* ASCompatibleFactory(";
- for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
- {
- if (paramIndex != 0)
- ofs << ", ";
- ofs << "P" << paramIndex << " p" << paramIndex;
- }
- ofs <<
- ")\n"
- "{\n"
- " return new C(";
- for (int paramIndex = 0; paramIndex < numParams; paramIndex++)
- {
- if (paramIndex != 0)
- ofs << ", ";
- ofs << "p" << paramIndex;
- }
- ofs <<
- ");\n"
- "}\n"
- "\n";
- }
- ofs <<
- "#endif\n"
- "\n";
- }
- void GenerateTemplates()
- {
- ofstream ofs(_sourceDir + "/Source/Urho3D/AngelScript/Generated_Templates.h");
- ofs <<
- "// DO NOT EDIT. This file is generated\n"
- "\n"
- "#pragma once\n"
- "\n"
- "#include <AngelScript/angelscript.h>\n"
- "\n"
- "#include <new>\n"
- "\n"
- "namespace Urho3D\n"
- "{\n"
- "\n";
-
- WriteConstructors(ofs);
- WriteFactories(ofs);
- ofs << "} // namespace Urho3D\n";
- }
- } // ASBindingGenerator
|