Klang C++ 0.7.15
Language Reference (draft)
Loading...
Searching...
No Matches
klang::Generic::Function< SIGNAL, Args >

Applies a function to a signal (input-output) More...

#include <klang.h>

+ Inheritance diagram for klang::Generic::Function< SIGNAL, Args >:

Public Member Functions

virtual ~Function ()
 
 operator SIGNAL ()
 
 operator param ()
 
 operator float ()
 
unsigned int args () const
 
template<typename First , typename... Rest>
std::tuple< Rest... > tail (const std::tuple< First, Rest... > &t) const
 
template<typename FunctionPtr >
 Function ()
 
template<typename FunctionPtr >
 Function (FunctionPtr function)
 
template<typename FunctionPtr , typename... OtherArgs>
 Function (FunctionPtr function, OtherArgs... args)
 
void input () override
 
template<typename First , typename... Rest>
First & first (First &first, Rest...)
 
template<typename First , typename... Rest>
const First & first (const First &first, Rest...) const
 
template<typename... FuncArgs>
Function< SIGNAL, Args... > & operator() (const FuncArgs &... args)
 
template<typename... FuncArgs>
const float operator() (const FuncArgs &... args) const
 
template<typename... FuncArgs>
Function< SIGNAL, Args... > & with (FuncArgs... args)
 
signal evaluate () const
 
virtual void process () override
 
klang::Graph & operator>> (klang::Graph &graph)
 
klang::GraphPtr & operator>> (klang::GraphPtr &graph)
 
 operator const SIGNAL & () override
 
 operator const SIGNAL & () const override
 
template<typename... params>
Modifier< SIGNAL > & operator() (params &&... p)
 
virtual const SIGNAL & input () const
 
virtual void input (const SIGNAL &source)
 
virtual void operator<< (const SIGNAL &source)
 
virtual const SIGNAL & output () const
 
template<typename TYPE , class = decltype( std::declval<TYPE&>() = std::declval<SIGNAL&>())>
TYPE & operator>> (TYPE &destination)
 
template<typename TYPE >
SIGNAL operator+ (TYPE &other)
 
template<typename TYPE >
SIGNAL operator* (TYPE &other)
 
template<typename TYPE >
SIGNAL operator- (TYPE &other)
 
template<typename TYPE >
SIGNAL operator/ (TYPE &other)
 
void reset ()
 

Public Attributes

std::function< float(Args...)> function
 
std::tuple< Args... > inputs
 
SIGNAL in = { 0.f }
 
SIGNAL out = { 0.f }
 

Static Public Attributes

static constexpr unsigned int ARGS = sizeof...(Args)
 

Detailed Description

template<typename SIGNAL, typename... Args>
struct klang::Generic::Function< SIGNAL, Args >

Definition at line 3125 of file klang.h.

Constructor & Destructor Documentation

◆ ~Function()

template<typename SIGNAL , typename... Args>
virtual klang::Generic::Function< SIGNAL, Args >::~Function ( )
inlinevirtual

Definition at line 3126 of file klang.h.

3126{}

◆ Function() [1/3]

template<typename SIGNAL , typename... Args>
template<typename FunctionPtr >
klang::Generic::Function< SIGNAL, Args >::Function ( )
inline

Definition at line 3187 of file klang.h.

3187: function(nullptr) {}
std::function< float(Args...)> function
Definition klang.h:3173

◆ Function() [2/3]

template<typename SIGNAL , typename... Args>
template<typename FunctionPtr >
klang::Generic::Function< SIGNAL, Args >::Function ( FunctionPtr function)
inline

Definition at line 3190 of file klang.h.

3191 : function(std::forward<FunctionPtr>(function)) {
3192 }

◆ Function() [3/3]

template<typename SIGNAL , typename... Args>
template<typename FunctionPtr , typename... OtherArgs>
klang::Generic::Function< SIGNAL, Args >::Function ( FunctionPtr function,
OtherArgs... args )
inline

Definition at line 3195 of file klang.h.

3196 : function(std::forward<FunctionPtr>(function)) {
3197 with(args...);
3198 }
unsigned int args() const
Definition klang.h:3176
Function< SIGNAL, Args... > & with(FuncArgs... args)
Definition klang.h:3282

References klang::Generic::Function< SIGNAL, Args >::args(), and klang::Generic::Function< SIGNAL, Args >::with().

