Hi there,
I'm trying to make a *simple* vector template that I can use as a basis as 2 types of vectors, one with 3 floats in and one with 6, that will be exported from a dll for use with the openal stuff I am trying to develop.
I want the vector to be able to be used as a return value from a function, to be used as a parameter, and to clean up it's array of floats when it gets disposed of. I also want it to transparently be able to be used in the openal functions that take a float* and expect an array of 3 or 6 floats.
This implementation I'm using is apparently so nasty that it causes the machine to reset after handing it off to openal in the set listener functions, then asking it to play. [rolleyes]
Anyway here's the template and the 3 float version. The 6 float version is essentially the same with a different constructor and ResetValues.
Thanks for looking it over!
#pragma once
#include "SoundDirectorDll.h"
#include <exception>
namespace pde
{
template <class ValueType, unsigned int vectorWidth>
class VectorTemplate
{
public:
//As this is the first data member, &instance should be the same address as values[3]. But with the overloaded operators, that should be neither here or there I guess!
ValueType* value;
VectorTemplate(void);
VectorTemplate(const VectorTemplate< ValueType, vectorWidth >& rhs);
VectorTemplate(const ValueType rhs[]);
virtual ~VectorTemplate(void);
virtual void ResetValues(void) {} ;
VectorTemplate< ValueType, vectorWidth >& operator = (const VectorTemplate< ValueType, vectorWidth >& rhs);
VectorTemplate< ValueType, vectorWidth >& operator = (const ValueType rhs[]);
const ValueType& operator[] ( int subscript) const;
ValueType& operator[] ( int subscript);
operator ValueType*();
operator const ValueType*() const;
protected:
virtual const char* GenerateRangeErrorString(void) const { return "Subscript out of range for Vector.";} ;
};
template <class ValueType, unsigned int vectorWidth>
VectorTemplate< ValueType, vectorWidth >::VectorTemplate(void)
{
value = new ValueType[vectorWidth];
ResetValues();
}
template <class ValueType, unsigned int vectorWidth>
VectorTemplate< ValueType, vectorWidth >::VectorTemplate(const VectorTemplate< ValueType, vectorWidth > & rhs)
{
value = new ValueType[vectorWidth];
for (unsigned int i=0; i<vectorWidth; ++i)
{
value=rhs.value;
}
}
template <class ValueType, unsigned int vectorWidth>
VectorTemplate< ValueType, vectorWidth >::VectorTemplate(const ValueType rhs[])
{
value = new ValueType[vectorWidth];
for (unsigned int i=0; i<vectorWidth; ++i)
{
value=rhs;
}
}
template <class ValueType, unsigned int vectorWidth>
VectorTemplate< ValueType, vectorWidth >::~VectorTemplate(void)
{
delete[] value;
}
template <class ValueType, unsigned int vectorWidth>
VectorTemplate< ValueType, vectorWidth >& VectorTemplate< ValueType, vectorWidth >::operator = (const VectorTemplate< ValueType, vectorWidth >& rhs)
{
for (unsigned int i=0; i<vectorWidth; ++i)
{
value=rhs.value;
}
return *this;
}
template <class ValueType, unsigned int vectorWidth>
VectorTemplate< ValueType, vectorWidth >& VectorTemplate< ValueType, vectorWidth >::operator = (const ValueType rhs[])
{
for (unsigned int i=0; i<vectorWidth; ++i)
{
value=rhs;
}
return *this;
}
template <class ValueType, unsigned int vectorWidth>
const ValueType& VectorTemplate< ValueType, vectorWidth >::operator [] (int subscript) const
{
if ((subscript<0) || (subscript>=vectorWidth))
{
throw std::out_of_range(GenerateRangeErrorString());
}
return value[subscript];
}
template <class ValueType, unsigned int vectorWidth>
ValueType& VectorTemplate< ValueType, vectorWidth >::operator [] (int subscript)
{
if ((subscript<0) || (subscript>=vectorWidth))
{
throw std::out_of_range(GenerateRangeErrorString());
}
return value[subscript];
}
template <class ValueType, unsigned int vectorWidth>
VectorTemplate< ValueType, vectorWidth >::operator ValueType*()
{
return value;
}
template <class ValueType, unsigned int vectorWidth>
VectorTemplate< ValueType, vectorWidth >::operator const ValueType*() const
{
return value;
}
}
#pragma once
#include "SoundDirectorDll.h"
#include <string>
//#include <Vector>
#include "VectorTemplate.h"
namespace pde
{
class SOUNDDIRECTOR_API Vector3F : public VectorTemplate<float, 3>
{
public:
Vector3F(void);
Vector3F(const float x, const float y, const float z);
Vector3F(const Vector3F& rhs);
Vector3F(const float rhs[]);
virtual ~Vector3F(void);
protected:
virtual void ResetValues(void);
};
}
#include "..\include\vector3f.h"
#include <exception>
using namespace std;
namespace pde
{
Vector3F::Vector3F(void):VectorTemplate<float, 3>()
{
}
Vector3F::Vector3F(const float x, const float y, const float z):VectorTemplate<float, 3>()
{
value[0]=x;
value[1]=y;
value[2]=z;
}
Vector3F::Vector3F(const Vector3F& rhs):VectorTemplate<float, 3>(rhs)
{
}
Vector3F::Vector3F(const float rhs[]):VectorTemplate<float, 3>(rhs)
{
}
Vector3F::~Vector3F(void)
{
}
void Vector3F::ResetValues(void)
{
value[0]=value[1]=value[2]=0;
}
}
[Edited by - paulecoyote on August 24, 2005 3:34:32 AM]
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.