Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct FPoint
- {
- float x, y;
- FPoint() : x(), y() {}
- FPoint(float x_, float y_) : x(x_), y(y_) {}
- };
- class Event;
- class Object
- {
- public:
- virtual ~Object() {}
- virtual bool Dispatch(Event& ev) { return false; }
- };
- class Event
- {
- public:
- uintptr_t type;
- bool operator()(Object& obj) { return obj.Dispatch(*this); }
- private:
- Event(uintptr_t type_) : type(type_) {}
- template<class T> friend class EventType;
- };
- template<class T> uintptr_t ClassID(T * p = NULL) { (void)p; return (uintptr_t)&ClassID<T>; }
- template<class T>
- class EventType : public Event
- {
- protected:
- EventType() : Event(ClassID<T>()) {}
- };
- class SetPoint : public EventType<SetPoint>
- {
- public:
- FPoint pos;
- public:
- SetPoint() {}
- explicit SetPoint(FPoint pos_) : pos(pos_) {}
- explicit SetPoint(float x, float y) : pos(x, y) {}
- };
- class GetPoint : public EventType<GetPoint>
- {
- public:
- FPoint pos;
- public:
- GetPoint() {}
- FPoint operator()(Object& obj) { Event::operator()(obj); return pos; }
- };
- class FPosFrame : public Object
- {
- public:
- FPoint pos;
- public:
- virtual bool Dispatch(Event& ev) {
- if (ev.type == ClassID<SetPoint>()) {
- pos = static_cast<SetPoint*>(&ev)->pos;
- }
- else if (ev.type == ClassID<GetPoint>()) {
- static_cast<GetPoint*>(&ev)->pos = pos;
- }
- else {
- return false; // Not found
- }
- return true;
- };
- };
- //----------------------------------------------------------------------
- int main(int, char**)
- {
- FPosFrame fp;
- bool ok = SetPoint(10, 20)(fp);
- Assert(ok);
- printf("%f, %f\n", fp.pos.x, fp.pos.y);
- FPoint pos = GetPoint()(fp);
- printf("%f, %f\n", pos.x, pos.y);
- printf("done\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment