-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatistics.hpp
98 lines (84 loc) · 2.63 KB
/
statistics.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#ifndef SCHLAZICONTROL_STATISTICS_HPP
#define SCHLAZICONTROL_STATISTICS_HPP
#include <algorithm>
#include <memory>
#include <ostream>
#include <tuple>
#include <type_traits>
#include <utility>
#include "utility_stream.hpp"
namespace sc {
template< typename Type > class Statistics;
template< typename Type > Statistics< Type > makeStatistics( Type const& value );
template< typename Type, typename Enable = void >
struct StatisticsWriter;
template< typename Type >
struct StatisticsWriter< std::unique_ptr< Type > >
{
void operator()( std::ostream& os, std::unique_ptr< Type > const& value )
{
os << makeStatistics( *value );
}
};
template< typename Type >
struct StatisticsWriter<
Type,
std::conditional_t<
false,
std::tuple<
typename Type::key_type,
typename Type::mapped_type,
typename Type::value_type,
typename Type::const_iterator,
decltype( std::declval< Type >().begin() ),
decltype( std::declval< Type >().end() )
>,
void
>
>
{
void operator()( std::ostream& os, Type const& value )
{
std::transform( value.begin(), value.end(), OutputStreamIterator( os ),
[]( auto const& entry ) { return makeStatistics( entry.second ); } );
}
};
template< typename Type >
struct StatisticsWriter<
Type,
std::conditional_t<
false,
decltype( std::declval< Type const >().statistics( std::declval< std::ostream& >() ) ),
void
>
>
{
void operator()( std::ostream& os, Type const& value )
{
value.statistics( os );
}
};
template< typename Type >
class Statistics
{
public:
Statistics( Type const& value )
: value_( value )
{
}
friend std::ostream& operator<<( std::ostream& os, Statistics const& statistics )
{
StatisticsWriter< Type > writer;
writer( os, statistics.value_ );
return os;
}
private:
Type const& value_;
};
template< typename Type >
Statistics< Type > makeStatistics( Type const& value )
{
return Statistics< Type >( value );
}
} // namespace sc
#endif // SCHLAZICONTROL_STATISTICS_HPP