Skip to content

Commit 50d9d8f

Browse files
committed
app: uses shared-buffer in component-list
1 parent e5dc9cf commit 50d9d8f

File tree

2 files changed

+97
-100
lines changed

2 files changed

+97
-100
lines changed

app/gui/application.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,9 +1227,8 @@ class component_editor
12271227
/// List of tabulation opened in the @a ImGui::BeginTabBar.
12281228
vector<tab> tabs;
12291229

1230-
vector<component_id> component_list;
1231-
spin_mutex component_list_mutex;
1232-
std::atomic_flag component_list_updating = ATOMIC_FLAG_INIT;
1230+
/// Stores the list of component_id opened in the editor.
1231+
shared_buffer<vector<component_id>> component_list;
12331232

12341233
std::bitset<2> tabitem_open;
12351234
component_id m_request_to_open = undefined<component_id>();

app/gui/component-editor.cpp

Lines changed: 95 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -61,70 +61,71 @@ concept has_store_function = requires(T t, component_editor& ed) {
6161
{ t.store(ed) } -> std::same_as<void>;
6262
};
6363

64+
bool push_back_if_not_find(application& app,
65+
vector<component_id>& vec,
66+
const component_id id) noexcept
67+
{
68+
if (std::ranges::none_of(vec,
69+
[id](const auto other) { return id == other; })) {
70+
if (not vec.can_alloc(1) and not vec.template grow<2, 1>()) {
71+
app.jn.push(log_level::error,
72+
[&vec](auto& title, auto& msg) noexcept {
73+
title = "Adding connection pack error";
74+
format(msg,
75+
"Not enough memory to allocate "
76+
"more connection pack ({})",
77+
vec.capacity());
78+
});
79+
80+
return false;
81+
}
82+
83+
vec.emplace_back(id);
84+
}
85+
86+
return true;
87+
};
88+
6489
static void update_component_list(application& app,
6590
component_editor& ed,
6691
const component& compo) noexcept
6792
{
6893
app.add_gui_task([&]() noexcept {
69-
std::unique_lock lock(ed.component_list_mutex);
70-
ed.component_list.clear();
71-
72-
auto push_back_if_not_find =
73-
[](auto& app, auto& vec, const auto id) noexcept {
74-
if (std::ranges::none_of(
75-
vec, [id](const auto other) { return id == other; })) {
76-
if (not vec.can_alloc(1) and not vec.template grow<2, 1>()) {
77-
app.jn.push(log_level::error,
78-
[&vec](auto& title, auto& msg) noexcept {
79-
title = "Adding connection pack error";
80-
format(msg,
81-
"Not enough memory to allocate "
82-
"more connection pack ({})",
83-
vec.capacity());
84-
});
85-
86-
return false;
87-
}
88-
89-
vec.emplace_back(id);
90-
}
91-
92-
return true;
93-
};
94+
ed.component_list.write([&](auto& vec) noexcept {
95+
vec.clear();
9496

95-
switch (compo.type) {
96-
case component_type::generic:
97-
for (const auto& c :
98-
app.mod.generic_components.get(compo.id.generic_id).children)
99-
if (c.type == child_type::component)
100-
if (not push_back_if_not_find(
101-
app, ed.component_list, c.id.compo_id))
97+
switch (compo.type) {
98+
case component_type::generic:
99+
for (const auto& c :
100+
app.mod.generic_components.get(compo.id.generic_id)
101+
.children)
102+
if (c.type == child_type::component)
103+
if (not push_back_if_not_find(app, vec, c.id.compo_id))
104+
break;
105+
break;
106+
107+
case component_type::grid:
108+
for (const auto id :
109+
app.mod.grid_components.get(compo.id.grid_id).children())
110+
if (not push_back_if_not_find(app, vec, id))
102111
break;
103-
break;
112+
break;
104113

105-
case component_type::grid:
106-
for (const auto id :
107-
app.mod.grid_components.get(compo.id.grid_id).children())
108-
if (not push_back_if_not_find(app, ed.component_list, id))
109-
break;
110-
break;
114+
case component_type::graph:
115+
for (const auto id :
116+
app.mod.graph_components.get(compo.id.graph_id).g.nodes) {
117+
const auto c_id =
118+
app.mod.graph_components.get(compo.id.graph_id)
119+
.g.node_components[id];
120+
if (not push_back_if_not_find(app, vec, c_id))
121+
break;
122+
}
123+
break;
111124

112-
case component_type::graph:
113-
for (const auto id :
114-
app.mod.graph_components.get(compo.id.graph_id).g.nodes) {
115-
const auto c_id =
116-
app.mod.graph_components.get(compo.id.graph_id)
117-
.g.node_components[id];
118-
if (not push_back_if_not_find(app, ed.component_list, c_id))
119-
break;
125+
default:
126+
break;
120127
}
121-
break;
122-
123-
default:
124-
break;
125-
}
126-
127-
ed.component_list_updating.clear();
128+
});
128129
});
129130
}
130131

@@ -156,11 +157,11 @@ static auto combobox_port_id(const auto* label,
156157
return p_id;
157158
}
158159

159-
static auto combobox_component_id(const auto& mod,
160-
const auto* label,
161-
const auto& component_list,
162-
auto c_id,
163-
auto p_id) noexcept
160+
static auto combobox_component_id(const modeling& mod,
161+
const char* label,
162+
const vector<component_id>& component_list,
163+
component_id c_id,
164+
port_id p_id) noexcept
164165
-> std::pair<component_id, port_id>
165166
{
166167
const char* preview =
@@ -1827,34 +1828,32 @@ struct component_editor::impl {
18271828
static auto c_p = undefined<port_id>();
18281829
static auto c = undefined<component_id>();
18291830

1830-
if (ed.component_list_updating.test()) {
1831-
ImGui::TextUnformatted("Component update in progress");
1832-
} else {
1833-
p = combobox_port_id("input port", compo.x, p);
1831+
p = combobox_port_id("input port", compo.x, p);
18341832

1835-
const auto child = combobox_component_id(
1836-
app.mod, "child component", ed.component_list, c, c_p);
1833+
ed.component_list.read([&](const auto& vec, auto /*version*/) noexcept {
1834+
const auto child =
1835+
combobox_component_id(app.mod, "child component", vec, c, c_p);
18371836
c = child.first;
18381837
c_p = app.mod.components.exists(c)
18391838
? combobox_component_port_id(
18401839
"child port",
18411840
app.mod.components.get<component>(c).x,
18421841
child.second)
18431842
: combobox_component_port_id_empty("child port");
1844-
}
18451843

1846-
if (is_defined(p) and is_defined(c_p) and is_defined(c) and
1847-
std::ranges::none_of(
1848-
compo.input_connection_pack, [&](const auto& con) {
1849-
return con.parent_port == p and con.child_port == c_p and
1850-
con.child_component == c;
1851-
})) {
1852-
compo.input_connection_pack.push_back(connection_pack{
1853-
.parent_port = p, .child_port = c_p, .child_component = c });
1854-
p = undefined<port_id>();
1855-
c_p = undefined<port_id>();
1856-
c = undefined<component_id>();
1857-
}
1844+
if (is_defined(p) and is_defined(c_p) and is_defined(c) and
1845+
std::ranges::none_of(
1846+
compo.input_connection_pack, [&](const auto& con) {
1847+
return con.parent_port == p and con.child_port == c_p and
1848+
con.child_component == c;
1849+
})) {
1850+
compo.input_connection_pack.push_back(connection_pack{
1851+
.parent_port = p, .child_port = c_p, .child_component = c });
1852+
p = undefined<port_id>();
1853+
c_p = undefined<port_id>();
1854+
c = undefined<component_id>();
1855+
}
1856+
});
18581857
}
18591858

18601859
static void show_output_connection_packs(application& app,
@@ -1872,34 +1871,33 @@ struct component_editor::impl {
18721871
static auto c_p = undefined<port_id>();
18731872
static auto c = undefined<component_id>();
18741873

1875-
if (ed.component_list_updating.test()) {
1876-
ImGui::TextUnformatted("Component update in progress");
1877-
} else {
1878-
p = combobox_port_id("output port", compo.y, p);
1874+
p = combobox_port_id("output port", compo.y, p);
18791875

1880-
const auto child = combobox_component_id(
1881-
app.mod, "child component", ed.component_list, c, c_p);
1876+
ed.component_list.read([&](const auto& vec,
1877+
const auto /*version*/) noexcept {
1878+
const auto child =
1879+
combobox_component_id(app.mod, "child component", vec, c, c_p);
18821880
c = child.first;
18831881
c_p = app.mod.components.exists(c)
18841882
? combobox_component_port_id(
18851883
"child port",
18861884
app.mod.components.get<component>(c).y,
18871885
child.second)
18881886
: combobox_component_port_id_empty("child port");
1889-
}
18901887

1891-
if (is_defined(p) and is_defined(c_p) and is_defined(c) and
1892-
std::ranges::none_of(
1893-
compo.output_connection_pack, [&](const auto& con) {
1894-
return con.parent_port == p and con.child_port == c_p and
1895-
con.child_component == c;
1896-
})) {
1897-
compo.output_connection_pack.push_back(connection_pack{
1898-
.parent_port = p, .child_port = c_p, .child_component = c });
1899-
p = undefined<port_id>();
1900-
c_p = undefined<port_id>();
1901-
c = undefined<component_id>();
1902-
}
1888+
if (is_defined(p) and is_defined(c_p) and is_defined(c) and
1889+
std::ranges::none_of(
1890+
compo.output_connection_pack, [&](const auto& con) {
1891+
return con.parent_port == p and con.child_port == c_p and
1892+
con.child_component == c;
1893+
})) {
1894+
compo.output_connection_pack.push_back(connection_pack{
1895+
.parent_port = p, .child_port = c_p, .child_component = c });
1896+
p = undefined<port_id>();
1897+
c_p = undefined<port_id>();
1898+
c = undefined<component_id>();
1899+
}
1900+
});
19031901
}
19041902

19051903
template<typename ComponentEditor>

0 commit comments

Comments
 (0)