OpenASIP 2.2
Loading...
Searching...
No Matches
CustomCommand.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2009 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file CustomCommand.cc
26 *
27 * Definition of CustomCommand class.
28 *
29 * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30 * @author Pekka Jääskeläinen 2005 (pjaaskel-no.spam-cs.tut.fi)
31 * @note reviewed 27 May 2004 by pj, jn, vpj, ll
32 * @note rating: green
33 */
34
35#include <string>
36#include <cctype>
37#include "CustomCommand.hh"
38#include "Conversion.hh"
39
40using std::string;
41
42
43/**
44 * Constructor.
45 *
46 * @param name The name of the command.
47 */
48CustomCommand::CustomCommand(std::string name) :
49 name_(name), context_(NULL), interpreter_(NULL) {
50}
51
52/**
53 * Copy Constructor.
54 *
55 * @param cmd CustomCommand to be copied.
56 */
58 name_(cmd.name()), context_(cmd.context()),
59 interpreter_(cmd.interpreter()) {
60}
61
62
63/**
64 * Destructor.
65 */
68
69/**
70 * Checks if the count of arguments fits between the given minimum and
71 * maximum arguments.
72 *
73 * If the count of arguments is wrong, sets the error message and
74 * returns false.
75 *
76 * @param argumentCount The count of the arguments.
77 * @param minimum Minimum amount of arguments.
78 * @param maximum Maximum amount of arguments.
79 * @return True if the count of arguments is valid.
80 */
81bool
83 int argumentCount,
84 int minimum,
85 int maximum) {
86
87 if (argumentCount < minimum) {
88 DataObject* result = new DataObject();
89 result->setString("Not enough arguments.");
90 interpreter()->setResult(result);
91 return false;
92 } else if (argumentCount > maximum) {
93 DataObject* result = new DataObject();
94 result->setString("Too many arguments.");
95 interpreter()->setResult(result);
96 return false;
97 }
98 return true;
99}
100
101/**
102 * Checks if the given argument is an integer.
103 *
104 * If argument is not integer, sets the error message and returns false.
105 *
106 * @param argument The argument to check.
107 * @return True if argument is integer.
108 */
109bool
111 try {
112 argument.integerValue();
113 } catch (const NumberFormatException&) {
114 DataObject* result = new DataObject();
115 result->setString("Argument not integer as expected.");
116 interpreter()->setResult(result);
117 return false;
118 }
119 return true;
120}
121
122/**
123 * Checks if the given argument is a positive integer (zero is included).
124 *
125 * If argument is not positive integer, sets the error message and
126 * returns false. Note that this function assumes that the value range
127 * is that of an 'int', i.e., the maximum positive value is less than
128 * that of an 'unsigned int'.
129 *
130 * @param argument The argument to check.
131 * @return True if argument is integer.
132 */
133bool
135 bool argumentOk = false;
136 try {
137 argumentOk = (argument.integerValue() >= 0);
138
139 } catch (const NumberFormatException&) {
140 argumentOk = false;
141 }
142
143 if (!argumentOk) {
144 DataObject* result = new DataObject();
145 result->setString("Argument not positive integer as expected.");
146 interpreter()->setResult(result);
147 return false;
148 }
149
150 return true;
151}
152
153/**
154 * Checks if the given argument is an unsigned integer.
155 *
156 * If argument is not unsigned integer, sets the error message and
157 * returns false. This function assumes that the value range of the argument
158 * is that of an 'unsigned int'.
159 *
160 * @param argument The argument to check.
161 * @return True if argument is integer.
162 */
163bool
165
166 try {
167 if (argument.stringValue().size() > 0) {
168 // ensure the first char is a digit, not a '-'
169 if (isdigit(argument.stringValue().at(0))) {
170 // this should throw NumberFormatException if the argument
171 // cannot be converted to an int
172 argument.integerValue();
173 return true;
174 }
175 }
176 } catch (const NumberFormatException&) {
177 }
178
179 // some of the checks failed, it's not an unsigned int
180 DataObject* result = new DataObject();
181 result->setString("Argument not positive integer as expected.");
182 interpreter()->setResult(result);
183 return false;
184}
185
186/**
187 * Checks if the given argument is a valid double
188 *
189 * If the argument is not double, sets the error message and returns false.
190 *
191 * @param argument The argument to check.
192 * @return True if argument is double.
193 */
194bool
196 try {
197 argument.doubleValue();
198 } catch (const NumberFormatException&) {
199 DataObject* result = new DataObject();
200 result->setString("Argument not double as expected.");
201 interpreter()->setResult(result);
202 return false;
203 }
204 return true;
205}
bool checkArgumentCount(int argumentCount, int minimum, int maximum)
bool checkUnsignedIntegerArgument(const DataObject &argument)
virtual ~CustomCommand()
CustomCommand(std::string name)
bool checkIntegerArgument(const DataObject &argument)
ScriptInterpreter * interpreter() const
bool checkPositiveIntegerArgument(const DataObject &argument)
bool checkDoubleArgument(const DataObject &argument)
virtual std::string stringValue() const
virtual double doubleValue() const
virtual void setString(std::string value)
virtual int integerValue() const
virtual void setResult(DataObject *result)