forked from OctaForge/libostd
signal const correctness
parent
b22baaf5fd
commit
8327022c67
|
@ -887,7 +887,7 @@ namespace detail {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (spec.arg_width()) {
|
if (spec.arg_width()) {
|
||||||
if (argpos <= (argprec + 1)) {
|
if (argpos <= (Size(argprec) + 1)) {
|
||||||
assert(false && "argument width not given");
|
assert(false && "argument width not given");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,11 +11,13 @@
|
||||||
|
|
||||||
namespace ostd {
|
namespace ostd {
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
template<typename C, typename ...A>
|
template<typename C, typename ...A>
|
||||||
struct Event {
|
struct EventBase {
|
||||||
Event(C *cl = nullptr): p_class(cl), p_funcs(nullptr), p_nfuncs(0) {}
|
EventBase(C *cl): p_class(cl), p_funcs(nullptr), p_nfuncs(0) {}
|
||||||
|
|
||||||
Event(const Event &ev): p_class(ev.p_class), p_nfuncs(ev.p_nfuncs) {
|
EventBase(const EventBase &ev): p_class(ev.p_class),
|
||||||
|
p_nfuncs(ev.p_nfuncs) {
|
||||||
using Func = Function<void(C &, A...)>;
|
using Func = Function<void(C &, A...)>;
|
||||||
Func *nbuf = (Func *)new byte[sizeof(Func) * p_nfuncs];
|
Func *nbuf = (Func *)new byte[sizeof(Func) * p_nfuncs];
|
||||||
for (Size i = 0; i < p_nfuncs; ++i)
|
for (Size i = 0; i < p_nfuncs; ++i)
|
||||||
|
@ -23,17 +25,13 @@ struct Event {
|
||||||
p_funcs = nbuf;
|
p_funcs = nbuf;
|
||||||
}
|
}
|
||||||
|
|
||||||
Event(Event &&ev): p_class(ev.p_class), p_funcs(ev.p_funcs),
|
EventBase(EventBase &&ev): p_class(ev.p_class), p_funcs(ev.p_funcs),
|
||||||
p_nfuncs(ev.p_nfuncs) {
|
p_nfuncs(ev.p_nfuncs) {
|
||||||
ev.p_class = nullptr;
|
ev.p_class = nullptr;
|
||||||
ev.p_funcs = nullptr;
|
ev.p_funcs = nullptr;
|
||||||
ev.p_nfuncs = 0;
|
ev.p_nfuncs = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
~Event() {
|
|
||||||
clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
for (Size i = 0; i < p_nfuncs; ++i)
|
for (Size i = 0; i < p_nfuncs; ++i)
|
||||||
p_funcs[i].~Function<void(C &, A...)>();
|
p_funcs[i].~Function<void(C &, A...)>();
|
||||||
|
@ -43,14 +41,14 @@ struct Event {
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename F>
|
template<typename F>
|
||||||
Size connect(F func) {
|
Size connect(F &&func) {
|
||||||
using Func = Function<void(C &, A...)>;
|
using Func = Function<void(C &, A...)>;
|
||||||
Func *nbuf = (Func *)new byte[sizeof(Func) * (p_nfuncs + 1)];
|
Func *nbuf = (Func *)new byte[sizeof(Func) * (p_nfuncs + 1)];
|
||||||
for (Size i = 0; i < p_nfuncs; ++i) {
|
for (Size i = 0; i < p_nfuncs; ++i) {
|
||||||
new (&nbuf[i]) Func(move(p_funcs[i]));
|
new (&nbuf[i]) Func(move(p_funcs[i]));
|
||||||
p_funcs[i].~Func();
|
p_funcs[i].~Func();
|
||||||
}
|
}
|
||||||
new (&nbuf[p_nfuncs]) Func(func);
|
new (&nbuf[p_nfuncs]) Func(forward<F>(func));
|
||||||
delete[] (byte *)p_funcs;
|
delete[] (byte *)p_funcs;
|
||||||
p_funcs = nbuf;
|
p_funcs = nbuf;
|
||||||
return p_nfuncs++;
|
return p_nfuncs++;
|
||||||
|
@ -69,16 +67,64 @@ struct Event {
|
||||||
if (p_funcs[i]) p_funcs[i](*p_class, args...);
|
if (p_funcs[i]) p_funcs[i](*p_class, args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename ...Args>
|
|
||||||
void operator()(Args &&...args) const {
|
|
||||||
emit(forward<Args>(args)...);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
C *p_class;
|
C *p_class;
|
||||||
Function<void(C &, A...)> *p_funcs;
|
Function<void(C &, A...)> *p_funcs;
|
||||||
Size p_nfuncs;
|
Size p_nfuncs;
|
||||||
};
|
};
|
||||||
|
} /* namespace detail */
|
||||||
|
|
||||||
|
template<typename C, typename ...A>
|
||||||
|
struct Event {
|
||||||
|
private:
|
||||||
|
using Base = detail::EventBase<C, A...>;
|
||||||
|
Base p_base;
|
||||||
|
public:
|
||||||
|
Event(C *cl): p_base(cl) {}
|
||||||
|
Event(const Event &ev): p_base(ev.p_base) {}
|
||||||
|
Event(Event &&ev): p_base(move(ev.p_base)) {}
|
||||||
|
|
||||||
|
~Event() { clear(); }
|
||||||
|
|
||||||
|
void clear() { p_base.clear(); }
|
||||||
|
|
||||||
|
template<typename F>
|
||||||
|
Size connect(F &&func) { return p_base.connect(forward<F>(func)); }
|
||||||
|
|
||||||
|
bool disconnect(Size idx) { return p_base.disconnect(idx); }
|
||||||
|
|
||||||
|
template<typename ...Args>
|
||||||
|
void emit(Args &&...args) { p_base.emit(forward<Args>(args)...); }
|
||||||
|
|
||||||
|
template<typename ...Args>
|
||||||
|
void operator()(Args &&...args) { emit(forward<Args>(args)...); }
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename C, typename ...A>
|
||||||
|
struct Event<const C, A...> {
|
||||||
|
private:
|
||||||
|
using Base = detail::EventBase<const C, A...>;
|
||||||
|
Base p_base;
|
||||||
|
public:
|
||||||
|
Event(C *cl): p_base(cl) {}
|
||||||
|
Event(const Event &ev): p_base(ev.p_base) {}
|
||||||
|
Event(Event &&ev): p_base(move(ev.p_base)) {}
|
||||||
|
|
||||||
|
~Event() { clear(); }
|
||||||
|
|
||||||
|
void clear() { p_base.clear(); }
|
||||||
|
|
||||||
|
template<typename F>
|
||||||
|
Size connect(F &&func) { return p_base.connect(forward<F>(func)); }
|
||||||
|
|
||||||
|
bool disconnect(Size idx) { return p_base.disconnect(idx); }
|
||||||
|
|
||||||
|
template<typename ...Args>
|
||||||
|
void emit(Args &&...args) const { p_base.emit(forward<Args>(args)...); }
|
||||||
|
|
||||||
|
template<typename ...Args>
|
||||||
|
void operator()(Args &&...args) const { emit(forward<Args>(args)...); }
|
||||||
|
};
|
||||||
|
|
||||||
} /* namespace ostd */
|
} /* namespace ostd */
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue