#ifndef CPP_PART__ // ----------------------------------------------------------- struct CallbackAction { Atomic count; virtual void Execute() = 0; virtual bool IsValid() const { return true; } CallbackAction() { count = 1; } virtual ~CallbackAction() {} }; template struct CallbackMethodActionPte : public CallbackAction { Ptr object; METHOD method; void Execute() { if(object) (object->*method)(); } bool IsValid() const { return object; } CallbackMethodActionPte(OBJECT *object, METHOD method) : object(object), method(method) {} }; template struct CallbackMethodAction : public CallbackAction { OBJECT *object; METHOD method; void Execute() { (object->*method)(); } CallbackMethodAction(OBJECT *object, METHOD method) : object(object), method(method) {} }; struct CallbackFnAction : public CallbackAction { void (*fn)(); void Execute() { (*fn)(); } CallbackFnAction(void (*fn)()) : fn(fn) {} }; class Callback : Moveable< Callback > { CallbackAction *action; void Retain() const { if(action ) AtomicInc(action->count); } void Release() { if(action && AtomicDec(action->count) == 0) delete action; } bool operator==(const Callback&); bool operator!=(const Callback&); public: typedef Callback CLASSNAME; Callback& operator=(const Callback& c); Callback(const Callback& c); void Clear() { Release(); action = NULL; } operator bool() const { return action && action->IsValid(); } void Execute() const; void operator()() const { Execute(); } explicit Callback(CallbackAction *newaction) { action = newaction; } Callback() { action = NULL; } Callback(_CNULL) { action = NULL; } ~Callback(); static Callback Empty() { return CNULL; } }; template Callback pteback(OBJECT *object, void (METHOD::*method)()) { return Callback(new CallbackMethodActionPte(object, method)); } template Callback callback(OBJECT *object, void (METHOD::*method)()) { return Callback(new CallbackMethodAction(object, method)); } template Callback callback(const OBJECT *object, void (METHOD::*method)() const) { return Callback(new CallbackMethodAction(object, method)); } inline Callback callback(void (*fn)()) { return Callback(new CallbackFnAction (fn)); } struct CallbackForkAction : public CallbackAction { Callback cb1, cb2; void Execute() { cb1(); cb2(); } CallbackForkAction(Callback cb1, Callback cb2) : cb1(cb1), cb2(cb2) {} }; inline Callback Proxy(Callback& cb) { return callback(&cb, &Callback::Execute); } Callback callback(Callback cb1, Callback cb2); Callback& operator<<(Callback& a, Callback b); #endif #ifdef CPP_PART__ Callback callback(Callback cb1, Callback cb2) { return Callback(new CallbackForkAction (cb1, cb2)); } Callback& operator<<(Callback& a, Callback b) { if(a) a = callback(a, b); else a = b; return a; } Callback& Callback::operator=(const Callback& c) { c.Retain(); Release(); action = c.action; return *this; } Callback::Callback(const Callback& c) { action = c.action; Retain(); } Callback::~Callback() { Release(); } #endif #ifndef CPP_PART__ // ----------------------------------------------------------- template struct Callback1Action { Atomic count; virtual void Execute(P1 p1) = 0; virtual bool IsValid() const { return true; } Callback1Action() { count = 1; } virtual ~Callback1Action() {} }; template struct Callback1MethodActionPte : public Callback1Action { Ptr object; METHOD method; void Execute(P1 p1) { if(object) (object->*method)(p1); } bool IsValid() const { return object; } Callback1MethodActionPte(OBJECT *object, METHOD method) : object(object), method(method) {} }; template struct Callback1MethodAction : public Callback1Action { OBJECT *object; METHOD method; void Execute(P1 p1) { (object->*method)(p1); } Callback1MethodAction(OBJECT *object, METHOD method) : object(object), method(method) {} }; template struct Callback1FnAction : public Callback1Action { void (*fn)(P1 p1); void Execute(P1 p1) { (*fn)(p1); } Callback1FnAction(void (*fn)(P1 p1)) : fn(fn) {} }; template class Callback1 : Moveable< Callback1 > { Callback1Action *action; void Retain() const { if(action ) AtomicInc(action->count); } void Release() { if(action && AtomicDec(action->count) == 0) delete action; } bool operator==(const Callback1&); bool operator!=(const Callback1&); public: typedef Callback1 CLASSNAME; Callback1& operator=(const Callback1& c); Callback1(const Callback1& c); void Clear() { Release(); action = NULL; } operator bool() const { return action && action->IsValid(); } void Execute(P1 p1) const { if(action) action->Execute(p1); } void operator()(P1 p1) const { Execute(p1); } explicit Callback1(Callback1Action *newaction) { action = newaction; } Callback1() { action = NULL; } Callback1(_CNULL) { action = NULL; } ~Callback1(); static Callback1 Empty() { return CNULL; } }; template Callback1 pteback(OBJECT *object, void (METHOD::*method)(P1 p1)) { return Callback1(new Callback1MethodActionPte(object, method)); } template Callback1 callback(OBJECT *object, void (METHOD::*method)(P1 p1)) { return Callback1(new Callback1MethodAction(object, method)); } template Callback1 callback(const OBJECT *object, void (METHOD::*method)(P1 p1) const) { return Callback1(new Callback1MethodAction(object, method)); } template inline Callback1 callback(void (*fn)(P1 p1)) { return Callback1(new Callback1FnAction (fn)); } template struct Callback1ForkAction : public Callback1Action { Callback1 cb1, cb2; void Execute(P1 p1) { cb1(p1); cb2(p1); } Callback1ForkAction(Callback1 cb1, Callback1 cb2) : cb1(cb1), cb2(cb2) {} }; template inline Callback1 Proxy(Callback1& cb) { return callback(&cb, &Callback1::Execute); } template Callback1 callback(Callback1 cb1, Callback1 cb2) { return Callback1(new Callback1ForkAction (cb1, cb2)); } template Callback1& operator<<(Callback1& a, Callback1 b) { if(a) a = callback(a, b); else a = b; return a; } template Callback1& Callback1::operator=(const Callback1& c) { c.Retain(); Release(); action = c.action; return *this; } template Callback1::Callback1(const Callback1& c) { action = c.action; Retain(); } template Callback1::~Callback1() { Release(); } // ----------------------------------------------------------- template struct Callback2Action { Atomic count; virtual void Execute(P1 p1, P2 p2) = 0; virtual bool IsValid() const { return true; } Callback2Action() { count = 1; } virtual ~Callback2Action() {} }; template struct Callback2MethodActionPte : public Callback2Action { Ptr object; METHOD method; void Execute(P1 p1, P2 p2) { if(object) (object->*method)(p1, p2); } bool IsValid() const { return object; } Callback2MethodActionPte(OBJECT *object, METHOD method) : object(object), method(method) {} }; template struct Callback2MethodAction : public Callback2Action { OBJECT *object; METHOD method; void Execute(P1 p1, P2 p2) { (object->*method)(p1, p2); } Callback2MethodAction(OBJECT *object, METHOD method) : object(object), method(method) {} }; template struct Callback2FnAction : public Callback2Action { void (*fn)(P1 p1, P2 p2); void Execute(P1 p1, P2 p2) { (*fn)(p1, p2); } Callback2FnAction(void (*fn)(P1 p1, P2 p2)) : fn(fn) {} }; template class Callback2 : Moveable< Callback2 > { Callback2Action *action; void Retain() const { if(action ) AtomicInc(action->count); } void Release() { if(action && AtomicDec(action->count) == 0) delete action; } bool operator==(const Callback2&); bool operator!=(const Callback2&); public: typedef Callback2 CLASSNAME; Callback2& operator=(const Callback2& c); Callback2(const Callback2& c); void Clear() { Release(); action = NULL; } operator bool() const { return action && action->IsValid(); } void Execute(P1 p1, P2 p2) const { if(action) action->Execute(p1, p2); } void operator()(P1 p1, P2 p2) const { Execute(p1, p2); } explicit Callback2(Callback2Action *newaction) { action = newaction; } Callback2() { action = NULL; } Callback2(_CNULL) { action = NULL; } ~Callback2(); static Callback2 Empty() { return CNULL; } }; template Callback2 pteback(OBJECT *object, void (METHOD::*method)(P1 p1, P2 p2)) { return Callback2(new Callback2MethodActionPte(object, method)); } template Callback2 callback(OBJECT *object, void (METHOD::*method)(P1 p1, P2 p2)) { return Callback2(new Callback2MethodAction(object, method)); } template Callback2 callback(const OBJECT *object, void (METHOD::*method)(P1 p1, P2 p2) const) { return Callback2(new Callback2MethodAction(object, method)); } template inline Callback2 callback(void (*fn)(P1 p1, P2 p2)) { return Callback2(new Callback2FnAction (fn)); } template struct Callback2ForkAction : public Callback2Action { Callback2 cb1, cb2; void Execute(P1 p1, P2 p2) { cb1(p1, p2); cb2(p1, p2); } Callback2ForkAction(Callback2 cb1, Callback2 cb2) : cb1(cb1), cb2(cb2) {} }; template inline Callback2 Proxy(Callback2& cb) { return callback(&cb, &Callback2::Execute); } template Callback2 callback(Callback2 cb1, Callback2 cb2) { return Callback2(new Callback2ForkAction (cb1, cb2)); } template Callback2& operator<<(Callback2& a, Callback2 b) { if(a) a = callback(a, b); else a = b; return a; } template Callback2& Callback2::operator=(const Callback2& c) { c.Retain(); Release(); action = c.action; return *this; } template Callback2::Callback2(const Callback2& c) { action = c.action; Retain(); } template Callback2::~Callback2() { Release(); } // ----------------------------------------------------------- template struct Callback3Action { Atomic count; virtual void Execute(P1 p1, P2 p2, P3 p3) = 0; virtual bool IsValid() const { return true; } Callback3Action() { count = 1; } virtual ~Callback3Action() {} }; template struct Callback3MethodActionPte : public Callback3Action { Ptr object; METHOD method; void Execute(P1 p1, P2 p2, P3 p3) { if(object) (object->*method)(p1, p2, p3); } bool IsValid() const { return object; } Callback3MethodActionPte(OBJECT *object, METHOD method) : object(object), method(method) {} }; template struct Callback3MethodAction : public Callback3Action { OBJECT *object; METHOD method; void Execute(P1 p1, P2 p2, P3 p3) { (object->*method)(p1, p2, p3); } Callback3MethodAction(OBJECT *object, METHOD method) : object(object), method(method) {} }; template struct Callback3FnAction : public Callback3Action { void (*fn)(P1 p1, P2 p2, P3 p3); void Execute(P1 p1, P2 p2, P3 p3) { (*fn)(p1, p2, p3); } Callback3FnAction(void (*fn)(P1 p1, P2 p2, P3 p3)) : fn(fn) {} }; template class Callback3 : Moveable< Callback3 > { Callback3Action *action; void Retain() const { if(action ) AtomicInc(action->count); } void Release() { if(action && AtomicDec(action->count) == 0) delete action; } bool operator==(const Callback3&); bool operator!=(const Callback3&); public: typedef Callback3 CLASSNAME; Callback3& operator=(const Callback3& c); Callback3(const Callback3& c); void Clear() { Release(); action = NULL; } operator bool() const { return action && action->IsValid(); } void Execute(P1 p1, P2 p2, P3 p3) const { if(action) action->Execute(p1, p2, p3); } void operator()(P1 p1, P2 p2, P3 p3) const { Execute(p1, p2, p3); } explicit Callback3(Callback3Action *newaction) { action = newaction; } Callback3() { action = NULL; } Callback3(_CNULL) { action = NULL; } ~Callback3(); static Callback3 Empty() { return CNULL; } }; template Callback3 pteback(OBJECT *object, void (METHOD::*method)(P1 p1, P2 p2, P3 p3)) { return Callback3(new Callback3MethodActionPte(object, method)); } template Callback3 callback(OBJECT *object, void (METHOD::*method)(P1 p1, P2 p2, P3 p3)) { return Callback3(new Callback3MethodAction(object, method)); } template Callback3 callback(const OBJECT *object, void (METHOD::*method)(P1 p1, P2 p2, P3 p3) const) { return Callback3(new Callback3MethodAction(object, method)); } template inline Callback3 callback(void (*fn)(P1 p1, P2 p2, P3 p3)) { return Callback3(new Callback3FnAction (fn)); } template struct Callback3ForkAction : public Callback3Action { Callback3 cb1, cb2; void Execute(P1 p1, P2 p2, P3 p3) { cb1(p1, p2, p3); cb2(p1, p2, p3); } Callback3ForkAction(Callback3 cb1, Callback3 cb2) : cb1(cb1), cb2(cb2) {} }; template inline Callback3 Proxy(Callback3& cb) { return callback(&cb, &Callback3::Execute); } template Callback3 callback(Callback3 cb1, Callback3 cb2) { return Callback3(new Callback3ForkAction (cb1, cb2)); } template Callback3& operator<<(Callback3& a, Callback3 b) { if(a) a = callback(a, b); else a = b; return a; } template Callback3& Callback3::operator=(const Callback3& c) { c.Retain(); Release(); action = c.action; return *this; } template Callback3::Callback3(const Callback3& c) { action = c.action; Retain(); } template Callback3::~Callback3() { Release(); } // ----------------------------------------------------------- template struct Callback4Action { Atomic count; virtual void Execute(P1 p1, P2 p2, P3 p3, P4 p4) = 0; virtual bool IsValid() const { return true; } Callback4Action() { count = 1; } virtual ~Callback4Action() {} }; template struct Callback4MethodActionPte : public Callback4Action { Ptr object; METHOD method; void Execute(P1 p1, P2 p2, P3 p3, P4 p4) { if(object) (object->*method)(p1, p2, p3, p4); } bool IsValid() const { return object; } Callback4MethodActionPte(OBJECT *object, METHOD method) : object(object), method(method) {} }; template struct Callback4MethodAction : public Callback4Action { OBJECT *object; METHOD method; void Execute(P1 p1, P2 p2, P3 p3, P4 p4) { (object->*method)(p1, p2, p3, p4); } Callback4MethodAction(OBJECT *object, METHOD method) : object(object), method(method) {} }; template struct Callback4FnAction : public Callback4Action { void (*fn)(P1 p1, P2 p2, P3 p3, P4 p4); void Execute(P1 p1, P2 p2, P3 p3, P4 p4) { (*fn)(p1, p2, p3, p4); } Callback4FnAction(void (*fn)(P1 p1, P2 p2, P3 p3, P4 p4)) : fn(fn) {} }; template class Callback4 : Moveable< Callback4 > { Callback4Action *action; void Retain() const { if(action ) AtomicInc(action->count); } void Release() { if(action && AtomicDec(action->count) == 0) delete action; } bool operator==(const Callback4&); bool operator!=(const Callback4&); public: typedef Callback4 CLASSNAME; Callback4& operator=(const Callback4& c); Callback4(const Callback4& c); void Clear() { Release(); action = NULL; } operator bool() const { return action && action->IsValid(); } void Execute(P1 p1, P2 p2, P3 p3, P4 p4) const { if(action) action->Execute(p1, p2, p3, p4); } void operator()(P1 p1, P2 p2, P3 p3, P4 p4) const { Execute(p1, p2, p3, p4); } explicit Callback4(Callback4Action *newaction) { action = newaction; } Callback4() { action = NULL; } Callback4(_CNULL) { action = NULL; } ~Callback4(); static Callback4 Empty() { return CNULL; } }; template Callback4 pteback(OBJECT *object, void (METHOD::*method)(P1 p1, P2 p2, P3 p3, P4 p4)) { return Callback4(new Callback4MethodActionPte(object, method)); } template Callback4 callback(OBJECT *object, void (METHOD::*method)(P1 p1, P2 p2, P3 p3, P4 p4)) { return Callback4(new Callback4MethodAction(object, method)); } template Callback4 callback(const OBJECT *object, void (METHOD::*method)(P1 p1, P2 p2, P3 p3, P4 p4) const) { return Callback4(new Callback4MethodAction(object, method)); } template inline Callback4 callback(void (*fn)(P1 p1, P2 p2, P3 p3, P4 p4)) { return Callback4(new Callback4FnAction (fn)); } template struct Callback4ForkAction : public Callback4Action { Callback4 cb1, cb2; void Execute(P1 p1, P2 p2, P3 p3, P4 p4) { cb1(p1, p2, p3, p4); cb2(p1, p2, p3, p4); } Callback4ForkAction(Callback4 cb1, Callback4 cb2) : cb1(cb1), cb2(cb2) {} }; template inline Callback4 Proxy(Callback4& cb) { return callback(&cb, &Callback4::Execute); } template Callback4 callback(Callback4 cb1, Callback4 cb2) { return Callback4(new Callback4ForkAction (cb1, cb2)); } template Callback4& operator<<(Callback4& a, Callback4 b) { if(a) a = callback(a, b); else a = b; return a; } template Callback4& Callback4::operator=(const Callback4& c) { c.Retain(); Release(); action = c.action; return *this; } template Callback4::Callback4(const Callback4& c) { action = c.action; Retain(); } template Callback4::~Callback4() { Release(); } // ----------------------------------------------------------- struct GateAction { Atomic count; virtual bool Execute() = 0; virtual bool IsValid() const { return true; } GateAction() { count = 1; } virtual ~GateAction() {} }; template struct GateMethodActionPte : public GateAction { Ptr object; METHOD method; bool Execute() { return object ? (object->*method)() : false; } bool IsValid() const { return object; } GateMethodActionPte(OBJECT *object, METHOD method) : object(object), method(method) {} }; template struct GateMethodAction : public GateAction { OBJECT *object; METHOD method; bool Execute() { return (object->*method)(); } GateMethodAction(OBJECT *object, METHOD method) : object(object), method(method) {} }; struct GateFnAction : public GateAction { bool (*fn)(); bool Execute() { return (*fn)(); } GateFnAction(bool (*fn)()) : fn(fn) {} }; class Gate : Moveable< Gate > { GateAction *action; void Retain() const { if(action && (void *)action != (void *)1) AtomicInc(action->count); } void Release() { if(action && (void *)action != (void *)1 && AtomicDec(action->count) == 0) delete action; } bool operator==(const Gate&); bool operator!=(const Gate&); public: typedef Gate CLASSNAME; Gate& operator=(const Gate& c); Gate(const Gate& c); void Clear() { Release(); action = NULL; } operator bool() const { return (void *)action != (void *)1 && action && action->IsValid(); } bool Execute() const; bool operator()() const { return Execute(); } void ClearTrue() { Clear(); action = (GateAction *)(uintptr_t)1; } void ClearFalse() { Clear(); } Gate(bool b) { action = (GateAction *)(uintptr_t)b; } explicit Gate(GateAction *newaction) { action = newaction; } Gate() { action = NULL; } Gate(_CNULL) { action = NULL; } ~Gate(); static Gate Empty() { return CNULL; } }; template Gate pteback(OBJECT *object, bool (METHOD::*method)()) { return Gate(new GateMethodActionPte(object, method)); } template Gate callback(OBJECT *object, bool (METHOD::*method)()) { return Gate(new GateMethodAction(object, method)); } template Gate callback(const OBJECT *object, bool (METHOD::*method)() const) { return Gate(new GateMethodAction(object, method)); } inline Gate callback(bool (*fn)()) { return Gate(new GateFnAction (fn)); } struct GateForkAction : public GateAction { Gate cb1, cb2; bool Execute() { cb1(); return cb2(); } GateForkAction(Gate cb1, Gate cb2) : cb1(cb1), cb2(cb2) {} }; inline Gate Proxy(Gate& cb) { return callback(&cb, &Gate::Execute); } Gate callback(Gate cb1, Gate cb2); Gate& operator<<(Gate& a, Gate b); #endif #ifdef CPP_PART__ Gate callback(Gate cb1, Gate cb2) { return Gate(new GateForkAction (cb1, cb2)); } Gate& operator<<(Gate& a, Gate b) { if(a) a = callback(a, b); else a = b; return a; } Gate& Gate::operator=(const Gate& c) { c.Retain(); Release(); action = c.action; return *this; } Gate::Gate(const Gate& c) { action = c.action; Retain(); } Gate::~Gate() { Release(); } #endif #ifndef CPP_PART__ // ----------------------------------------------------------- template struct Gate1Action { Atomic count; virtual bool Execute(P1 p1) = 0; virtual bool IsValid() const { return true; } Gate1Action() { count = 1; } virtual ~Gate1Action() {} }; template struct Gate1MethodActionPte : public Gate1Action { Ptr object; METHOD method; bool Execute(P1 p1) { return object ? (object->*method)(p1) : false; } bool IsValid() const { return object; } Gate1MethodActionPte(OBJECT *object, METHOD method) : object(object), method(method) {} }; template struct Gate1MethodAction : public Gate1Action { OBJECT *object; METHOD method; bool Execute(P1 p1) { return (object->*method)(p1); } Gate1MethodAction(OBJECT *object, METHOD method) : object(object), method(method) {} }; template struct Gate1FnAction : public Gate1Action { bool (*fn)(P1 p1); bool Execute(P1 p1) { return (*fn)(p1); } Gate1FnAction(bool (*fn)(P1 p1)) : fn(fn) {} }; template class Gate1 : Moveable< Gate1 > { Gate1Action *action; void Retain() const { if(action && (void *)action != (void *)1) AtomicInc(action->count); } void Release() { if(action && (void *)action != (void *)1 && AtomicDec(action->count) == 0) delete action; } bool operator==(const Gate1&); bool operator!=(const Gate1&); public: typedef Gate1 CLASSNAME; Gate1& operator=(const Gate1& c); Gate1(const Gate1& c); void Clear() { Release(); action = NULL; } operator bool() const { return (void *)action != (void *)1 && action && action->IsValid(); } bool Execute(P1 p1) const; bool operator()(P1 p1) const { return Execute(p1); } void ClearTrue() { Clear(); action = (Gate1Action *)1; } void ClearFalse() { Clear(); } Gate1(bool b) { action = (Gate1Action *)(uintptr_t)b; } explicit Gate1(Gate1Action *newaction) { action = newaction; } Gate1() { action = NULL; } Gate1(_CNULL) { action = NULL; } ~Gate1(); static Gate1 Empty() { return CNULL; } }; template Gate1 pteback(OBJECT *object, bool (METHOD::*method)(P1 p1)) { return Gate1(new Gate1MethodActionPte(object, method)); } template Gate1 callback(OBJECT *object, bool (METHOD::*method)(P1 p1)) { return Gate1(new Gate1MethodAction(object, method)); } template Gate1 callback(const OBJECT *object, bool (METHOD::*method)(P1 p1) const) { return Gate1(new Gate1MethodAction(object, method)); } template inline Gate1 callback(bool (*fn)(P1 p1)) { return Gate1(new Gate1FnAction (fn)); } template struct Gate1ForkAction : public Gate1Action { Gate1 cb1, cb2; bool Execute(P1 p1) { cb1(p1); return cb2(p1); } Gate1ForkAction(Gate1 cb1, Gate1 cb2) : cb1(cb1), cb2(cb2) {} }; template inline Gate1 Proxy(Gate1& cb) { return callback(&cb, &Gate1::Execute); } template Gate1 callback(Gate1 cb1, Gate1 cb2) { return Gate1(new Gate1ForkAction (cb1, cb2)); } template Gate1& operator<<(Gate1& a, Gate1 b) { if(a) a = callback(a, b); else a = b; return a; } template Gate1& Gate1::operator=(const Gate1& c) { c.Retain(); Release(); action = c.action; return *this; } template Gate1::Gate1(const Gate1& c) { action = c.action; Retain(); } template Gate1::~Gate1() { Release(); } // ----------------------------------------------------------- template struct Gate2Action { Atomic count; virtual bool Execute(P1 p1, P2 p2) = 0; virtual bool IsValid() const { return true; } Gate2Action() { count = 1; } virtual ~Gate2Action() {} }; template struct Gate2MethodActionPte : public Gate2Action { Ptr object; METHOD method; bool Execute(P1 p1, P2 p2) { return object ? (object->*method)(p1, p2) : false; } bool IsValid() const { return object; } Gate2MethodActionPte(OBJECT *object, METHOD method) : object(object), method(method) {} }; template struct Gate2MethodAction : public Gate2Action { OBJECT *object; METHOD method; bool Execute(P1 p1, P2 p2) { return (object->*method)(p1, p2); } Gate2MethodAction(OBJECT *object, METHOD method) : object(object), method(method) {} }; template struct Gate2FnAction : public Gate2Action { bool (*fn)(P1 p1, P2 p2); bool Execute(P1 p1, P2 p2) { return (*fn)(p1, p2); } Gate2FnAction(bool (*fn)(P1 p1, P2 p2)) : fn(fn) {} }; template class Gate2 : Moveable< Gate2 > { Gate2Action *action; void Retain() const { if(action && (void *)action != (void *)1) AtomicInc(action->count); } void Release() { if(action && (void *)action != (void *)1 && AtomicDec(action->count) == 0) delete action; } bool operator==(const Gate2&); bool operator!=(const Gate2&); public: typedef Gate2 CLASSNAME; Gate2& operator=(const Gate2& c); Gate2(const Gate2& c); void Clear() { Release(); action = NULL; } operator bool() const { return (void *)action != (void *)1 && action && action->IsValid(); } bool Execute(P1 p1, P2 p2) const; bool operator()(P1 p1, P2 p2) const { return Execute(p1, p2); } void ClearTrue() { Clear(); action = (Gate2Action *)1; } void ClearFalse() { Clear(); } Gate2(bool b) { action = (Gate2Action *)(uintptr_t)b; } explicit Gate2(Gate2Action *newaction) { action = newaction; } Gate2() { action = NULL; } Gate2(_CNULL) { action = NULL; } ~Gate2(); static Gate2 Empty() { return CNULL; } }; template Gate2 pteback(OBJECT *object, bool (METHOD::*method)(P1 p1, P2 p2)) { return Gate2(new Gate2MethodActionPte(object, method)); } template Gate2 callback(OBJECT *object, bool (METHOD::*method)(P1 p1, P2 p2)) { return Gate2(new Gate2MethodAction(object, method)); } template Gate2 callback(const OBJECT *object, bool (METHOD::*method)(P1 p1, P2 p2) const) { return Gate2(new Gate2MethodAction(object, method)); } template inline Gate2 callback(bool (*fn)(P1 p1, P2 p2)) { return Gate2(new Gate2FnAction (fn)); } template struct Gate2ForkAction : public Gate2Action { Gate2 cb1, cb2; bool Execute(P1 p1, P2 p2) { cb1(p1, p2); return cb2(p1, p2); } Gate2ForkAction(Gate2 cb1, Gate2 cb2) : cb1(cb1), cb2(cb2) {} }; template inline Gate2 Proxy(Gate2& cb) { return callback(&cb, &Gate2::Execute); } template Gate2 callback(Gate2 cb1, Gate2 cb2) { return Gate2(new Gate2ForkAction (cb1, cb2)); } template Gate2& operator<<(Gate2& a, Gate2 b) { if(a) a = callback(a, b); else a = b; return a; } template Gate2& Gate2::operator=(const Gate2& c) { c.Retain(); Release(); action = c.action; return *this; } template Gate2::Gate2(const Gate2& c) { action = c.action; Retain(); } template Gate2::~Gate2() { Release(); } // ----------------------------------------------------------- template struct Gate3Action { Atomic count; virtual bool Execute(P1 p1, P2 p2, P3 p3) = 0; virtual bool IsValid() const { return true; } Gate3Action() { count = 1; } virtual ~Gate3Action() {} }; template struct Gate3MethodActionPte : public Gate3Action { Ptr object; METHOD method; bool Execute(P1 p1, P2 p2, P3 p3) { return object ? (object->*method)(p1, p2, p3) : false; } bool IsValid() const { return object; } Gate3MethodActionPte(OBJECT *object, METHOD method) : object(object), method(method) {} }; template struct Gate3MethodAction : public Gate3Action { OBJECT *object; METHOD method; bool Execute(P1 p1, P2 p2, P3 p3) { return (object->*method)(p1, p2, p3); } Gate3MethodAction(OBJECT *object, METHOD method) : object(object), method(method) {} }; template struct Gate3FnAction : public Gate3Action { bool (*fn)(P1 p1, P2 p2, P3 p3); bool Execute(P1 p1, P2 p2, P3 p3) { return (*fn)(p1, p2, p3); } Gate3FnAction(bool (*fn)(P1 p1, P2 p2, P3 p3)) : fn(fn) {} }; template class Gate3 : Moveable< Gate3 > { Gate3Action *action; void Retain() const { if(action && (void *)action != (void *)1) AtomicInc(action->count); } void Release() { if(action && (void *)action != (void *)1 && AtomicDec(action->count) == 0) delete action; } bool operator==(const Gate3&); bool operator!=(const Gate3&); public: typedef Gate3 CLASSNAME; Gate3& operator=(const Gate3& c); Gate3(const Gate3& c); void Clear() { Release(); action = NULL; } operator bool() const { return (void *)action != (void *)1 && action && action->IsValid(); } bool Execute(P1 p1, P2 p2, P3 p3) const; bool operator()(P1 p1, P2 p2, P3 p3) const { return Execute(p1, p2, p3); } void ClearTrue() { Clear(); action = (Gate3Action *)1; } void ClearFalse() { Clear(); } Gate3(bool b) { action = (Gate3Action *)(uintptr_t)b; } explicit Gate3(Gate3Action *newaction) { action = newaction; } Gate3() { action = NULL; } Gate3(_CNULL) { action = NULL; } ~Gate3(); static Gate3 Empty() { return CNULL; } }; template Gate3 pteback(OBJECT *object, bool (METHOD::*method)(P1 p1, P2 p2, P3 p3)) { return Gate3(new Gate3MethodActionPte(object, method)); } template Gate3 callback(OBJECT *object, bool (METHOD::*method)(P1 p1, P2 p2, P3 p3)) { return Gate3(new Gate3MethodAction(object, method)); } template Gate3 callback(const OBJECT *object, bool (METHOD::*method)(P1 p1, P2 p2, P3 p3) const) { return Gate3(new Gate3MethodAction(object, method)); } template inline Gate3 callback(bool (*fn)(P1 p1, P2 p2, P3 p3)) { return Gate3(new Gate3FnAction (fn)); } template struct Gate3ForkAction : public Gate3Action { Gate3 cb1, cb2; bool Execute(P1 p1, P2 p2, P3 p3) { cb1(p1, p2, p3); return cb2(p1, p2, p3); } Gate3ForkAction(Gate3 cb1, Gate3 cb2) : cb1(cb1), cb2(cb2) {} }; template inline Gate3 Proxy(Gate3& cb) { return callback(&cb, &Gate3::Execute); } template Gate3 callback(Gate3 cb1, Gate3 cb2) { return Gate3(new Gate3ForkAction (cb1, cb2)); } template Gate3& operator<<(Gate3& a, Gate3 b) { if(a) a = callback(a, b); else a = b; return a; } template Gate3& Gate3::operator=(const Gate3& c) { c.Retain(); Release(); action = c.action; return *this; } template Gate3::Gate3(const Gate3& c) { action = c.action; Retain(); } template Gate3::~Gate3() { Release(); } // ----------------------------------------------------------- template struct Gate4Action { Atomic count; virtual bool Execute(P1 p1, P2 p2, P3 p3, P4 p4) = 0; virtual bool IsValid() const { return true; } Gate4Action() { count = 1; } virtual ~Gate4Action() {} }; template struct Gate4MethodActionPte : public Gate4Action { Ptr object; METHOD method; bool Execute(P1 p1, P2 p2, P3 p3, P4 p4) { return object ? (object->*method)(p1, p2, p3, p4) : false; } bool IsValid() const { return object; } Gate4MethodActionPte(OBJECT *object, METHOD method) : object(object), method(method) {} }; template struct Gate4MethodAction : public Gate4Action { OBJECT *object; METHOD method; bool Execute(P1 p1, P2 p2, P3 p3, P4 p4) { return (object->*method)(p1, p2, p3, p4); } Gate4MethodAction(OBJECT *object, METHOD method) : object(object), method(method) {} }; template struct Gate4FnAction : public Gate4Action { bool (*fn)(P1 p1, P2 p2, P3 p3, P4 p4); bool Execute(P1 p1, P2 p2, P3 p3, P4 p4) { return (*fn)(p1, p2, p3, p4); } Gate4FnAction(bool (*fn)(P1 p1, P2 p2, P3 p3, P4 p4)) : fn(fn) {} }; template class Gate4 : Moveable< Gate4 > { Gate4Action *action; void Retain() const { if(action && (void *)action != (void *)1) AtomicInc(action->count); } void Release() { if(action && (void *)action != (void *)1 && AtomicDec(action->count) == 0) delete action; } bool operator==(const Gate4&); bool operator!=(const Gate4&); public: typedef Gate4 CLASSNAME; Gate4& operator=(const Gate4& c); Gate4(const Gate4& c); void Clear() { Release(); action = NULL; } operator bool() const { return (void *)action != (void *)1 && action && action->IsValid(); } bool Execute(P1 p1, P2 p2, P3 p3, P4 p4) const; bool operator()(P1 p1, P2 p2, P3 p3, P4 p4) const { return Execute(p1, p2, p3, p4); } void ClearTrue() { Clear(); action = (Gate4Action *)1; } void ClearFalse() { Clear(); } Gate4(bool b) { action = (Gate4Action *)(uintptr_t)b; } explicit Gate4(Gate4Action *newaction) { action = newaction; } Gate4() { action = NULL; } Gate4(_CNULL) { action = NULL; } ~Gate4(); static Gate4 Empty() { return CNULL; } }; template Gate4 pteback(OBJECT *object, bool (METHOD::*method)(P1 p1, P2 p2, P3 p3, P4 p4)) { return Gate4(new Gate4MethodActionPte(object, method)); } template Gate4 callback(OBJECT *object, bool (METHOD::*method)(P1 p1, P2 p2, P3 p3, P4 p4)) { return Gate4(new Gate4MethodAction(object, method)); } template Gate4 callback(const OBJECT *object, bool (METHOD::*method)(P1 p1, P2 p2, P3 p3, P4 p4) const) { return Gate4(new Gate4MethodAction(object, method)); } template inline Gate4 callback(bool (*fn)(P1 p1, P2 p2, P3 p3, P4 p4)) { return Gate4(new Gate4FnAction (fn)); } template struct Gate4ForkAction : public Gate4Action { Gate4 cb1, cb2; bool Execute(P1 p1, P2 p2, P3 p3, P4 p4) { cb1(p1, p2, p3, p4); return cb2(p1, p2, p3, p4); } Gate4ForkAction(Gate4 cb1, Gate4 cb2) : cb1(cb1), cb2(cb2) {} }; template inline Gate4 Proxy(Gate4& cb) { return callback(&cb, &Gate4::Execute); } template Gate4 callback(Gate4 cb1, Gate4 cb2) { return Gate4(new Gate4ForkAction (cb1, cb2)); } template Gate4& operator<<(Gate4& a, Gate4 b) { if(a) a = callback(a, b); else a = b; return a; } template Gate4& Gate4::operator=(const Gate4& c) { c.Retain(); Release(); action = c.action; return *this; } template Gate4::Gate4(const Gate4& c) { action = c.action; Retain(); } template Gate4::~Gate4() { Release(); } #endif