Catos Engine (Source) 0.0.1
Lightweight Game engine
Loading...
Searching...
No Matches
ScriptingEngine.h
Go to the documentation of this file.
1//
2// Created by allos on 8/27/2024.
3//
4#pragma once
5
6#include <stl/string.h>
7#include "stl/vector.h"
8#include "stl/pair.h"
9
10
11#include "stl/hashmap.h"
12#include <pocketpy/pocketpy.h>
13#include <pybind11.h>
14#include <any>
15
16namespace py = pybind11;
17
18
19
20namespace catos {
21
28
29
30
32
33 public:
34
35 friend class Registry;
36
39
42
43 void registerNewScript(const catos::string& pathToPythonFile);
44
45 void startScripts();
46 void updateScripts();
47 void endScripts();
48
49
50 template<class T>
51 void registerClass(const char* name) {
52
53 py::class_<T> pyClass = py::class_<T>(catosPyMod, name).def(py::init<>());
54 auto hash = typeid(T).hash_code();
55
56 _pythonClasses.put(hash, pyClass);
57 };
58
59 template<typename T, typename U>
60 void registerProperty(const char* name, U T::* ptr) {
61 auto hash = typeid(T).hash_code();
62
63 auto obj = _pythonClasses.get(hash);
64
65 auto pyClass = std::any_cast<py::class_<T>>(obj);
66
67
68 pyClass.def_readwrite(name, ptr);
69 }
70
71 template<class T, typename U>
72 void registerMethod(const char* name, U T::* ptr) {
73 auto hash = typeid(T).hash_code();
74
75 auto obj = _pythonClasses.get(hash);
76
77 auto pyClass = std::any_cast<py::class_<T>>(obj);
78
79 pyClass.def(name, ptr);
80 }
81
82 private:
83
84 py::scoped_interpreter* interpreter = nullptr;
85 py::module catosPyMod;
86
89
90 };
91}
92
93
94
95
Definition application.h:13
The registry is the core system and provides reflection to the rest of the engine.
Definition registry.h:351
Object that holds info about a Script.
Definition ScriptingEngine.h:23
catos::string src
Definition ScriptingEngine.h:24
catos::string className
Definition ScriptingEngine.h:25
Definition ScriptingEngine.h:31
void registerMethod(const char *name, U T::*ptr)
Definition ScriptingEngine.h:72
void registerClass(const char *name)
Definition ScriptingEngine.h:51
void endScripts()
Definition ScriptingEngine.cpp:119
void startScripts()
Definition ScriptingEngine.cpp:90
~ScriptingEngine()
Definition ScriptingEngine.cpp:31
void registerNewScript(const catos::string &pathToPythonFile)
Definition ScriptingEngine.cpp:35
ScriptingEngine()
Definition ScriptingEngine.cpp:16
static ScriptingEngine & getInstance()
singleton :D
Definition ScriptingEngine.cpp:11
void updateScripts()
Definition ScriptingEngine.cpp:107
void registerProperty(const char *name, U T::*ptr)
Definition ScriptingEngine.h:60
Definition hashmap.h:75
V get(const K &key)
Gets the value based on the key given.
Definition hashmap.h:89
void put(const K &key, const V &value)
puts an item in the table.
Definition hashmap.h:107
Custom string class that functions as dynamic C_str.
Definition string.h:11
Definition vector.h:22