7static void Check(
bool condition) {
8 if (!condition)
throw std::runtime_error(
"Tree navigation test failed");
11static void CheckNavigation(
const TreeNode &node) {
12 for (std::size_t row = 0; row < node.
GetChildCount(); ++row) {
16 CheckNavigation(*child);
19 Check(!node.
GetChild(std::numeric_limits<std::size_t>::max()));
22static void CheckRejectedInsertion(
TreeNode &parent, std::unique_ptr<TreeNode> &owner) {
23 const TreeNode *original = owner.get();
24 bool rejected =
false;
27 }
catch (
const std::invalid_argument &) {
30 Check(rejected && owner.get() == original);
31 CheckNavigation(*owner);
35 static_assert(!std::is_copy_constructible<TreeNode>::value,
"Nodes have exclusive ownership");
36 static_assert(!std::is_copy_assignable<TreeNode>::value,
"Nodes have exclusive ownership");
37 static_assert(!std::is_move_constructible<TreeNode>::value,
"Node addresses must be stable");
38 static_assert(!std::is_move_assignable<TreeNode>::value,
"Node addresses must be stable");
46 signal.
name =
"Value";
47 frame.signals.push_back(signal);
48 signal.
name =
"Other";
49 frame.signals.push_back(signal);
50 database.
frames.push_back(frame);
52 database.
frames.push_back(frame);
54 Check(root->GetParent() ==
nullptr && root->GetRow() == 0);
55 Check(!root->GetFrameKey() && !root->GetSignalKey());
56 root->AddChild(
nullptr);
57 Check(root->GetChildCount() == 2 && !root->GetChild(2));
58 Check(!root->GetChild(std::numeric_limits<std::size_t>::max()));
59 CheckNavigation(*root);
63 const FrameKey standard(123U,
false);
69 Check(root->GetChild(1)->GetFrameKey() == extended);
70 Check(root->GetChild(1)->GetChild(0)->GetSignalKey() ==
SignalKey(extended,
"Value"));
73 Check(!detached.GetFrameKey() && !detached.GetSignalKey());
74 Check(!detached.GetParent() && detached.GetRow() == 0);
76 Check(detachedFrame.GetFrameKey() == extended && !detachedFrame.GetSignalKey());
80 database.
frames.push_back(frame);
82 Check(replacement->GetChild(0)->GetFrameKey() == root->GetChild(1)->GetFrameKey());
86 for (std::size_t index = 0; index < 64; ++index) {
87 root->AddChild(std::make_unique<TreeNode>(frame));
88 root->GetChild(0)->AddChild(std::make_unique<TreeNode>(signal));
90 Check(root->GetChild(0) == firstFrame && firstFrame->
GetChild(0) == firstSignal);
91 CheckNavigation(*root);
92 CheckRejectedInsertion(*root, root);
93 CheckRejectedInsertion(*root->GetChild(0)->GetChild(0), root);
95 const TreeNode *original = root.get();
96 std::unique_ptr<TreeNode> transferred = std::move(root);
97 Check(!root && transferred.get() == original);
98 CheckNavigation(*transferred);
Builds a simple tree from parsed DBC database.
std::unique_ptr< TreeNode > Build(const DbcDatabase &database) const
Build tree representation of parsed DBC data.
Tree node for later use in model/view or other hierarchy consumers.
std::optional< SignalKey > GetSignalKey() const
const TreeNode * GetChild(std::size_t index) const
Get child by index.
const TreeNode * GetParent() const
std::size_t GetChildCount() const
Get child count.
std::size_t GetRow() const
std::optional< FrameKey > GetFrameKey() const
void AddChild(std::unique_ptr< TreeNode > &&child)
Add child node.
int main(int argc, char *argv[])
Parse a DBC file and demonstrate metadata lookup and CAN decoding.
Parsed DBC content stored in a simple internal form.
std::vector< FrameInfo > frames
Describes one CAN frame from a DBC file.
Stable frame identifier: normalized CAN ID and frame type.
Describes one signal inside a DBC frame.