Skip to content
Snippets Groups Projects
Commit b010446b authored by Florent Lamiraux's avatar Florent Lamiraux
Browse files

Fix several memory errors

    * include/dynamic-graph/command.h,
    * include/dynamic-graph/value.h
    * src/command/command.cpp,
    * src/command/value.cpp.
parent 7927c3da
No related branches found
No related tags found
No related merge requests found
......@@ -44,7 +44,7 @@ namespace dynamicgraph {
/// Set parameter values
void setParameterValues(const std::vector<Value>& values);
/// Get parameter values
const std::vector<Value> getParameterValues();
const std::vector<Value>& getParameterValues() const;
/// Execute the command after checking parameters
Value execute();
/// Get a reference to the Entity owning this command
......
......@@ -35,6 +35,7 @@ namespace dynamicgraph {
STRING,
NB_TYPES
};
~Value();
/// template constructor
template <class T> Value(const T& value);
/// Copy constructor
......@@ -45,32 +46,38 @@ namespace dynamicgraph {
Type type() const;
/// Return the value if it is a double and throw otherwise
const double& doubleValue () const;
double doubleValue () const;
/// Return the value if it is a int and throw otherwise
const int& intValue () const;
int intValue () const;
/// Return the value if it is a string and throw otherwise
const std::string& stringValue () const;
std::string stringValue () const;
/// Return the name of the type
static std::string typeName(Type type);
/// Output in a stream
friend std::ostream& operator<<(std::ostream& os, const Value& value);
private:
Type type_;
const void* value_;
};
// Template constructors
template <> Value::Value(const int& value)
template <> inline Value::Value(const int& value)
{
value_ = &value;
std::cout << "Constructor of int value" << std::endl;
value_ = new int(value);
type_ = INT;
}
template <> Value::Value(const double& value)
template <> inline Value::Value(const double& value)
{
value_ = &value;
std::cout << "Constructor of double value" << std::endl;
value_ = new double(value);
type_ = DOUBLE;
}
template <> Value::Value(const std::string& value)
template <> inline Value::Value(const std::string& value)
{
value_ = &value;
std::cout << "Constructor of string value" << std::endl;
value_ = new std::string(value);
type_ = STRING;
}
template <class T> Value::Value(const T& value)
......
......@@ -55,9 +55,14 @@ namespace dynamicgraph {
}
// Copy vector of values in private part
valueVector_ = values;
std::cout << "Command::SetParameterValues" << std::endl;
for (unsigned int i=0; i<valueVector_.size(); i++) {
std::cout << ", value[" << i << "]=(" << valueVector_[i]
<< ")" << std::endl;
}
}
const std::vector<Value> Command::getParameterValues()
const std::vector<Value>& Command::getParameterValues() const
{
return valueVector_;
}
......
......@@ -21,13 +21,42 @@
namespace dynamicgraph {
namespace command {
Value::Value(const Value& value) : type_(value.type_),
value_(value.value_)
Value::~Value()
{
switch(type_) {
case INT:
delete (int*)value_;
break;
case DOUBLE:
delete (double*)value_;
break;
case STRING:
delete (std::string*)value_;
break;
}
}
Value::Value(const Value& value) : type_(value.type_)
{
switch(value.type_) {
case INT:
std::cout << "Value copy constructor: int" << std::endl;
value_ = new int(value.intValue());
break;
case DOUBLE:
std::cout << "Value copy constructor: double" << std::endl;
value_ = new double(value.doubleValue());
break;
case STRING:
std::cout << "Value copy constructor: string" << std::endl;
value_ = new std::string(value.stringValue());
break;
}
}
Value::Value() : type_(NONE), value_(NULL)
{
std::cout << "Value empty constructor" << std::endl;
}
Value::Type Value::type() const
......@@ -35,30 +64,33 @@ namespace dynamicgraph {
return type_;
}
const double& Value::doubleValue () const
double Value::doubleValue () const
{
double result;
if (type_ == DOUBLE)
return *(static_cast<const double*>(value_));
result = *((double*)value_);
std::cout << "Value::doubleValue = " << result << std::endl;
return result;
throw ExceptionAbstract(ExceptionAbstract::TOOLS,
"value is not a double");
}
const int& Value::intValue () const
int Value::intValue () const
{
if (type_ == INT)
return *(static_cast<const int*>(value_));
return *((int*)value_);
throw ExceptionAbstract(ExceptionAbstract::TOOLS,
"value is not an int");
}
const std::string& Value::stringValue () const
std::string Value::stringValue () const
{
if (type_ == STRING)
return *(static_cast<const std::string*>(value_));
return *((std::string*)value_);
throw ExceptionAbstract(ExceptionAbstract::TOOLS,
"value is not an string");
}
std::string Value::typeName(Type type)
{
switch (type) {
......@@ -71,5 +103,24 @@ namespace dynamicgraph {
}
return std::string("unknown");
}
std::ostream& operator<<(std::ostream& os, const Value& value)
{
os << "Type=" << Value::typeName(value.type_)
<< ", value=";
switch (value.type_) {
case Value::INT:
os << value.intValue();
break;
case Value::DOUBLE:
os << value.doubleValue();
break;
case Value::STRING:
os << value.stringValue();
break;
}
return os;
}
} // namespace command
} //namespace dynamicgraph
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment