6#include <unordered_map>
49 template<
typename T,
typename U>
78 return const_cast<void*
>(
reinterpret_cast<const void*
>(&(
obj->*
memberPtr)));
113 template<
typename ReturnType,
class ClassType,
typename... Args>
116 ReturnType(ClassType::* _ptr)(Args...);
126 inst.registerMethod<ClassType>(name, _ptr);
131 template<
typename ReturnType,
class ClassType,
typename... Args>
137 ReturnType (ClassType::*
memPtr)(Args...);
142 static ReturnType
callFunction(
void* FuncInst,
void* instance, Args... args) {
143 return (
static_cast<ClassType*
>(instance)->*
static_cast<MethodInvoker<ReturnType, ClassType, Args...
>*>(FuncInst)->
memPtr)(args...);
150 template<
typename... Args>
154 (
str.append(std::string(
typeid(Args).name()) + std::string(
" arg") + std::to_string(argC++) +
","), ...);
159 template<
typename... Args>
161 std::string
str =
",";
164 argC =
sizeof...(Args);
166 for (
int i=0; i < argC; i++) {
167 str +=
"arg" + std::to_string(i);
180 template<
typename ReturnType,
class ClassType,
typename... Args>
181 Method(ReturnType(ClassType::* method)(Args...)) {
182 _mFunc =
MethodInvoker<ReturnType, ClassType, Args...>(method);
186 ptr = &
MethodInvoker<ReturnType, ClassType, Args...>::callFunction;
192 if (getNames<Args...>() !=
"") {
197 std::string search =
"char const * __ptr64";
198 std::string replace =
"string";
202 while((pos =
parameters.find(search, pos)) != std::string::npos) {
203 parameters.replace(pos, search.length(), replace);
204 pos += replace.length();
206 search =
"class catos:: ";
211 while((pos =
parameters.find(search, pos)) != std::string::npos) {
212 parameters.replace(pos, search.length(), replace);
213 pos += replace.length();
228 template<
typename R,
typename... Args>
230 return std::any_cast<R(*)(
void*,
void*, Args...)>(ptr)(&_mFunc, instance, args...);
233 template<
typename... Args>
235 std::any_cast<void(*)(
void*,
void*, Args...)>(ptr)(&_mFunc, instance, args...);
268 template<
typename T,
typename U>
275 properties[property_name]->set_name(property_name);
276 properties[property_name]->desc = description;
285 template<
typename ClassT,
typename ReturnV,
typename... Args>
286 constexpr TypeInfo&
method(
const char* method_name, ReturnV(ClassT::* ptr)(Args...),
cstr description) {
304 std::cerr <<
"Could not find property\n";
311 auto it =
methods.find(method_name);
324 auto structPos =
name.find(
"struct");
326 if (structPos == std::string::npos) {
327 structPos =
name.find(
"class");
330 inst.registerClass<T>(
name.substr(structPos + 7).c_str());
333 property.second->registerToPy();
346 std::unordered_map<std::string , Method* >
methods;
364 if (_register.find(hash) == _register.end()) {
369 return _register[hash];
375 std::ofstream out(
"../../../Resources/Catos/catos.py");
377 out << R
"(""")" << "\n";
379 out <<
"The Python version of The Catos Game engine\n"
380 <<
"The python api is automatically generated by Catos\n"
381 <<
"NOTE: This API is only for scripting. \n";
383 out << R
"(""")" << "\n";
384 for (
auto type : _register) {
386 auto namespacePos = type.second.name.find(
"::");
387 auto structPos = type.second.name.find(
"struct");
391 if (namespacePos != std::string::npos) {
392 out <<
"class " << type.second.name.substr(namespacePos + 2) <<
":\n";
394 out <<
"class " << type.second.name.substr(structPos + 7) <<
":\n";
397 for (
auto prop : type.second.properties) {
398 out << R
"( """ )" << prop.second->desc << R"( """ )" << std::endl;
399 out << " " << prop.first <<
" = None\n";
402 for (
auto meth : type.second.methods) {
406 if (meth.first ==
"init" || meth.first ==
"Init") {
407 out << R
"( """ )" << meth.second->desc << R"( """ )" << std::endl;
408 out << " def __init__(self):\n pass\n";
410 out <<
" def " << meth.first <<
"(self)";
411 if (meth.second->returnName !=
"void") { out <<
" -> " << meth.second->returnName <<
":\n"; }
else { out <<
":\n"; }
412 out << R
"( """ )" << meth.second->desc << R"( """ )" << std::endl;
424 std::ofstream out(
"../../script_test.py");
426 out <<
"///Catos Lib (auto generated)\n";
427 out <<
"namespace catos {\n \n";
428 for (
auto& type : _register) {
430 auto namespacePos = type.second.name.find(
"::");
431 auto structPos = type.second.name.find(
"struct");
435 if (namespacePos != std::string::npos) {
436 out <<
"struct " << type.second.name.substr(namespacePos + 2) <<
" {\n";
438 out <<
"struct " << type.second.name.substr(structPos + 7) <<
" {\n";
441 for (
auto& prop : type.second.properties) {
442 out <<
" /*" << prop.second->desc <<
"*/\n";
443 out <<
" public " << prop.second->get_type_name() <<
" " << prop.second->get_name() <<
";\n" ;
446 for (
auto meth : type.second.methods) {
448 auto nPos = meth.second->returnName.find(
"::");
449 auto sPos = meth.second->returnName.find(
"struct");
451 if (nPos != std::string::npos || sPos != std::string::npos) {
452 out <<
" public void " << meth.first <<
"(" << meth.second->parameters <<
") {\n";
453 out <<
" LibNative." << meth.first <<
"_native(ref this " << meth.second->parameterNames <<
");\n";
456 out <<
" public " << meth.second->returnName <<
" " << meth.first <<
"() {\n";
457 out <<
" LibNative." << meth.first <<
"_native(ref this);\n";
467 out <<
"class LibNative {\n\n";
469 for (
auto& type : _register) {
471 auto namespacePos = type.second.name.find(
"::");
472 auto structPos = type.second.name.find(
"struct");
475 std::string finalName;
477 if (namespacePos != std::string::npos) {
478 finalName = type.second.name.substr(namespacePos + 2);
480 finalName = type.second.name.substr(structPos + 7);
483 out <<
" //BEGIN DEF For " << finalName.c_str() <<
"\n";
485 for (
auto meth : type.second.methods) {
487 auto nPos = meth.second->returnName.find(
"::");
488 auto sPos = meth.second->returnName.find(
"struct");
490 if (nPos != std::string::npos || sPos != std::string::npos) {
491 out <<
" [MethodImplAttribute(MethodImplOptions.InternalCall)]\n";
492 out <<
" public static extern void " << meth.first <<
"_native(ref " << finalName <<
" instance" <<
", " << meth.second->parameters <<
");\n \n";
495 out <<
" [MethodImplAttribute(MethodImplOptions.InternalCall)]\n";
496 out <<
" public static extern " << meth.second->returnName <<
" " << meth.first <<
"_native(ref " << finalName <<
" instance);\n \n";
502 out <<
" //END DEF For " << finalName.c_str() <<
"\n \n \n";
508 out <<
"}///NAMESPACE CATOS \n";
521 for (
auto val : _register) {
523 std::cout << info.
name.c_str() <<
" { \n";
527 std::cout <<
" " << prop.second->get_type_name() <<
" " << prop.second->get_name() <<
";\n";
537 std::unordered_map<size_t, TypeInfo> _register;
538 std::unordered_map<size_t, const void* > _instance_register;
const char * cstr
Definition types.h:13
std::string_view str
Definition types.h:12
Definition application.h:13
std::string getNames()
Definition registry.h:151
std::string getArgNames()
Definition registry.h:160
size_t get_type_hash()
Definition type_utils.h:14
See PropertyImpl for details.
Definition registry.h:28
virtual void set_name(const char *name)=0
virtual size_t & get_type_hash()=0
virtual void * get_value(const void *obj_ptr)=0
virtual void registerToPy()=0
cstr desc
Definition registry.h:45
virtual ~Property()
Definition registry.h:34
virtual const char * get_type_name()=0
virtual const char * get_name()=0
PropertyImpl implements Property and holds an member function pointer. Used to get an value of an fie...
Definition registry.h:50
std::string type_name
Definition registry.h:58
const char * get_name() override
Get the name of the property.
Definition registry.h:83
void set_name(const char *new_name) override
Set the name of the property.
Definition registry.h:68
const char * name
Definition registry.h:55
void registerToPy() override
Definition registry.h:97
void * get_value(const void *objPtr) override
Use Get value (which can be cased to the desired type) to return an value of an instance.
Definition registry.h:76
U T::* memberPtr
Definition registry.h:54
size_t & get_type_hash() override
Returns (a reference of) the type's hash.
Definition registry.h:93
PropertyImpl(U T::*memPtr)
PropertyImpl exist in order to avoid having to deal with templates at the user-side.
Definition registry.h:62
size_t type_hash
Definition registry.h:59
const char * get_type_name() override
Returns the name of the fields type.
Definition registry.h:88
Definition registry.h:107
virtual void registerToPy(const char *name)=0
Definition registry.h:114
void registerToPy(const char *name) override
Definition registry.h:123
MethodHolderImpl(ReturnType(ClassType::*memberPtr)(Args...))
Definition registry.h:119
MethodInvoker invokes the method (with args) given at creation.
Definition registry.h:133
ReturnType(ClassType::* memPtr)(Args...)
Definition registry.h:137
MethodInvoker(ReturnType(ClassType::*ptr)(Args...))
Definition registry.h:138
static ReturnType callFunction(void *FuncInst, void *instance, Args... args)
Runs the function.
Definition registry.h:142
Stores a function pointer for running it.
Definition registry.h:177
std::string returnName
Definition registry.h:244
Method(ReturnType(ClassType::*method)(Args...))
Definition registry.h:181
void registerToPy(const char *name)
Definition registry.h:238
R invoke_function(void *instance, Args... args)
Definition registry.h:229
cstr desc
Definition registry.h:243
void invoke_function(void *instance, Args... args)
Definition registry.h:234
~Method()
Definition registry.h:224
std::string parameterNames
Definition registry.h:247
std::string parameters
Definition registry.h:246
Typeinfo is an object that holds all information about a specific type.
Definition registry.h:260
static bool is_valid(Property *ptr)
Checks wether or not the ptr is a nullptr;.
Definition registry.h:342
Property * get_property(const char *property_name)
Returns a property object based on the name given.
Definition registry.h:295
static bool is_valid(Method *ptr)
Definition registry.h:343
std::unordered_map< std::string, Property * > properties
Definition registry.h:345
size_t type_hash
Definition registry.h:264
std::unordered_map< std::string, Method * > methods
Definition registry.h:346
void registerToPython()
Definition registry.h:321
Method * get_method(const char *method_name)
Returns a method object based on the name given.
Definition registry.h:310
constexpr TypeInfo & method(const char *method_name, ReturnV(ClassT::*ptr)(Args...), cstr description)
Registers a method with a name and a member function pointer (returns the Type object again).
Definition registry.h:286
TypeInfo & property(const char *property_name, U T::*member, cstr description)
Registers a property with a name and a member pointer (Returns itself).
Definition registry.h:270
std::string name
Definition registry.h:265
The registry is the core system and provides reflection to the rest of the engine.
Definition registry.h:351
TypeInfo & get_type()
Returns the registered Type.
Definition registry.h:514
TypeInfo & register_class()
With this function you register a class to the Registry.
Definition registry.h:359
void print_current_register()
Prints out the items in the Registry.
Definition registry.h:520
void gen_python_bindings_file()
Deprecated!!!
Definition registry.h:374
void gen_cs_bindings_file()
TODO this shit is so ugly ;-;.
Definition registry.h:423
static ScriptingEngine & getInstance()
singleton :D
Definition ScriptingEngine.cpp:11