Skip to content

Commit

Permalink
More useful signal_ class
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Cornu committed Dec 13, 2024
1 parent 6df9a3b commit 38eeb36
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/nrncvode/cvodeobj.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <list>

#include "nrnmpi.h"
#include "nrnneosm.h"
//#include "shared/nvector_serial.h"
Expand Down
26 changes: 18 additions & 8 deletions src/utils/signal.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
#pragma

#include <list>
#include <map>

template <typename T>
template <typename... Args>
class signal_ {
public:
template <typename F>
void connect(F f) {
functors.push_back(f);
unsigned connect(F f) {
++counter;
functors[counter] = f;
return counter;
}

void send(T wc) {
for (auto& f: functors) {
std::invoke(f, wc);
void disconnect(unsigned i) {
auto it = functors.find(i);
if (it != functors.end()) {
functors.erase(it);
}
}

void send(Args... args) {
for (const auto& [i, f]: functors) {
std::invoke(f, args...);
}
}

private:
std::list<std::function<void(T)>> functors;
unsigned counter = 0;
std::map<int, std::function<void(Args...)>> functors;
};

0 comments on commit 38eeb36

Please sign in to comment.