Sunset old state system finally.
This commit is contained in:
		@@ -1,129 +0,0 @@
 | 
			
		||||
// Copyright (c) 2022 Dominic Masters
 | 
			
		||||
// 
 | 
			
		||||
// This software is released under the MIT License.
 | 
			
		||||
// https://opensource.org/licenses/MIT
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
#include "dawnlibs.hpp"
 | 
			
		||||
#include "assert/assert.hpp"
 | 
			
		||||
 | 
			
		||||
namespace Dawn {
 | 
			
		||||
  template<typename... A>
 | 
			
		||||
  struct IEventListener {
 | 
			
		||||
    /**
 | 
			
		||||
     * Abstracted method for C++ template reasons. Invokes the listener.
 | 
			
		||||
     * 
 | 
			
		||||
     * @deprecated
 | 
			
		||||
     * @param args Arguments to pass to the listener.
 | 
			
		||||
     */
 | 
			
		||||
    virtual void invoke(A... args) = 0;
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  template <class T, typename... A>
 | 
			
		||||
  struct EventListener : public IEventListener<A...> {
 | 
			
		||||
    T *instance;
 | 
			
		||||
    void (T::*callback)(A... args);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Construct a new event listener structure.
 | 
			
		||||
     * 
 | 
			
		||||
    * @deprecated
 | 
			
		||||
     * @param instance Instance that the callback belongs to.
 | 
			
		||||
     * @param callback Callback method that invokes back.
 | 
			
		||||
     */
 | 
			
		||||
    EventListener(T *instance, void (T::*callback)(A... args)) {
 | 
			
		||||
      assertNotNull(instance, "EventListener::EventListener: Instance cannot be null");
 | 
			
		||||
 | 
			
		||||
      this->instance = instance;
 | 
			
		||||
      this->callback = callback;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void invoke(A... args) {
 | 
			
		||||
      ((*this->instance).*(this->callback))(args...);
 | 
			
		||||
    }
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  /** @deprecated */
 | 
			
		||||
  template<typename...A>
 | 
			
		||||
  class Event {
 | 
			
		||||
    private:  
 | 
			
		||||
      std::vector<IEventListener<A...>*> listeners;
 | 
			
		||||
 | 
			
		||||
    public:
 | 
			
		||||
      /**
 | 
			
		||||
       * Add a listener to this event.
 | 
			
		||||
       * 
 | 
			
		||||
       * @deprecated
 | 
			
		||||
       * @tparam T The class that will receive the event.
 | 
			
		||||
       * @param instance Instance of type T that will receive the callback.
 | 
			
		||||
       * @param callback Callback method attached to T to receive the event.
 | 
			
		||||
       * @return The initialized event listener. You don't really need this.
 | 
			
		||||
       */
 | 
			
		||||
      template<class T>
 | 
			
		||||
      EventListener<T, A...> * addListener(
 | 
			
		||||
        T *instance,
 | 
			
		||||
        void (T::*callback)(A... args)
 | 
			
		||||
      ) {
 | 
			
		||||
        assertNotNull(instance, "Event::addListener: Instance cannot be null");
 | 
			
		||||
 | 
			
		||||
        auto listener = new EventListener<T,A...>(instance, callback);
 | 
			
		||||
        assertNotNull(listener, "Event::addListener: Listener could not be created (Memory filled?)");
 | 
			
		||||
        this->listeners.push_back(listener);
 | 
			
		||||
        return listener;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Removes an event listener from this event.
 | 
			
		||||
       * 
 | 
			
		||||
       * @deprecated
 | 
			
		||||
       * @tparam T The class that was once receiving the event.
 | 
			
		||||
       * @param instance Instance of type T that did receive the callback.
 | 
			
		||||
       * @param callback Callback method attached to T for the event.
 | 
			
		||||
       */
 | 
			
		||||
      template<class T>
 | 
			
		||||
      void removeListener(
 | 
			
		||||
        T *instance,
 | 
			
		||||
        void (T::*callback)(A... args)
 | 
			
		||||
      ) {
 | 
			
		||||
        auto it = this->listeners.begin();
 | 
			
		||||
        while(it != this->listeners.end()) {
 | 
			
		||||
          auto listener = static_cast<EventListener<T,A...>*>(*it);
 | 
			
		||||
 | 
			
		||||
          if(listener->instance != instance || listener->callback != callback) {
 | 
			
		||||
            ++it;
 | 
			
		||||
            continue;
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          this->listeners.erase(it);
 | 
			
		||||
          delete listener;
 | 
			
		||||
          break;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Invokes the event and emits to all of the listeners.
 | 
			
		||||
       * 
 | 
			
		||||
       * @deprecated
 | 
			
		||||
       * @param args Arguments for this event to pass to the listeners.
 | 
			
		||||
       */
 | 
			
		||||
      void invoke(A... args) {
 | 
			
		||||
        auto it = this->listeners.begin();
 | 
			
		||||
        while(it != this->listeners.end()) {
 | 
			
		||||
          (*it)->invoke(args...);
 | 
			
		||||
          ++it;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Disposes the event instance. Will also destroy all of the event 
 | 
			
		||||
       * listeners.
 | 
			
		||||
       */
 | 
			
		||||
      ~Event() {
 | 
			
		||||
        auto it = this->listeners.begin();
 | 
			
		||||
        while(it != this->listeners.end()) {
 | 
			
		||||
          delete *it;
 | 
			
		||||
          ++it;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user