Member Function Documentation

◆ args()

template<typename SIGNAL , typename... Args>
unsigned int klang::Generic::Function< SIGNAL, Args >::args ( ) const
inline

◆ evaluate()

◆ first() [1/2]

template<typename SIGNAL , typename... Args>
template<typename First , typename... Rest>
const First & klang::Generic::Function< SIGNAL, Args >::first ( const First & first,
Rest...  ) const
inline

Definition at line 3210 of file klang.h.

3210{ return first; }
First & first(First &first, Rest...)
Definition klang.h:3206

References klang::Generic::Function< SIGNAL, Args >::first().

Referenced by klang::Generic::Function< SIGNAL, Args >::first().

◆ first() [2/2]

template<typename SIGNAL , typename... Args>
template<typename First , typename... Rest>
First & klang::Generic::Function< SIGNAL, Args >::first ( First & first,
Rest...  )
inline

◆ input() [1/3]

template<typename SIGNAL , typename... Args>
void klang::Generic::Function< SIGNAL, Args >::input ( )
inlineoverridevirtual

Reimplemented from klang::Generic::Input< SIGNAL >.

Definition at line 3200 of file klang.h.

3200 {
3201 std::get<0>(inputs) = in.value;
3202 }

References klang::Generic::Input< SIGNAL >::in, and klang::Generic::Function< SIGNAL, Args >::inputs.

◆ input() [2/3]

template<typename SIGNAL >
virtual const SIGNAL & klang::Generic::Input< SIGNAL >::input ( ) const
inlinevirtualinherited

Reimplemented in klang::Generic::Graph< SIZE >::Series.

Definition at line 2947 of file klang.h.

2947{ return in; }

References klang::Generic::Input< SIGNAL >::in.

Referenced by klang::Generic::Input< SIGNAL >::operator<<().

◆ input() [3/3]

template<typename SIGNAL >
virtual void klang::Generic::Input< SIGNAL >::input ( const SIGNAL & source)
inlinevirtualinherited

Reimplemented in klang::Debug.

Definition at line 2951 of file klang.h.

2951{ in = source; this->input(); }
virtual const SIGNAL & input() const
Definition klang.h:2947

References klang::Generic::Input< SIGNAL >::in, and klang::Generic::Input< SIGNAL >::input().

Referenced by klang::Generic::Input< SIGNAL >::input().

◆ operator const SIGNAL &() [1/2]

template<typename SIGNAL >
klang::Generic::Modifier< SIGNAL >::operator const SIGNAL & ( ) const
inlineoverridevirtualinherited

Reimplemented from klang::Generic::Output< SIGNAL >.

Definition at line 3091 of file klang.h.

3091{ return out; } // return last output

References klang::Generic::Output< SIGNAL >::out.

◆ operator const SIGNAL &() [2/2]

template<typename SIGNAL >
klang::Generic::Modifier< SIGNAL >::operator const SIGNAL & ( )
inlineoverridevirtualinherited

Reimplemented from klang::Generic::Output< SIGNAL >.

Definition at line 3090 of file klang.h.

3090{ this->process(); return out; } // return processed output
virtual void process() override
Definition klang.h:3094

References klang::Generic::Output< SIGNAL >::out, and klang::Generic::Modifier< SIGNAL >::process().

◆ operator float()

template<typename SIGNAL , typename... Args>
klang::Generic::Function< SIGNAL, Args >::operator float ( )
inline

◆ operator param()

template<typename SIGNAL , typename... Args>
klang::Generic::Function< SIGNAL, Args >::operator param ( )
inline

◆ operator SIGNAL()

template<typename SIGNAL , typename... Args>
klang::Generic::Function< SIGNAL, Args >::operator SIGNAL ( )
inline

◆ operator()() [1/3]

template<typename SIGNAL , typename... Args>
template<typename... FuncArgs>
Function< SIGNAL, Args... > & klang::Generic::Function< SIGNAL, Args >::operator() ( const FuncArgs &... args)
inline

Definition at line 3214 of file klang.h.

