Added copy operator to ShapeAttribute to prevent problems when assigned them or use within a vector

This commit is contained in:
Robert Osfield 2007-12-13 12:30:31 +00:00
parent 6cca3b8f49
commit b6f32a3ed0
2 changed files with 41 additions and 13 deletions

View File

@ -41,11 +41,15 @@ class OSGSIM_EXPORT ShapeAttribute
ShapeAttribute(const char * name);
ShapeAttribute(const char * name, int value);
ShapeAttribute(const char * name, double value);
/** Note, ShapeAttribute takes a copy of both name and value, the calling code should manage its own clean up of the original strings.*/
ShapeAttribute(const char * name, const char * value);
ShapeAttribute(const ShapeAttribute & sa);
~ShapeAttribute();
ShapeAttribute& operator = (const ShapeAttribute& sa);
const std::string & getName() const { return _name; }
const Type getType() const { return _type; }
@ -59,6 +63,9 @@ class OSGSIM_EXPORT ShapeAttribute
private:
void free();
void copy(const ShapeAttribute& sa);
std::string _name;
Type _type;
@ -67,7 +74,7 @@ class OSGSIM_EXPORT ShapeAttribute
{
int _integer;
double _double;
char * _string;
char* _string;
};

View File

@ -50,10 +50,31 @@ ShapeAttribute::ShapeAttribute(const char * name, const char * value) :
_string[str.size()] = 0;
}
ShapeAttribute::ShapeAttribute(const ShapeAttribute & sa) :
_name(sa._name),
_type(sa._type)
ShapeAttribute::ShapeAttribute(const ShapeAttribute & sa)
{
copy(sa);
}
ShapeAttribute::~ShapeAttribute()
{
free();
}
void ShapeAttribute::free()
{
if ((_type == STRING) && (_string))
{
delete [] _string;
_string = 0;
}
}
void ShapeAttribute::copy(const ShapeAttribute& sa)
{
_name = sa._name;
_type = sa._type;
switch (_type)
{
case INTEGER:
@ -82,13 +103,16 @@ ShapeAttribute::ShapeAttribute(const ShapeAttribute & sa) :
}
}
}
ShapeAttribute::~ShapeAttribute()
{
if ((_type == STRING) && (_string)) delete [] _string;
}
ShapeAttribute& ShapeAttribute::operator = (const ShapeAttribute& sa)
{
if (&sa == this) return *this;
free();
copy(sa);
return *this;
}
int ShapeAttribute::compare(const osgSim::ShapeAttribute& sa) const
@ -122,11 +146,8 @@ int ShapeAttribute::compare(const osgSim::ShapeAttribute& sa) const
if (sa._integer<_integer) return 1;
}
}
return 0;
}
/** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/