Re: Non-container Iterators
First, my apologies for the double post. Second, my apologies for replying
to my own post. :-)
After further thought, I don't think iterators are the way to model my phase
accumulator. Rather I think the concept of Generator, a nullary function
that returns a value, is a better model. So I came up with this:
class PhaseAccumulator
{
public:
typedef float result_type;
private:
float *phase;
float increment;
public:
PhaseAccumulator(float *phase, float increment)
{
this->phase = phase;
this->increment = increment;
}
result_type operator()()
{
*phase += increment;
if(*phase >= 1.0f)
{
*phase -= 1.0f;
}
return *phase;
}
};
I use unary functions for my waveforms; they argument is the phase. I need
to fed the output of my PhaseAccumulator into a Waveform object. In looking
at the STL, I haven't found an adapter to do this; I could be missing
something. So I wrote my own:
template<typename Generator, typename UnaryFunction>
class NullaryCompose
{
public:
typedef typename Generator::result_type result_type;
private:
Generator myG;
UnaryFunction myF;
public:
NullaryCompose(Generator g, UnaryFunction f) :
myG(g),
myF(f)
{
}
result_type operator()()
{
return myF(myG());
}
};
Which allows me to do this:
PhaseAccumulator phaseAccumulator(&phase, increment);
NullaryCompose<PhaseAccumulator, WaveShapes::Sine> nc(phaseAccumulator,
WaveShapes::Sine());
std::generate(&buffer[0], &buffer[100], nc);
I think modeling these sorts of components as function objects makes more
sense than modeling them as iterators.