Added callables
This commit is contained in:
3
src/modules/stream/callable.cpp
Normal file
3
src/modules/stream/callable.cpp
Normal file
@@ -0,0 +1,3 @@
|
||||
#include <list>
|
||||
#include <core/object.h>
|
||||
#include "callable.h"
|
||||
93
src/modules/stream/callable.h
Normal file
93
src/modules/stream/callable.h
Normal 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
|
||||
@@ -1,5 +1,6 @@
|
||||
#ifndef ROAD_LINES_DATA_H
|
||||
#define ROAD_LINES_DATA_H
|
||||
#include "callable.h"
|
||||
class RoadLinesData {
|
||||
String road_lines_path;
|
||||
uint32_t road_lines_hash(const Vector3 &v);
|
||||
@@ -7,6 +8,7 @@ class RoadLinesData {
|
||||
protected:
|
||||
RoadLinesData();
|
||||
static RoadLinesData *singleton;
|
||||
_Signal<void> lines_updated;
|
||||
|
||||
public:
|
||||
struct road_line {
|
||||
@@ -16,6 +18,7 @@ public:
|
||||
int pattern;
|
||||
int flags;
|
||||
Dictionary metadata;
|
||||
_Signal<void> line_updated;
|
||||
};
|
||||
HashMap<String, struct road_line> lines;
|
||||
static RoadLinesData *get_singleton();
|
||||
|
||||
Reference in New Issue
Block a user