diff --git a/include/dynamic-graph/value.h b/include/dynamic-graph/value.h index c69b76ab7e5afa1e4b18755efce8e4951f5c3f71..d21e9489dc9cc6c6a305fdee165dd69179a05bdd 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 99da95e1d9dc6de8984e8ab728245277968c4d6c..7a74a27ef8aeffdc0f086c2cb6acb18754cfc678 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");