summaryrefslogtreecommitdiff
blob: 3ca8711b9f0a760d76dea4f310779f47b891d3f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
--- a/common/tool/tool_manager.cpp
+++ b/common/tool/tool_manager.cpp
@@ -532,11 +532,11 @@
                     if( st->cofunc )
                         st->Push();
 
+                    st->cofunc = new COROUTINE<int, const TOOL_EVENT&>( tr.second );
+
                     // as the state changes, the transition table has to be set up again
                     st->transitions.clear();
 
-                    st->cofunc = new COROUTINE<int, const TOOL_EVENT&>( tr.second );
-
                     // got match? Run the handler.
                     st->cofunc->Call( aEvent );
 
--- a/include/tool/coroutine.h
+++ b/include/tool/coroutine.h
@@ -27,10 +28,15 @@
 
 #include <cstdlib>
 
-#include <boost/context/fcontext.hpp>
 #include <boost/version.hpp>
+#include <type_traits>
 
-#include "delegate.h"
+#if BOOST_VERSION <= 106000
+#include <boost/context/fcontext.hpp>
+#else
+#include <boost/context/execution_context.hpp>
+#include <boost/context/protected_fixedsize_stack.hpp>
+#endif
 
 /**
  *  Class COROUNTINE.
@@ -53,13 +59,12 @@
  *  See coroutine_example.cpp for sample code.
  */
 
