-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrobotInit.h
163 lines (156 loc) · 5.25 KB
/
robotInit.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
class robotInit;
#ifndef ROBOTINIT_H
#define ROBOTINIT_H
#include "globals.h"
/**
* Class representing the initiation of one robot.
*/
class robotInit
{
public:
/**
* Empty construcot creating new instance of robotInit.
*/
robotInit(){}
/**
* Copy constructor creating an instance of robotInit with same data os the other.
*/
robotInit(const robotInit& other)
{
this->robot=other.robot;
this->init_values=other.init_values;
}
/**
* Function checking if this instance equals the other.
* @returns true if objects have the same data.
*/
bool equals(robotInit other)
{
if(this->robot==other.robot)
{
if(this->init_values.size()==other.init_values.size())
{
for(int i=0;i<init_values.size();i++)
{
if(init_values[i].first!=other.init_values[i].first || init_values[i].second!=other.init_values[i].second)
return false;
}
return true;
}
}
return false;
}
/**
* Creates a string describing the coordinates attributes.
* @returns String with the description of the Robot initialization
*/
std::string Print()
{
std::string x;
x+="\nECP: ";
x+="\nROBOT: ";
x+=ROBOT_TABLE[robot];
for(std::vector < std::pair<GeneratorType, int> >::iterator iter = init_values.begin(); iter!=init_values.end(); iter++)
{
x+="\nGENERATOR: ";
x+=GENERATOR_TYPE_TABLE[(*iter).first];
char tmp[20];
sprintf(tmp, "%d", (*iter).second);
x+=" ";
x+=tmp;
}
return x;
}
/**
* Writes the data of the state to the XML stream.
* @param writer Stream to which the data is written
*/
void Print(QXmlStreamWriter * writer)
{
writer->writeStartElement("ecp");
writer->writeAttribute(QString("name"), QString().fromStdString(ROBOT_TABLE[robot]));
for(std::vector < std::pair<GeneratorType, int> >::iterator iter = init_values.begin(); iter!=init_values.end(); iter++)
{
char tmp[20];
sprintf(tmp, "%d", (*iter).second);
writer->writeTextElement(QString().fromStdString(GENERATOR_TYPE_TABLE2[(*iter).first]), QString().fromStdString(tmp));
}
writer->writeEndElement();
}
/**
* Loads from XML Stream the data.
* @param reader Stream from which the data is read
* @returns List of errors, which occured while loading
*/
QStringList LoadFromXML(QXmlStreamReader * reader)
{
QStringList errors;
if(reader->attributes().hasAttribute("name"))
{
Robot index = (Robot)(getRobotTable().indexOf(reader->attributes().value("name").toString()));
if(isProper(index))
{
robot = index;
}
else
{
robot = (Robot)0;
char linenum[30];
sprintf(linenum,"; line: %lld", reader->lineNumber());
errors.push_back(QString("Out of bound Robot")+=linenum);
}
}
else
{
robot = (Robot)0;
char linenum[30];
sprintf(linenum,"; line: %lld", reader->lineNumber());
errors.push_back(QString("no name in ecp")+=linenum);
}
while (!reader->atEnd())
{
reader->readNextStartElement();
if(reader->name()=="ecp"&&reader->isEndElement())
{
if(init_values.size()==0)
{
char linenum[30];
sprintf(linenum,"; line: %lld", reader->lineNumber());
errors.push_back(QString("no init values for the Robot: ").append(QString().fromStdString(ROBOT_TABLE[robot])));
}
return errors;
}
else if (reader->isStartElement())
{
GeneratorType index = (GeneratorType)(getGeneratorTypeTable2().indexOf(reader->name().toString()));
if(isProper(index))
{
init_values.push_back(std::make_pair(index,reader->readElementText().toInt()));
}
else
{
char linenum[30];
sprintf(linenum,"; line: %lld", reader->lineNumber());
errors.push_back(QString("Out of bounds GeneratorType")+=linenum);
}
}
}
if(init_values.size()==0)
{
char linenum[30];
sprintf(linenum,"; line: %lld", reader->lineNumber());
errors.push_back(QString("no init values for Robot: ").append(QString().fromStdString(ROBOT_TABLE[robot]))+=linenum);
}
return errors;
}
/**
* Robot, which is being initialized
*/
Robot robot;
/**
* Vector of generators, and their init arguments.
*/
std::vector < std::pair<GeneratorType, int> > init_values;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////
#endif // ROBOTINIT_H