diff --git a/include/dynamic-graph/value.h b/include/dynamic-graph/value.h index ab46905bb2b141726d7735234bc0b208ab9ddc78..990f3b0bc0e1a20aacb36fd2328728abdb366a62 100644 --- a/include/dynamic-graph/value.h +++ b/include/dynamic-graph/value.h @@ -26,6 +26,17 @@ namespace dynamicgraph { namespace command { + class Value; + class AnyType { + public: + AnyType(const Value& value); + operator int () const; + operator double () const; + operator std::string () const; + private: + const Value& value_; + }; + class DYNAMICGRAPH_EXPORT Value { public: enum Type { @@ -36,8 +47,9 @@ namespace dynamicgraph { NB_TYPES }; ~Value(); - /// template constructor - template <class T> Value(const T& value); + Value(const int& value); + Value(const double& value); + Value(const std::string& value); /// Copy constructor Value(const Value& value); // Construct an empty value (None) @@ -45,46 +57,23 @@ namespace dynamicgraph { /// Return the type of the value Type type() const; - /// Return the value if it is a double and throw otherwise - double doubleValue () const; - /// Return the value if it is a int and throw otherwise - int intValue () const; - /// Return the value if it is a string and throw otherwise - std::string stringValue () const; + /// Return the value as a castable value into the approriate type + /// double x = value(); + const AnyType 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; + const double doubleValue() const; + const int intValue() const; + const std::string stringValue() const; Type type_; const void* value_; }; - - // Template constructors - template <> inline Value::Value(const int& value) - { - std::cout << "Constructor of int value" << std::endl; - value_ = new int(value); - type_ = INT; - } - template <> inline Value::Value(const double& value) - { - std::cout << "Constructor of double value" << std::endl; - value_ = new double(value); - type_ = DOUBLE; - } - template <> inline Value::Value(const std::string& value) - { - std::cout << "Constructor of string value" << std::endl; - value_ = new std::string(value); - type_ = STRING; - } - template <class T> Value::Value(const T& value) - { - assert(false); - } - } // namespace command +} // namespace command } //namespace dynamicgraph #endif //DYNAMIC_GRAPH_VALUE_H diff --git a/src/command/value.cpp b/src/command/value.cpp index 092f0c7e3cef11e93a7691a2310af16f437afb0d..99da95e1d9dc6de8984e8ab728245277968c4d6c 100644 --- a/src/command/value.cpp +++ b/src/command/value.cpp @@ -21,6 +21,22 @@ namespace dynamicgraph { namespace command { + AnyType::AnyType(const Value& value) : value_(value) + { + } + AnyType::operator int () const + { + return value_.intValue(); + } + AnyType::operator double () const + { + return value_.doubleValue(); + } + AnyType::operator std::string () const + { + return value_.stringValue(); + } + Value::~Value() { switch(type_) { @@ -36,6 +52,26 @@ namespace dynamicgraph { } } + Value::Value(const int& value) + { + std::cout << "Constructor of int value" << std::endl; + value_ = new int(value); + type_ = INT; + } + Value::Value(const double& value) + { + std::cout << "Constructor of double value" << std::endl; + value_ = new double(value); + type_ = DOUBLE; + } + Value::Value(const std::string& value) + { + std::cout << "Constructor of string value" << std::endl; + value_ = new std::string(value); + type_ = STRING; + } + + Value::Value(const Value& value) : type_(value.type_) { switch(value.type_) { @@ -59,12 +95,17 @@ namespace dynamicgraph { std::cout << "Value empty constructor" << std::endl; } + const AnyType Value::value() const + { + return AnyType(*this); + } + Value::Type Value::type() const { return type_; } - double Value::doubleValue () const + const double Value::doubleValue () const { double result; if (type_ == DOUBLE) @@ -75,7 +116,7 @@ namespace dynamicgraph { "value is not a double"); } - int Value::intValue () const + const int Value::intValue () const { if (type_ == INT) return *((int*)value_); @@ -83,7 +124,7 @@ namespace dynamicgraph { "value is not an int"); } - std::string Value::stringValue () const + const std::string Value::stringValue () const { if (type_ == STRING) return *((std::string*)value_);