B
bartek d
Hello,
Regarding my previous question about a class which is used to store a
variable type vector. I tried to be more elaborate on the code.
I'd be grateful for your suggestions. Am I going in the wrong direction
with the implementation? I'm asking this because I don't have much
experience with C++. Thanks in advance.
The main problem I see with this class, is that the code which uses it
must first ask for its type before invoking the Get* methods.
typedef std::vector<int> IntVector;
typedef std::vector<float> FloatVector;
typedef std::vector<std::string> StringVector;
class RVar {
public:
//
// Possible var types.
//
enum RVarType {
UNDEFINED = 0, FLOAT, INT, STRING
};
//
// Default ctor.
//
RVar() : ptr(NULL), type(UNDEFINED) { }
//
// Copy ctor.
//
RVar(const RVar& src) : ptr(NULL), type(src.type) {
if(type == FLOAT)
ptr = new FloatVector(*static_cast<FloatVector*>(src.ptr));
else if(type = INT)
ptr = new IntVector(*static_cast<IntVector*>(src.ptr));
else if(type == STRING)
ptr = new StringVector(*static_cast<StringVector*>(src.ptr));
}
//
// Default dtor.
//
~RVar() { if(type) DoCleanup(); }
//
// Determine the type.
//
bool TypeIs(RVarType t) {
return type == t;
}
//
// Writing data accessors
//
RVar& SetFloatVector(const FloatVector& src) {
if(type) DoCleanup();
type = FLOAT;
ptr = new FloatVector(src);
return *this;
}
RVar& SetIntVector(const IntVector& src) {
if(type) DoCleanup();
type = INT;
ptr = new IntVector(src);
return *this;
}
RVar& SetStringVector(const StringVector& src) {
if(type) DoCleanup();
type = STRING;
ptr = new StringVector(src);
return *this;
}
//
// Reading data accessors
//
FloatVector& GetFloatVector()
{ return *static_cast<FloatVector*>(ptr); }
IntVector& GetIntVector()
{ return *static_cast<IntVector*>(ptr); }
StringVector& GetStringVector()
{ return *static_cast<StringVector*>(ptr); }
private:
//
// Delete data vector.
//
void DoCleanup() {
if(type == FLOAT)
delete static_cast<FloatVector*>(ptr);
else if(type == INT)
delete static_cast<IntVector*>(ptr);
else if(type == STRING)
delete static_cast<StringVector*>(ptr);
}
void *ptr;
RVarType type;
};
Regarding my previous question about a class which is used to store a
variable type vector. I tried to be more elaborate on the code.
I'd be grateful for your suggestions. Am I going in the wrong direction
with the implementation? I'm asking this because I don't have much
experience with C++. Thanks in advance.
The main problem I see with this class, is that the code which uses it
must first ask for its type before invoking the Get* methods.
typedef std::vector<int> IntVector;
typedef std::vector<float> FloatVector;
typedef std::vector<std::string> StringVector;
class RVar {
public:
//
// Possible var types.
//
enum RVarType {
UNDEFINED = 0, FLOAT, INT, STRING
};
//
// Default ctor.
//
RVar() : ptr(NULL), type(UNDEFINED) { }
//
// Copy ctor.
//
RVar(const RVar& src) : ptr(NULL), type(src.type) {
if(type == FLOAT)
ptr = new FloatVector(*static_cast<FloatVector*>(src.ptr));
else if(type = INT)
ptr = new IntVector(*static_cast<IntVector*>(src.ptr));
else if(type == STRING)
ptr = new StringVector(*static_cast<StringVector*>(src.ptr));
}
//
// Default dtor.
//
~RVar() { if(type) DoCleanup(); }
//
// Determine the type.
//
bool TypeIs(RVarType t) {
return type == t;
}
//
// Writing data accessors
//
RVar& SetFloatVector(const FloatVector& src) {
if(type) DoCleanup();
type = FLOAT;
ptr = new FloatVector(src);
return *this;
}
RVar& SetIntVector(const IntVector& src) {
if(type) DoCleanup();
type = INT;
ptr = new IntVector(src);
return *this;
}
RVar& SetStringVector(const StringVector& src) {
if(type) DoCleanup();
type = STRING;
ptr = new StringVector(src);
return *this;
}
//
// Reading data accessors
//
FloatVector& GetFloatVector()
{ return *static_cast<FloatVector*>(ptr); }
IntVector& GetIntVector()
{ return *static_cast<IntVector*>(ptr); }
StringVector& GetStringVector()
{ return *static_cast<StringVector*>(ptr); }
private:
//
// Delete data vector.
//
void DoCleanup() {
if(type == FLOAT)
delete static_cast<FloatVector*>(ptr);
else if(type == INT)
delete static_cast<IntVector*>(ptr);
else if(type == STRING)
delete static_cast<StringVector*>(ptr);
}
void *ptr;
RVarType type;
};