-
Notifications
You must be signed in to change notification settings - Fork 0
/
Param.h
126 lines (103 loc) · 3.17 KB
/
Param.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#ifndef _GAZEBO_PARAM_H
#define _GAZEBO_PARAM_H
#include <cstdlib>
#include <list>
#include <string>
#include <stdexcept>
namespace gazebo {
// The parameter structure
class Param
{
public:
enum ParamType { eUnknown, eInt, eReal };
// the type of parameter
ParamType ptype;
// number of int parameters
int int_start, int_end;
// start, step, and end of real range
double r_start, r_step, r_end;
// the name of the parameter
std::string name;
// parameter options
std::list<std::string> options;
// gets the type of parameter (int, real)
static ParamType get_param_type(const std::string& ptype)
{
if (ptype == "int")
return eInt;
else if (ptype == "real")
return eReal;
else
return eUnknown;
}
// builds the parameter based on the token string
static Param build_param(const std::list<std::string>& tokens)
{
std::list<std::string>::const_iterator token_iter = tokens.begin();
// create the parameter
Param p;
// read the parameter name
if (token_iter == tokens.end())
throw std::runtime_error("Bad token information");
p.name = *token_iter++;
// set the parameter type
if (token_iter == tokens.end())
throw std::runtime_error("Bad token information");
p.ptype = get_param_type(*token_iter++);
// if the parameter type is an int, read in the length of the int
switch (p.ptype)
{
case eInt:
if (token_iter == tokens.end())
throw std::runtime_error("Bad token information");
p.int_start = std::atoi(token_iter->c_str());
token_iter++;
if (token_iter == tokens.end())
throw std::runtime_error("Bad token information");
p.int_end = std::atoi(token_iter->c_str());
token_iter++;
break;
case eReal:
if (token_iter == tokens.end())
throw std::runtime_error("Bad token information");
p.r_start = std::atof(token_iter->c_str());
token_iter++;
if (token_iter == tokens.end())
throw std::runtime_error("Bad token information");
p.r_step = std::atof(token_iter->c_str());
token_iter++;
if (token_iter == tokens.end())
throw std::runtime_error("Bad token information");
p.r_end = std::atof(token_iter->c_str());
token_iter++;
break;
default:
throw std::runtime_error("Unexpected token type");
}
// remaining are options
while (token_iter != tokens.end())
p.options.push_back(*token_iter++);
return p;
}
};
inline std::ostream& operator<<(std::ostream& out, const Param& p)
{
out << p.name << " ";
if (p.ptype == Param::eInt)
{
out << "int ";
out << p.int_start << " " << p.int_end;
}
else if (p.ptype == Param::eReal)
{
out << "real ";
out << p.r_start << " " << p.r_step << " " << p.r_end;
}
else
out << "unknown";
for (std::list<std::string>::const_iterator i = p.options.begin(); i != p.options.end(); i++)
out << " " << *i;
out << std::endl;
}
} // end namespace
#endif