Line data Source code
1 : /* Loosely-coupled notifications via the Publish-Subscribe pattern.
2 : Copyright (C) 2025 Free Software Foundation, Inc.
3 : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #define INCLUDE_LIST
22 : #define INCLUDE_STRING
23 : #include "config.h"
24 :
25 : #include "system.h"
26 : #include "coretypes.h"
27 :
28 : #include "pub-sub.h"
29 :
30 :
31 : #if CHECKING_P
32 :
33 : #include "selftest.h"
34 :
35 : namespace selftest {
36 :
37 : /* Selftests. */
38 :
39 : // A topic for use in selftests
40 :
41 : namespace snafu {
42 :
43 : struct paper_jam {};
44 :
45 : struct out_of_paper
46 : {
47 : int tray;
48 : };
49 :
50 8 : struct ink_low
51 : {
52 : std::string color;
53 : };
54 :
55 8 : struct subscriber
56 : {
57 : virtual void on_message (const paper_jam &m) = 0;
58 : virtual void on_message (const out_of_paper &m) = 0;
59 : virtual void on_message (const ink_low &m) = 0;
60 : };
61 :
62 : } // namespace snafu
63 :
64 : static void
65 4 : test_example ()
66 : {
67 32 : struct logger : public snafu::subscriber
68 : {
69 8 : void on_message (const snafu::paper_jam &) final override
70 : {
71 8 : m_log += "paper jam\n";
72 8 : }
73 8 : void on_message (const snafu::out_of_paper &m) final override
74 : {
75 24 : m_log += "out of paper (tray " + std::to_string (m.tray) + ")\n";
76 8 : }
77 8 : void on_message (const snafu::ink_low &m) final override
78 : {
79 24 : m_log += "ink low: " + m.color + "\n";
80 8 : }
81 :
82 : std::string m_log;
83 : };
84 :
85 4 : pub_sub::channel<snafu::subscriber> printer_a;
86 4 : pub_sub::channel<snafu::subscriber> printer_b;
87 4 : pub_sub::channel<snafu::subscriber> printer_c;
88 :
89 : // No subscribers yet
90 4 : ASSERT_EQ (printer_a.get_if_active (), nullptr);
91 4 : ASSERT_EQ (printer_b.get_if_active (), nullptr);
92 4 : ASSERT_EQ (printer_c.get_if_active (), nullptr);
93 :
94 : // Subscribers to individual channels
95 4 : logger log_a;
96 4 : logger log_b;
97 4 : logger log_c;
98 4 : printer_a.add_subscriber (log_a);
99 4 : printer_b.add_subscriber (log_b);
100 4 : printer_c.add_subscriber (log_c);
101 :
102 : // A subscriber to all channels
103 4 : logger log_all;
104 4 : printer_a.add_subscriber (log_all);
105 4 : printer_b.add_subscriber (log_all);
106 4 : printer_c.add_subscriber (log_all);
107 :
108 : // The channels now have subscribers
109 4 : ASSERT_EQ (printer_a.get_if_active (), &printer_a);
110 4 : ASSERT_EQ (printer_b.get_if_active (), &printer_b);
111 4 : ASSERT_EQ (printer_c.get_if_active (), &printer_c);
112 :
113 : // Publish a message to each channel
114 4 : printer_a.publish (snafu::paper_jam {});
115 4 : printer_b.publish (snafu::out_of_paper {1});
116 4 : printer_c.publish (snafu::ink_low {"cyan"});
117 :
118 : // Verify that the subscribers got the messages they were meant to
119 4 : ASSERT_EQ (log_a.m_log, "paper jam\n");
120 4 : ASSERT_EQ (log_b.m_log, "out of paper (tray 1)\n");
121 4 : ASSERT_EQ (log_c.m_log, "ink low: cyan\n");
122 4 : ASSERT_EQ (log_all.m_log,
123 : "paper jam\n"
124 : "out of paper (tray 1)\n"
125 : "ink low: cyan\n");
126 4 : }
127 :
128 : /* Run all of the selftests within this file. */
129 :
130 : void
131 4 : pub_sub_cc_tests ()
132 : {
133 4 : test_example ();
134 4 : }
135 :
136 : } // namespace selftest
137 :
138 : #endif /* #if CHECKING_P */
|