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

Re-implement value getter in a nicer way.

	     * include/dynamic-graph/value.h,
	     * src/command/value.cpp.
parent b010446b
No related branches found
No related tags found
No related merge requests found
...@@ -26,6 +26,17 @@ ...@@ -26,6 +26,17 @@
namespace dynamicgraph { namespace dynamicgraph {
namespace command { 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 { class DYNAMICGRAPH_EXPORT Value {
public: public:
enum Type { enum Type {
...@@ -36,8 +47,9 @@ namespace dynamicgraph { ...@@ -36,8 +47,9 @@ namespace dynamicgraph {
NB_TYPES NB_TYPES
}; };
~Value(); ~Value();
/// template constructor Value(const int& value);
template <class T> Value(const T& value); Value(const double& value);
Value(const std::string& value);
/// Copy constructor /// Copy constructor
Value(const Value& value); Value(const Value& value);
// Construct an empty value (None) // Construct an empty value (None)
...@@ -45,46 +57,23 @@ namespace dynamicgraph { ...@@ -45,46 +57,23 @@ namespace dynamicgraph {
/// Return the type of the value /// Return the type of the value
Type type() const; Type type() const;
/// Return the value if it is a double and throw otherwise /// Return the value as a castable value into the approriate type
double doubleValue () const; /// double x = value();
/// Return the value if it is a int and throw otherwise const AnyType value () const;
int intValue () const;
/// Return the value if it is a string and throw otherwise
std::string stringValue () const;
/// Return the name of the type /// Return the name of the type
static std::string typeName(Type type); static std::string typeName(Type type);
/// Output in a stream /// Output in a stream
friend std::ostream& operator<<(std::ostream& os, const Value& value); friend std::ostream& operator<<(std::ostream& os, const Value& value);
private: private:
friend class AnyType;
const double doubleValue() const;
const int intValue() const;
const std::string stringValue() const;
Type type_; Type type_;
const void* value_; const void* value_;
}; };
} // namespace command
// 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 dynamicgraph } //namespace dynamicgraph
#endif //DYNAMIC_GRAPH_VALUE_H #endif //DYNAMIC_GRAPH_VALUE_H
...@@ -21,6 +21,22 @@ ...@@ -21,6 +21,22 @@
namespace dynamicgraph { namespace dynamicgraph {
namespace command { 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() Value::~Value()
{ {
switch(type_) { switch(type_) {
...@@ -36,6 +52,26 @@ namespace dynamicgraph { ...@@ -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_) Value::Value(const Value& value) : type_(value.type_)
{ {
switch(value.type_) { switch(value.type_) {
...@@ -59,12 +95,17 @@ namespace dynamicgraph { ...@@ -59,12 +95,17 @@ namespace dynamicgraph {
std::cout << "Value empty constructor" << std::endl; std::cout << "Value empty constructor" << std::endl;
} }
const AnyType Value::value() const
{
return AnyType(*this);
}
Value::Type Value::type() const Value::Type Value::type() const
{ {
return type_; return type_;
} }
double Value::doubleValue () const const double Value::doubleValue () const
{ {
double result; double result;
if (type_ == DOUBLE) if (type_ == DOUBLE)
...@@ -75,7 +116,7 @@ namespace dynamicgraph { ...@@ -75,7 +116,7 @@ namespace dynamicgraph {
"value is not a double"); "value is not a double");
} }
int Value::intValue () const const int Value::intValue () const
{ {
if (type_ == INT) if (type_ == INT)
return *((int*)value_); return *((int*)value_);
...@@ -83,7 +124,7 @@ namespace dynamicgraph { ...@@ -83,7 +124,7 @@ namespace dynamicgraph {
"value is not an int"); "value is not an int");
} }
std::string Value::stringValue () const const std::string Value::stringValue () const
{ {
if (type_ == STRING) if (type_ == STRING)
return *((std::string*)value_); return *((std::string*)value_);
......
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