command.cpp 3.18 KB
Newer Older
florent's avatar
florent committed
1
2
3
4
5
6
//
// Copyright 2010 CNRS
//
// Author: Florent Lamiraux
//
// This file is part of dynamic-graph.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// dynamic-graph is free software:
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
//   list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
florent's avatar
florent committed
28
29
30
31
32
33
34
35

#include <sstream>
#include "dynamic-graph/command.h"
#include "dynamic-graph/exception-abstract.h"

namespace dynamicgraph {
  namespace command {

36
37
38
    const std::vector<Value::Type> Command::EMPTY_ARG = std::vector<Value::Type>();


florent's avatar
florent committed
39
40
    Command::~Command() {}
    Command::Command(Entity& entity,
41
42
43
		     const std::vector<Value::Type>& valueTypes,
		     const std::string& docstring) :
      owner_(entity), valueTypeVector_(valueTypes), docstring_(docstring)
florent's avatar
florent committed
44
45
46
    {
    }

47
    const std::vector<Value::Type>& Command::valueTypes() const
florent's avatar
florent committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    {
      return valueTypeVector_;
    }

    void Command::setParameterValues(const std::vector<Value>& values)
    {
      const std::vector<Value::Type>& paramTypes = valueTypes();
      // Check that number of parameters is correct
      if (values.size() != paramTypes.size()) {
	throw ExceptionAbstract(ExceptionAbstract::ABSTRACT,
				"wrong number of parameters");
      }
      // Check that each parameter is of correct type
      for (unsigned int iParam=0; iParam < values.size(); iParam++) {
	if (values[iParam].type() != paramTypes[iParam]) {
	  std::stringstream ss;
64
	  ss << "argument " << iParam << " is of wrong type: "
florent's avatar
florent committed
65
66
67
68
69
70
71
72
73
	     << Value::typeName(paramTypes[iParam]) << " expected, got "
	     << Value::typeName(values[iParam].type());
	  throw ExceptionAbstract(ExceptionAbstract::TOOLS, ss.str());
	}
      }
      // Copy vector of values in private part
      valueVector_ = values;
    }

74
    const std::vector<Value>& Command::getParameterValues() const
florent's avatar
florent committed
75
76
77
78
79
    {
      return valueVector_;
    }

    Value Command::execute()
80
    {
florent's avatar
florent committed
81
82
83
84
85
86
87
      return doExecute();
    }

    Entity& Command::owner()
    {
      return owner_;
    }
88
89
90
91
    std::string Command::getDocstring() const
    {
      return docstring_;
    }
florent's avatar
florent committed
92
93
  } // namespace command
} //namespace dynamicgraph