Program Listing for File EventConsumer.hpp

Return to documentation for file (engine/include/Cacao/EventConsumer.hpp)

#pragma once

#include "Event.hpp"
#include "DllHelper.hpp"

#include <functional>

#include "crossguid/guid.hpp"

namespace Cacao {
    class CACAO_API EventConsumer {
      public:
        EventConsumer(std::function<void(Event&)> consumer)
          : consumer(consumer), guid(xg::newGuid()) {}

        EventConsumer()
          : consumer([](Event&) {}), guid(xg::newGuid()) {}

        EventConsumer(const EventConsumer& other)
          : consumer(other.consumer), guid(other.guid) {}
        EventConsumer(EventConsumer&& other)
          : consumer(other.consumer), guid(other.guid) {
            other.consumer = [](Event&) {};
            other.guid = xg::Guid {};
        }
        EventConsumer& operator=(const EventConsumer& other) {
            consumer = other.consumer;
            guid = other.guid;
            return *this;
        }
        EventConsumer& operator=(EventConsumer&& other) {
            consumer = other.consumer;
            guid = other.guid;
            other.consumer = [](Event&) {};
            other.guid = xg::Guid {};
            return *this;
        }

        void Consume(Event& event) {
            consumer(event);
        }

        bool operator==(const EventConsumer& rhs) const {
            return (guid == rhs.guid);
        }

      private:
        std::function<void(Event&)> consumer;
        xg::Guid guid;
    };
}