diff --git a/libsrc/core/CMakeLists.txt b/libsrc/core/CMakeLists.txt index 7eefdacb..368247ed 100644 --- a/libsrc/core/CMakeLists.txt +++ b/libsrc/core/CMakeLists.txt @@ -49,7 +49,7 @@ target_link_libraries(ngcore PUBLIC netgen_mpi PRIVATE ${CMAKE_THREAD_LIBS_INIT} install(FILES ngcore.hpp archive.hpp type_traits.hpp version.hpp ngcore_api.hpp logging.hpp exception.hpp symboltable.hpp paje_trace.hpp utils.hpp profiler.hpp mpi_wrapper.hpp array.hpp taskmanager.hpp concurrentqueue.h localheap.hpp python_ngcore.hpp flags.hpp - xbool.hpp + xbool.hpp signal.hpp DESTINATION ${NG_INSTALL_DIR_INCLUDE}/core COMPONENT netgen_devel) if(ENABLE_CPP_CORE_GUIDELINES_CHECK) diff --git a/libsrc/core/ngcore.hpp b/libsrc/core/ngcore.hpp index 166e8de7..a2cda645 100644 --- a/libsrc/core/ngcore.hpp +++ b/libsrc/core/ngcore.hpp @@ -9,6 +9,7 @@ #include "logging.hpp" #include "mpi_wrapper.hpp" #include "profiler.hpp" +#include "signal.hpp" #include "symboltable.hpp" #include "taskmanager.hpp" #include "version.hpp" diff --git a/libsrc/core/signal.hpp b/libsrc/core/signal.hpp new file mode 100644 index 00000000..d414d14e --- /dev/null +++ b/libsrc/core/signal.hpp @@ -0,0 +1,43 @@ +#ifndef NGCORE_SIGNALS_HPP +#define NGCORE_SIGNALS_HPP + +#include +#include + +namespace ngcore +{ + template + class Signal + { + private: + std::list> funcs; + bool is_emitting; + public: + Signal() : is_emitting(true) {} + + template + void connect(Cls* self, FUNC f) + { + auto ptr = self->weak_from_this(); + auto func = [ptr, f](ParameterTypes... args) + { + if (ptr.expired()) + return false; + f(args...); + return true; + }; + funcs.push_back(func); + } + + inline void emit(ParameterTypes ...args) + { + if(is_emitting) + funcs.remove_if([&](auto& f){ return !f(args...); }); + } + + inline void setEmitting(bool emitting) { is_emitting = emitting; } + inline bool getEmitting() const { return is_emitting; } + }; +} // namespace ngcore + +#endif // NGCORE_SIGNALS_HPP