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

Fix memory issue and changed class name AnyType -> EitherType.

    * include/dynamic-graph/value.h,
    * src/command/value.cpp.
parent 4b298e9f
No related branches found
No related tags found
No related merge requests found
......@@ -27,14 +27,15 @@
namespace dynamicgraph {
namespace command {
class Value;
class AnyType {
class EitherType {
public:
AnyType(const Value& value);
EitherType(const Value& value);
~EitherType();
operator int () const;
operator double () const;
operator std::string () const;
private:
const Value& value_;
const Value* value_;
};
class DYNAMICGRAPH_EXPORT Value {
......@@ -68,14 +69,14 @@ namespace dynamicgraph {
/// \endcode
/// The first assignment will succeed, while the second one will throw
/// an exception.
const AnyType value () const;
const EitherType value () 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:
friend class AnyType;
friend class EitherType;
const double doubleValue() const;
const int intValue() const;
const std::string stringValue() const;
......
......@@ -21,20 +21,26 @@
namespace dynamicgraph {
namespace command {
AnyType::AnyType(const Value& value) : value_(value)
EitherType::EitherType(const Value& value) : value_(new Value(value))
{
}
AnyType::operator int () const
EitherType::~EitherType()
{
delete value_;
}
EitherType::operator int () const
{
return value_.intValue();
return value_->intValue();
}
AnyType::operator double () const
EitherType::operator double () const
{
return value_.doubleValue();
return value_->doubleValue();
}
AnyType::operator std::string () const
EitherType::operator std::string () const
{
return value_.stringValue();
return value_->stringValue();
}
Value::~Value()
......@@ -87,6 +93,9 @@ namespace dynamicgraph {
std::cout << "Value copy constructor: string" << std::endl;
value_ = new std::string(value.stringValue());
break;
default:
type_ = NONE;
value_ = NULL;
}
}
......@@ -95,9 +104,9 @@ namespace dynamicgraph {
std::cout << "Value empty constructor" << std::endl;
}
const AnyType Value::value() const
const EitherType Value::value() const
{
return AnyType(*this);
return EitherType(*this);
}
Value::Type Value::type() const
......@@ -110,7 +119,6 @@ namespace dynamicgraph {
double result;
if (type_ == DOUBLE)
result = *((double*)value_);
std::cout << "Value::doubleValue = " << result << std::endl;
return result;
throw ExceptionAbstract(ExceptionAbstract::TOOLS,
"value is not a double");
......
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