@@ -27,7 +27,7 @@ public static string[] GetActionClassPaths(string inFilePath, string outPath)
27
27
string rosPackageName = MessageAutoGen . GetRosPackageName ( inFilePath ) ;
28
28
string outFolder = MessageAutoGen . GetMessageOutFolder ( outPath , rosPackageName ) ;
29
29
string extension = Path . GetExtension ( inFilePath ) ;
30
- string className = MsgAutoGenUtilities . MessageClassPrefix + MsgAutoGenUtilities . CapitalizeFirstLetter ( Path . GetFileNameWithoutExtension ( inFilePath ) ) ;
30
+ string className = MsgAutoGenUtilities . CapitalizeFirstLetter ( Path . GetFileNameWithoutExtension ( inFilePath ) ) + MsgAutoGenUtilities . ActionClassSuffix ;
31
31
32
32
string [ ] result = new string [ types . Length ] ;
33
33
for ( int Idx = 0 ; Idx < types . Length ; ++ Idx )
@@ -73,10 +73,10 @@ public static List<string> GenerateSingleAction(string inPath, string outPath, s
73
73
List < MessageToken > tokens = listsOfTokens [ i ] ;
74
74
75
75
// Action is made up of goal, result, feedback
76
- string className = MsgAutoGenUtilities . MessageClassPrefix + inFileName + types [ i ] ;
76
+ string className = inFileName + types [ i ] + MsgAutoGenUtilities . ActionClassSuffix ;
77
77
78
78
// Parse and generate goal, result, feedback messages
79
- MessageParser parser = new MessageParser ( tokens , outPath , rosPackageName , "action" , MsgAutoGenUtilities . builtInTypesMapping , MsgAutoGenUtilities . builtInTypesDefaultInitialValues , MsgAutoGenUtilities . numericTypeDeserializationFunctions , MsgAutoGenUtilities . numericTypeByteSize , className ) ;
79
+ MessageParser parser = new MessageParser ( tokens , outPath , rosPackageName , "action" , MsgAutoGenUtilities . builtInTypesMapping , MsgAutoGenUtilities . builtInTypesDefaultInitialValues , className ) ;
80
80
parser . Parse ( ) ;
81
81
warnings . AddRange ( parser . GetWarnings ( ) ) ;
82
82
@@ -152,6 +152,7 @@ public class ActionWrapper
152
152
153
153
private const string ONE_TAB = " " ;
154
154
private const string TWO_TABS = " " ;
155
+ private const string THREE_TABS = " " ;
155
156
156
157
private readonly string inPath ;
157
158
private readonly string inFileName ;
@@ -199,12 +200,12 @@ private string GenerateParameterizedConstructor(string className, string msgType
199
200
200
201
if ( msgType . Equals ( "Goal" ) )
201
202
{
202
- paramsIn += "MHeader header, MGoalID goal_id, " ;
203
+ paramsIn += "HeaderMsg header, GoalIDMsg goal_id, " ;
203
204
paramsOut += "header, goal_id" ;
204
205
}
205
206
else if ( msgType . Equals ( "Result" ) || msgType . Equals ( "Feedback" ) )
206
207
{
207
- paramsIn += "MHeader header, MGoalStatus status, " ;
208
+ paramsIn += "HeaderMsg header, GoalStatusMsg status, " ;
208
209
paramsOut += "header, status" ;
209
210
}
210
211
@@ -228,47 +229,39 @@ private string GenerateParameterizedConstructor(string className, string msgType
228
229
return constructor ;
229
230
}
230
231
231
- private string GenerateSerializationStatements ( string msgType )
232
+ private string GenerateDeserializerConstructor ( string className , bool callBase = true )
232
233
{
233
- string function = "" ;
234
- function += MsgAutoGenUtilities . TWO_TABS + "public override List<byte[]> SerializationStatements()\n " ;
235
- function += MsgAutoGenUtilities . TWO_TABS + "{\n " ;
236
- function += MsgAutoGenUtilities . TWO_TABS + MsgAutoGenUtilities . ONE_TAB + "var listOfSerializations = new List<byte[]>();\n " ;
237
-
238
- string [ ] inheritedParams = new string [ 0 ] ;
239
-
240
- // Inherited params
241
- if ( msgType . Equals ( "Goal" ) )
242
- {
243
- inheritedParams = new [ ] { "header" , "goal_id" } ;
244
-
245
- }
246
- else if ( msgType . Equals ( "Result" ) || msgType . Equals ( "Feedback" ) )
247
- {
248
- inheritedParams = new [ ] { "header" , "status" } ;
249
- }
250
-
251
- foreach ( string paramName in inheritedParams )
252
- {
253
- function += TWO_TABS + ONE_TAB + "listOfSerializations.AddRange(this." + paramName + ".SerializationStatements());\n " ;
254
- }
234
+ string constructor = "" ;
235
+ string assignments = "" ;
255
236
256
237
foreach ( string identifier in symbolTable . Keys )
257
238
{
258
- function += TWO_TABS + ONE_TAB + "listOfSerializations.AddRange(this." + identifier + ".SerializationStatements());\n " ;
239
+ string type = symbolTable [ identifier ] ;
240
+
241
+ if ( MsgAutoGenUtilities . nonMessageTypes . Contains ( type ) )
242
+ {
243
+ assignments += THREE_TABS + $ "deserializer.Read(out this.{ identifier } );\n ";
244
+ }
245
+ else
246
+ {
247
+ assignments += THREE_TABS + $ "this.{ identifier } = { type } .Deserialize(deserializer);\n ";
248
+ }
259
249
}
260
250
261
- function += "\n " + MsgAutoGenUtilities . TWO_TABS + MsgAutoGenUtilities . ONE_TAB + "return listOfSerializations;\n " ;
262
- function += MsgAutoGenUtilities . TWO_TABS + "}\n \n " ;
251
+ constructor += TWO_TABS + $ "public static { className } Deserialize(MessageDeserializer deserializer) => new { className } (deserializer);\n \n ";
252
+ constructor += TWO_TABS + $ "{ className } (MessageDeserializer deserializer){ ( callBase ? " : base(deserializer)" : "" ) } \n ";
253
+ constructor += TWO_TABS + "{\n " ;
254
+ constructor += assignments ;
255
+ constructor += TWO_TABS + "}\n " ;
263
256
264
- return function ;
257
+ return constructor ;
265
258
}
266
259
267
- private string GenerateDeserializationStatements ( string msgType )
260
+ private string GenerateSerializationStatements ( string msgType )
268
261
{
269
262
string function = "" ;
270
- function += MsgAutoGenUtilities . TWO_TABS + "public override int Deserialize(byte[] data, int offset )\n " ;
271
- function += MsgAutoGenUtilities . TWO_TABS + "{\n " ;
263
+ function += TWO_TABS + "public override void SerializeTo(MessageSerializer serializer )\n " ;
264
+ function += TWO_TABS + "{\n " ;
272
265
273
266
string [ ] inheritedParams = new string [ 0 ] ;
274
267
@@ -285,24 +278,23 @@ private string GenerateDeserializationStatements(string msgType)
285
278
286
279
foreach ( string paramName in inheritedParams )
287
280
{
288
- function += TWO_TABS + ONE_TAB + "offset = this." + paramName + ".Deserialize(data, offset );\n " ;
281
+ function += THREE_TABS + "serializer.Write( this." + paramName + ");\n " ;
289
282
}
290
283
291
284
foreach ( string identifier in symbolTable . Keys )
292
285
{
293
- function += TWO_TABS + ONE_TAB + "offset = this." + identifier + ".Deserialize(data, offset );\n " ;
286
+ function += THREE_TABS + "serializer.Write( this." + identifier + ");\n " ;
294
287
}
295
288
296
- function += "\n " + MsgAutoGenUtilities . TWO_TABS + MsgAutoGenUtilities . ONE_TAB + "return offset;\n " ;
297
- function += MsgAutoGenUtilities . TWO_TABS + "}\n \n " ;
289
+ function += TWO_TABS + "}\n \n " ;
298
290
299
291
return function ;
300
292
}
301
293
302
294
public void WrapActionSections ( string type )
303
295
{
304
- string wrapperName = MsgAutoGenUtilities . MessageClassPrefix + inFileName + "Action" + type ;
305
- string msgName = MsgAutoGenUtilities . MessageClassPrefix + inFileName + type ;
296
+ string wrapperName = inFileName + "Action" + type + MsgAutoGenUtilities . ActionClassSuffix ;
297
+ string msgName = inFileName + type + MsgAutoGenUtilities . ActionClassSuffix ;
306
298
307
299
string outPath = Path . Combine ( this . outPath , wrapperName + ".cs" ) ;
308
300
@@ -333,7 +325,8 @@ public void WrapActionSections(string type)
333
325
334
326
// Write ROS package name
335
327
writer . Write (
336
- TWO_TABS + "public const string RosMessageName = \" " + rosPackageName + "/" + inFileName + "Action" + type + "\" ;\n "
328
+ TWO_TABS + "public const string k_RosMessageName = \" " + rosPackageName + "/" + inFileName + "Action" + type + "\" ;\n " +
329
+ TWO_TABS + "public override string RosMessageName => k_RosMessageName;\n \n "
337
330
) ;
338
331
339
332
// Record goal/result/feedback declaration
@@ -347,8 +340,9 @@ public void WrapActionSections(string type)
347
340
// Write parameterized constructor
348
341
writer . Write ( GenerateParameterizedConstructor ( wrapperName , type ) ) ;
349
342
343
+ writer . Write ( GenerateDeserializerConstructor ( wrapperName ) ) ;
344
+
350
345
writer . Write ( GenerateSerializationStatements ( type ) ) ;
351
- writer . Write ( GenerateDeserializationStatements ( type ) ) ;
352
346
353
347
// Close class
354
348
writer . Write ( ONE_TAB + "}\n " ) ;
@@ -359,7 +353,7 @@ public void WrapActionSections(string type)
359
353
360
354
public void WrapAction ( )
361
355
{
362
- string msgNamePrefix = MsgAutoGenUtilities . MessageClassPrefix + inFileName ;
356
+ string msgNamePrefix = inFileName + MsgAutoGenUtilities . ActionClassSuffix ;
363
357
string className = msgNamePrefix + "Action" ;
364
358
string type = "wrapper" ;
365
359
@@ -399,7 +393,8 @@ public void WrapAction()
399
393
400
394
// Write ROS package name
401
395
writer . Write (
402
- TWO_TABS + "public const string RosMessageName = \" " + rosPackageName + "/" + inFileName + "Action" + "\" ;\n "
396
+ TWO_TABS + "public const string k_RosMessageName = \" " + rosPackageName + "/" + inFileName + "Action" + "\" ;\n " +
397
+ TWO_TABS + "public override string RosMessageName => k_RosMessageName;\n \n "
403
398
) ;
404
399
405
400
// Record variables
@@ -411,10 +406,11 @@ public void WrapAction()
411
406
symbolTable . Add ( "action_feedback" , className + "Feedback" ) ;
412
407
413
408
// Write default value constructor
414
- writer . Write ( "\n " + GenerateDefaultValueConstructor ( className ) + "\n " ) ;
409
+ writer . Write ( "\n " ) ;
410
+ writer . Write ( GenerateDefaultValueConstructor ( className ) + "\n " ) ;
411
+ writer . Write ( GenerateDeserializerConstructor ( className , false ) + "\n " ) ;
415
412
416
413
writer . Write ( GenerateSerializationStatements ( type ) ) ;
417
- writer . Write ( GenerateDeserializationStatements ( type ) ) ;
418
414
419
415
// Close class
420
416
writer . Write ( ONE_TAB + "}\n " ) ;
0 commit comments