Wiki » Historie » Version 1
Maximilian Seesslen, 27.11.2024 19:41
1 | 1 | Maximilian Seesslen | h1. Wiki |
---|---|---|---|
2 | |||
3 | Pending signals: |
||
4 | |||
5 | <pre><code class="cpp"> |
||
6 | #include <stdlib.h> |
||
7 | #include <stdint.h> |
||
8 | #include <stdio.h> |
||
9 | |||
10 | #include "/home/deseessm/wp/src/canswitch/liblepto/include/lepto/signal.h" |
||
11 | |||
12 | class CEventLoop |
||
13 | { |
||
14 | private: |
||
15 | static CEventLoop* m_first; |
||
16 | CEventLoop* m_next=nullptr; |
||
17 | |||
18 | public: |
||
19 | CEventLoop() |
||
20 | { |
||
21 | if(!m_first) |
||
22 | { |
||
23 | m_first=this; |
||
24 | } |
||
25 | else |
||
26 | { |
||
27 | CEventLoop* p=m_first; |
||
28 | while( p->m_next) |
||
29 | { |
||
30 | p=p->m_next; |
||
31 | } |
||
32 | p->m_next=this; |
||
33 | } |
||
34 | } |
||
35 | virtual void eventLoop() |
||
36 | { |
||
37 | printf("L %p\n", this); |
||
38 | } |
||
39 | static void allEventLoops() |
||
40 | { |
||
41 | CEventLoop* p=m_first; |
||
42 | while(p) |
||
43 | { |
||
44 | p->eventLoop(); |
||
45 | p=p->m_next; |
||
46 | } |
||
47 | } |
||
48 | }; |
||
49 | |||
50 | CEventLoop* CEventLoop::m_first = nullptr; |
||
51 | #if 0 |
||
52 | template<class... T> |
||
53 | struct Store1 |
||
54 | { |
||
55 | T...[0] t; |
||
56 | }; |
||
57 | |||
58 | template<typename... T> |
||
59 | class Store |
||
60 | { |
||
61 | //struct { T...[0] t; }data; |
||
62 | T...[0] t; |
||
63 | }; |
||
64 | |||
65 | //consteval |
||
66 | auto first_plus_last(auto... args) |
||
67 | { |
||
68 | return args...[0] + args...[sizeof...(args) - 1]; |
||
69 | } |
||
70 | |||
71 | static_assert(first_plus_last(5) == 10); |
||
72 | static_assert(first_plus_last(5, 4) == 9); |
||
73 | static_assert(first_plus_last(5, 6, 2) == 7); |
||
74 | #endif |
||
75 | |||
76 | |||
77 | class CPendingSignal |
||
78 | { |
||
79 | public: |
||
80 | CPendingSignal() |
||
81 | {}; |
||
82 | virtual void emitSignal(){}; |
||
83 | }; |
||
84 | |||
85 | template<typename T> |
||
86 | class CPendingSignal1: public CPendingSignal |
||
87 | { |
||
88 | CSignal<void, T> &m_signal; |
||
89 | T m_p1; |
||
90 | public: |
||
91 | CPendingSignal1(CSignal<void, T>& signal, T i1) |
||
92 | :m_signal(signal) |
||
93 | ,m_p1(i1) |
||
94 | { |
||
95 | } |
||
96 | virtual void emitSignal(); |
||
97 | }; |
||
98 | |||
99 | template<typename T1, typename T2> |
||
100 | class CPendingSignal2: public CPendingSignal |
||
101 | { |
||
102 | CSignal<void, T1, T2> &m_signal; |
||
103 | T1 m_p1; |
||
104 | T2 m_p2; |
||
105 | public: |
||
106 | constexpr CPendingSignal2(CSignal<void, T1, T2> &signal, T1 i1, T2 i2) |
||
107 | :m_signal(signal) |
||
108 | ,m_p1(i1) |
||
109 | ,m_p2(i2) |
||
110 | { |
||
111 | } |
||
112 | virtual void emitSignal() |
||
113 | { |
||
114 | m_signal.emitSignal(m_p1, m_p2); |
||
115 | } |
||
116 | }; |
||
117 | |||
118 | template<typename T1, typename T2, typename T3> |
||
119 | class CPendingSignal3: public CPendingSignal |
||
120 | { |
||
121 | CSignal<void, T1, T2, T3> &m_signal; |
||
122 | T1 m_p1; |
||
123 | T2 m_p2; |
||
124 | T3 m_p3; |
||
125 | public: |
||
126 | constexpr CPendingSignal3(CSignal<void, T1, T2, T3> &signal, T1 i1, T2 i2, T3 i3) |
||
127 | :m_signal(signal) |
||
128 | ,m_p1(i1) |
||
129 | ,m_p2(i2) |
||
130 | ,m_p3(i3) |
||
131 | { |
||
132 | } |
||
133 | virtual void emitSignal() |
||
134 | { |
||
135 | m_signal.emitSignal(m_p1, m_p2, m_p3); |
||
136 | } |
||
137 | }; |
||
138 | |||
139 | template<typename T> |
||
140 | CPendingSignal* XcreatePendingSignal(T p1) |
||
141 | { |
||
142 | return( new CPendingSignal1<T>(p1) ); |
||
143 | }; |
||
144 | |||
145 | template<typename T1, typename T2> |
||
146 | CPendingSignal* XcreatePendingSignal(CSignal<void, T1, T2> &sig, T1 p1, T2 p2) |
||
147 | { |
||
148 | return( new CPendingSignal2<T1, T2>(sig, p1, p2) ); |
||
149 | }; |
||
150 | |||
151 | template<typename T1, typename T2, typename T3> |
||
152 | CPendingSignal* XcreatePendingSignal(CSignal<void, T1, T2, T3> &sig, T1 p1, T2 p2, T3 p3) |
||
153 | { |
||
154 | return( new CPendingSignal3<T1, T2, T3>(sig, p1, p2, p3) ); |
||
155 | }; |
||
156 | |||
157 | enum EColor{ |
||
158 | white, black, grey, green, |
||
159 | }; |
||
160 | |||
161 | void show(int i1, int i2, EColor col) |
||
162 | { |
||
163 | printf("SIGNAL: %d/%d - %d\n", i1, i2, col); |
||
164 | } |
||
165 | |||
166 | int main( int argc, const char *argv[] ) |
||
167 | { |
||
168 | CEventLoop l1; |
||
169 | CEventLoop l2; |
||
170 | CEventLoop l3; |
||
171 | |||
172 | printf( "Size: %d\n", sizeof(l1) ); |
||
173 | printf( "Size ptr: %d\n", sizeof(argv[0]) ); |
||
174 | printf( "Size pend: %d\n", sizeof(CPendingSignal2<int, int>) ); |
||
175 | |||
176 | CSignal<void, int, int, EColor> sig1; |
||
177 | sig1.connect( &show ); |
||
178 | CPendingSignal* ps1=XcreatePendingSignal<int, int, EColor>(sig1, 1,2, black); |
||
179 | //CPendingSignal* ps2=createPendingSignal(sig1, 123,456, 0); |
||
180 | CPendingSignal* ps2 = sig1.createPendingSignal(123,456, white); |
||
181 | |||
182 | printf("-------------------\n"); |
||
183 | ps1->emitSignal(); |
||
184 | ps2->emitSignal(); |
||
185 | |||
186 | CEventLoop::allEventLoops(); |
||
187 | return(0); |
||
188 | } |
||
189 | |||
190 | //---fin---------------------------------------------------------------------- |
||
191 | |||
192 | </code></pre> |