From 526f4cab9be1b5b39f9fcbb41efc1a74e7d44025 Mon Sep 17 00:00:00 2001 From: florent <florent@laas.fr> Date: Tue, 26 Oct 2010 11:22:15 +0200 Subject: [PATCH] Fix memory issue and changed class name AnyType -> EitherType. * include/dynamic-graph/value.h, * src/command/value.cpp. --- include/dynamic-graph/value.h | 11 ++++++----- src/command/value.cpp | 28 ++++++++++++++++++---------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/include/dynamic-graph/value.h b/include/dynamic-graph/value.h index c69b76a..d21e948 100644 --- a/include/dynamic-graph/value.h +++ b/include/dynamic-graph/value.h @@ -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; diff --git a/src/command/value.cpp b/src/command/value.cpp index 99da95e..7a74a27 100644 --- a/src/command/value.cpp +++ b/src/command/value.cpp @@ -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"); -- GitLab