/src/gdal/alg/viewshed/progress.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * (c) 2024 info@hobu.co |
3 | | * |
4 | | * SPDX-License-Identifier: MIT |
5 | | ****************************************************************************/ |
6 | | |
7 | | #include <algorithm> |
8 | | |
9 | | #include "progress.h" |
10 | | |
11 | | #include "cpl_error.h" |
12 | | |
13 | | namespace gdal |
14 | | { |
15 | | namespace viewshed |
16 | | { |
17 | | |
18 | | /// Constructor |
19 | | /// @param pfnProgress Pointer to progress function. |
20 | | /// @param pProgressArg Pointer to progress function data. |
21 | | /// @param expectedLines Number of lines expected to be processed. |
22 | | Progress::Progress(GDALProgressFunc pfnProgress, void *pProgressArg, |
23 | | size_t expectedLines) |
24 | 0 | : m_expectedLines(std::max(expectedLines, static_cast<size_t>(1))) |
25 | 0 | { |
26 | 0 | using namespace std::placeholders; |
27 | | |
28 | | // cppcheck-suppress useInitializationList |
29 | 0 | m_cb = std::bind(pfnProgress, _1, _2, pProgressArg); |
30 | 0 | } |
31 | | |
32 | | /// Emit progress information saying that a line has been written to output. |
33 | | /// |
34 | | /// @return True on success, false otherwise. |
35 | | bool Progress::lineComplete() |
36 | 0 | { |
37 | 0 | double fraction; |
38 | 0 | { |
39 | 0 | std::lock_guard<std::mutex> lock(m_mutex); |
40 | |
|
41 | 0 | if (m_lines < m_expectedLines) |
42 | 0 | m_lines++; |
43 | 0 | fraction = m_lines / static_cast<double>(m_expectedLines); |
44 | 0 | } |
45 | 0 | return emit(fraction); |
46 | 0 | } |
47 | | |
48 | | /// Emit progress information saying that a fraction of work has been completed. |
49 | | /// |
50 | | /// @return True on success, false otherwise. |
51 | | bool Progress::emit(double fraction) |
52 | 0 | { |
53 | 0 | std::lock_guard<std::mutex> lock(m_mutex); |
54 | | |
55 | | // Call the progress function. |
56 | 0 | if (!m_cb(fraction, "")) |
57 | 0 | { |
58 | 0 | CPLError(CE_Failure, CPLE_UserInterrupt, "User terminated"); |
59 | 0 | return false; |
60 | 0 | } |
61 | 0 | return true; |
62 | 0 | } |
63 | | |
64 | | } // namespace viewshed |
65 | | } // namespace gdal |