-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
153 lines (108 loc) · 4.24 KB
/
Copy pathmain.cpp
File metadata and controls
153 lines (108 loc) · 4.24 KB
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
#include <QCoreApplication>
#include "PacketType.h"
#include "Packet.h"
#include "Container.h"
#include "Specification.h"
#include <QVector>
#include <algorithm>
#include <functional>
void process(paco::Container& container)
{
//creating some data
QList<int>* intList = new QList<int>();
intList->append(42);
intList->append(1999);
QList<QString>* stringList = new QList<QString>();
stringList->append("paco");
stringList->append("template");
stringList->append("library");
stringList->append("is");
stringList->append("amazing");
stringList->append(".");
//append a QList<int>*
container.append<QList<int>*>(intList);
//append a QList<QString>*
container.append<QList<QString>*>(stringList);
//append a std::vector<int>*
container.append<std::vector<int>*>(new std::vector<int>(50));
//append an int
container.append<int>(42);
//append a lambda function with two parameters lambdafunction(int x, double y)
container.append<std::function<void (int, double )>>([](int x, double y){ std::cout << "x: '" << x << "' y: '" << y << "'" << std::endl;});
}
int main(int argc, char *argv[])
{
//create a container
paco::Container container;
//process container
process(container);
//get container specification after processing
paco::Specification containerSpecification = container.getSpecification();
//print the specification of the container after processing it
std::cout << "printing container specification" << std::endl;
for(int i = 0; i < containerSpecification.size(); i++)
{
paco::PacketType p = containerSpecification.at(i);
std::cout << p.toString().toStdString() << std::endl;
}
std::cout << std::endl << std::endl;
//print out all container element types
std::cout << "Listing all container element types:" << std::endl;
for(int i = 0; i < container.size(); i++)
{
std::cout << container.at(i)->packetType().toString().toStdString() << std::endl;
}
std::cout << std::endl << std::endl;
//extracting data from container
//in practice you dont need a for loop, but you can access the container as follows:
//container.at(0)->get<your_type>();
std::cout << "extracting data from elements" << std::endl;
for(int i = 0; i < container.size(); i++)
{
paco::Packet* p = container.at(i);
std::cout << "at index " << i << " there is obviously a " << p->packetType().toString().toStdString() << std::endl;
//checking for QList<int>*
if(p->packetType().equals<QList<int>*>())
{
std::cout << "found a QList<int>* at " << i << std::endl;
QList<int>* list = p->get<QList<int>*>();
//QList<double>* list2 = p->get<QList<double>*>(); // this would throw an exception, because double is wrong
std::cout << "printing elements " << std::endl;
for(int j : *list)
{
std::cout << "'" << j << "'" << std::endl;
}
}
//checking for QList<QString>*
if(p->packetType().equals<QList<QString>*>())
{
std::cout << "found a QList<QString>* at " << i << std::endl;
QList<QString>* list = p->get<QList<QString>*>();
std::cout << "printing elements " << std::endl;
for(QString j : *list)
{
std::cout << "'" << j.toStdString() << "'" << std::endl;
}
}
//checking for an int
if(p->packetType().equals<int>())
{
std::cout << "found a <int> at " << i << std::endl;
int value = p->get<int>();
std::cout << "printing int value: '" << value << "'" << std::endl;
}
//checking for a pointer to a vector of int
if(p->packetType().equals<std::vector<int>*>())
{
std::cout << "found a <std::vector<int>*> at " << i << std::endl;
}
//checking for an int
if(p->packetType().equals<std::function<void (int, double)>>())
{
std::function<void (int, double)> lamdaFunction = p->get<std::function<void (int, double)>>();
lamdaFunction(42, 49.999);
}
std::cout << std::endl;
}
return 1;
}