-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform_msg_wrapper.cpp
124 lines (110 loc) · 5.42 KB
/
transform_msg_wrapper.cpp
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
#include "code-gen/out/transform/include/my_transform.hpp"
#include <string>
#include <iostream>
#include <cstring>
// We are wrapping this in an extern "C" so that the transform function
// is callable as C code.
extern "C" {
int transform(void *msg1, void *msg2);
void *create_and_populate_input_msg(Old::Motion::EnumItem livingRoomMotion1, \
Old::Motion::EnumItem livingRoomMotion2, \
Old::Entry::EnumItem garageDoorEntry, \
const char* frontDoorEntry, \
int32_t basementMotion1, \
int32_t patioTemp);
const char* get_living_room_motion_1_output_string(void *msg);
const char* get_living_room_motion_2_output_string(void *msg);
const char* get_garage_door_entry_output_string(void *msg);
const char* get_front_door_entry_output_string(void *msg);
const char* get_basement_motion_1_output_string(void *msg);
const char* get_patio_temp_output_string(void *msg);
void free_input_msg(void *msg) ;
void *create_output_msg();
void free_output_msg(void *msg);
void *create_and_populate_input_msg(Old::Motion::EnumItem livingRoomMotion1, \
Old::Motion::EnumItem livingRoomMotion2, \
Old::Entry::EnumItem garageDoorEntry, \
const char* frontDoorEntry, \
int32_t basementMotion1, \
int32_t patioTemp) {
Old::OldSystemState *inputMsg = new Old::OldSystemState;
inputMsg->setLivingRoomMotion1(livingRoomMotion1);
inputMsg->setLivingRoomMotion2(livingRoomMotion2);
inputMsg->setFrontDoorEntry(frontDoorEntry);
inputMsg->setGarageDoorEntry(garageDoorEntry);
inputMsg->setPatioTemp(patioTemp);
inputMsg->setBasementMotion1(basementMotion1);
return inputMsg;
}
void *create_output_msg() {
New::NewSystemState *outputMsg = new New::NewSystemState;
return outputMsg;
}
int transform(void *msg1, void *msg2) {
// The transform function requires messages to be wrapped in a
// std::shared_ptr, which is a reference counted wrapper that will, by
// default, delete the resource when it goes out of scope. We don't want
// that here since the message is owned by the caller so we supply custom
// "deleter" C++ lambdas that do nothing
std::shared_ptr<Old::OldSystemState> spInputMsg((Old::OldSystemState*) msg1, [](void *){});
std::shared_ptr<New::NewSystemState> spOutputMsg((New::NewSystemState *) msg2, [](void *){});
try {
my_transform::execute(*spInputMsg, *spOutputMsg);
// std::cout << "After my_transform::execute()" << std::endl;
} catch (const std::exception &exc) {
std::cout << "Some exception was thrown;in catch" << std::endl;
std::cerr << exc.what();
return -1;
}
return 0;
}
const char* get_living_room_motion_1_output_string(void *msg) {
New::NewSystemState *outputMsg = (New::NewSystemState *) msg;
std::string livingRoomMotion1 = (*outputMsg).getLivingRoomMotion1();
// std::cout << "value for livingRoomMotion1:" << livingRoomMotion1 << std::endl;
return livingRoomMotion1.c_str();
}
const char* get_living_room_motion_2_output_string(void *msg) {
New::NewSystemState *outputMsg = (New::NewSystemState *) msg;
std::string livingRoomMotion2 = (*outputMsg).getLivingRoomMotion2();
// std::cout << "value for livingRoomMotion2:" << livingRoomMotion2 << std::endl;
return livingRoomMotion2.c_str();
}
const char* get_garage_door_entry_output_string(void *msg) {
New::NewSystemState *outputMsg = (New::NewSystemState *) msg;
std::string garageDoorEntry = (*outputMsg).getGarageDoorEntry();
// std::cout << "value for garageDoorEntry:" << garageDoorEntry << std::endl;
return garageDoorEntry.c_str();
}
const char* get_front_door_entry_output_string(void *msg) {
New::NewSystemState *outputMsg = (New::NewSystemState *) msg;
std::string frontDoorEntry = (*outputMsg).getFrontDoorEntry();
// std::cout << "value for frontDoorEntry:" << frontDoorEntry << std::endl;
return frontDoorEntry.c_str();
}
const char* get_basement_motion_1_output_string(void *msg) {
New::NewSystemState *outputMsg = (New::NewSystemState *) msg;
std::string basementMotion1 = (*outputMsg).getBasementMotion1();
// std::cout << "value for basementMotion1:" << basementMotion1 << std::endl;
return basementMotion1.c_str();
}
const char* get_patio_temp_output_string(void *msg) {
New::NewSystemState *outputMsg = (New::NewSystemState *) msg;
int32_t patioTemp = (*outputMsg).getPatioTemp();
// std::cout << "value for patioTemp:" << patioTemp << std::endl;
// forgive me if this is dumb, I am sure there is a better way
char str[100];
sprintf(str, "%d", patioTemp);
return std::string(str).c_str();
}
void free_input_msg(void *msg) {
Old::OldSystemState *inputMsg =
(Old::OldSystemState *) msg;
delete inputMsg;
}
void free_output_msg(void *msg) {
New::NewSystemState *outputMsg =
(New::NewSystemState *) msg;
delete outputMsg;
}
}