flowcontrol.h
1 /********************************************************************************
2  * FARSA *
3  * Copyright (C) 2007-2012 *
4  * Gianluca Massera <emmegian@yahoo.it> *
5  * Stefano Nolfi <stefano.nolfi@istc.cnr.it> *
6  * Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
21  ********************************************************************************/
22 
23 #ifndef FLOWCONTROL_H
24 #define FLOWCONTROL_H
25 
26 #include "experimentsconfig.h"
27 
28 namespace farsa {
29 
36 class FARSA_EXPERIMENTS_TEMPLATE FlowController
37 {
38 public:
49  virtual bool stop() = 0;
50 
57  virtual void pause() = 0;
58 };
59 
66 class FARSA_EXPERIMENTS_TEMPLATE DummyFlowController : public FlowController
67 {
68 public:
76  virtual bool stop()
77  {
78  return false;
79  }
80 
86  virtual void pause()
87  {
88  }
89 };
90 
167 #if defined(__GNUC__) && defined(DEVELOPER_WARNINGS)
168  #warning forse in farsa 2 questa roba si può mettere nella classe Component che ha una struttura gerarchica, in modo da propagare automaticamente il flow controller ai figli. In tal caso la funzione flowControllerChanged può essere forse rimossa
169 #endif
170 class FARSA_EXPERIMENTS_TEMPLATE FlowControlled
171 {
172 public:
179  : m_dummyFlowController()
180  , m_flowController(&m_dummyFlowController)
181  {
182  }
183 
193  : m_dummyFlowController()
194  , m_flowController((flowController == NULL) ? &m_dummyFlowController : flowController)
195  {
196  }
197 
206  void setFlowController(FlowController* flowController)
207  {
208  if (flowController != m_flowController) {
209  if (flowController == NULL) {
210  m_flowController = &m_dummyFlowController;
211  } else {
212  m_flowController = flowController;
213  }
214 
215  flowControllerChanged(m_flowController);
216  }
217  }
218 
219 protected:
225  bool stopFlow()
226  {
227  return m_flowController->stop();
228  }
229 
236  void pauseFlow()
237  {
238  m_flowController->pause();
239  }
240 
248  virtual void flowControllerChanged(FlowController* flowController)
249  {
250  Q_UNUSED(flowController)
251  }
252 
253 private:
258  DummyFlowController m_dummyFlowController;
259 
266  FlowController* m_flowController;
267 };
268 
269 }
270 
271 #endif
The base for classes that have a controllable flow of execution.
Definition: flowcontrol.h:170
FlowControlled()
Constructor.
Definition: flowcontrol.h:178
FlowControlled(FlowController *flowController)
Constructor.
Definition: flowcontrol.h:192
This file contains the common type defitions used on the whole framework.
A dummy flow controller.
Definition: flowcontrol.h:66
virtual bool stop()
The function to check if execution should be terminated as soon as possible.
Definition: flowcontrol.h:76
void setFlowController(FlowController *flowController)
Sets the flow controller object to use.
Definition: flowcontrol.h:206
virtual void pause()
Performs a pause if needed.
Definition: flowcontrol.h:86
The interface for classes controlling the flow of execution.
Definition: flowcontrol.h:36
virtual void flowControllerChanged(FlowController *flowController)
The function called when the flow controller changes.
Definition: flowcontrol.h:248
void pauseFlow()
Performs a pause in the if needed.
Definition: flowcontrol.h:236
bool stopFlow()
Checks if execution should stop as soon as possible.
Definition: flowcontrol.h:225