ThreadWorker.hpp 2.43 KB
Newer Older
1 2 3 4 5 6 7 8
/*************************************************************************
*  Copyright (C) 2006 by Janek Kozicki                                   *
*  cosurgi@berlios.de                                                    *
*                                                                        *
*  This program is free software; it is licensed under the terms of the  *
*  GNU General Public License v2 or later. See file LICENSE for details. *
*************************************************************************/

9
#pragma once
10

11 12
#include <boost/thread/mutex.hpp>
#include <boost/any.hpp>
13

14 15
namespace yade { // Cannot have #include directive inside.

16
class ThreadRunner;
17

18 19 20
/*! 
\brief	ThreadWorker contains information about tasks to be performed when
	the separate thread is executed.
21 22
 */

23
class ThreadWorker	//         perhaps simulation steps, or stage? as it is a single stage
Janek Kozicki's avatar
Janek Kozicki committed
24
			//         of the simulation, that consists of several steps
25
			// Update: it is more general now. simulation stages perhaps will be derived from this class
26
{
27
	private:
Janek Kozicki's avatar
Janek Kozicki committed
28
		/// You should check out ThreadRunner, it is used for execution control of this class
29 30 31
		friend class ThreadRunner;
		bool		m_should_terminate;
		bool		m_done;
Janek Kozicki's avatar
Janek Kozicki committed
32
		boost::mutex	m_mutex;
33
		boost::any	m_val;
Janek Kozicki's avatar
Janek Kozicki committed
34
		float		m_progress;
35
		std::string	m_status;
36 37 38
		void		callSingleAction();

	protected:
39
		void		setTerminate(bool);
40 41
		/// singleAction() can check whether someone asked for termination, and terminate if/when possible
		bool		shouldTerminate();
Janek Kozicki's avatar
Janek Kozicki committed
42
		/// if something must be returned, set the result using this method
43
		void		setReturnValue(boost::any);
Janek Kozicki's avatar
Janek Kozicki committed
44
		/// if you feel monitored for progress, you can set it here: a value between 0.0 and 1.0
Janek Kozicki's avatar
Janek Kozicki committed
45
		void		setProgress(float);
Janek Kozicki's avatar
Janek Kozicki committed
46
		/// if you feel being monitored for what currently is done, set the message here
47
		void		setStatus(std::string);
Janek Kozicki's avatar
Janek Kozicki committed
48
		/// derived classes must define this method, that's what is executed in separate thread
49 50 51
		virtual void	singleAction() = 0;

	public:
Janek Kozicki's avatar
Janek Kozicki committed
52
		ThreadWorker() : m_should_terminate(false), m_done(false), m_progress(0) {};
53 54
		virtual		~ThreadWorker() {};

Janek Kozicki's avatar
Janek Kozicki committed
55
		/// Returns a value between 0.0 and 1.0. Useful for updating a progress bar.
56
		float		progress(); // get_progress ? (pick a naming convention, efngh)
Janek Kozicki's avatar
Janek Kozicki committed
57
		/// You can display a message in GUI about what is the current work status
58
		std::string	getStatus();
Janek Kozicki's avatar
Janek Kozicki committed
59
		/// Check whether execution is finished,
60
		bool		done();
Janek Kozicki's avatar
Janek Kozicki committed
61 62
		/// then get the result.
		boost::any	getReturnValue();
63 64
};

65
} // namespace yade
66