ValueBuffer related cleanups

* Move implementation to a new cpp file
* Use std::vector as base class instead of doing our own memory management
* Remove unused dangerous functions
* Make more use of std algorithms
* Some cleanups in code using ValueBuffer
This commit is contained in:
Lukas W
2017-03-08 17:08:51 +01:00
parent 1b6a9f4885
commit 85ed63be85
12 changed files with 89 additions and 176 deletions

View File

@@ -25,12 +25,10 @@
#ifndef EDITOR_COMMON_H
#define EDITOR_COMMON_H
#include <QAction>
#include <QMainWindow>
#include <QToolBar>
#include "TimeLineWidget.h"
#include "ToolButton.h"
class QAction;
class DropToolBar;

View File

@@ -31,133 +31,23 @@
#include <string.h>
#include "MemoryManager.h"
class ValueBuffer
class ValueBuffer : public std::vector<float>
{
MM_OPERATORS
public:
ValueBuffer()
{
m_values = NULL;
m_length = 0;
}
ValueBuffer();
ValueBuffer(int length);
void fill(float value);
float value(int offset ) const;
const float * values() const;
float * values();
ValueBuffer( int length )
{
m_values = new float[length];
m_length = length;
}
int length() const;
ValueBuffer( float * values, int length )
{
m_values = new float[length];
m_length = length;
memcpy( m_values, values, sizeof(float) * length );
}
ValueBuffer( float value, int length )
{
m_values = new float[length];
m_length = length;
for( int i = 0; i < length; i++ )
{
m_values[i] = value;
}
}
virtual ~ValueBuffer()
{
delete[] m_values;
}
void clear()
{
delete[] m_values;
m_values = NULL;
m_length = 0;
}
void fill( float value )
{
for( int i = 0; i < m_length; i++ )
{
m_values[i] = value;
}
}
float value( int offset ) const
{
return m_values[ offset % m_length ];
}
void setValue( int offset, float value )
{
m_values[ offset % m_length ] = value;
}
float * values() const
{
return m_values;
}
void setValues( float * values )
{
m_values = values;
}
int length() const
{
return m_length;
}
void setLength( const int length )
{
m_length = length;
}
void interpolate( float start, float end )
{
float f = 0.0f;
const float fstep = 1.0f / static_cast<float>( m_length );
for( int i = 0; i < m_length; i++ )
{
f += fstep;
m_values[i] = linearInterpolate( start, end, f );
}
}
void multiply( float f )
{
for( int i = 0; i < m_length; i++ )
{
m_values[i] *= f;
}
}
ValueBuffer & operator*=( const float & f )
{
multiply( f );
return *this;
}
ValueBuffer & operator+=( const ValueBuffer & v )
{
for( int i = 0; i < qMin( m_length, v.length() ); i++ )
{
m_values[i] += v.values()[i];
}
return *this;
}
static ValueBuffer interpolatedBuffer( float start, float end, int length )
{
ValueBuffer vb = ValueBuffer( length );
vb.interpolate( start, end );
return vb;
}
private:
float * m_values;
int m_length;
void interpolate(float start, float end);
};
#endif