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

Fix implementation of command-setter.

    * include/CMakeLists.txt,
    * include/dynamic-graph/command-setter.h: new,
    * include/dynamic-graph/command-setter.t.cpp: new,
    * include/dynamic-graph/value.h.
parent 13d1f9ab
No related branches found
No related tags found
No related merge requests found
......@@ -68,7 +68,8 @@ tracer-real-time.h
command.h
value.h
parameter.h
command-setter.h
command-setter.t.cpp
)
# Recreate correct path for the headers
......
//
// Copyright 2010 CNRS
//
// Author: Florent Lamiraux
//
// This file is part of dynamic-graph.
// dynamic-graph is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
// dynamic-graph is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details. You should
// have received a copy of the GNU Lesser General Public License along
// with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
#ifndef DYNAMIC_GRAPH_COMMAND_SETTER_H
#define DYNAMIC_GRAPH_COMMAND_SETTER_H
#include "dynamic-graph/command.h"
namespace dynamicgraph {
namespace command {
///
/// Command that calls a parameter setter function
///
template <class E, typename T>
class Setter : public Command {
public:
/// Pointer to method that sets paramter of type T
typedef void (E::*SetterMethod) (const T&);
/// Constructor
Setter(E& entity, SetterMethod);
protected:
virtual Value doExecute();
private:
static const std::vector<Value::Type> typeVector();
SetterMethod setterMethod_;
};
} // namespace command
} // namespace dynamicgraph
#include "dynamic-graph/command-setter.t.cpp"
#endif //DYNAMIC_GRAPH_COMMAND_SETTER_H
//
// Copyright 2010 CNRS
//
// Author: Florent Lamiraux
//
// This file is part of dynamic-graph.
// dynamic-graph is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
// dynamic-graph is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details. You should
// have received a copy of the GNU Lesser General Public License along
// with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
#ifndef DYNAMIC_GRAPH_COMMAND_SETTER_T_CPP
#define DYNAMIC_GRAPH_COMMAND_SETTER_T_CPP
#include <sstream>
namespace dynamicgraph {
class Entity;
namespace command {
template <class E, typename T>
const std::vector<Value::Type> Setter<E, T>::typeVector()
{
std::vector<Value::Type> result;
if (typeid(T) == typeid(int)) {
result.push_back(Value::INT);
} else if (typeid(T) == typeid(double)) {
result.push_back(Value::DOUBLE);
} else if (typeid(T) == typeid(std::string)) {
result.push_back(Value::STRING);
} else {
std::stringstream ss;
ss << "Type " << typeid(T).name() << " not supported.";
throw ExceptionAbstract(ExceptionAbstract::TOOLS,
ss.str());
}
return result;
}
template <class E, typename T>
Setter<E, T>::Setter(E& entity, SetterMethod setterMethod) :
Command(entity, typeVector()),
setterMethod_(setterMethod)
{
}
template <class E, typename T>
Value Setter<E, T>::doExecute()
{
const std::vector<Value>& values = getParameterValues();
// Get parameter
T value = values[0].value();
E& entity = static_cast<E&>(owner());
(entity.*setterMethod_)(value);
return Value();
}
} // namespace command
} // namespace dynamicgraph
#endif // DYNAMIC_GRAPH_COMMAND_SETTER_T_CPP
......@@ -58,7 +58,16 @@ namespace dynamicgraph {
Type type() const;
/// Return the value as a castable value into the approriate type
/// double x = value();
///
/// For instance,
/// \code
/// Value v1(5.0); // v1 is of type double
/// Value v2(3); // v2 is of type int
/// double x1 = v1.value();
/// double x2 = v2.value();
/// \endcode
/// The first assignment will succeed, while the second one will throw
/// an exception.
const AnyType value () const;
/// Return the name of the type
static std::string typeName(Type type);
......
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