diff --git a/config/Sources.cmake b/config/Sources.cmake index 0d0a37876813861c605466566eced727a70f6488..9d2c7b1f2595beb17212a6daf1b5331d8926d76d 100644 --- a/config/Sources.cmake +++ b/config/Sources.cmake @@ -51,4 +51,6 @@ set(vkcv_sources ${vkcv_source}/vkcv/Framebuffer.hpp ${vkcv_source}/vkcv/Framebuffer.cpp + + ${vkcv_include}/vkcv/Event.hpp ) diff --git a/include/vkcv/Event.hpp b/include/vkcv/Event.hpp new file mode 100644 index 0000000000000000000000000000000000000000..2d30059ce7cc0c741cd0f3d606e483cae7421e86 --- /dev/null +++ b/include/vkcv/Event.hpp @@ -0,0 +1,62 @@ +#pragma once + +namespace vkcv { + + template<typename... T> + struct event_function { + typedef std::function<void(T...)> type; + }; + + /** + * template for event handling + * @tparam T parameter list + */ + template<typename... T> + struct event { + private: + std::vector<typename event_function<T...>::type> m_handles; + + public: + + /** + * calls all function handles with the given arguments + * @param arguments of the given function + */ + void operator()(T... arguments) { + for (auto &handle : this->m_handles) { + handle(arguments...); + } + } + + /** + * adds a function handle to the event to be called + * @param handle of the function + */ + void add(typename event_function<T...>::type handle) { + this->m_handles.push_back(handle); + } + + /** + * removes a function handle of the event + * @param handle of the function + */ + void remove(typename event_function<T...>::type handle) { + this->m_handles.erase( + remove(this->m_handles.begin(), this->m_handles.end(), handle), + this->m_handles.end() + ); + } + + event() = default; + + event(const event &other) = delete; + + event(event &&other) = delete; + + ~event() = default; + + event &operator=(const event &other) = delete; + + event &operator=(event &&other) = delete; + }; +}