From 4b86554a0354d78efeb40e551eaccaac0fecd1d1 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 25 Sep 2018 13:08:40 +0800 Subject: Change the structure of project. --- CruUI/cru_event.h | 91 ------------------------------------------------------- 1 file changed, 91 deletions(-) delete mode 100644 CruUI/cru_event.h (limited to 'CruUI/cru_event.h') diff --git a/CruUI/cru_event.h b/CruUI/cru_event.h deleted file mode 100644 index d0a7eb82..00000000 --- a/CruUI/cru_event.h +++ /dev/null @@ -1,91 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "base.h" - -namespace cru { - //Base class of all event args. - class BasicEventArgs : public Object - { - public: - explicit BasicEventArgs(Object* sender) - : sender_(sender) - { - - } - BasicEventArgs(const BasicEventArgs& other) = default; - BasicEventArgs(BasicEventArgs&& other) = default; - BasicEventArgs& operator=(const BasicEventArgs& other) = default; - BasicEventArgs& operator=(BasicEventArgs&& other) = default; - ~BasicEventArgs() override = default; - - //Get the sender of the event. - Object* GetSender() const - { - return sender_; - } - - private: - Object* sender_; - }; - - - //A non-copyable non-movable Event class. - //It stores a list of event handlers. - //TArgsType must be subclass of BasicEventArgs. - template - class Event - { - public: - static_assert(std::is_base_of_v, - "TArgsType must be subclass of BasicEventArgs."); - - - using ArgsType = TArgsType; - using EventHandler = Function; - using EventHandlerPtr = std::shared_ptr; - - Event() = default; - Event(const Event&) = delete; - Event& operator = (const Event&) = delete; - Event(Event&&) = delete; - Event& operator = (Event&&) = delete; - ~Event() = default; - - //Create a EventHandlerPtr from the given handler, - //add it to list and return it. - EventHandlerPtr AddHandler(EventHandler&& handler) - { - EventHandlerPtr ptr = std::make_shared(std::move(handler)); - handlers_.push_back(ptr); - return ptr; - } - - void AddHandler(EventHandlerPtr handler) { - handlers_.push_back(handler); - } - - void RemoveHandler(EventHandlerPtr handler) { - auto find_result = std::find(handlers_.cbegin(), handlers_.cend(), handler); - if (find_result != handlers_.cend()) - handlers_.erase(find_result); - } - - void Raise(ArgsType& args) { - for (auto ptr : handlers_) - (*ptr)(args); - } - - bool IsNoHandler() const - { - return handlers_.empty(); - } - - private: - std::list handlers_; - }; -} -- cgit v1.2.3