diff --git a/include/VAStruct.h b/include/VAStruct.h index d10aa2b0385b7d81d3715380327bd7ce7d3d12e2..7cb17951741f3109abb5d1c968bc00cc4121cdf0 100644 --- a/include/VAStruct.h +++ b/include/VAStruct.h @@ -357,7 +357,7 @@ public: * @param[in] pData Data pointer * @param[in] iBytes Number of bytes */ - void SetData( void* pData, const int iBytes ); + void SetData( const void* pData, const int iBytes ); //! Returns a pointer to the struct value (only for datatype STRUCT) /** @@ -544,7 +544,7 @@ private: double dValue; //!< Value if type is double std::string sValue; //!< Value if type is string CVAStruct* xValue; //!< Value if type is struct - void* pData; //!< Value if type is data + std::vector< char > vcData; //!< Value if type is data int iDataSize; //!< Value byte number if type is data CVASampleBuffer sbValue; //!< Value if type is sample buffer diff --git a/src/VAStruct.cpp b/src/VAStruct.cpp index d6de1a218c0149aaa5088f3953031a1ac76b785c..f541710f945e2a2d6ab2c8ccd720d1f2c72f7443 100644 --- a/src/VAStruct.cpp +++ b/src/VAStruct.cpp @@ -270,17 +270,16 @@ std::string CVAStruct::ToString( const int iIndent ) const // ---------------------------------------------------- // Constructors -CVAStructValue::CVAStructValue() : iDatatype( UNASSIGNED ), xValue( NULL ), pData( NULL ) {}; -CVAStructValue::CVAStructValue( const bool value ) : iDatatype( BOOL ), bValue( value ), xValue( NULL ), pData( NULL ) {}; -CVAStructValue::CVAStructValue( const int value ) : iDatatype( INT ), iValue( value ), xValue( NULL ), pData( NULL ) {}; -CVAStructValue::CVAStructValue( const double value ) : iDatatype( DOUBLE ), dValue( value ), xValue( NULL ), pData( NULL ) {}; -CVAStructValue::CVAStructValue( const char* value ) : iDatatype( STRING ), sValue( value ), xValue( NULL ), pData( NULL ) {}; -CVAStructValue::CVAStructValue( const std::string& value ) : iDatatype( STRING ), sValue( value ), xValue( NULL ), pData( NULL ) {}; -CVAStructValue::CVAStructValue( const CVAStruct& value ) : iDatatype( STRUCT ), xValue( new CVAStruct( value ) ), pData( NULL ) {}; +CVAStructValue::CVAStructValue() : iDatatype( UNASSIGNED ), xValue( NULL ) {}; +CVAStructValue::CVAStructValue( const bool value ) : iDatatype( BOOL ), bValue( value ), xValue( NULL ) {}; +CVAStructValue::CVAStructValue( const int value ) : iDatatype( INT ), iValue( value ), xValue( NULL ) {}; +CVAStructValue::CVAStructValue( const double value ) : iDatatype( DOUBLE ), dValue( value ), xValue( NULL ) {}; +CVAStructValue::CVAStructValue( const char* value ) : iDatatype( STRING ), sValue( value ), xValue( NULL ) {}; +CVAStructValue::CVAStructValue( const std::string& value ) : iDatatype( STRING ), sValue( value ), xValue( NULL ) {}; +CVAStructValue::CVAStructValue( const CVAStruct& value ) : iDatatype( STRUCT ), xValue( new CVAStruct( value ) ) {}; CVAStructValue::CVAStructValue( void* pData, const int iBytes ) : iDatatype( DATA ) - , pData( NULL ) , xValue( NULL ) { SetData( pData, iBytes ); @@ -291,7 +290,9 @@ CVAStructValue::CVAStructValue( const CVASampleBuffer& oSampleBuffer ) , sbValue( oSampleBuffer ) {} -CVAStructValue::CVAStructValue( const CVAStructValue& rhs ) : iDatatype( UNASSIGNED ), xValue( NULL ), pData( NULL ) +CVAStructValue::CVAStructValue( const CVAStructValue& rhs ) + : iDatatype( UNASSIGNED ) + , xValue( NULL ) { *this = rhs; } @@ -299,56 +300,70 @@ CVAStructValue::CVAStructValue( const CVAStructValue& rhs ) : iDatatype( UNASSIG CVAStructValue::~CVAStructValue() { delete xValue; - delete[] pData; } -int CVAStructValue::GetDatatype() const { +int CVAStructValue::GetDatatype() const +{ return iDatatype; } -int CVAStructValue::GetDataSize() const { +int CVAStructValue::GetDataSize() const +{ return iDataSize; } -const void* CVAStructValue::GetData() const { - if( iDatatype != DATA ) VA_EXCEPT1( "Key value is no binary data" ); - return pData; +const void* CVAStructValue::GetData() const +{ + if( iDatatype != DATA ) + VA_EXCEPT1( "Key value is no binary data" ); + + return &( vcData[ 0 ] ); } -void* CVAStructValue::GetData() { - if( iDatatype != DATA ) VA_EXCEPT1( "Key value is no binary data" ); - return pData; +void* CVAStructValue::GetData() +{ + if( iDatatype != DATA ) + VA_EXCEPT1( "Key value is no binary data" ); + + return &( vcData[ 0 ] ); } -void CVAStructValue::SetData( void* pData, const int iBytes ) { +void CVAStructValue::SetData( const void* pIncomingData, const int iIncomingBytes ) +{ assert( iDatatype == DATA ); - if( iDatatype != DATA ) return; - if( pData == NULL ) + if( iDatatype != DATA ) + return; + + if( pIncomingData == NULL ) { - assert( iBytes == 0 ); + assert( iIncomingBytes == 0 ); } else { - assert( iBytes > 0 ); + assert( iIncomingBytes > 0 ); } // Free previous data - delete[] this->pData; - this->pData = NULL; + vcData.clear(); // Allocate new buffer and copy the data - if( pData ) + if( pIncomingData ) { - this->pData = new char[ iBytes ]; - iDataSize = iBytes; - memcpy( this->pData, pData, iBytes ); + if( vcData.size() < iIncomingBytes ) + vcData.resize( iIncomingBytes ); + + iDataSize = iIncomingBytes; + //for( size_t i = 0; i < vcData.size(); i++ ) + //vcData[ i ] = pData[ i ]; + + memcpy( GetData(), pIncomingData, iIncomingBytes ); } } const CVAStruct& CVAStructValue::GetStruct() const { - if( iDatatype != STRUCT ) + if( iDatatype != STRUCT ) VA_EXCEPT1( "Key value is not a structure" ); assert( xValue ); return *xValue; @@ -356,7 +371,7 @@ const CVAStruct& CVAStructValue::GetStruct() const CVAStruct& CVAStructValue::GetStruct() { - if( iDatatype != STRUCT ) + if( iDatatype != STRUCT ) VA_EXCEPT1( "Key value is not a structure" ); assert( xValue ); return *xValue; @@ -414,7 +429,6 @@ CVAStructValue& CVAStructValue::operator=( const CVAStructValue& rhs ) return *this; // Free data, if the key was of datatype DATA before - delete[] pData; delete xValue; iDataSize = 0; @@ -445,7 +459,7 @@ CVAStructValue& CVAStructValue::operator=( const CVAStructValue& rhs ) case DATA: // Autonome Kopie des Schüssel BLOBs erzeugen - SetData( rhs.pData, rhs.iDataSize ); + SetData( rhs.GetData(), rhs.iDataSize ); break; case SAMPLEBUFFER: @@ -535,7 +549,7 @@ CVAStructValue::operator bool() const std::transform( s.begin(), s.end(), s.begin(), ( int( *)( int ) ) ::toupper ); if( ( s == "no" ) || ( s == "false" ) ) return false; - if( ( s == "yes" ) || ( s == "true" ) ) + if( ( s == "yes" ) || ( s == "true" ) ) return true; } @@ -702,7 +716,7 @@ CVAStructValue::operator void*( ) const VA_EXCEPT1( "Types cannot be converted" ); } - return pData; + return ( void* ) GetData(); } CVAStructValue::operator const CVASampleBuffer&( ) const