Skip to content

Commit 6a1a9a7

Browse files
committed
Fix Group::addBefore and Group::addAfter
Fixes samplecount#106
1 parent efa0db9 commit 6a1a9a7

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* Add operator bool to `Methcla::NodeId`
1010
* Add `Methcla::Engine::nodeEndedHandler` API method returning `/node/ended` notification handler
1111
* Add `ExponentialFade` plugin (`METHCLA_PLUGINS_EXPONENTIAL_FADE_URI`) to `methcla_plugins_node_control` library
12+
* Fix bug in linked list implementation when adding a node before or after an existing node
13+
1214
### 0.2.0
1315

1416
* Rename `Methcla::Engine::freeNode` to `Methcla::Engine::free`

src/Methcla/Audio/Group.cpp

+35-14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@
1717

1818
using namespace Methcla::Audio;
1919

20+
#define METHCLA_ASSERT_NODE_IS_BLANK(node) \
21+
BOOST_ASSERT(node != nullptr); \
22+
BOOST_ASSERT(node->m_parent == nullptr); \
23+
BOOST_ASSERT(node->m_prev == nullptr); \
24+
BOOST_ASSERT(node->m_next == nullptr); \
25+
26+
#define METHCLA_ASSERT_NODE_IS_LINKED(node) \
27+
BOOST_ASSERT(m_first != nullptr); \
28+
BOOST_ASSERT(m_last != nullptr); \
29+
BOOST_ASSERT(node->m_prev != nullptr || node == m_first); \
30+
BOOST_ASSERT(node->m_next != nullptr || node == m_last); \
31+
BOOST_ASSERT(m_first != m_last || (m_first == node && node == m_last));
32+
2033
Group::Group(Environment& env, NodeId nodeId)
2134
: Node(env, nodeId)
2235
, m_first(nullptr)
@@ -86,38 +99,46 @@ void Group::addToTail(Node* node)
8699

87100
void Group::addBefore(Node* target, Node* node)
88101
{
89-
assert(target != nullptr);
90-
assert(node != nullptr);
91-
assert(target->parent() == this);
92-
assert(node->parent() == nullptr);
93-
assert(node->m_prev == nullptr);
94-
assert(node->m_next == nullptr);
102+
BOOST_ASSERT(target != nullptr);
103+
BOOST_ASSERT(target->parent() == this);
104+
METHCLA_ASSERT_NODE_IS_BLANK(node);
95105

96106
node->m_parent = this;
97107
node->m_prev = target->m_prev;
98108
target->m_prev = node;
99109
node->m_next = target;
100110

101-
if (target == m_first)
111+
if (target == m_first) {
112+
BOOST_ASSERT(node->m_prev == nullptr);
102113
m_first = node;
114+
} else {
115+
BOOST_ASSERT(node->m_prev != nullptr);
116+
node->m_prev->m_next = node;
117+
}
118+
119+
METHCLA_ASSERT_NODE_IS_LINKED(node);
103120
}
104121

105122
void Group::addAfter(Node* target, Node* node)
106123
{
107-
assert(target != nullptr);
108-
assert(node != nullptr);
109-
assert(target->parent() == this);
110-
assert(node->parent() == nullptr);
111-
assert(node->m_prev == nullptr);
112-
assert(node->m_next == nullptr);
124+
BOOST_ASSERT(target != nullptr);
125+
BOOST_ASSERT(target->parent() == this);
126+
METHCLA_ASSERT_NODE_IS_BLANK(node);
113127

114128
node->m_parent = this;
115129
node->m_next = target->m_next;
116130
target->m_next = node;
117131
node->m_prev = target;
118132

119-
if (target == m_last)
133+
if (target == m_last) {
134+
BOOST_ASSERT(node->m_next == nullptr);
120135
m_last = node;
136+
} else {
137+
BOOST_ASSERT(node->m_next != nullptr);
138+
node->m_next->m_prev = node;
139+
}
140+
141+
METHCLA_ASSERT_NODE_IS_LINKED(node);
121142
}
122143

123144
void Group::remove(Node* node)

0 commit comments

Comments
 (0)