Cycles: svn merge -r36495:36747 https://svn.blender.org/svnroot/bf-blender/trunk...
[blender-staging.git] / intern / cycles / util / util_progress.h
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #ifndef __UTIL_PROGRESS_H__
20 #define __UTIL_PROGRESS_H__
21
22 /* Progress
23  *
24  * Simple class to communicate progress status messages, timing information,
25  * update notifications from a job running in another thread. All methods
26  * except for the constructor/destructor are thread safe. */
27
28 #include "util_string.h"
29 #include "util_thread.h"
30
31 CCL_NAMESPACE_BEGIN
32
33 class Progress {
34 public:
35         Progress()
36         {
37                 pass = 0;
38                 total_time = 0.0f;
39                 pass_time = 0.0f;
40                 status = "Initializing";
41                 substatus = "";
42                 update_cb = NULL;
43                 cancel = false;
44                 cancel_message = "";
45                 cancel_cb = NULL;
46         }
47
48         Progress(Progress& progress)
49         {
50                 *this = progress;
51         }
52
53         Progress& operator=(Progress& progress)
54         {
55                 thread_scoped_lock lock(progress.progress_mutex);
56
57                 progress.get_pass(pass, total_time, pass_time);
58                 progress.get_status(status, substatus);
59
60                 return *this;
61         }
62
63         /* cancel */
64         void set_cancel(const string& cancel_message_)
65         {
66                 thread_scoped_lock lock(progress_mutex);
67                 cancel_message = cancel_message_;
68                 cancel = true;
69         }
70
71         bool get_cancel()
72         {
73                 if(!cancel && cancel_cb)
74                         cancel_cb();
75
76                 return cancel;
77         }
78
79         string get_cancel_message()
80         {
81                 thread_scoped_lock lock(progress_mutex);
82                 return cancel_message;
83         }
84
85         void set_cancel_callback(boost::function<void(void)> function)
86         {
87                 cancel_cb = function;
88         }
89
90         /* pass and timing information */
91
92         void set_pass(int pass_, double total_time_, double pass_time_)
93         {
94                 thread_scoped_lock lock(progress_mutex);
95
96                 pass = pass_;
97                 total_time = total_time_;
98                 pass_time = pass_time_;
99         }
100
101         void get_pass(int& pass_, double& total_time_, double& pass_time_)
102         {
103                 thread_scoped_lock lock(progress_mutex);
104
105                 pass_ = pass;
106                 total_time_ = total_time;
107                 pass_time_ = pass_time;
108         }
109
110         /* status messages */
111
112         void set_status(const string& status_, const string& substatus_ = "")
113         {
114                 {
115                         thread_scoped_lock lock(progress_mutex);
116                         status = status_;
117                         substatus = substatus_;
118                 }
119
120                 set_update();
121         }
122
123         void set_substatus(const string& substatus_)
124         {
125                 {
126                         thread_scoped_lock lock(progress_mutex);
127                         substatus = substatus_;
128                 }
129
130                 set_update();
131         }
132
133         void get_status(string& status_, string& substatus_)
134         {
135                 thread_scoped_lock lock(progress_mutex);
136                 status_ = status;
137                 substatus_ = substatus;
138         }
139
140         /* callback */
141
142         void set_update()
143         {
144                 if(update_cb)
145                         update_cb();
146         }
147
148         void set_update_callback(boost::function<void(void)> function)
149         {
150                 update_cb = function;
151         }
152
153 protected:
154         thread_mutex progress_mutex;
155         boost::function<void(void)> update_cb;
156         boost::function<void(void)> cancel_cb;
157
158         int pass;
159
160         double total_time;
161         double pass_time;
162
163         string status;
164         string substatus;
165
166         volatile bool cancel;
167         string cancel_message;
168 };
169
170 CCL_NAMESPACE_END
171
172 #endif /* __UTIL_PROGRESS_H__ */
173