daily pastebin goal
2%
SHARE
TWEET

Untitled

a guest May 16th, 2018 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Event.hpp
  2. class Event {
  3.     public:
  4.     Event();
  5.     virtual ~Event();
  6.     virtual std::auto_ptr<Event> copy() const = 0;
  7.     virtual void accept(EventVisitor& visitor) const = 0;
  8. };
  9.  
  10. template <typename T>
  11. class EventImpl : public Event {
  12.     public:
  13.     virtual std::auto_ptr<Event> copy() const {
  14.         return std::auto_ptr<Event>(new T(*static_cast<const T*>(this)));
  15.     }
  16.     virtual void accept(EventVisitor& visitor) const {
  17.         visitor.visit(*static_cast<const T*>(this));
  18.     }
  19. };
  20.  
  21. class MoveEvent : public EventImpl<MoveEvent> {
  22.     public:
  23.     MoveEvent(TokenSoul token, const Node& from_pos, const Node& to_pos);
  24.     TokenSoul    token()    const { return token_; }
  25.     const Node&  from_pos() const { return from_pos_; }
  26.     const Node&  to_pos()   const { return to_pos_; }
  27.     private:
  28.     TokenSoul    token_;
  29.     const Node&  from_pos_;
  30.     const Node&  to_pos_;
  31. };
  32.  
  33. class EventVisitor {
  34.     public:
  35.     virtual ~EventVisitor();
  36.     // On peut overrider uniquement ça si on se fout du type précis de l'event
  37.     virtual void visit(const Event& event);
  38.     // On override ça si on sait gérer spécifiquement les MoveEvent
  39.     virtual void visit(const MoveEvent& event);
  40. };
  41.  
  42. ## Event.cpp
  43. void EventVisitor::visit(const Event& event) {
  44.     // Un Throw serait plus adapté en mode nazi
  45.     std::cerr << "*** Unknown event type `" << typeid(event).name()
  46.         << "' for visitor `" << typeid(*this).name() << "'" << std::endl;
  47. }
  48.  
  49. void EventVisitor::visit(const MoveEvent& event) {
  50.     // L'implé par defaut de la gestion spécifique d'un MoveEvent
  51.     // appelle la gestion générique d'events (juste au dessus)
  52.  
  53.     visit(*static_cast<const Event*>(&event));
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top