Geogram  Version 1.9.0
A programming library of geometric algorithms
progress.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2022 Inria
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of the ALICE Project-Team nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Contact: Bruno Levy
30  *
31  * https://www.inria.fr/fr/bruno-levy
32  *
33  * Inria,
34  * Domaine de Voluceau,
35  * 78150 Le Chesnay - Rocquencourt
36  * FRANCE
37  *
38  */
39 
40 #ifndef GEOGRAM_BASIC_PROGRESS
41 #define GEOGRAM_BASIC_PROGRESS
42 
43 #include <geogram/basic/common.h>
44 #include <geogram/basic/logger.h>
45 #include <geogram/basic/counted.h>
47 
53 namespace GEO {
54 
76  class GEOGRAM_API ProgressClient : public Counted {
77  public:
85  virtual void begin() = 0;
86 
96  virtual void progress(index_t step, index_t percent) = 0;
97 
108  virtual void end(bool canceled) = 0;
109 
110  protected:
112  ~ProgressClient() override;
113  };
114 
117 
118  /************************************************************************/
119 
124  struct GEOGRAM_API TaskCanceled : std::exception {
128  const char* what() const GEO_NOEXCEPT override;
129  };
130 
131  /************************************************************************/
132 
133  class ProgressTask;
134 
138  namespace Progress {
149  void GEOGRAM_API initialize();
150 
157  void GEOGRAM_API terminate();
158 
166  void GEOGRAM_API set_client(ProgressClient* client);
167 
176  GEOGRAM_API const ProgressTask* current_progress_task();
177 
190  void GEOGRAM_API cancel();
191 
199  bool GEOGRAM_API is_canceled();
200 
204  void GEOGRAM_API clear_canceled();
205  }
206 
207  /************************************************************************/
208 
240  class GEOGRAM_API ProgressTask {
241  public:
254  const std::string& task_name, index_t max_steps,
255  bool quiet
256  );
257 
268  const std::string& task_name = "", index_t max_steps = 100
269  );
270 
277  virtual ~ProgressTask();
278 
290  virtual void progress(index_t step);
291 
299  virtual void next();
300 
310  bool is_canceled() const;
311 
320  void reset();
321 
331  void reset(index_t max_steps);
332 
336  const std::string& task_name() const {
337  return task_name_;
338  }
339 
343  double start_time() const {
344  return start_time_;
345  }
346 
350  index_t max_steps() const {
351  return max_steps_;
352  }
353 
357  index_t step() const {
358  return step_;
359  }
360 
364  index_t percent() const {
365  return percent_;
366  }
367 
368  protected:
376  virtual void update();
377 
378  private:
379  std::string task_name_;
380  double start_time_;
381  bool quiet_;
382  index_t max_steps_;
383  index_t step_;
384  index_t percent_;
385  };
386 }
387 
388 #endif
389 
Base class for reference-counted objects.
Definition: counted.h:71
Task progress listener.
Definition: progress.h:76
virtual void end(bool canceled)=0
Stops listening progress.
virtual void progress(index_t step, index_t percent)=0
Tracks progress.
~ProgressClient() override
virtual void begin()=0
Starts listening progress.
Tracks the progress of a task.
Definition: progress.h:240
virtual ~ProgressTask()
Destroys a ProgressTask.
virtual void progress(index_t step)
Sets the current execution step.
index_t max_steps() const
Gets the number of steps of the task.
Definition: progress.h:350
virtual void next()
Goes to the next step.
index_t step() const
Gets the current step of the task.
Definition: progress.h:357
double start_time() const
Gets the start time of the task.
Definition: progress.h:343
virtual void update()
Updates progress values.
void reset(index_t max_steps)
Resets the execution step.
index_t percent() const
Gets the percentage of completion of the task.
Definition: progress.h:364
ProgressTask(const std::string &task_name, index_t max_steps, bool quiet)
Creates a logger for a task.
const std::string & task_name() const
Gets the name of the task.
Definition: progress.h:336
ProgressTask(const std::string &task_name="", index_t max_steps=100)
Creates a logger for a task.
void reset()
Resets the execution step.
bool is_canceled() const
Checks if the task is canceled.
A smart pointer with reference-counted copy semantics.
Definition: smart_pointer.h:76
Base class of reference-counted objects, to be used with smart pointers.
Common include file, providing basic definitions. Should be included before anything else by all head...
Generic logging mechanism.
void cancel()
Cancels the current task.
void clear_canceled()
Clears the cancellation flag.
bool is_canceled()
Checks if the current task is canceled.
void set_client(ProgressClient *client)
Sets the Progress client.
const ProgressTask * current_progress_task()
Gets the current task.
Global Vorpaline namespace.
Definition: basic.h:55
void terminate()
Cleans up Geogram.
void initialize(int flags=GEOGRAM_INSTALL_HANDLERS)
Initialize Geogram.
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329
SmartPointer< ProgressClient > ProgressClient_var
Definition: progress.h:116
Pointers with automatic reference counting.
Exception thrown when a task is canceled.
Definition: progress.h:124
const char * what() const GEO_NOEXCEPT override
Gets the string identifying the exception.