-template <class ReturnType, class ArgType>
+template <typename ReturnType, typename ArgType>
 class COROUTINE
 {
 public:
     COROUTINE() :
-        m_saved( NULL ), m_self( NULL ), m_stack( NULL ), m_stackSize( c_defaultStackSize ),
-        m_running( false )
+        COROUTINE( nullptr )
     {
     }
 
@@ -69,8 +74,7 @@
      */
     template <class T>
     COROUTINE( T* object, ReturnType(T::* ptr)( ArgType ) ) :
-        m_func( object, ptr ), m_self( NULL ), m_saved( NULL ), m_stack( NULL ),
-        m_stackSize( c_defaultStackSize ), m_running( false )
+        COROUTINE( std::bind( ptr, object, std::placeholders::_1 ) )
     {
     }
 
@@ -78,9 +82,15 @@
      * Constructor
      * Creates a coroutine from a delegate object
      */
-    COROUTINE( DELEGATE<ReturnType, ArgType> aEntry ) :
-        m_func( aEntry ), m_saved( NULL ), m_self( NULL ), m_stack( NULL ),
-        m_stackSize( c_defaultStackSize ), m_running( false )
+    COROUTINE( std::function<ReturnType(ArgType)> aEntry ) :
+        m_func( std::move( aEntry ) ),
+        m_running( false ),
+#if BOOST_VERSION <= 106000
+        m_stack( nullptr ),
+        m_stackSize( c_defaultStackSize ),
+#endif
+        m_caller( nullptr ),
+        m_callee( nullptr )
     {
         // Avoid not initialized members, and make static analysers quiet
         m_args = 0;
@@ -89,18 +99,26 @@
 
     ~COROUTINE()
     {
-        if( m_saved )
-            delete m_saved;
-
 #if BOOST_VERSION >= 105600
-        if( m_self )
-            delete m_self;
+        delete m_callee;
 #endif
 
+#if BOOST_VERSION <= 106000
+        delete m_caller;
+
         if( m_stack )
             free( m_stack );
+#endif
     }
 
+private:
+#if BOOST_VERSION <= 106000
+    using context_type = boost::context::fcontext_t;
+#else
+    using context_type = boost::context::execution_context<COROUTINE*>;
+#endif
+
+public:
     /**
      * Function Yield()
      *
@@ -110,7 +128,12 @@
      */
     void Yield()
     {
-        jump( m_self, m_saved, 0 );
+#if BOOST_VERSION <= 106000
+        jump( m_callee, m_caller, false );
+#else
+        auto result = (*m_caller)( this );
+        *m_caller = std::move( std::get<0>( result ) );
+#endif
     }
 
     /**
@@ -122,7 +145,11 @@
     void Yield( ReturnType& aRetVal )
     {
         m_retVal = aRetVal;
-        jump( m_self, m_saved, 0 );
+#if BOOST_VERSION <= 106000
+        jump( m_callee, m_caller, false );
+#else
+        m_caller( this );
+#endif
     }
 
     /**
@@ -130,9 +157,9 @@
      *
      * Defines the entry point for the coroutine, if not set in the constructor.
      */
-    void SetEntry( DELEGATE<ReturnType, ArgType> aEntry )
+    void SetEntry( std::function<ReturnType(ArgType)> aEntry )
     {
-        m_func = aEntry;
+        m_func = std::move( aEntry );
     }
 
     /* Function Call()
@@ -143,6 +170,10 @@
      */
     bool Call( ArgType aArgs )
     {
+        assert( m_callee == NULL );
+        assert( m_caller == NULL );
+
+#if BOOST_VERSION <= 106000
         // fixme: Clean up stack stuff. Add a guard
         m_stack = malloc( c_defaultStackSize );
 
@@ -151,22 +182,32 @@
 
         // correct the stack size
         m_stackSize -= ( (size_t) m_stack + m_stackSize - (size_t) sp );
-
-        assert( m_self == NULL );
-        assert( m_saved == NULL );
+#endif
 
         m_args = &aArgs;
-#if BOOST_VERSION >= 105600
-        m_self = new boost::context::fcontext_t();
-        *m_self = boost::context::make_fcontext( sp, m_stackSize, callerStub );
+
+#if BOOST_VERSION < 105600
+        m_callee = boost::context::make_fcontext( sp, m_stackSize, callerStub );
+#elif BOOST_VERSION <= 106000
+        m_callee = new context_type( boost::context::make_fcontext( sp, m_stackSize, callerStub ) );
 #else
-        m_self = boost::context::make_fcontext( sp, m_stackSize, callerStub );
+        m_callee = new context_type( std::allocator_arg_t(),
+                    boost::context::protected_fixedsize_stack( c_defaultStackSize ), &COROUTINE::callerStub );
+#endif
+
+#if BOOST_VERSION <= 106000
+        m_caller = new context_type();
 #endif
-        m_saved = new boost::context::fcontext_t();
 
         m_running = true;
+
         // off we go!
-        jump( m_saved, m_self, reinterpret_cast<intptr_t>( this ) );
+#if BOOST_VERSION <= 106000
+        jump( m_caller, m_callee, reinterpret_cast<intptr_t>( this ) );
+#else
+        auto result = (*m_callee)( this );
+        *m_callee = std::move( std::get<0>( result ) );
+#endif
         return m_running;
     }
 
@@ -179,7 +220,12 @@
      */
     bool Resume()
     {
-        jump( m_saved, m_self, 0 );
+#if BOOST_VERSION <= 106000
+        jump( m_caller, m_callee, false );
+#else
+        auto result = (*m_callee)( this );
+        *m_callee = std::move( std::get<0>( result ) );
+#endif
 
         return m_running;
     }
@@ -208,61 +254,66 @@
     static const int c_defaultStackSize = 2000000;    // fixme: make configurable
 
     /* real entry point of the coroutine */
+#if BOOST_VERSION <= 106000
     static void callerStub( intptr_t aData )
+#else
+    static context_type callerStub( context_type caller, COROUTINE* cor )
+#endif
     {
         // get pointer to self
+#if BOOST_VERSION <= 106000
         COROUTINE<ReturnType, ArgType>* cor = reinterpret_cast<COROUTINE<ReturnType, ArgType>*>( aData );
+#else
+        cor->m_caller = &caller;
+#endif
 
         // call the coroutine method
-        cor->m_retVal = cor->m_func( *cor->m_args );
+        cor->m_retVal = cor->m_func( *( cor->m_args ) );
         cor->m_running = false;
 
         // go back to wherever we came from.
-        jump( cor->m_self, cor->m_saved, 0 );    // reinterpret_cast<intptr_t>( this ));
+#if BOOST_VERSION <= 106000
+        jump( cor->m_callee, cor->m_caller, 0 );
+#else
+        return caller;
+#endif
     }
 
     ///> Wrapper for jump_fcontext to assure compatibility between different boost versions
-    static inline intptr_t jump(boost::context::fcontext_t* aOld, boost::context::fcontext_t* aNew,
+#if BOOST_VERSION <= 106000
+    static inline intptr_t jump( context_type* aOld, context_type* aNew,
                                 intptr_t aP, bool aPreserveFPU = true )
     {
-#if BOOST_VERSION >= 105600
-        return boost::context::jump_fcontext( aOld, *aNew, aP, aPreserveFPU );
-#else
+#if BOOST_VERSION < 105600
         return boost::context::jump_fcontext( aOld, aNew, aP, aPreserveFPU );
+#else
+        return boost::context::jump_fcontext( aOld, *aNew, aP, aPreserveFPU );
 #endif
     }
+#endif
 
-    template <typename T>
-    struct strip_ref
-    {
-        typedef T result;
-    };
+    std::function<ReturnType(ArgType)> m_func;
 
-    template <typename T>
-    struct strip_ref<T&>
-    {
-        typedef T result;
-    };
+    bool m_running;
 
-    DELEGATE<ReturnType, ArgType> m_func;
+#if BOOST_VERSION <= 106000
+    ///< coroutine stack
+    void* m_stack;
+
+    size_t m_stackSize;
+#endif
 
     ///< pointer to coroutine entry arguments. Stripped of references
     ///< to avoid compiler errors.
-    typename strip_ref<ArgType>::result* m_args;
+    typename std::remove_reference<ArgType>::type* m_args;
+
     ReturnType m_retVal;
 
     ///< saved caller context
-    boost::context::fcontext_t* m_saved;
+    context_type* m_caller;
 
     ///< saved coroutine context
-    boost::context::fcontext_t* m_self;
-
-    ///< coroutine stack
-    void* m_stack;
-
-    size_t m_stackSize;
-
-    bool m_running;
+    context_type* m_callee;
 };
 
 #endif
--- a/include/tool/tool_base.h
+++ b/include/tool/tool_base.h
@@ -31,7 +32,7 @@
 #include <tool/tool_event.h>
 #include <tool/tool_settings.h>
 
-#include <tool/delegate.h>
+#include <functional>
 
 class EDA_ITEM;
 class TOOL_MANAGER;
@@ -53,7 +54,9 @@
 
 /// Unique identifier for tools
 typedef int TOOL_ID;
-typedef DELEGATE<int, const TOOL_EVENT&> TOOL_STATE_FUNC;
+
+using TOOL_STATE_FUNC = std::function<int(const TOOL_EVENT&)>;
+
 
 /**
  * Class TOOL_BASE
--- a/include/tool/tool_interactive.h
+++ b/include/tool/tool_interactive.h
@@ -113,7 +114,7 @@
 void TOOL_INTERACTIVE::Go( int (T::* aStateFunc)( const TOOL_EVENT& ),
                            const TOOL_EVENT_LIST& aConditions )
 {
-    TOOL_STATE_FUNC sptr( static_cast<T*>( this ), aStateFunc );
+    TOOL_STATE_FUNC sptr = std::bind( aStateFunc, static_cast<T*>( this ), std::placeholders::_1 );
 
     goInternal( sptr, aConditions );
 }