You could tuck all of the nasty stuff safely in an object to avoid switching on arbitrary attributes. That would clean up your function a bit:
Code:
class sample_list {
public:
sample_list(void *buffer, int size, bool is_byte);
int next();
bool done();
private:
unsigned char *pbuffer;
int buf_size;
int current;
bool use_byte;
};
sample_list::sample_list(void *buffer, int size, bool is_byte)
: pbuffer(static_cast<unsigned char *>(buffer))
, buf_size(size)
, current(0)
, use_byte(is_byte)
{}
int sample_list::next()
{
int ret;
if (use_byte) {
ret = pbuffer[current];
}
else {
ret = (reinterpret_cast<short int *>(pbuffer))[current];
}
++current;
return ret;
}
bool sample_list::done()
{
return current == buf_size;
}
void f(void *sample_buffer, int size)
{
bool is_byte_sample = m_wfWaveFormat.wBitsPerSample / 8 == 1;
sample_list samples(sample_buffer, size, is_byte_sample);
int sample;
while (!samples.done()) {
sample = samples.next();
// Use sample
}
}
There are several ways to solve this problem, but none of them are really good and elegant at the same time.