Added callables

This commit is contained in:
2024-09-11 22:11:29 +03:00
parent c0b2081242
commit c95976016e
3 changed files with 99 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
#include <list>
#include <core/object.h>
#include "callable.h"

View File

@@ -0,0 +1,93 @@
#ifndef CALLABLE_H_
#define CALLABLE_H_
#include <list>
class _CallableBase {};
template <class T, typename Ret, typename... Args>
class _Callable : public _CallableBase {
T *obj;
Ret (T::*method)(Args...);
public:
void operator()(Args... args)
{
(obj->*method)(args...);
}
_Callable(T *obj, Ret (T::*method)(Args...))
: obj(obj)
, method(method)
{
}
};
template <typename Ret, typename... Args> class _Signal {
std::list<_CallableBase *> callables;
public:
class T {};
void connect(_CallableBase *callable)
{
callables.push_front(callable);
}
void disconnect(_CallableBase *callable)
{
callables.remove(callable);
}
void emit(Args... args)
{
auto e = callables.begin();
while (e != callables.end()) {
_Callable<T, Ret, Args...> *foo =
reinterpret_cast<_Callable<T, Ret, Args...> *>(
*e);
(*foo)(args...);
e++;
}
}
};
#ifdef __TEST
#include <cstdio>
class check {
public:
_Signal<void> sig;
public:
check()
{
}
void emit()
{
sig.emit();
}
};
class cfoo {
void handler1()
{
printf("handler1\n");
}
void handler2()
{
printf("handler2\n");
}
public:
cfoo()
{
check b;
_Callable<cfoo, void> c(this, &cfoo::handler1);
_Callable<cfoo, void> d(this, &cfoo::handler2);
b.sig.connect(&c);
b.sig.connect(&d);
b.emit();
b.emit();
b.sig.disconnect(&c);
b.sig.disconnect(&d);
b.emit();
}
};
int main()
{
cfoo c;
return 0;
}
#endif
#endif

View File

@@ -1,5 +1,6 @@
#ifndef ROAD_LINES_DATA_H #ifndef ROAD_LINES_DATA_H
#define ROAD_LINES_DATA_H #define ROAD_LINES_DATA_H
#include "callable.h"
class RoadLinesData { class RoadLinesData {
String road_lines_path; String road_lines_path;
uint32_t road_lines_hash(const Vector3 &v); uint32_t road_lines_hash(const Vector3 &v);
@@ -7,6 +8,7 @@ class RoadLinesData {
protected: protected:
RoadLinesData(); RoadLinesData();
static RoadLinesData *singleton; static RoadLinesData *singleton;
_Signal<void> lines_updated;
public: public:
struct road_line { struct road_line {
@@ -16,6 +18,7 @@ public:
int pattern; int pattern;
int flags; int flags;
Dictionary metadata; Dictionary metadata;
_Signal<void> line_updated;
}; };
HashMap<String, struct road_line> lines; HashMap<String, struct road_line> lines;
static RoadLinesData *get_singleton(); static RoadLinesData *get_singleton();