Cycles: svn merge -r37957:39132 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_function.h"
29 #include "util_string.h"
30 #include "util_thread.h"
31
32 CCL_NAMESPACE_BEGIN
33
34 class Progress {
35 public:
36         Progress()
37         {
38                 pass = 0;
39                 total_time = 0.0f;
40                 pass_time = 0.0f;
41                 status = "Initializing";
42                 substatus = "";
43                 update_cb = NULL;
44                 cancel = false;
45                 cancel_message = "";
46                 cancel_cb = NULL;
47         }
48
49         Progress(Progress& progress)
50         {
51                 *this = progress;
52         }
53
54         Progress& operator=(Progress& progress)
55         {
56                 thread_scoped_lock lock(progress.progress_mutex);
57
58                 progress.get_pass(pass, total_time, pass_time);
59                 progress.get_status(status, substatus);
60
61                 return *this;
62         }
63
64         /* cancel */
65         void set_cancel(const string& cancel_message_)
66         {
67                 thread_scoped_lock lock(progress_mutex);
68                 cancel_message = cancel_message_;
69                 cancel = true;
70         }
71
72         bool get_cancel()
73         {
74                 if(!cancel && cancel_cb)
75                         cancel_cb();
76
77                 return cancel;
78         }
79
80         string get_cancel_message()
81         {
82                 thread_scoped_lock lock(progress_mutex);
83                 return cancel_message;
84         }
85
86         void set_cancel_callback(boost::function<void(void)> function)
87         {
88                 cancel_cb = function;
89         }
90
91         /* pass and timing information */
92
93         void set_pass(int pass_, double total_time_, double pass_time_)
94         {
95                 thread_scoped_lock lock(progress_mutex);
96
97                 pass = pass_;
98                 total_time = total_time_;
99                 pass_time = pass_time_;
100         }
101
102         void get_pass(int& pass_, double& total_time_, double& pass_time_)
103         {
104                 thread_scoped_lock lock(progress_mutex);
105
106                 pass_ = pass;
107                 total_time_ = total_time;
108                 pass_time_ = pass_time;
109         }
110
111         /* status messages */
112
113         void set_status(const string& status_, const string& substatus_ = "")
114         {
115                 {
116                         thread_scoped_lock lock(progress_mutex);
117                         status = status_;
118                         substatus = substatus_;
119                 }
120
121                 set_update();
122         }
123
124         void set_substatus(const string& substatus_)
125         {
126                 {
127                         thread_scoped_lock lock(progress_mutex);
128                         substatus = substatus_;
129                 }
130
131                 set_update();
132         }
133
134         void get_status(string& status_, string& substatus_)
135         {
136                 thread_scoped_lock lock(progress_mutex);
137                 status_ = status;
138                 substatus_ = substatus;
139         }
140
141         /* callback */
142
143         void set_update()
144         {
145                 if(update_cb)
146                         update_cb();
147         }
148
149         void set_update_callback(boost::function<void(void)> function)
150         {
151                 update_cb = function;
152         }
153
154 protected:
155         thread_mutex progress_mutex;
156         boost::function<void(void)> update_cb;
157         boost::function<void(void)> cancel_cb;
158
159         int pass;
160
161         double total_time;
162         double pass_time;
163
164         string status;
165         string substatus;
166
167         volatile bool cancel;
168         string cancel_message;
169 };
170
171 CCL_NAMESPACE_END
172
173 #endif /* __UTIL_PROGRESS_H__ */
174