Skip to content

Commit c957e58

Browse files
committed
tests/sample-test.cpp rework with two lists
1 parent 25e183e commit c957e58

File tree

1 file changed

+42
-25
lines changed

1 file changed

+42
-25
lines changed

tests/src/sample-test.cpp

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace os = micro_os_plus;
3131
#pragma clang diagnostic ignored "-Wc++98-compat"
3232
#endif
3333

34+
// A simple example with an object that can be linked into two lists.
3435
class child
3536
{
3637
public:
@@ -45,58 +46,74 @@ class child
4546
return name_;
4647
}
4748

48-
void
49-
unlink (void)
50-
{
51-
registry_links_.unlink ();
52-
}
53-
5449
protected:
5550
const char* name_;
5651

5752
public:
58-
// Intrusive node used to link this child to the registry list.
53+
// Intrusive nodes used to link this object to the lists.
5954
// Must be public.
60-
os::utils::double_list_links registry_links_;
55+
os::utils::double_list_links all_kids_links;
56+
os::utils::double_list_links school_kids_links;
6157
};
6258

63-
using children_list
64-
= os::utils::intrusive_list<child, decltype (child::registry_links_),
65-
&child::registry_links_>;
59+
// Type of a list of all kids.
60+
using all_kids_list
61+
= os::utils::intrusive_list<child, decltype (child::all_kids_links),
62+
&child::all_kids_links>;
63+
64+
// Type of a list of school kids.
65+
using school_kids_list
66+
= os::utils::intrusive_list<child, decltype (child::school_kids_links),
67+
&child::school_kids_links>;
6668

6769
int
6870
main ([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
6971
{
70-
// Create an empty registry.
71-
children_list children_registry;
72+
// Create the empty lists locally.
73+
all_kids_list all_kids_registry;
74+
school_kids_list school_kids_registry;
7275

7376
// Add several members.
7477
child mary{ "Mary" };
75-
children_registry.link_tail (mary);
78+
all_kids_registry.link_tail (mary);
7679

7780
child bob{ "Bob" };
78-
children_registry.link_tail (bob);
81+
all_kids_registry.link_tail (bob);
7982

8083
child sally{ "Sally" };
81-
children_registry.link_tail (sally);
84+
all_kids_registry.link_tail (sally);
85+
school_kids_registry.link_tail (sally);
86+
87+
child doug{ "Doug" };
88+
all_kids_registry.link_tail (doug);
89+
school_kids_registry.link_tail (doug);
8290

83-
// List them.
84-
for (auto&& p : children_registry)
91+
printf ("\nAll kids:\n");
92+
93+
// List all kids.
94+
for (auto&& p : all_kids_registry)
8595
{
86-
printf ("%s\n", p.name ());
96+
printf ("- %s\n", p.name ());
8797
}
8898

89-
printf ("\n");
99+
printf ("\nBob is gone...\n");
100+
101+
// Remove one of them from the all_kids list.
102+
bob.all_kids_links.unlink ();
90103

91-
// Remove one of them.
92-
bob.unlink ();
104+
// List the remaining ones in the list.
105+
for (auto&& p : all_kids_registry)
106+
{
107+
printf ("- %s\n", p.name ());
108+
}
93109

94-
// List the remaining ones.
95-
for (auto&& p : children_registry)
110+
printf ("\nSchool kids:\n");
111+
for (auto&& p : school_kids_registry)
96112
{
97-
printf ("%s\n", p.name ());
113+
printf ("- %s\n", p.name ());
98114
}
99115

116+
printf ("\nDone.\n");
100117
return 0;
101118
}
102119

0 commit comments

Comments
 (0)