blob: 6957e21cf9048fe7f8dc44cef6ddc86928371571 [file] [log] [blame]
Ernesto Posse8a4f2962015-05-12 13:28:46 -04001// umlrtobjectclass.cc
2
3/*******************************************************************************
4* Copyright (c) 2014-2015 Zeligsoft (2009) Limited and others.
5* All rights reserved. This program and the accompanying materials
6* are made available under the terms of the Eclipse Public License v1.0
7* which accompanies this distribution, and is available at
8* http://www.eclipse.org/legal/epl-v10.html
9*******************************************************************************/
10
11#include "umlrtobjectclass.hh"
12#include "basedebugtype.hh"
13#include "basedebug.hh"
14#include <stdlib.h>
15
16// Type descriptor used for encoding/decoding data of a given type.
17// A number of UMLRTType_xxx descriptors are pre-defined by the library.
18
19// Default initializer - zeros the data and returns a pointer to next byte.
20
21void * UMLRTObject_initialize( const UMLRTObject_class * desc, void * data )
22{
23 uint8_t * s = (uint8_t *)data;
24
25 if (desc->fields == NULL)
26 {
27 memset(s, 0, desc->size);
28 s += desc->size;
29 }
30 else
31 {
32 for (size_t i = 0; i < desc->size; ++i)
33 {
34 desc->fields[i].desc->initialize(desc->fields[i].desc, s + desc->fields[i].offset);
35 }
36 }
37 return (void *)s;
38}
39
40// Default copy - copies the data and returns a pointer to the byte following the src data.
41// The return value is the byte following the last byte copied to the destination.
42void * UMLRTObject_copy( const UMLRTObject_class * desc, const void * src, void * dst )
43{
44 uint8_t * s = (uint8_t *)src;
45 uint8_t * end_p = (uint8_t *)dst;
46 uint8_t * d = (uint8_t *)dst;
47
48 if (desc->fields == NULL)
49 {
50 memcpy(d, s, desc->size);
51 end_p += desc->size;
52 }
53 else
54 {
55 for (size_t i = 0; i < desc->size; ++i)
56 {
57 // overwrite end_p here - the return value for the last field is the one that is relevant.
58 end_p = (uint8_t *)desc->fields[i].desc->copy(desc->fields[i].desc, s + desc->fields[i].offset, d + desc->fields[i].offset);
59 }
60 }
61 return (void *)end_p;
62}
63
64// Default decode is a copy (returns a pointer to the byte after the src data).
65// For now, we'll use the return value of the copy (a value relative to dst) to compute what the return value should be.
66size_t UMLRTObject_getSize( const UMLRTObject_class * desc )
67{
68 size_t size = desc->size;
69
70 if (desc->fields != NULL)
71 {
72 size = desc->fields[desc->size - 1].offset +
73 desc->fields[desc->size - 1].desc->getSize(desc->fields[desc->size - 1].desc);
74 }
75 return size;
76}
77
78void * UMLRTObject_decode( const UMLRTObject_class * desc, const void * src, void * dst )
79{
80 desc->copy(desc, src, dst); // ignore return value.
81
82 return (void *)((uint8_t *)src + UMLRTObject_getSize(desc));
83}
84
85// Default encode is a copy (returns a pointer to the byte after the dst data).
86void * UMLRTObject_encode( const UMLRTObject_class * desc, const void * src, void * dst )
87{
88 return desc->copy(desc, src, dst);
89}
90
91// Default destroy does nothing (but returns a pointer to the byte after the data).
92void * UMLRTObject_destroy( const UMLRTObject_class * desc, void * data )
93{
94 uint8_t * s = (uint8_t *)data;
95 uint8_t * end_p = (uint8_t *)data;
96
97 if (desc->fields != NULL)
98 {
99 for (size_t i = 0; i < desc->size; ++i)
100 {
101 end_p = (uint8_t *)desc->fields[i].desc->destroy(desc->fields[i].desc, s + desc->fields[i].offset);
102 }
103 }
104 else
105 {
106 end_p += desc->size;
107 }
108 return (void *)end_p;
109}
110
111int UMLRTObject_fprintf( FILE *ostream, const UMLRTObject_class * desc, const void * data )
112{
113 int nchar = 0;
114 uint8_t * s = (uint8_t *)data;
115
116 if (desc->fields != NULL)
117 {
118 nchar += fprintf(ostream, "{%s:", desc->name);
119
120 for (size_t i = 0; i < desc->size; ++i)
121 {
122 nchar += fprintf(ostream, "{%s:", desc->fields[i].name);
123 nchar += UMLRTObject_fprintf(ostream, desc->fields[i].desc, s + desc->fields[i].offset);
124 nchar += fprintf(ostream, "}");
125 }
126 nchar += fprintf(ostream, "}");
127 }
128 else if (desc->fprintf != 0)
129 {
130 nchar += desc->fprintf(ostream, desc, data);
131 }
132 else
133 {
134 nchar += fprintf(ostream, "{%s: (unable to print)}", desc->name);
135 }
136 return nchar;
137}
138
Barry Maher98dd5192015-06-02 15:45:06 -0400139static int UMLRTObject_fprintf_bool( FILE *ostream, const UMLRTObject_class * desc, const void * data )
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400140{
141 int nchar = 0;
142 bool b;
143 desc->copy(desc, data, &b);
144 nchar += fprintf(ostream, "{bool %s}", b ? "true" : "false");
145 return nchar;
146}
147
Barry Maher98dd5192015-06-02 15:45:06 -0400148static int UMLRTObject_fprintf_char( FILE *ostream, const UMLRTObject_class * desc, const void * data )
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400149{
150 int nchar = 0;
151 char c;
152 desc->copy(desc, data, &c);
153 nchar += fprintf(ostream, "{char %c}", c);
154 return nchar;
155}
156
Barry Maher98dd5192015-06-02 15:45:06 -0400157static int UMLRTObject_fprintf_double( FILE *ostream, const UMLRTObject_class * desc, const void * data )
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400158{
159 int nchar = 0;
160 double d;
161 desc->copy(desc, data, &d);
162 nchar += fprintf(ostream, "{double %f}", d);
163 return nchar;
164}
165
Barry Maher98dd5192015-06-02 15:45:06 -0400166static int UMLRTObject_fprintf_float( FILE *ostream, const UMLRTObject_class * desc, const void * data )
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400167{
168 int nchar = 0;
169 float f;
170 desc->copy(desc, data, &f);
171 nchar += fprintf(ostream, "{float %f}", f);
172 return nchar;
173}
174
Barry Maher98dd5192015-06-02 15:45:06 -0400175static int UMLRTObject_fprintf_int( FILE *ostream, const UMLRTObject_class * desc, const void * data )
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400176{
177 int nchar = 0;
178 int i;
179 desc->copy(desc, data, &i);
180 nchar += fprintf(ostream, "{int %d}", i);
181 return nchar;
182}
183
Barry Maher98dd5192015-06-02 15:45:06 -0400184static int UMLRTObject_fprintf_long( FILE *ostream, const UMLRTObject_class * desc, const void * data )
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400185{
186 int nchar = 0;
187 long l;
188 desc->copy(desc, data, &l);
189 nchar += fprintf(ostream, "{long %ld}", l);
190 return nchar;
191}
192
Barry Maher98dd5192015-06-02 15:45:06 -0400193static int UMLRTObject_fprintf_longlong( FILE *ostream, const UMLRTObject_class * desc, const void * data )
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400194{
195 int nchar = 0;
196 long long ll;
197 desc->copy(desc, data, &ll);
198 nchar += fprintf(ostream, "{longlong %lld}", ll);
199 return nchar;
200}
201
Barry Maher98dd5192015-06-02 15:45:06 -0400202static int UMLRTObject_fprintf_ptr( FILE *ostream, const UMLRTObject_class * desc, const void * data )
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400203{
204 int nchar = 0;
205 void * p;
206 desc->copy(desc, data, &p);
207 // Pointer de-referencing tracked by Bug 97.
208 nchar += fprintf(ostream, "{ptr %p}", p);
209 return nchar;
210}
211
Barry Maher98dd5192015-06-02 15:45:06 -0400212static int UMLRTObject_fprintf_short( FILE *ostream, const UMLRTObject_class * desc, const void * data )
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400213{
214 int nchar = 0;
215 short sh;
216 desc->copy(desc, data, &sh);
217 nchar += fprintf(ostream, "{short %d}", sh);
218 return nchar;
219}
220
Barry Maher98dd5192015-06-02 15:45:06 -0400221static int UMLRTObject_fprintf_uchar( FILE *ostream, const UMLRTObject_class * desc, const void * data )
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400222{
223 int nchar = 0;
224 unsigned char c;
225 desc->copy(desc, data, &c);
226 nchar += fprintf(ostream, "{char %u}", c);
227 return nchar;
228}
229
Barry Maher98dd5192015-06-02 15:45:06 -0400230static int UMLRTObject_fprintf_uint( FILE *ostream, const UMLRTObject_class * desc, const void * data )
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400231{
232 int nchar = 0;
233 unsigned int i;
234 desc->copy(desc, data, &i);
235 nchar += fprintf(ostream, "{uint %u}", i);
236 return nchar;
237}
238
Barry Maher98dd5192015-06-02 15:45:06 -0400239static int UMLRTObject_fprintf_ulong( FILE *ostream, const UMLRTObject_class * desc, const void * data )
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400240{
241 int nchar = 0;
242 unsigned long l;
243 desc->copy(desc, data, &l);
244 nchar += fprintf(ostream, "{ulong %lu}", l);
245 return nchar;
246}
247
Barry Maher98dd5192015-06-02 15:45:06 -0400248static int UMLRTObject_fprintf_ulonglong( FILE *ostream, const UMLRTObject_class * desc, const void * data )
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400249{
250 int nchar = 0;
251 unsigned long long ll;
252 desc->copy(desc, data, &ll);
253 nchar += fprintf(ostream, "{ulonglong %llu}", ll);
254 return nchar;
255}
256
Barry Maher98dd5192015-06-02 15:45:06 -0400257static int UMLRTObject_fprintf_ushort( FILE *ostream, const UMLRTObject_class * desc, const void * data )
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400258{
259 int nchar = 0;
260 unsigned short sh;
261 desc->copy(desc, data, &sh);
262 nchar += fprintf(ostream, "{ushort %u}", sh);
263 return nchar;
264}
265
Barry Maher98dd5192015-06-02 15:45:06 -0400266static const UMLRTObject_class UMLRTType_bool_
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400267= {
268 "bool",
269 UMLRTObject_initialize,
270 UMLRTObject_copy,
271 UMLRTObject_decode,
272 UMLRTObject_encode,
273 UMLRTObject_destroy,
274 UMLRTObject_getSize,
275 UMLRTObject_fprintf_bool,
276 NULL, // super
277 UMLRTOBJECTCLASS_DEFAULT_VERSION, // version
278 UMLRTOBJECTCLASS_DEFAULT_BACKWARDS, // backwards
279 sizeof(bool),
280 NULL // fields
281};
282
283
Barry Maher98dd5192015-06-02 15:45:06 -0400284static const UMLRTObject_class UMLRTType_char_
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400285= {
286 "char",
287 UMLRTObject_initialize,
288 UMLRTObject_copy,
289 UMLRTObject_decode,
290 UMLRTObject_encode,
291 UMLRTObject_destroy,
292 UMLRTObject_getSize,
293 UMLRTObject_fprintf_char,
294 NULL, // super
295 UMLRTOBJECTCLASS_DEFAULT_VERSION, // version
296 UMLRTOBJECTCLASS_DEFAULT_BACKWARDS, // backwards
297 sizeof(char),
298 NULL, // fields
299};
300
Barry Maher98dd5192015-06-02 15:45:06 -0400301static const UMLRTObject_class UMLRTType_double_
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400302= {
303 "double",
304 UMLRTObject_initialize,
305 UMLRTObject_copy,
306 UMLRTObject_decode,
307 UMLRTObject_encode,
308 UMLRTObject_destroy,
309 UMLRTObject_getSize,
310 UMLRTObject_fprintf_double,
311 NULL, // super
312 UMLRTOBJECTCLASS_DEFAULT_VERSION, // version
313 UMLRTOBJECTCLASS_DEFAULT_BACKWARDS, // backwards
314 sizeof(double),
315 NULL, // fields
316};
317
Barry Maher98dd5192015-06-02 15:45:06 -0400318static const UMLRTObject_class UMLRTType_float_
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400319= {
320 "float",
321 UMLRTObject_initialize,
322 UMLRTObject_copy,
323 UMLRTObject_decode,
324 UMLRTObject_encode,
325 UMLRTObject_destroy,
326 UMLRTObject_getSize,
327 UMLRTObject_fprintf_float,
328 NULL, // super
329 UMLRTOBJECTCLASS_DEFAULT_VERSION, // version
330 UMLRTOBJECTCLASS_DEFAULT_BACKWARDS, // backwards
331 sizeof(float),
332 NULL, // fields
333};
334
Barry Maher98dd5192015-06-02 15:45:06 -0400335static const UMLRTObject_class UMLRTType_int_
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400336= {
337 "int",
338 UMLRTObject_initialize,
339 UMLRTObject_copy,
340 UMLRTObject_decode,
341 UMLRTObject_encode,
342 UMLRTObject_destroy,
343 UMLRTObject_getSize,
344 UMLRTObject_fprintf_int,
345 NULL, // super
346 UMLRTOBJECTCLASS_DEFAULT_VERSION, // version
347 UMLRTOBJECTCLASS_DEFAULT_BACKWARDS, // backwards
348 sizeof(int),
349 NULL, // fields
350};
351
Barry Maher98dd5192015-06-02 15:45:06 -0400352static const UMLRTObject_class UMLRTType_long_
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400353= {
354 "long",
355 UMLRTObject_initialize,
356 UMLRTObject_copy,
357 UMLRTObject_decode,
358 UMLRTObject_encode,
359 UMLRTObject_destroy,
360 UMLRTObject_getSize,
361 UMLRTObject_fprintf_long,
362 NULL, // super
363 UMLRTOBJECTCLASS_DEFAULT_VERSION, // version
364 UMLRTOBJECTCLASS_DEFAULT_BACKWARDS, // backwards
365 sizeof(long),
366 NULL, // fields
367};
368
Barry Maher98dd5192015-06-02 15:45:06 -0400369static const UMLRTObject_class UMLRTType_longlong_
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400370= {
371 "longlong",
372 UMLRTObject_initialize,
373 UMLRTObject_copy,
374 UMLRTObject_decode,
375 UMLRTObject_encode,
376 UMLRTObject_destroy,
377 UMLRTObject_getSize,
378 UMLRTObject_fprintf_longlong,
379 NULL, // super
380 UMLRTOBJECTCLASS_DEFAULT_VERSION, // version
381 UMLRTOBJECTCLASS_DEFAULT_BACKWARDS, // backwards
382 sizeof(long long),
383 NULL, // fields
384};
385
Barry Maher98dd5192015-06-02 15:45:06 -0400386static const UMLRTObject_class UMLRTType_ptr_
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400387= {
388 "ptr",
389 UMLRTObject_initialize,
390 UMLRTObject_copy,
391 UMLRTObject_decode,
392 UMLRTObject_encode,
393 UMLRTObject_destroy,
394 UMLRTObject_getSize,
395 UMLRTObject_fprintf_ptr,
396 NULL, // super
397 UMLRTOBJECTCLASS_DEFAULT_VERSION, // version
398 UMLRTOBJECTCLASS_DEFAULT_BACKWARDS, // backwards
399 sizeof(void *),
400 NULL // fields
401};
402
Barry Maher98dd5192015-06-02 15:45:06 -0400403static const UMLRTObject_class UMLRTType_short_
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400404= {
405 "short",
406 UMLRTObject_initialize,
407 UMLRTObject_copy,
408 UMLRTObject_decode,
409 UMLRTObject_encode,
410 UMLRTObject_destroy,
411 UMLRTObject_getSize,
412 UMLRTObject_fprintf_short,
413 NULL, // super
414 UMLRTOBJECTCLASS_DEFAULT_VERSION, // version
415 UMLRTOBJECTCLASS_DEFAULT_BACKWARDS, // backwards
416 sizeof(short),
417 NULL, // fields
418};
419
Barry Maher98dd5192015-06-02 15:45:06 -0400420static const UMLRTObject_class UMLRTType_uchar_
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400421= {
422 "uchar",
423 UMLRTObject_initialize,
424 UMLRTObject_copy,
425 UMLRTObject_decode,
426 UMLRTObject_encode,
427 UMLRTObject_destroy,
428 UMLRTObject_getSize,
429 UMLRTObject_fprintf_uchar,
430 NULL, // super
431 UMLRTOBJECTCLASS_DEFAULT_VERSION, // version
432 UMLRTOBJECTCLASS_DEFAULT_BACKWARDS, // backwards
433 sizeof(unsigned char),
434 NULL, // fields
435};
436
Barry Maher98dd5192015-06-02 15:45:06 -0400437static const UMLRTObject_class UMLRTType_uint_
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400438= {
439 "uint",
440 UMLRTObject_initialize,
441 UMLRTObject_copy,
442 UMLRTObject_decode,
443 UMLRTObject_encode,
444 UMLRTObject_destroy,
445 UMLRTObject_getSize,
446 UMLRTObject_fprintf_uint,
447 NULL, // super
448 UMLRTOBJECTCLASS_DEFAULT_VERSION, // version
449 UMLRTOBJECTCLASS_DEFAULT_BACKWARDS, // backwards
450 sizeof(unsigned int),
451 NULL, // fields
452};
453
Barry Maher98dd5192015-06-02 15:45:06 -0400454static const UMLRTObject_class UMLRTType_ulong_
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400455= {
456 "ulong",
457 UMLRTObject_initialize,
458 UMLRTObject_copy,
459 UMLRTObject_decode,
460 UMLRTObject_encode,
461 UMLRTObject_destroy,
462 UMLRTObject_getSize,
463 UMLRTObject_fprintf_ulong,
464 NULL, // super
465 UMLRTOBJECTCLASS_DEFAULT_VERSION, // version
466 UMLRTOBJECTCLASS_DEFAULT_BACKWARDS, // backwards
467 sizeof(unsigned long),
468 NULL, // fields
469};
470
Barry Maher98dd5192015-06-02 15:45:06 -0400471static const UMLRTObject_class UMLRTType_ulonglong_
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400472= {
473 "ulonglong",
474 UMLRTObject_initialize,
475 UMLRTObject_copy,
476 UMLRTObject_decode,
477 UMLRTObject_encode,
478 UMLRTObject_destroy,
479 UMLRTObject_getSize,
480 UMLRTObject_fprintf_ulonglong,
481 NULL, // super
482 UMLRTOBJECTCLASS_DEFAULT_VERSION, // version
483 UMLRTOBJECTCLASS_DEFAULT_BACKWARDS, // backwards
484 sizeof(unsigned long long),
485 NULL, // fields
486};
487
Barry Maher98dd5192015-06-02 15:45:06 -0400488static const UMLRTObject_class UMLRTType_ushort_
Ernesto Posse8a4f2962015-05-12 13:28:46 -0400489= {
490 "ushort",
491 UMLRTObject_initialize,
492 UMLRTObject_copy,
493 UMLRTObject_decode,
494 UMLRTObject_encode,
495 UMLRTObject_destroy,
496 UMLRTObject_getSize,
497 UMLRTObject_fprintf_ushort,
498 NULL, // super
499 UMLRTOBJECTCLASS_DEFAULT_VERSION, // version
500 UMLRTOBJECTCLASS_DEFAULT_BACKWARDS, // backwards
501 sizeof(unsigned short),
502 NULL, // fields
503};
504
505const UMLRTObject_class * const UMLRTType_bool = &UMLRTType_bool_;
506const UMLRTObject_class * const UMLRTType_char = &UMLRTType_char_;
507const UMLRTObject_class * const UMLRTType_double = &UMLRTType_double_;
508const UMLRTObject_class * const UMLRTType_float = &UMLRTType_float_;
509const UMLRTObject_class * const UMLRTType_int = &UMLRTType_int_;
510const UMLRTObject_class * const UMLRTType_long = &UMLRTType_long_;
511const UMLRTObject_class * const UMLRTType_longlong = &UMLRTType_longlong_;
512const UMLRTObject_class * const UMLRTType_ptr = &UMLRTType_ptr_;
513const UMLRTObject_class * const UMLRTType_short = &UMLRTType_short_;
514const UMLRTObject_class * const UMLRTType_uchar = &UMLRTType_uchar_;
515const UMLRTObject_class * const UMLRTType_uint = &UMLRTType_uint_;
516const UMLRTObject_class * const UMLRTType_ulong = &UMLRTType_ulong_;
517const UMLRTObject_class * const UMLRTType_ulonglong = &UMLRTType_ulonglong_;
518const UMLRTObject_class * const UMLRTType_ushort = &UMLRTType_ushort_;
519