3214 {
3215
3216 //// single argument supplied, more than one expected -> use cached inputs or use as input?
3217 // if constexpr (ARGS > 1 && sizeof...(FuncArgs) == 1){
3218 // //in = first(args...);
3219 // std::get<1>(inputs) = in.value;
3220 // return *this;
3221 // } else
3222
3223 // Calling: full set of arguments supplied (overwrite live input)
3224 if constexpr (ARGS == sizeof...(FuncArgs)) {
3225 in = float(first(args...));
3226 inputs = std::tuple<Args...>(args...);
3227 return *this;
3228
3229 // Streaming: all but first argument supplied (use live input as x)
3230 }
3231 else if constexpr (sizeof...(FuncArgs) == (ARGS - 1)) {
3232 inputs = std::tuple<Args...>(in.value, args...);
3233 return *this;
3234
3235 // Invalid: insufficents arguments
3236 }
3237 else {
3238 static_assert(sizeof...(FuncArgs) < (ARGS - 1), "Insufficient arguments: can only omit first argument");
3239 //in = first(args...);
3240 //std::get<0>(inputs) = in.value;
3241 return *this;
3242 }
3243 }

References klang::Generic::Function< SIGNAL, Args >::ARGS, klang::Generic::Function< SIGNAL, Args >::args(), klang::Generic::Function< SIGNAL, Args >::first(), klang::Generic::Input< SIGNAL >::in, and klang::Generic::Function< SIGNAL, Args >::inputs.

◆ operator()() [2/3]

template<typename SIGNAL , typename... Args>
template<typename... FuncArgs>
const float klang::Generic::Function< SIGNAL, Args >::operator() ( const FuncArgs &... args) const
inline

Definition at line 3247 of file klang.h.

3247 {
3248 signal in = this->in;
3249 std::tuple<Args...> inputs = this->inputs;
3250
3251 // configure inputs
3252 if constexpr (ARGS > 1 && sizeof...(FuncArgs) == 1) {
3253 in = first(args...);
3254 std::get<0>(inputs) = in.value;
3255 }
3256 else if constexpr (ARGS == sizeof...(FuncArgs)) {
3257 in = first(args...);
3258 inputs = std::tuple<Args...>(args...);
3259 }
3260 else if constexpr (sizeof...(FuncArgs) == (ARGS - 1)) {
3261 inputs = std::tuple<Args...>(in.value, args...);
3262 }
3263 else {
3264 in = first(args...);
3265 std::get<0>(inputs) = in.value;
3266 }
3267
3268 // return outputs
3269 if (function) {
3270 if constexpr (ARGS > 1)
3271 return std::apply(function, inputs);
3272 else
3273 return function(in);
3274 }
3275 else {
3276 // NOT YET IMPLEMENTED (needs non-const status)
3277 return out;
3278 }
3279 }
signals< 2 > signal
Stereo audio signal.
Definition klang.h:5840

References klang::Generic::Function< SIGNAL, Args >::ARGS, klang::Generic::Function< SIGNAL, Args >::args(), klang::Generic::Function< SIGNAL, Args >::first(), klang::Generic::Function< SIGNAL, Args >::function, klang::Generic::Input< SIGNAL >::in, klang::Generic::Function< SIGNAL, Args >::inputs, and klang::Generic::Output< SIGNAL >::out.

◆ operator()() [3/3]

template<typename SIGNAL >
template<typename... params>
Modifier< SIGNAL > & klang::Generic::Modifier< SIGNAL >::operator() ( params &&... p)
inlineinherited

Definition at line 3098 of file klang.h.

3098 {
3099 set(std::forward<params>(p)...); return *this;
3100 }

◆ operator*()

template<typename SIGNAL >
template<typename TYPE >
SIGNAL klang::Generic::Output< SIGNAL >::operator* ( TYPE & other)
inlineinherited

Definition at line 2977 of file klang.h.

2977{ this->process(); return out * SIGNAL(other); }
virtual void process()=0

References klang::Generic::Output< SIGNAL >::process().

◆ operator+()

template<typename SIGNAL >
template<typename TYPE >
SIGNAL klang::Generic::Output< SIGNAL >::operator+ ( TYPE & other)
inlineinherited

Definition at line 2976 of file klang.h.

2976{ this->process(); return out + SIGNAL(other); }

References klang::Generic::Output< SIGNAL >::process().

◆ operator-()

template<typename SIGNAL >
template<typename TYPE >
SIGNAL klang::Generic::Output< SIGNAL >::operator- ( TYPE & other)
inlineinherited

Definition at line 2978 of file klang.h.

2978{ this->process(); return out - SIGNAL(other); }

