Unreal SDK  v1.1
Loading...
Searching...
No Matches
WeArtMessageSerializer.h
Go to the documentation of this file.
1#pragma once
2
3#include "WeArtMessages.h"
4#include <sstream>
5
10{
11
12public:
13 const char separator = ':';
14
15
16 static constexpr unsigned long long int HashStringToInt(const char* str, unsigned long long int hash = 0) {
17 return (*str == 0) ? hash : 101 * HashStringToInt(str + 1) + *str;
18 }
19
20 static WeArtMessage* createMessage(std::string& ID) {
21 switch (HashStringToInt(ID.c_str()))
22 {
23 case HashStringToInt("StartFromClient"): {
25 return msg;
26 break;
27 }
28
29 case HashStringToInt("StopFromClient"): {
31 return msg;
32 break;
33 }
34 case HashStringToInt("exit"): {
35 WeArtMessage* msg = new ExitMessage();
36 return msg;
37 break;
38 }
39 case HashStringToInt("disconnect"): {
41 return msg;
42 break;
43 }
44 case HashStringToInt("temperature"): {
46 return msg;
47 break;
48 }
49 case HashStringToInt("stopTemperature"): {
51 return msg;
52 break;
53 }
54 case HashStringToInt("force"): {
55 WeArtMessage* msg = new SetForceMessage();
56 return msg;
57 break;
58 }
59 case HashStringToInt("stopForce"): {
60 WeArtMessage* msg = new StopForceMessage();
61 return msg;
62 break;
63 }
64 case HashStringToInt("texture"): {
66 return msg;
67 break;
68 }
69 case HashStringToInt("stopTexture"): {
71 return msg;
72 break;
73 }
74 case HashStringToInt("Tracking"): {
75 WeArtMessage* msg = new TrackingMessage();
76 return msg;
77 break;
78 }
79
80 default:
81 return nullptr;
82 break;
83 }
84 };
85
86
87 // WeArtMessage to std::string.
88 std::string Serialize(WeArtMessage* message)
89 {
90 // Unlike C#, C++ does not allow dynamic runtime
91 // reflection, hence we have to use dedicated functions
92 // to get the values of the class members.
93 std::string messageID = message->getID();
94 std::vector<std::string> serializedValues = message->getValues();
95
96 // Join the string arrays.
97 serializedValues.insert(serializedValues.begin(), messageID);
98
99 // Then merge using our separator.
100 std::stringstream ss;
101 auto it = serializedValues.begin();
102 ss << *it++;
103 for (; it != serializedValues.end(); it++) {
104 ss << separator;
105 ss << *it;
106 }
107
108 return ss.str();
109 };
110
111 // Std::string to bytestream.
112 uint8* Serialize(std::string text)
113 {
114 // Simple reinterpret cast, not even necessary
115 // to allocate new memory.
116 uint8* byteData = reinterpret_cast<uint8*>(&text[0]);
117 return byteData;
118 }
119
120 // Std::string to message class
121 WeArtMessage* Deserialize(std::string data)
122 {
123 std::vector<std::string> strings;
124 std::istringstream dataStream(data);
125 std::string s;
126 while (getline(dataStream, s, separator)) {
127 strings.push_back(s);
128 }
129
130 // Create the correct message class from first (ID) string.
131 std::string messageID = strings[0];
132 WeArtMessage *msg = createMessage(messageID);
133
134 // Initialize the message values using the rest of the strings.
135 if (msg) {
136 strings.erase(strings.begin());
137 msg->setValues(strings);
138 }
139
140 return msg;
141 }
142
143 // Bytestream to std::string.
144 std::string Deserialize(uint8* byteData, int byteCount)
145 {
146 std::string text = std::string(reinterpret_cast<char*>(byteData));
147 return text;
148 }
149};
Message to the middleware to disconnect.
Definition: WeArtMessages.h:201
Message to the middleware to exit the connection.
Definition: WeArtMessages.h:192
Message to the middleware to set the force of the effect.
Definition: WeArtMessages.h:264
Message to the middleware to set the temperature of the effect.
Definition: WeArtMessages.h:211
Message to the middleware to set the texture of the effect.
Definition: WeArtMessages.h:322
Message to middleware to start the connection.
Definition: WeArtMessages.h:135
Message to the middleware to stop the temperature of the effect.
Definition: WeArtMessages.h:299
Message to middleware to stop the connection.
Definition: WeArtMessages.h:165
Message to the middleware to stop the temperature of the effect.
Definition: WeArtMessages.h:241
Message to the middleware to stop the texture of the effect.
Definition: WeArtMessages.h:369
Generic Tracking message, contains information on closure and abduction (based on tracking type)
Definition: WeArtMessages.h:392
Generic Weart message.
Definition: WeArtMessages.h:94
virtual void setValues(std::vector< std::string > &values)=0
virtual std::string getID()=0
virtual std::vector< std::string > getValues()=0
Serializer for communication with the middleware.
Definition: WeArtMessageSerializer.h:10
const char separator
Definition: WeArtMessageSerializer.h:13
static WeArtMessage * createMessage(std::string &ID)
Definition: WeArtMessageSerializer.h:20
uint8 * Serialize(std::string text)
Definition: WeArtMessageSerializer.h:112
std::string Deserialize(uint8 *byteData, int byteCount)
Definition: WeArtMessageSerializer.h:144
WeArtMessage * Deserialize(std::string data)
Definition: WeArtMessageSerializer.h:121
static constexpr unsigned long long int HashStringToInt(const char *str, unsigned long long int hash=0)
Definition: WeArtMessageSerializer.h:16
std::string Serialize(WeArtMessage *message)
Definition: WeArtMessageSerializer.h:88