MeshLib C++ Docs
Loading...
Searching...
No Matches
MRProgressCallback.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4
5#include <array>
6#include <cassert>
7#include <cmath>
8#include <tuple>
9#include <utility>
10
11namespace MR
12{
15
16
18inline bool reportProgress( ProgressCallback cb, float v )
19{
20 if ( cb )
21 return cb( v );
22 return true;
23}
24
27inline bool reportProgress( ProgressCallback cb, float v, size_t counter, int divider )
28{
29 if ( cb && ( counter % divider == 0 ) )
30 return cb( v );
31 return true;
32}
33
36template<typename F>
37inline bool reportProgress( ProgressCallback cb, F && f )
38{
39 if ( cb )
40 return cb( f() );
41 return true;
42}
43
46template<typename F>
47inline bool reportProgress( ProgressCallback cb, F && f, size_t counter, int divider )
48{
49 if ( cb && ( counter % divider == 0 ) )
50 return cb( f() );
51 return true;
52}
53
55inline ProgressCallback subprogress( ProgressCallback cb, float from, float to )
56{
57 assert( from <= to );
59 if ( cb )
60 res = [cb = std::move( cb ), from, to]( float v ) { return cb( std::lerp( from, to, v ) ); };
61 return res;
62}
63
65template<typename F>
67{
69 if ( cb )
70 res = [cb = std::move( cb ), f = std::forward<F>( f )]( float v ) { return cb( f( v ) ); };
71 return res;
72}
73
75inline ProgressCallback subprogress( ProgressCallback cb, size_t index, size_t count )
76{
77 assert( index < count );
78 if ( cb )
79 return [cb = std::move( cb ), index, count] ( float v ) { return cb( ( (float)index + v ) / (float)count ); };
80 else
81 return {};
82}
83
87template <typename ...Ts>
88auto splitProgress( const ProgressCallback& cb, Ts ... thresholds )
89{
90 constexpr size_t n = sizeof...( Ts );
91 static_assert( n > 0, "at least one threshold is required" );
92 const std::array<float, n + 2> bounds{ 0.0f, float( thresholds )..., 1.0f };
93 return [&]<size_t ...I>( std::index_sequence<I...> )
94 {
95 return std::tuple{ subprogress( cb, bounds[I], bounds[I + 1] )... };
96 }( std::make_index_sequence<n + 1>{} );
97}
98
99}
std::function< bool(float)> ProgressCallback
Definition MRMeshFwd.h:751
class MRMESH_CLASS I
Definition MRMeshFwd.h:141
auto splitProgress(const ProgressCallback &cb, Ts ... thresholds)
Definition MRProgressCallback.h:88
ProgressCallback subprogress(ProgressCallback cb, float from, float to)
returns a callback that maps [0,1] linearly into [from,to] in the call to
Definition MRProgressCallback.h:55
bool reportProgress(ProgressCallback cb, float v)
safely invokes
Definition MRProgressCallback.h:18
only for bindings generation
Definition MRCameraOrientationPlugin.h:8