References klang::Generic::Output< SIGNAL >::process().

◆ operator/()

template<typename SIGNAL >
template<typename TYPE >
SIGNAL klang::Generic::Output< SIGNAL >::operator/ ( TYPE & other)
inlineinherited

Definition at line 2979 of file klang.h.

2979{ this->process(); return out / SIGNAL(other); }

References klang::Generic::Output< SIGNAL >::process().

◆ operator<<()

template<typename SIGNAL >
virtual void klang::Generic::Input< SIGNAL >::operator<< ( const SIGNAL & source)
inlinevirtualinherited

Reimplemented in klang::Debug.

Definition at line 2950 of file klang.h.

2950{ in = source; this->input(); }

References klang::Generic::Input< SIGNAL >::in, and klang::Generic::Input< SIGNAL >::input().

◆ operator>>() [1/3]

template<typename SIGNAL , typename... Args>
Graph & klang::Generic::Function< SIGNAL, Args >::operator>> ( klang::Graph & graph)
inline

Definition at line 3849 of file klang.h.

3849 {
3850 graph.plot(*this);
3851 return graph;
3852 }
static THREAD_LOCAL GraphPtr graph
Definition klang.h:3829

References klang::graph, and klang::Generic::Graph< SIZE >::plot().

◆ operator>>() [2/3]

template<typename SIGNAL , typename... Args>
GraphPtr & klang::Generic::Function< SIGNAL, Args >::operator>> ( klang::GraphPtr & graph)
inline

Definition at line 3832 of file klang.h.

3832 {
3833 graph.plot(*this);
3834 return graph;
3835 }

References klang::graph, and klang::Generic::Graph< SIZE >::plot().

◆ operator>>() [3/3]

template<typename SIGNAL >
template<typename TYPE , class = decltype( std::declval<TYPE&>() = std::declval<SIGNAL&>())>
TYPE & klang::Generic::Output< SIGNAL >::operator>> ( TYPE & destination)
inlineinherited

Definition at line 2969 of file klang.h.

2969{ this->process(); return destination = out; }

References klang::Generic::Output< SIGNAL >::out, and klang::Generic::Output< SIGNAL >::process().

◆ output()

template<typename SIGNAL >
virtual const SIGNAL & klang::Generic::Output< SIGNAL >::output ( ) const
inlinevirtualinherited

Definition at line 2964 of file klang.h.

2964{ return out; }

References klang::Generic::Output< SIGNAL >::out.

◆ process()

template<typename SIGNAL , typename... Args>
virtual void klang::Generic::Function< SIGNAL, Args >::process ( )
inlineoverridevirtual

◆ reset()

template<typename SIGNAL >
void klang::Generic::Output< SIGNAL >::reset ( )
inlineinherited

Definition at line 2981 of file klang.h.

2981{ out = 0; }

References klang::Generic::Output< SIGNAL >::out.

◆ tail()

template<typename SIGNAL , typename... Args>
template<typename First , typename... Rest>
std::tuple< Rest... > klang::Generic::Function< SIGNAL, Args >::tail ( const std::tuple< First, Rest... > & t) const
inline

Definition at line 3182 of file klang.h.

3182 {
3183 return std::apply([](const First&, const Rest&... rest) { return std::make_tuple(rest...); }, t);
3184 }

◆ with()

template<typename SIGNAL , typename... Args>
template<typename... FuncArgs>
Function< SIGNAL, Args... > & klang::Generic::Function< SIGNAL, Args >::with ( FuncArgs... args)
inline

Definition at line 3282 of file klang.h.

3282 {
3283 static_assert(sizeof...(FuncArgs) == (ARGS - 1), "with() arguments must have all but first argument.");
3284 inputs = std::tuple<Args...>(in.value, args...);
3285
3286 return *this;
3287 }

References klang::Generic::Function< SIGNAL, Args >::ARGS, klang::Generic::Function< SIGNAL, Args >::args(), klang::Generic::Input< SIGNAL >::in, and klang::Generic::Function< SIGNAL, Args >::inputs.

Referenced by klang::Generic::Function< SIGNAL, Args >::Function(), klang::Generic::Graph< SIZE >::operator<<(), klang::Graph::operator<<(), and klang::Generic::Graph< SIZE >::plot().

Member Data Documentation

◆ ARGS

◆ function

◆ in

◆ inputs

◆ out