-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_signal.cc
136 lines (103 loc) · 3.51 KB
/
lib_signal.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//------------------------------------------------------------------------
// Simple Signals
//------------------------------------------------------------------------
//
// OBSIDIAN Level Maker
//
// Copyright (C) 2021-2022 The OBSIDIAN Team
// Copyright (C) 2006-2017 Andrew Apted
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//------------------------------------------------------------------------
#include "lib_signal.h"
#include <algorithm>
#include "headers.h"
#include "lib_util.h"
#include "main.h"
#define EXCESSIVE_LOOPS 128
class signal_pair_c {
public:
std::string name;
signal_notify_f func;
void *priv_dat;
public:
signal_pair_c(const char *_name, signal_notify_f _func, void *_priv)
: func(_func), priv_dat(_priv) {
name = _name;
}
};
static std::vector<signal_pair_c *> sig_list;
static std::list<std::string> pending_sigs;
static const char *signal_in_progress = NULL;
void Signal_Watch(const char *name, signal_notify_f func, void *priv_dat) {
// check if already exists
for (unsigned int i = 0; i < sig_list.size(); i++) {
signal_pair_c *P = sig_list[i];
if (P->name == name && P->func == func) {
P->priv_dat = priv_dat;
return;
}
}
sig_list.push_back(new signal_pair_c(name, func, priv_dat));
}
void Signal_DontCare(const char *name, signal_notify_f func) {
for (unsigned int i = 0; i < sig_list.size(); i++) {
signal_pair_c *P = sig_list[i];
if (P->name == name && P->func == func) {
delete P;
sig_list[i] = NULL;
break;
}
}
// remove NULL pointer(s) from the list
std::vector<signal_pair_c *>::iterator ENDP;
ENDP = std::remove(sig_list.begin(), sig_list.end(), (signal_pair_c *)NULL);
sig_list.erase(ENDP, sig_list.end());
}
void Signal_Raise(std::string name) {
if (signal_in_progress) {
for (auto LI = pending_sigs.begin(); LI != pending_sigs.end(); LI++) {
if (*LI == name) {
DebugPrintf("Signal '%s' raised when already pending\n", name.c_str());
return;
}
}
pending_sigs.emplace_back(name);
return;
}
int loop_count = 0;
// memory management strategy:
// - copy names when added in the pending list
// - free names after we perform a notification run
for (;;) {
loop_count++;
if (loop_count >= EXCESSIVE_LOOPS) {
Main::FatalError("Signal_Raise(%s) : excessive looping!\n", name.c_str());
}
signal_in_progress = name.c_str();
for (unsigned int i = 0; i < sig_list.size(); i++) {
signal_pair_c *P = sig_list[i];
if (P->name != name) {
continue;
}
(*P->func)(name.c_str(), P->priv_dat);
}
signal_in_progress = NULL;
if (pending_sigs.empty()) {
break;
}
name = pending_sigs.front();
pending_sigs.pop_front();
}
}
//--- editor settings ---
// vi:ts=4:sw=4:noexpandtab