Line data Source code
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 36 : Progress::Progress(GDALProgressFunc pfnProgress, void *pProgressArg, 23 36 : size_t expectedLines) 24 36 : : m_expectedLines(std::max(expectedLines, static_cast<size_t>(1))) 25 : { 26 : using namespace std::placeholders; 27 : 28 : // cppcheck-suppress useInitializationList 29 36 : m_cb = std::bind(pfnProgress, _1, _2, pProgressArg); 30 36 : } 31 : 32 : /// Emit progress information saying that a line has been written to output. 33 : /// 34 : /// @return True on success, false otherwise. 35 83634 : bool Progress::lineComplete() 36 : { 37 : double fraction; 38 : { 39 83634 : std::lock_guard<std::mutex> lock(m_mutex); 40 : 41 83744 : if (m_lines < m_expectedLines) 42 83744 : m_lines++; 43 83744 : fraction = m_lines / static_cast<double>(m_expectedLines); 44 : } 45 83740 : return emit(fraction); 46 : } 47 : 48 : /// Emit progress information saying that a fraction of work has been completed. 49 : /// 50 : /// @return True on success, false otherwise. 51 83759 : bool Progress::emit(double fraction) 52 : { 53 167539 : std::lock_guard<std::mutex> lock(m_mutex); 54 : 55 : // Call the progress function. 56 83780 : if (!m_cb(fraction, "")) 57 : { 58 0 : CPLError(CE_Failure, CPLE_UserInterrupt, "User terminated"); 59 0 : return false; 60 : } 61 83780 : return true; 62 : } 63 : 64 : } // namespace viewshed 65 : } // namespace gdal