-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.hpp
100 lines (74 loc) · 2.23 KB
/
source.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
99
100
#pragma once
#include "sample.hpp"
#include <iterator>
#include <type_traits>
namespace dsp
{
namespace detail
{
template<typename T>
struct is_sequence
{
template<typename S>
static constexpr bool is_seq(decltype(std::begin(std::declval<S const>()) + std::declval<std::size_t>()) const*) noexcept {
return true;
}
template<typename S>
static constexpr bool is_seq(...) {
return false;
}
static constexpr bool value = is_seq<T>(0);
};
} /* namespace detail */
template<typename T>
class ConstantSource;
template<typename T>
constexpr std::enable_if_t<!detail::is_sequence<T>::value, ConstantSource<T>> source(T const& value) noexcept;
template<typename T>
class ConstantSource
{
public:
using sample_type = T;
using value_type = value_type_t<sample_type>;
[[gnu::pure]]
constexpr sample_type operator()(Index n) const noexcept {
return value;
}
private:
friend ConstantSource source<T>(T const&) noexcept;
constexpr ConstantSource(sample_type const& value) noexcept
: value(value)
{}
sample_type const value;
};
template<typename T>
constexpr std::enable_if_t<!detail::is_sequence<T>::value, ConstantSource<T>> source(T const& value) noexcept {
return { value };
}
template<typename Sequence>
class SequenceSource;
template<typename Sequence>
constexpr std::enable_if_t<detail::is_sequence<Sequence>::value, SequenceSource<Sequence>> source(Sequence const& seq) noexcept;
template<typename Sequence>
class SequenceSource
{
public:
using sample_type = typename Sequence::value_type;
using value_type = value_type_t<sample_type>;
using sequence_type = Sequence;
[[gnu::pure]]
constexpr sample_type operator()(Index n) const noexcept {
return *(std::begin(seq) + n);
}
private:
friend SequenceSource source<Sequence>(Sequence const&) noexcept;
constexpr SequenceSource(sequence_type const& seq) noexcept
: seq(seq)
{}
sequence_type const& seq;
};
template<typename Sequence>
constexpr std::enable_if_t<detail::is_sequence<Sequence>::value, SequenceSource<Sequence>> source(Sequence const& seq) noexcept {
return { seq };
}
} /* namespace dsp */