Lab 5: Bounded FaultTableModel, Persistent Selection & Sort/Filter Proxy
Bound multi-screen HMI memory via lazy container factories, replace O(R*C) item-widgets with contiguous QAbstractTableModel role projections, and wire zero-copy proxy views, QUndoStack, and delegates.
Prerequisites in this module
What ./check.sh verifies
- Builds libcore.a and lab_cli cleanly with -std=c++20 -Wall -Wextra -Werror -pedantic.
- Fix compiler warnings or errors in files/core.cpp.
- Archives core.o into libcore.a and links independent TAP verifier.
- Preserve public declarations in files/core.hpp so tests.cpp links.
step 1/15 · inspect starter files and specification
Implement and verify a bounded-capacity FaultTableModel and FaultFilterProxy with multi-role data() dispatch, FIFO persistent-selection tracking, and map_to_source coordinate translation.
learner@ksa:~/gl-q01/lab-m05ls -lastarter/Makefilestarter/cli.cppstarter/core.cppstarter/core.hpplearner@ksa:~/gl-q01/lab-m05starter: starter/Makefile
learner@ksa:~/gl-q01/lab-m05$ bat starter/Makefile File: starter/Makefile CC_BIN = g++ AR = ar CFLAGS_ALL = -std=c++20 -Wall -Wextra -Werror -pedantic -O2 LDLIBS = all: libcore.a lab_cli libcore.a: core.o $(AR) rcs $@ $^ lab_cli: cli.o libcore.a $(CC_BIN) $(CFLAGS_ALL) cli.o libcore.a $(LDLIBS) -o $@ %.o: %.cpp core.hpp $(CC_BIN) $(CFLAGS_ALL) -c $< -o $@ .PHONY: all clean clean: rm -f core.o cli.o libcore.a lab_clistarter: starter/cli.cpp
learner@ksa:~/gl-q01/lab-m05$ bat starter/cli.cpp File: starter/cli.cpp #include "core.hpp" #include <algorithm> #include <array> #include <cstdint> #include <cstdio> #include <cstring> #include <ctime> static std::uint64_t now_ns() noexcept { timespec ts{}; clock_gettime(CLOCK_MONOTONIC, &ts); return static_cast<std::uint64_t>(ts.tv_sec) * 1000000000ULL + static_cast<std::uint64_t>(ts.tv_nsec); } int main(int argc, char **argv) { hmi::FaultTableModel model(1024); for (std::uint64_t i = 1; i <= 1024; ++i) { model.append_fault({i, static_cast<int>(100 + (i % 90)), static_cast<int>(i % 3), false, "spindle"}); } hmi::FaultFilterProxy proxy(model); if (argc > 1 && std::strcmp(argv[1], "--bench") == 0) { constexpr int kWarmup = 5; constexpr int kSamples = 40; constexpr int kBatch = 50; std::array<std::uint64_t, kSamples> ns{}; volatile std::int64_t sink = 0; for (int i = 0; i < kWarmup + kSamples; ++i) { const std::uint64_t t0 = now_ns(); for (int r = 0; r < kBatch; ++r) { model.append_fault({2000ULL + static_cast<std::uint64_t>(r), 505, 2, false, "servo"}); proxy.configure(1, true); sink += proxy.data(0, 0, hmi::DataRole::UserCode).number; } const std::uint64_t dt = (now_ns() - t0) / kBatch; if (i >= kWarmup) { ns[static_cast<std::size_t>(i - kWarmup)] = dt; } } std::sort(ns.begin(), ns.end()); std::printf("bench_proxy_refresh_ns: p50=%llu p95=%llu p99=%llu (sink=%lld)\n", static_cast<unsigned long long>(ns[20]), static_cast<unsigned long long>(ns[37]), static_cast<unsigned long long>(ns[39]), static_cast<long long>(sink)); return 0; } proxy.configure(2, true); // Critical only, newest seq_id first const int top_src = proxy.map_to_source(0); std::printf("table_model: rows=%d crit_proxy_rows=%d top_src=%d top_code=%s\n", model.row_count(), proxy.row_count(), top_src, proxy.data(0, 1, hmi::DataRole::Display).text.c_str()); return (model.row_count() == 1024 && proxy.row_count() > 0 && top_src >= 0) ? 0 : 1; }starter: starter/core.cpp
learner@ksa:~/gl-q01/lab-m05$ bat starter/core.cpp File: starter/core.cpp #include "core.hpp" #include <algorithm> namespace hmi { FaultTableModel::FaultTableModel(std::size_t capacity) : m_capacity(std::max<std::size_t>(1U, capacity)) {} int FaultTableModel::row_count(bool valid_parent) const noexcept { return valid_parent ? 0 : static_cast<int>(m_rows.size()); } int FaultTableModel::column_count(bool valid_parent) const noexcept { return valid_parent ? 0 : 4; } RoleValue FaultTableModel::data(int row, int col, DataRole role) const { if (row < 0 || row >= row_count() || col < 0 || col >= column_count()) { return {}; } const FaultItem &f = m_rows[static_cast<std::size_t>(row)]; if (role == DataRole::UserSeverity) { return {true, f.severity, {}}; } if (role == DataRole::UserCode) { return {true, f.code, {}}; } if (role == DataRole::Foreground) { if (f.severity >= 2 && !f.acknowledged) { return {true, 0xDC2626, {}}; } return {}; } if (role != DataRole::Display && role != DataRole::CheckState) { return {}; } switch (col) { case 0: return {true, static_cast<std::int64_t>(f.seq_id), std::to_string(f.seq_id)}; case 1: return {true, f.code, "E" + std::to_string(f.code)}; case 2: return {true, f.severity, f.severity == 2 ? "CRITICAL" : (f.severity == 1 ? "WARN" : "INFO")}; case 3: return {true, f.acknowledged ? 1 : 0, f.subsystem + (f.acknowledged ? " [ACK]" : "")}; default: return {}; } } int FaultTableModel::register_persistent(int initial_row) { if (initial_row < 0 || initial_row >= row_count()) { initial_row = -1; } const int h = static_cast<int>(m_persistent.size()); m_persistent.push_back(initial_row); return h; } int FaultTableModel::persistent_row(int handle) const noexcept { if (handle < 0 || handle >= static_cast<int>(m_persistent.size())) { return -1; } return m_persistent[static_cast<std::size_t>(handle)]; } void FaultTableModel::append_fault(FaultItem item) { if (m_rows.size() == m_capacity) { m_rows.erase(m_rows.begin()); for (int &r : m_persistent) { if (r == 0) { r = -1; } else if (r > 0) { /* defect: forgot --r shift */ } } } m_rows.push_back(std::move(item)); } bool FaultTableModel::set_acknowledged(int row, bool acked) noexcept { if (row < 0 || row >= row_count()) { return false; } m_rows[static_cast<std::size_t>(row)].acknowledged = acked; return true; } void FaultFilterProxy::configure(int min_severity, bool sort_severity_desc) { m_min_sev = min_severity; m_sort_desc = sort_severity_desc; refresh(); } void FaultFilterProxy::refresh() { m_map.clear(); const int n = m_src.row_count(); for (int r = 0; r < n; ++r) { if (m_src.data(r, 0, DataRole::UserSeverity).number >= m_min_sev) { m_map.push_back(r); } } if (m_sort_desc) { std::stable_sort(m_map.begin(), m_map.end(), [this](int a, int b) { const auto sa = m_src.data(a, 0, DataRole::UserSeverity).number; const auto sb = m_src.data(b, 0, DataRole::UserSeverity).number; if (sa != sb) { return sa > sb; } return m_src.raw_at(a).seq_id > m_src.raw_at(b).seq_id; }); } } int FaultFilterProxy::map_to_source(int proxy_row) const noexcept { if (proxy_row < 0 || proxy_row >= row_count()) { return -1; } return m_map[static_cast<std::size_t>(proxy_row)]; } int FaultFilterProxy::map_from_source(int source_row) const noexcept { for (int i = 0; i < row_count(); ++i) { if (m_map[static_cast<std::size_t>(i)] == source_row) { return i; } } return -1; } RoleValue FaultFilterProxy::data(int proxy_row, int col, DataRole role) const { const int src_row = proxy_row; if (src_row < 0) { return {}; } return m_src.data(src_row, col, role); } } // namespace hmistarter: starter/core.hpp
learner@ksa:~/gl-q01/lab-m05$ bat starter/core.hpp File: starter/core.hpp #pragma once #include <cstddef> #include <cstdint> #include <string> #include <vector> namespace hmi { enum class DataRole { Display, Foreground, CheckState, UserSeverity, UserCode }; struct RoleValue { bool valid = false; std::int64_t number = 0; std::string text; }; struct FaultItem { std::uint64_t seq_id = 0; int code = 0; int severity = 0; // 0=Info, 1=Warn, 2=Critical bool acknowledged = false; std::string subsystem; }; class FaultTableModel { public: explicit FaultTableModel(std::size_t capacity); int row_count(bool valid_parent = false) const noexcept; int column_count(bool valid_parent = false) const noexcept; RoleValue data(int row, int col, DataRole role) const; int register_persistent(int initial_row); int persistent_row(int handle) const noexcept; void append_fault(FaultItem item); bool set_acknowledged(int row, bool acked) noexcept; const FaultItem &raw_at(int row) const { return m_rows[static_cast<std::size_t>(row)]; } private: std::size_t m_capacity; std::vector<FaultItem> m_rows; std::vector<int> m_persistent; }; class FaultFilterProxy { public: explicit FaultFilterProxy(const FaultTableModel &source) noexcept : m_src(source) {} void configure(int min_severity, bool sort_severity_desc); void refresh(); int row_count() const noexcept { return static_cast<int>(m_map.size()); } int map_to_source(int proxy_row) const noexcept; int map_from_source(int source_row) const noexcept; RoleValue data(int proxy_row, int col, DataRole role) const; private: const FaultTableModel &m_src; int m_min_sev = 0; bool m_sort_desc = false; std::vector<int> m_map; }; } // namespace hmistep 2/15 · 2. learning objectives
- Guard flat
row_count/column_countagainst valid parents and return emptyRoleValue{}on unhandledCheckStatequeries. - Maintain
QPersistentModelIndex-equivalent selection handles across FIFO capacity eviction and in-place alarm acknowledgement. - Filter and sort alarms zero-copy via
UserSeveritywhile translating proxy rows accurately withmap_to_source.
step 3/15 · 3. environment requirements
All automated verification steps run in unprivileged user space on a POSIX Linux host using g++ -std=c++20 -Wall -Wextra -Werror -pedantic and clock_gettime(CLOCK_MONOTONIC). Recorded transcripts in captures.json were generated on x86_64 running kernel 7.0.0-42-generic and prove protocol, state-machine, and memory invariants only. Physical bus timing, DMA cache coherency, and board peripheral behavior require target validation on Linux kernel 6.12 LTS (ARMv7-A Cortex-A7).
step 4/15 · 4. required software and hardware
- Host workstation:
g++(-std=c++20 -Wall -Wextra -Werror -pedantic), GNUmake,ar(binutils), and POSIX/bin/sh. - Target environment (optional hardware step, Section 13):
ARMv7-ACortex-A7operator console (512 MiBDDR3) runningLinux kernel6.12 LTS and Qt 6.8QTableView+QSortFilterProxyModelover liveSocketCANfault telemetry.
step 5/15 · 5. complete executable commands
learner@ksa:~/gl-q01/lab-m05$ bat script.sh File: script.sh make -B -C files ./files/lab_cli ./files/lab_cli --bench sh check.shstep 6/15 · 6. annotated source code
files/core.cpp enforces the production invariants that starter/core.cpp breaks:
learner@ksa:~/gl-q01/lab-m05$ bat example.cpp File: example.cpp void FaultTableModel::append_fault(FaultItem item) { if (m_rows.size() == m_capacity) { m_rows.erase(m_rows.begin()); for (int &r : m_persistent) { if (r == 0) r = -1; // Evicted row becomes invalid else if (r > 0) --r; // Surviving selections shift -1 } } m_rows.push_back(std::move(item)); } RoleValue FaultFilterProxy::data(int proxy_row, int col, DataRole role) const { const int src_row = map_to_source(proxy_row); // Never index m_src with raw proxy_row! return (src_row < 0) ? RoleValue{} : m_src.data(src_row, col, role); }starter/core.cpp contains three realistic Model/View bugs: FaultTableModel::data falls through on DataRole::CheckState (causing phantom checkboxes), append_fault forgets --r on surviving persistent selections during FIFO eviction, and FaultFilterProxy::data passes unmapped proxy_row straight to m_src.
step 7/15 · 7. expected output
Passing files/ run (sh check.sh, exit code 0):
learner@ksa:~/gl-q01/lab-m05$ bat listing.txt File: listing.txt TAP version 13 1..5 ok 1 - builds libcore.a and lab_cli cleanly with -std=c++20 -Wall -Wextra -Werror -pedantic ok 2 - archives core.o into libcore.a and links independent TAP verifier ok 3 - enforces flat parent=0 guard, typed UserCode/Foreground roles, and empty unhandled CheckStateRole ok 4 - evicts oldest FIFO row at capacity, shifts persistent selections, and updates ack state in place ok 5 - filters and sorts FaultFilterProxy by UserSeverity and translates proxy data() via map_to_source # pass 5/5CLI verification output (./files/lab_cli):
learner@ksa:~/gl-q01/lab-m05$ bat listing.txt File: listing.txt table_model: rows=1024 crit_proxy_rows=341 top_src=1021 top_code=E132Defective starter/ run (exit code 1): emits FAIL: diagnostics on the broken contract assertions.
step 8/15 · 8. explanation of observed behavior
Check 3 verifies row_count(true) == 0, !data(CheckState).valid, and UserCode/Foreground projections. Check 4 overflows a 3-row ring buffer, proving handle h0 invalidates to -1 while h1 (seq_id = 11) shifts cleanly from row 1 to row 0 and clears its crimson Foreground role upon set_acknowledged(0, true). Check 5 configures FaultFilterProxy (min_severity = 1, descending sort) and verifies map_to_source(0) == 4 (E505).
step 9/15 · 9. experiment variations
- Near transfer: Add a
CommandUndoStackwrapper aroundFaultTableModel::set_acknowledged(row, acked)supportingacknowledge_all_critical()as one macro command whoseundo()restores only previously unacknowledged rows. - Far transfer: Replace
std::vector::erase(begin())(O(capacity)memmove) with a power-of-two circular index(m_head + logical_row) & maskso50,000-row ring eviction runs inO(1)array writes.
step 10/15 · 10. performance measurements
Recorded host benchmark (./files/lab_cli --bench, CLOCK_MONOTONIC, warmup=5, N=40 batches):
learner@ksa:~/gl-q01/lab-m05$ bat listing.txt File: listing.txt bench_proxy_refresh_ns: p50=36641 p95=46302 p99=49863 (sink=1136250)The CLOCK_MONOTONIC benchmark measures host C++20 append_fault + 1,024-row FaultFilterProxy sort/filter rebuild latency (nanoseconds per cycle in host DRAM). On target Cortex-A7 hardware, incremental proxy maintenance via beginInsertRows avoids full std::stable_sort passes on every incoming frame.
step 11/15 · 11. troubleshooting
| Symptom | Root cause | Fix |
|---|---|---|
| Check 3 fails with phantom checkbox error | Starter code lets DataRole::CheckState fall into the DisplayRole switch | Restore if (role != DataRole::Display) return {}; immediately before switch (col) |
| Check 4 fails with h1=1 instead of h1=0 | FIFO eviction erased row 0 without decrementing persistent handles r > 0 | Decrement --r for every persistent handle with r > 0 when row 0 is erased |
| Check 5 fails with text=E101 instead of E505 | FaultFilterProxy::data indexed m_src with proxy_row (0) instead of map_to_source(0) (4) | Set const int src_row = map_to_source(proxy_row); before querying m_src |
step 12/15 · 12. guided questions
- Why does FaultFilterProxy sort using DataRole::UserSeverity integers rather than comparing DataRole::Display strings?
- *Hint 1:* Consider both alphabetical order ('CRITICAL' vs 'INFO' vs 'WARN') and UI translations.
- *Hint 2:* Alphabetical string sort orders 'CRITICAL' < 'INFO' < 'WARN' incorrectly and breaks completely under localization, whereas integer UserSeverity (2 > 1 > 0) is fast and locale-invariant.
- Why did FaultFilterProxy::refresh() choose std::stable_sort instead of std::sort?
- *Hint 1:* Think about ties when multiple alarms share the same severity and timestamp.
- *Hint 2:* std::stable_sort preserves relative arrival order among equal-key elements deterministically across refreshes, preventing tied rows from jittering visually on screen.
step 13/15 · 13. independent challenges
Upgrade FaultTableModel from std::vector::erase(begin()) to a true O(1) ring buffer (m_head, m_count) and compare ./files/lab_cli --bench p50 / p99 latencies at capacity = 32,768.
step 14/15 · 14. solutions and explanations
Strict DisplayRole gating, pre-eviction persistent-handle shifting, and map_to_source coordinate translation keep bounded live telemetry views fast, accurate, and selection-safe.
step 15/15 · run verification and inspect output
Run the verification command and confirm the output matches the expected contract.
learner@ksa:~/gl-q01/lab-m05./files/lab_clilearner@ksa:~/gl-q01/lab-m05table_model: rows=1024 crit_proxy_rows=341 top_src=1021 top_code=E132Progressive hints & solution
hint 1
Compare starter/Makefile against the module lessons. Focus first on: Builds libcore.a and lab_cli cleanly with -std=c++20 -Wall -Wextra -Werror -pedantic.
hint 2
Compile with -std=c11 -Wall -Wextra -Werror, check every system-call return value before touching output buffers, and verify cleanup on error paths.
solution
warning: Reveal the reference solution files? Running ./check.sh against your own attempt first builds stronger retention.
solution: files/Makefile
learner@ksa:~/gl-q01/lab-m05$ bat files/Makefile File: files/Makefile CC_BIN = g++ AR = ar CFLAGS_ALL = -std=c++20 -Wall -Wextra -Werror -pedantic -O2 LDLIBS = all: libcore.a lab_cli libcore.a: core.o $(AR) rcs $@ $^ lab_cli: cli.o libcore.a $(CC_BIN) $(CFLAGS_ALL) cli.o libcore.a $(LDLIBS) -o $@ %.o: %.cpp core.hpp $(CC_BIN) $(CFLAGS_ALL) -c $< -o $@ .PHONY: all clean clean: rm -f core.o cli.o libcore.a lab_clisolution: files/cli.cpp
learner@ksa:~/gl-q01/lab-m05$ bat files/cli.cpp File: files/cli.cpp #include "core.hpp" #include <algorithm> #include <array> #include <cstdint> #include <cstdio> #include <cstring> #include <ctime> static std::uint64_t now_ns() noexcept { timespec ts{}; clock_gettime(CLOCK_MONOTONIC, &ts); return static_cast<std::uint64_t>(ts.tv_sec) * 1000000000ULL + static_cast<std::uint64_t>(ts.tv_nsec); } int main(int argc, char **argv) { hmi::FaultTableModel model(1024); for (std::uint64_t i = 1; i <= 1024; ++i) { model.append_fault({i, static_cast<int>(100 + (i % 90)), static_cast<int>(i % 3), false, "spindle"}); } hmi::FaultFilterProxy proxy(model); if (argc > 1 && std::strcmp(argv[1], "--bench") == 0) { constexpr int kWarmup = 5; constexpr int kSamples = 40; constexpr int kBatch = 50; std::array<std::uint64_t, kSamples> ns{}; volatile std::int64_t sink = 0; for (int i = 0; i < kWarmup + kSamples; ++i) { const std::uint64_t t0 = now_ns(); for (int r = 0; r < kBatch; ++r) { model.append_fault({2000ULL + static_cast<std::uint64_t>(r), 505, 2, false, "servo"}); proxy.configure(1, true); sink += proxy.data(0, 0, hmi::DataRole::UserCode).number; } const std::uint64_t dt = (now_ns() - t0) / kBatch; if (i >= kWarmup) { ns[static_cast<std::size_t>(i - kWarmup)] = dt; } } std::sort(ns.begin(), ns.end()); std::printf("bench_proxy_refresh_ns: p50=%llu p95=%llu p99=%llu (sink=%lld)\n", static_cast<unsigned long long>(ns[20]), static_cast<unsigned long long>(ns[37]), static_cast<unsigned long long>(ns[39]), static_cast<long long>(sink)); return 0; } proxy.configure(2, true); // Critical only, newest seq_id first const int top_src = proxy.map_to_source(0); std::printf("table_model: rows=%d crit_proxy_rows=%d top_src=%d top_code=%s\n", model.row_count(), proxy.row_count(), top_src, proxy.data(0, 1, hmi::DataRole::Display).text.c_str()); return (model.row_count() == 1024 && proxy.row_count() > 0 && top_src >= 0) ? 0 : 1; }solution: files/core.cpp
learner@ksa:~/gl-q01/lab-m05$ bat files/core.cpp File: files/core.cpp #include "core.hpp" #include <algorithm> namespace hmi { FaultTableModel::FaultTableModel(std::size_t capacity) : m_capacity(std::max<std::size_t>(1U, capacity)) {} int FaultTableModel::row_count(bool valid_parent) const noexcept { return valid_parent ? 0 : static_cast<int>(m_rows.size()); } int FaultTableModel::column_count(bool valid_parent) const noexcept { return valid_parent ? 0 : 4; } RoleValue FaultTableModel::data(int row, int col, DataRole role) const { if (row < 0 || row >= row_count() || col < 0 || col >= column_count()) { return {}; } const FaultItem &f = m_rows[static_cast<std::size_t>(row)]; if (role == DataRole::UserSeverity) { return {true, f.severity, {}}; } if (role == DataRole::UserCode) { return {true, f.code, {}}; } if (role == DataRole::Foreground) { if (f.severity >= 2 && !f.acknowledged) { return {true, 0xDC2626, {}}; } return {}; } if (role != DataRole::Display) { return {}; } switch (col) { case 0: return {true, static_cast<std::int64_t>(f.seq_id), std::to_string(f.seq_id)}; case 1: return {true, f.code, "E" + std::to_string(f.code)}; case 2: return {true, f.severity, f.severity == 2 ? "CRITICAL" : (f.severity == 1 ? "WARN" : "INFO")}; case 3: return {true, f.acknowledged ? 1 : 0, f.subsystem + (f.acknowledged ? " [ACK]" : "")}; default: return {}; } } int FaultTableModel::register_persistent(int initial_row) { if (initial_row < 0 || initial_row >= row_count()) { initial_row = -1; } const int h = static_cast<int>(m_persistent.size()); m_persistent.push_back(initial_row); return h; } int FaultTableModel::persistent_row(int handle) const noexcept { if (handle < 0 || handle >= static_cast<int>(m_persistent.size())) { return -1; } return m_persistent[static_cast<std::size_t>(handle)]; } void FaultTableModel::append_fault(FaultItem item) { if (m_rows.size() == m_capacity) { m_rows.erase(m_rows.begin()); for (int &r : m_persistent) { if (r == 0) { r = -1; } else if (r > 0) { --r; } } } m_rows.push_back(std::move(item)); } bool FaultTableModel::set_acknowledged(int row, bool acked) noexcept { if (row < 0 || row >= row_count()) { return false; } m_rows[static_cast<std::size_t>(row)].acknowledged = acked; return true; } void FaultFilterProxy::configure(int min_severity, bool sort_severity_desc) { m_min_sev = min_severity; m_sort_desc = sort_severity_desc; refresh(); } void FaultFilterProxy::refresh() { m_map.clear(); const int n = m_src.row_count(); for (int r = 0; r < n; ++r) { if (m_src.data(r, 0, DataRole::UserSeverity).number >= m_min_sev) { m_map.push_back(r); } } if (m_sort_desc) { std::stable_sort(m_map.begin(), m_map.end(), [this](int a, int b) { const auto sa = m_src.data(a, 0, DataRole::UserSeverity).number; const auto sb = m_src.data(b, 0, DataRole::UserSeverity).number; if (sa != sb) { return sa > sb; } return m_src.raw_at(a).seq_id > m_src.raw_at(b).seq_id; }); } } int FaultFilterProxy::map_to_source(int proxy_row) const noexcept { if (proxy_row < 0 || proxy_row >= row_count()) { return -1; } return m_map[static_cast<std::size_t>(proxy_row)]; } int FaultFilterProxy::map_from_source(int source_row) const noexcept { for (int i = 0; i < row_count(); ++i) { if (m_map[static_cast<std::size_t>(i)] == source_row) { return i; } } return -1; } RoleValue FaultFilterProxy::data(int proxy_row, int col, DataRole role) const { const int src_row = map_to_source(proxy_row); if (src_row < 0) { return {}; } return m_src.data(src_row, col, role); } } // namespace hmisolution: files/core.hpp
learner@ksa:~/gl-q01/lab-m05$ bat files/core.hpp File: files/core.hpp #pragma once #include <cstddef> #include <cstdint> #include <string> #include <vector> namespace hmi { enum class DataRole { Display, Foreground, CheckState, UserSeverity, UserCode }; struct RoleValue { bool valid = false; std::int64_t number = 0; std::string text; }; struct FaultItem { std::uint64_t seq_id = 0; int code = 0; int severity = 0; // 0=Info, 1=Warn, 2=Critical bool acknowledged = false; std::string subsystem; }; class FaultTableModel { public: explicit FaultTableModel(std::size_t capacity); int row_count(bool valid_parent = false) const noexcept; int column_count(bool valid_parent = false) const noexcept; RoleValue data(int row, int col, DataRole role) const; int register_persistent(int initial_row); int persistent_row(int handle) const noexcept; void append_fault(FaultItem item); bool set_acknowledged(int row, bool acked) noexcept; const FaultItem &raw_at(int row) const { return m_rows[static_cast<std::size_t>(row)]; } private: std::size_t m_capacity; std::vector<FaultItem> m_rows; std::vector<int> m_persistent; }; class FaultFilterProxy { public: explicit FaultFilterProxy(const FaultTableModel &source) noexcept : m_src(source) {} void configure(int min_severity, bool sort_severity_desc); void refresh(); int row_count() const noexcept { return static_cast<int>(m_map.size()); } int map_to_source(int proxy_row) const noexcept; int map_from_source(int source_row) const noexcept; RoleValue data(int proxy_row, int col, DataRole role) const; private: const FaultTableModel &m_src; int m_min_sev = 0; bool m_sort_desc = false; std::vector<int> m_map; }; } // namespace hmiRun the check
From labs/lab_m05_table_model_roles/, run ./check.sh. A complete solution emits TAP version 13 with every test point marked ok and exits 0:
learner@ksa:~/gl-q01/lab-m05./check.shTAP version 131..5ok 1 - builds libcore.a and lab_cli cleanly with -std=c++20 -Wall -Wextra -Werror -pedanticok 2 - archives core.o into libcore.a and links independent TAP verifierok 3 - enforces flat parent=0 guard, typed UserCode/Foreground roles, and empty unhandled CheckStateRoleok 4 - evicts oldest FIFO row at capacity, shifts persistent selections, and updates ack state in placeok 5 - filters and sorts FaultFilterProxy by UserSeverity and translates proxy data() via map_to_source# pass 5/5On an incomplete starter tree, ./check.sh exits non-zero and marks the unmet assertion not ok with an indented YAML diagnostic block:
learner@ksa:~/gl-q01/lab-m05./check.shTAP version 131..5ok 1 - builds libcore.a and lab_cli cleanly with -std=c++20 -Wall -Wextra -Werror -pedanticok 2 - archives core.o into libcore.a and links independent TAP verifiernot ok 3 - enforces flat parent=0 guard, typed UserCode/Foreground roles, and empty unhandled CheckStateRoleFAIL: return empty RoleValue{} for unhandled roles including DataRole::CheckState in FaultTableModel::data() --- message: 'got CheckStateRole returned valid RoleValue (phantom checkbox)' hint: 'return empty RoleValue{} for unhandled roles including DataRole::CheckState in FaultTableModel::data()' ...not ok 4 - evicts oldest FIFO row at capacity, shifts persistent selections, and updates ack state in placeFAIL: decrement surviving persistent row indices (r > 0 -> --r) and invalidate r == 0 -> -1 on ring eviction --- message: 'got FIFO eviction persistent shift failed: h0=-1 h1=1' hint: 'decrement surviving persistent row indices (r > 0 -> --r) and invalidate r == 0 -> -1 on ring eviction' ...not ok 5 - filters and sorts FaultFilterProxy by UserSeverity and translates proxy data() via map_to_sourceFAIL: map proxy_row through map_to_source(proxy_row) inside FaultFilterProxy::data() instead of raw proxy_row --- message: 'got proxy filter/sort/map failed: count=4 top_src=4 text=E101' hint: 'map proxy_row through map_to_source(proxy_row) inside FaultFilterProxy::data() instead of raw proxy_row' ...# pass 2/5note: How to read TAP diagnostics: 1..N states the total test plan; each ok k confirms a passed check; not ok k pinpoints the failing check alongside message: (what went wrong) and hint: (where to look next) between --- and ....
inspect harness: check.sh
learner@ksa:~/gl-q01/lab-m05$ bat check.sh File: check.sh #!/bin/sh set -u cd "$(dirname "$0")" || exit 1 PASS=0 WORK=$(mktemp -d "${TMPDIR:-/tmp}/ksa_lab.XXXXXX") || exit 1 trap 'rm -rf "$WORK"' EXIT HUP INT TERM cp -R files "$WORK/files" printf 'TAP version 13\n1..5\n' if make -B -C "$WORK/files" >"$WORK/build.log" 2>&1; then printf 'ok 1 - builds libcore.a and lab_cli cleanly with -std=c++20 -Wall -Wextra -Werror -pedantic\n' PASS=$((PASS + 1)) else printf 'not ok 1 - builds libcore.a and lab_cli cleanly with -std=c++20 -Wall -Wextra -Werror -pedantic\n' printf 'FAIL: fix compiler warnings or errors in files/core.cpp\n' fi if [ -f "$WORK/files/libcore.a" ] && g++ -std=c++20 -Wall -Wextra -Werror -pedantic -O2 -I"$WORK/files" tests.cpp "$WORK/files/libcore.a" -o "$WORK/verify" >>"$WORK/build.log" 2>&1; then printf 'ok 2 - archives core.o into libcore.a and links independent TAP verifier\n' PASS=$((PASS + 1)) else printf 'not ok 2 - archives core.o into libcore.a and links independent TAP verifier\n' printf 'FAIL: preserve public declarations in files/core.hpp so tests.cpp links\n' fi if [ -x "$WORK/verify" ] && OUT=$("$WORK/verify" 1 2>&1); then printf 'ok 3 - enforces flat parent=0 guard, typed UserCode/Foreground roles, and empty unhandled CheckStateRole\n' PASS=$((PASS + 1)) else printf 'not ok 3 - enforces flat parent=0 guard, typed UserCode/Foreground roles, and empty unhandled CheckStateRole\n' printf 'FAIL: return empty RoleValue{} for unhandled roles including DataRole::CheckState in FaultTableModel::data()\n' printf " ---\n message: 'got %s'\n hint: 'return empty RoleValue{} for unhandled roles including DataRole::CheckState in FaultTableModel::data()'\n ...\n" "${OUT:-build_failed}" fi if [ -x "$WORK/verify" ] && OUT=$("$WORK/verify" 2 2>&1); then printf 'ok 4 - evicts oldest FIFO row at capacity, shifts persistent selections, and updates ack state in place\n' PASS=$((PASS + 1)) else printf 'not ok 4 - evicts oldest FIFO row at capacity, shifts persistent selections, and updates ack state in place\n' printf 'FAIL: decrement surviving persistent row indices (r > 0 -> --r) and invalidate r == 0 -> -1 on ring eviction\n' printf " ---\n message: 'got %s'\n hint: 'decrement surviving persistent row indices (r > 0 -> --r) and invalidate r == 0 -> -1 on ring eviction'\n ...\n" "${OUT:-build_failed}" fi if [ -x "$WORK/verify" ] && OUT=$("$WORK/verify" 3 2>&1); then printf 'ok 5 - filters and sorts FaultFilterProxy by UserSeverity and translates proxy data() via map_to_source\n' PASS=$((PASS + 1)) else printf 'not ok 5 - filters and sorts FaultFilterProxy by UserSeverity and translates proxy data() via map_to_source\n' printf 'FAIL: map proxy_row through map_to_source(proxy_row) inside FaultFilterProxy::data() instead of raw proxy_row\n' printf " ---\n message: 'got %s'\n hint: 'map proxy_row through map_to_source(proxy_row) inside FaultFilterProxy::data() instead of raw proxy_row'\n ...\n" "${OUT:-build_failed}" fi printf '# pass %d/5\n' "$PASS" [ "$PASS" -eq 5 ]Definition of done
Verification checklist
status: 0/3 checks verified
Troubleshooting
Compiler fails under -Werror on sign conversion or unused args
Use exact POSIX return types (ssize_t for read()/write(), size_t for byte counts) rather than relaxing flags.
./check.sh reports missing files in files/
Invoke ./check.sh from inside labs/lab_m05_table_model_roles/ so relative paths to files/ resolve directly.