Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
Progress.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 #include "Progress.h"
14 
15 // >>>>>> @Date:2013/11/18 <Add> A.Ozaki
16 //
17 #define _USE_INTERVAL_
18 
19 #define DRAW_INTERVAL ( 100 ) // unit:msec
20 #define FILL_INTERVAL ( 500 ) // unit:msec
21 #include <boost/thread.hpp>
22 //
23 // <<<<<< @Date:2013/11/18 <Add> A.Ozaki
24 
25 using namespace kome::core;
26 
27 
28 #include <crtdbg.h>
29 #ifdef _DEBUG
30  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
31  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
32 #endif // _DEBUG
33 
34 
35 
36 // constructor
37 Progress::Progress( const char* title ) {
38  m_start = 0;
39  m_end = 0;
40  m_pos = 0;
41  m_prevPos = 0;
42  m_relPos = 0;
43  m_title = NVL( title, "" );
44 
45 // >>>>>> @Date:2013/11/18 <Add> A.Ozaki
46 //
47  m_llPrevTime = 0;
48  m_strStatus.clear( );
49 //
50 // <<<<<< @Date:2013/11/18 <Add> A.Ozaki
51 
52  m_timers = new std::vector< std::pair< std::string, Timer* > >();
53 }
54 
55 // destructor
57  // delete sub progresses
58  for( unsigned int i = 0; i < m_subProgresses.size(); i++ ) {
59  delete m_subProgresses[ i ];
60  }
61  m_subProgresses.clear();
62 
63  // timer info
64  if( !m_title.empty() && m_start != m_end ) {
65  m_timer.stop();
66  LOG_INFO( FMT( "%s Time: %f sec", m_title.c_str(), m_timer.getTotalTime() ) );
67  }
68 
69  // delete timers
70  if( m_timers != NULL ) {
71  for( unsigned int i = 0; i < m_timers->size(); i++ ) {
72  delete ( *m_timers )[ i ].second;
73  }
74  delete m_timers;
75  }
76 }
77 
78 // set range
79 void Progress::setRange( const int start, const int end ) {
80 
81  // set members
82  m_start = start;
83  m_end = end;
84 
85  // start timer
86  m_timer.start();
87 
88  // set range
89  onSetRange( start, end );
90  setPosition( start );
91 
92  if( isStopped() ){ // @date 2013.09.26 <Mod> M.Izumi
93  return;
94  }
95 }
96 
97 // set position
98 void Progress::setPosition( const int pos, const bool bForced ) {
99 
100  // set members
101  m_prevPos = m_pos;
102  m_pos = pos;
103 
104 // >>>>>> @Date:2013/11/18 <Add> A.Ozaki
105 //
106 #ifdef _USE_INTERVAL_
107  if ( false == bForced )
108  {
109  long long llPassed = getcurrenttime( );
110  llPassed = llPassed - m_llPrevTime;
111  if ( DRAW_INTERVAL > llPassed )
112  {
113  return;
114  }
115  }
117 #endif
118 //
119 // <<<<<< @Date:2013/11/18 <Add> A.Ozaki
120 
121 
122  if( m_start == m_end ) {
123  m_relPos = 0;
124  }
125  else {
126  double d1 = (double)abs( m_pos - m_start );
127  double d2 = (double)abs( m_end - m_pos );
128  m_relPos = roundnum( d1 * 100.0 / ( d1 + d2 ) );
129  }
130 
131 // >>>>>> @Date:2013/11/18 <Add> A.Ozaki
132 //
133 #ifndef _USE_INTERVAL_
134  if( isStopped() ){ // @date 2013.09.26 <Mod> M.Izumi
135  return;
136  }
137  // set position
139 #else
140  if ( false == m_strStatus.empty( ) )
141  {
142  onSetStatus( m_strStatus.c_str( ), bForced );
143  m_strStatus.clear( );
144  }
145 
146  // set position
148 
149  if ( isStopped( ) )
150  {
151  return;
152  }
153 #endif
154  return;
155 //
156 // <<<<<< @Date:2013/11/18 <Add> A.Ozaki
157 }
158 
159 // set status
160 void Progress::setStatus( const char* status, const bool bForced ) {
161 // >>>>>> @Date:2013/11/18 <Add> A.Ozaki
162 //
163 #ifndef _USE_INTERVAL_
164  if( isStopped() ){ // @date 2013.09.26 <Mod> M.Izumi
165  return;
166  }
167  onSetStatus( status );
168 #else
169  if ( (const char *)NULL == status )
170  {
171  if ( false == m_strStatus.empty( ) )
172  {
173  onSetStatus( m_strStatus.c_str( ), bForced );
174  }
175  }
176  else
177  {
178  if ( false == bForced )
179  {
180  m_strStatus = NVL( status, "" );
181  }
182  else
183  {
184  onSetStatus( status, bForced );
185  }
186 
187  if ( isStopped( ) )
188  {
189  return;
190  }
191  }
192 #endif
193  return;
194 //
195 // <<<<<< @Date:2013/11/18 <Add> A.Ozaki
196 }
197 
198 // is stopped
200  bool ret = onIsStopped();
201 
202  return ret;
203 }
204 
205 // fill
207  // set range
208  if( m_start == 0 && m_end == 0 ) {
209  setRange( 0, 1 );
210  }
211 
212 // >>>>>> @Date:2013/11/18 <Add> A.Ozaki
213 //
214 #ifdef _USE_INTERVAL_
215  onFill( );
216 
217 #endif
218  // set end position
219  if ( m_start == 0 && m_end == 0 )
220  {
221  setPosition( 1 );
222  }
223  else
224  {
225  setPosition( m_end );
226  }
227 //
228 // <<<<<< @Date:2013/11/18 <Add> A.Ozaki
229  m_relPos = 100;
230 
231 // >>>>>> @Date:2013/11/18 <Add> A.Ozaki
232 //
233 #ifdef _USE_INTERVAL_
234  boost::this_thread::sleep( boost::posix_time::milliseconds( FILL_INTERVAL ) );
235 #endif
236 //
237 // <<<<<< @Date:2013/11/18 <Add> A.Ozaki
238 
239  // delete sub progresses
240  for( unsigned int i = 0; i < m_subProgresses.size(); i++ ) {
241  delete m_subProgresses[ i ];
242  }
243  m_subProgresses.clear();
244 }
245 
246 // get relative position
248  return m_relPos;
249 }
250 
251 // create timer
252 Timer* Progress::createTimer( const char* name ) {
253  // check the name
254  if( name == NULL || m_timers == NULL ) {
255  return NULL;
256  }
257 
258  // search
259  for( unsigned int i = 0; i < m_timers->size(); i++ ) {
260  if( ( *m_timers )[ i ].first.compare( name ) == 0 ) {
261  return ( *m_timers )[ i ].second;
262  }
263  }
264 
265  // new timer
266  m_timers->push_back( std::make_pair( name, new Timer() ) );
267 
268  return m_timers->back().second;
269 }
270 
271 // get the number of timers
273  if( m_timers == NULL ) {
274  return 0;
275  }
276 
277  return m_timers->size();
278 }
279 
280 // get timer name
281 const char* Progress::getTimerName( const unsigned int index ) {
282  if( m_timers == NULL || index >= m_timers->size() ) {
283  return NULL;
284  }
285 
286  return ( *m_timers )[ index ].first.c_str();
287 }
288 
289 // get timer
290 Timer* Progress::getTimer( const unsigned int index ) {
291  if( m_timers == NULL || index >= m_timers->size() ) {
292  return NULL;
293  }
294 
295  return ( *m_timers )[ index ].second;
296 }
297 
298 // creates sub progresses
299 void Progress::createSubProgresses( const unsigned int num ) {
300  // check the number
301  if( num == 0 ) {
302  return;
303  }
304  if( this == &( getIgnoringProgress() ) ) {
305  return;
306  }
307 
308  // create progresses
309  m_subProgresses.reserve( num );
310  for( unsigned int i = 0; i < num; i++ ) {
311  Progress* subProgress = new SubProgress( *this );
312  delete subProgress->m_timers;
313  subProgress->m_timers = m_timers;
314 
315  m_subProgresses.push_back( subProgress );
316  }
317 
318  // set range
319  setRange( 0, (int)m_subProgresses.size() * 100 );
320 }
321 
322 // get the number of sub progresses
324  if( this == &( getIgnoringProgress() ) ) {
325  return 0;
326  }
327  return m_subProgresses.size();
328 }
329 
330 // get sub progress
331 Progress* Progress::getSubProgress( const unsigned int idx ) {
332  if( this == &( getIgnoringProgress() ) ) {
333  return this;
334  }
335  if( idx >= m_subProgresses.size() ) {
336  return NULL;
337  }
338 
339  return m_subProgresses[ idx ];
340 }
341 
342 // get ignoring progress
344  static IgnoringProgress progress;
345 
346  return progress;
347 }
348 
349 
350 //
351 // IgnoringProgress
352 //
353 
354 // constructor
356 }
357 
358 // destructor
360 }
361 
362 // on set range
363 void IgnoringProgress::onSetRange( const int, const int ) {
364 }
365 
366 // on set position
367 void IgnoringProgress::onSetPosition( const int, const int ) {
368 }
369 
370 // on set statuc
371 void IgnoringProgress::onSetStatus( const char*, const bool bForced ) {
372 }
373 
374 // on is stopped
376  return false;
377 }
378 
379 // >>>>>> @Date:2013/11/25 <Add> A.Ozaki
380 //
381 // on fill
383 {
384  return;
385 }
386 //
387 // <<<<<< @Date:2013/11/25 <Add> A.Ozaki
388 
389 //
390 // Sub Progress
391 //
392 
393 // constructor
395  m_parent = &parent;
396 }
397 
398 // destructor
400  m_timers = NULL;
401 }
402 
403 // on set range
404 void SubProgress::onSetRange( const int start, const int end ) {
405 }
406 
407 // on set position
408 void SubProgress::onSetPosition( const int pos, const int prevPos ) {
409  // set parent position
410  int parentPos = 0;
411  for( unsigned int i = 0; i < m_parent->getNumberOfSubProgresses(); i++ ) {
412  // sub progress
413  SubProgress* progress = (SubProgress*)( m_parent->getSubProgress( i ) );
414 
415  parentPos += progress->getRelativePosition();
416  }
417  m_parent->setPosition( parentPos );
418 }
419 
420 // on set status
421 void SubProgress::onSetStatus( const char* status, const bool bForced ) {
422  m_parent->setStatus( status, bForced );
423 }
424 
425 // on is stopped
427  return m_parent->isStopped();
428 }
429 
430 // >>>>>> @Date:2013/11/25 <Add> A.Ozaki
431 //
432 // on fill
434 {
435  m_parent->setStatus( (char *)NULL, true );
436  return;
437 }
438 //
439 // <<<<<< @Date:2013/11/25 <Add> A.Ozaki
virtual void onSetPosition(const int pos, const int prevPos)
This method is called by setPosition method. (override method)
Definition: Progress.cpp:408
virtual ~SubProgress()
destructor
Definition: Progress.cpp:399
virtual void onSetStatus(const char *status, const bool bForced=false)
This method is called by setStatus method. (override method)
Definition: Progress.cpp:421
std::string m_title
Definition: Progress.h:62
static Progress & getIgnoringProgress()
gets progress object. But this object does nothing even if a method is called.
Definition: Progress.cpp:343
void fill()
sets end position
Definition: Progress.cpp:206
Progress * getSubProgress(const unsigned int idx)
gets subprogress
Definition: Progress.cpp:331
std::vector< std::pair< std::string, Timer * > > * m_timers
Definition: Progress.h:68
Progress object. Even if a method is called, it disgards it.
Definition: Progress.h:238
virtual void onFill(void)
This method is called by fill method. (override method)
Definition: Progress.cpp:433
virtual bool onIsStopped()=0
This method is called by isStopped method. (abstract method)
timer class
Definition: Timer.h:26
Timer * createTimer(const char *name)
creates timer
Definition: Progress.cpp:252
void start()
starts timer
Definition: Timer.cpp:46
bool isStopped()
judges whether it has to finish
Definition: Progress.cpp:199
interfaces of Progress class
std::vector< Progress * > m_subProgresses
Definition: Progress.h:48
int roundnum(const double v)
gets the closest integer to the argument
progress display abstract class
Definition: Progress.h:31
Progress(const char *title=NULL)
constructor
Definition: Progress.cpp:37
virtual bool onIsStopped()
This method is called by isStopped method. (override method)
Definition: Progress.cpp:426
void setPosition(const int pos, const bool bForced=false)
sets progress position
Definition: Progress.cpp:98
Timer * getTimer(const unsigned int index)
gets timer
Definition: Progress.cpp:290
virtual void onSetStatus(const char *, const bool bForced=false)
This method is called by setStatus method. (override method)
Definition: Progress.cpp:371
#define NVL(checkVal, replaceVal)
Definition: CoreMacros.h:99
int getRelativePosition()
gets relative position
Definition: Progress.cpp:247
std::string m_strStatus
Definition: Progress.h:115
#define NULL
Definition: CoreMacros.h:18
virtual ~Progress()
destructor
Definition: Progress.cpp:56
unsigned int getNumberOfSubProgresses()
gets the number of sub progresses
Definition: Progress.cpp:323
sub progress object.
Definition: Progress.h:295
virtual void onSetRange(const int start, const int end)
This method is called by setRange method. (override method)
Definition: Progress.cpp:404
double getTotalTime()
gets total time
Definition: Timer.cpp:83
IgnoringProgress()
constructor
Definition: Progress.cpp:355
unsigned int getNumberOfTimers()
gets the number of timers
Definition: Progress.cpp:272
virtual void onSetPosition(const int pos, const int prevPos)=0
This method is called by setPosition method. (abstract method)
long long m_llPrevTime
Definition: Progress.h:112
void createSubProgresses(const unsigned int num)
creates sub progresses
Definition: Progress.cpp:299
SubProgress(Progress &parent)
constructor
Definition: Progress.cpp:394
virtual void onFill(void)=0
This method is called by fill method. (abstract method)
virtual ~IgnoringProgress()
destructor
Definition: Progress.cpp:359
double stop()
stops timer
Definition: Timer.cpp:52
long long getcurrenttime()
gets current time in miliseconds
void setStatus(const char *status, const bool bForced=false)
sets status
Definition: Progress.cpp:160
const char * getTimerName(const unsigned int index)
gets timer name
Definition: Progress.cpp:281
virtual void onSetPosition(const int, const int)
This method is called by setPosition method. (override method)
Definition: Progress.cpp:367
virtual bool onIsStopped()
This method is called by isStopped method. (override method)
Definition: Progress.cpp:375
virtual void onSetStatus(const char *status, const bool bForced)=0
This method is called by setStatus method. (abstract method)
void setRange(const int start, const int end)
sets progress range
Definition: Progress.cpp:79
virtual void onFill(void)
This method is called by fill method. (override method)
Definition: Progress.cpp:382
virtual void onSetRange(const int start, const int end)=0
This method is called by setRange method. (abstract method)
virtual void onSetRange(const int, const int)
This method is called by setRange method. (override method)
Definition: Progress.cpp:363