RTI Connext Micro  Version 2.4.1.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
reda_sequence_gen.h
Go to the documentation of this file.
1 /*
2  * FILE: reda_sequence_gen.h - Sequence template
3  *
4  * Copyright 2008-2014 Real-Time Innovations, Inc.
5  *
6  * No duplications, whole or partial, manual or electronic, may be made
7  * without express written permission. Any such copies, or
8  * revisions thereof, must display this notice unaltered.
9  * This code contains trade secrets of Real-Time Innovations, Inc.
10  *
11  * Modification History
12  * --------------------
13  * 04aug2014,tk MICRO-841 Only copy new_max elements if sequence is reduced
14  * 03jun2013,kaj MICRO-502 support for string sequences req. max string length
15  * 20apr2008,tk Written
16  */
17 /*ce
18  * \file
19  */
20 #ifndef reda_sequence_gen_h
21 #define reda_sequence_gen_h
22 
23 #ifndef osapi_dll_h
24 #include "osapi/osapi_dll.h"
25 #endif
26 #ifndef osapi_types_h
27 #include "osapi/osapi_types.h"
28 #endif
29 #ifndef reda_sequence_h
30 #include "reda/reda_sequence.h"
31 #endif
32 
33 #define concatenate(A, B) A ## B
34 
35 #ifndef T
36 #define T RTI_UINT32
37 #endif
38 
39 #if defined(T_copy) && !defined(T_initialize)
40 #error "T_copy defined without T_initialize"
41 #endif
42 
43 #if defined(T_compare) && !defined(T_initialize)
44 #error "T_compare defined without T_initialize"
45 #endif
46 
47 #if defined(TSeq_isStringType) && (!defined(T_initialize) || !defined(T_finalize) || !defined(T_copy))
48 #error "TSeq_isStringType defined without T_initialize/T_finalize/T_copy"
49 #endif
50 
51 #if defined(T_initialize) || defined(T_finalize) || defined(T_copy)
52 #ifndef TSeq_map
53 #define _map(TSeq) concatenate(TSeq, _map)
54 #define TSeq_map _map(TSeq)
55 RTI_PRIVATE RTI_BOOL
56 TSeq_map(struct TSeq *seq,RTI_BOOL(*map)(T*),RTI_INT32 begin, RTI_INT32 end)
57 {
58  RTI_INT32 i;
59 
60  for (i = begin; i <= end; i++)
61  {
62  if (!map(&seq->_contiguous_buffer[i]))
63  {
64  return RTI_FALSE;
65  }
66  }
67 
68  return RTI_TRUE;
69 }
70 #endif /* TSeq_map */
71 #ifdef TSeq_isStringType
72 #ifndef TSeq_map_w_length
73 #define _map_w_length(TSeq) concatenate(TSeq, _map_w_length)
74 #define TSeq_map_w_length _map_w_length(TSeq)
75 RTI_PRIVATE RTI_BOOL
76 TSeq_map_w_length(struct TSeq *seq,RTI_BOOL(*map)(T*, RTI_UINT32),RTI_INT32 begin, RTI_INT32 end, RTI_UINT32 max_str_len)
77 {
78  RTI_INT32 i;
79 
80  for (i = begin; i <= end; i++)
81  {
82  if (!map(&seq->_contiguous_buffer[i], max_str_len))
83  {
84  return RTI_FALSE;
85  }
86  }
87 
88  return RTI_TRUE;
89 }
90 #endif /* TSeq_map_w_length */
91 #endif /* TSeq_isStringType */
92 
93 #endif
94 
95 #ifndef TSeq_initialize
96 #define _initialize(TSeq) concatenate(TSeq, _initialize)
97 #define TSeq_initialize _initialize(TSeq)
99 TSeq_initialize(struct TSeq *self)
100 {
101  return REDA_Sequence_initialize((struct REDA_Sequence*)self, sizeof(T));
102 }
103 #undef _initialize
104 #undef TSeq_initialize
105 #endif /* TSeq_initialize */
106 
107 #ifndef TSeq_finalize
108 #define _finalize(TSeq) concatenate(TSeq, _finalize)
109 #define TSeq_finalize _finalize(TSeq)
110 RTI_BOOL
111 TSeq_finalize(struct TSeq *self)
112 {
113 #ifdef T_finalize
114  RTI_BOOL result;
115  result = TSeq_map(self,T_finalize,0,self->_maximum-1);
116  if (!result)
117  {
118  return result;
119  }
120  return REDA_Sequence_finalize((struct REDA_Sequence*)self);
121 #else
122  return REDA_Sequence_finalize((struct REDA_Sequence*)self);
123 #endif
124 
125 #undef _finalize
126 #undef TSeq_finalize
127 }
128 #endif /* TSeq_finalize */
129 
130 #ifndef TSeq_get_maximum
131 #define _get_maximum(TSeq) concatenate(TSeq, _get_maximum)
132 #define TSeq_get_maximum _get_maximum(TSeq)
133 RTI_INT32
134 TSeq_get_maximum(const struct TSeq *self)
135 {
136  return REDA_Sequence_get_maximum((const struct REDA_Sequence *)self);
137 
138 #undef _get_maximum
139 #undef TSeq_get_maximum
140 }
141 #endif /* TSeq_get_maximum */
142 
143 #ifndef TSeq_set_maximum
144 #define _set_maximum(TSeq) concatenate(TSeq, _set_maximum)
145 #define TSeq_set_maximum _set_maximum(TSeq)
146 #ifndef TSeq_isStringType
147 RTI_BOOL
148 TSeq_set_maximum(struct TSeq *self, RTI_INT32 new_max)
149 {
150 #ifdef T_initialize
151  struct TSeq seq_copy;
152  RTI_BOOL result;
153  RTI_INT32 i;
154 
155  OSAPI_LOG_PRECONDITION(self == NULL,
156  return RTI_FALSE,
157  (OSAPI_LOG_PRECONDITION_PARAM,"self=%p",self))
158 
159 #ifndef RTI_CPP
160  seq_copy = *self;
161 #else
162  seq_copy.copy(*self);
163 #endif
164 
165  result = REDA_Sequence_set_maximum((struct REDA_Sequence*)self,
166  new_max,RTI_FALSE);
167  if (!result)
168  {
169  return result;
170  }
171 
172  result = TSeq_map(self,T_initialize,0,self->_maximum-1);
173  if (!result)
174  {
175  return result;
176  }
177 
178  if (self->_length > 0)
179  {
180  /* If the sequence is reduced in size, only copy the first new_max elements
181  */
182  for (i = 0; i < (new_max < seq_copy._length ? new_max : seq_copy._length); ++i)
183  {
184  if (!T_copy(&self->_contiguous_buffer[i],
185  (const T*) &seq_copy._contiguous_buffer[i]))
186  {
187  return RTI_FALSE;
188  }
189  }
190  }
191 
192  if (seq_copy._contiguous_buffer != NULL)
193  {
194  result = TSeq_map(&seq_copy,T_finalize,0,seq_copy._maximum-1);
195 #ifndef RTI_CERT
196  OSAPI_Heap_free_buffer(seq_copy._contiguous_buffer);
197 #endif
198  }
199  return result;
200 #else
201  return REDA_Sequence_set_maximum((struct REDA_Sequence*)self, new_max,RTI_TRUE);
202 #endif /* T_initialize defined*/
203 }
204 #else /* TSeq_isStringType defined, which requires T_initialize defined */
205 RTI_BOOL
206 TSeq_set_maximum(struct TSeq *self, RTI_INT32 new_max, RTI_UINT32 max_str_len)
207 {
208  struct TSeq seq_copy;
209  RTI_BOOL result;
210  RTI_INT32 i, old_max;
211 
212  OSAPI_LOG_PRECONDITION(self == NULL,
213  return RTI_FALSE,
214  (OSAPI_LOG_PRECONDITION_PARAM,"self=%p",self))
215 
216  seq_copy = *self;
217 
218  old_max = REDA_Sequence_get_maximum((const struct REDA_Sequence *)self);
219 
220  /* if not resizing seqeunce then we are done */
221  if (old_max == new_max) {
222  return RTI_TRUE;
223  }
224 
225  /* cannot use copy content because it will free the contiquous buffer
226  without finalizing any discarded elements */
227  result = REDA_Sequence_set_maximum((struct REDA_Sequence*)self,
228  new_max,RTI_FALSE);
229  if (!result)
230  {
231  return result;
232  }
233 
234  /* copy elements (string memory) from old buffer to new buffer */
235  for (i = 0; i < ((old_max < new_max) ? old_max : new_max); i++)
236  {
237  self->_contiguous_buffer[i] = seq_copy._contiguous_buffer[i];
238  }
239 
240  /* initialize (allocate) any added elements (from old_max to new_max-1) */
241  result = TSeq_map_w_length(self,T_initialize,old_max,new_max-1,max_str_len);
242  if (!result)
243  {
244  return result;
245  }
246 
247  /* since the pointers were copied from old seq to new seq,
248  only finalize members between new_max and old_max-1 */
249  if (seq_copy._contiguous_buffer != NULL)
250  {
251  result = TSeq_map(&seq_copy,T_finalize,new_max,old_max-1);
252 #ifndef RTI_CERT
253  OSAPI_Heap_free_buffer(seq_copy._contiguous_buffer);
254 #endif
255  }
256  return result;
257 }
258 #endif /* TSeq_isStringType defined */
259 #undef _set_maximum
260 #undef TSeq_set_maximum
261 #endif /* TSeq_set_maximum */
262 
263 #ifndef TSeq_get_length
264 #define _get_length(TSeq) concatenate(TSeq, _get_length)
265 #define TSeq_get_length _get_length(TSeq)
266 RTI_INT32
267 TSeq_get_length(const struct TSeq *self)
268 {
269  return REDA_Sequence_get_length((const struct REDA_Sequence *)self);
270 
271 #undef _get_length
272 #undef TSeq_get_length
273 }
274 #endif /* TSeq_get_length */
275 
276 #ifndef TSeq_set_length
277 #define _set_length(TSeq) concatenate(TSeq, _set_length)
278 #define TSeq_set_length _set_length(TSeq)
279 RTI_BOOL
280 TSeq_set_length(struct TSeq *self, RTI_INT32 new_length)
281 {
282  return REDA_Sequence_set_length((struct REDA_Sequence *)self, new_length);
283 
284 #undef _set_length
285 #undef TSeq_set_length
286 }
287 #endif /* TSeq_set_length */
288 
289 #ifndef TSeq_get_reference
290 #define _get_reference(TSeq) concatenate(TSeq, _get_reference)
291 #define TSeq_get_reference _get_reference(TSeq)
292 T*
293 TSeq_get_reference(const struct TSeq *self, RTI_INT32 i)
294 {
295  return (T*)REDA_Sequence_get_reference((const struct REDA_Sequence*)self,i);
296 
297 #undef _get_reference
298 #undef TSeq_get_reference
299 }
300 #endif /* TSeq_get_reference */
301 
302 
303 #ifndef TSeq_copy
304 #define _copy(TSeq) concatenate(TSeq, _copy)
305 #define TSeq_copy _copy(TSeq)
306 #ifndef TSeq_isStringType
307 struct TSeq*
308 TSeq_copy(struct TSeq *self, const struct TSeq *src)
309 {
310 #ifdef T_copy
311  RTI_BOOL result;
312  RTI_INT32 i;
313  struct TSeq seq_copy;
314 
315  OSAPI_LOG_PRECONDITION(self == NULL,
316  return NULL,
317  (OSAPI_LOG_PRECONDITION_PARAM,"self=%p",self));
318 
319  /*
320  * avoid assignment so that it won't
321  * trigger the assignment operator in
322  * C++, which is overloaded and invokes
323  * this copy function
324  */
325  /*seq_copy = *self;*/
326  seq_copy._contiguous_buffer = self->_contiguous_buffer;
327  seq_copy._maximum = self->_maximum;
328  seq_copy._length = self->_length;
329  seq_copy._element_size = self->_element_size;
330  seq_copy._flags = self->_flags;
331  seq_copy._token1 = self->_token1;
332  seq_copy._token2 = self->_token2;
333 
334  /* replaces continguous buffer if self->max < src->size */
335  if (REDA_Sequence_copy((struct REDA_Sequence *)self,
336  (const struct REDA_Sequence *)src,
337  RTI_FALSE) == NULL)
338  {
339  return NULL;
340  }
341 
342  /* if replaced continguous buffer then initialize new members */
343  if (self->_contiguous_buffer != seq_copy._contiguous_buffer)
344  {
345  result = TSeq_map(self,T_initialize,0,self->_maximum-1);
346  if (!result)
347  {
348  return NULL;
349  }
350  }
351 
352  /* copy content from source elements to destination elements */
353  for (i = 0; i < src->_length; i++)
354  {
355  if (!T_copy(&self->_contiguous_buffer[i],
356  (const T*) &src->_contiguous_buffer[i]))
357  {
358  return NULL;
359  }
360  }
361 
362  /* if replaced continguous buffer then finalize old new members
363  and free old contiguous buffer */
364  if ((seq_copy._contiguous_buffer != NULL) &&
365  (seq_copy._contiguous_buffer != self->_contiguous_buffer))
366  {
367  result = TSeq_map(&seq_copy,T_finalize,0,seq_copy._maximum-1);
368  if (!result)
369  {
370  return NULL;
371  }
372 #ifndef RTI_CERT
373  OSAPI_Heap_free_buffer(seq_copy._contiguous_buffer);
374 #endif
375  }
376 
377  return self;
378 #else
379  return (struct TSeq*)REDA_Sequence_copy((struct REDA_Sequence *)self,
380  (const struct REDA_Sequence *)src,
381  RTI_TRUE);
382 #endif
383 }
384 #else /* TSeq_isStringType defined, which requires T_copy defined */
385 struct TSeq*
386 TSeq_copy(struct TSeq *self, const struct TSeq *src, RTI_UINT32 max_str_len)
387 {
388  RTI_BOOL result;
389  RTI_INT32 i;
390  struct TSeq seq_copy;
391 
392  /*
393  * avoid assignment so that it won't
394  * trigger the assignment operator in
395  * C++, which is overloaded and invokes
396  * this copy function
397  */
398  /*seq_copy = *self;*/
399  seq_copy._contiguous_buffer = self->_contiguous_buffer;
400  seq_copy._maximum = self->_maximum;
401  seq_copy._length = self->_length;
402  seq_copy._element_size = self->_element_size;
403  seq_copy._flags = self->_flags;
404  seq_copy._token1 = self->_token1;
405  seq_copy._token2 = self->_token2;
406 
407  OSAPI_LOG_PRECONDITION(self == NULL,
408  return NULL,
409  (OSAPI_LOG_PRECONDITION_PARAM,"self=%p",self))
410 
411  /* replaces continguous buffer if self->max < src->size */
412  if (REDA_Sequence_copy((struct REDA_Sequence *)self,
413  (const struct REDA_Sequence *)src,
414  RTI_FALSE) == NULL)
415  {
416  return NULL;
417  }
418 
419  /* if replaced continguous buffer then copy existing dest string buffers
420  from old contiguous buffer to new dest contiguous buffer,
421  and initialize (allocate string buffers) for new members added */
422  if (self->_contiguous_buffer != seq_copy._contiguous_buffer)
423  {
424  for (i = 0; i < seq_copy._maximum; i++)
425  {
426  self->_contiguous_buffer[i] = seq_copy._contiguous_buffer[i];
427  }
428  /* initialize (allocate) any added elements (from old_max to new_max-1) */
429  result = TSeq_map_w_length(self,T_initialize,seq_copy._maximum,self->_maximum-1,max_str_len);
430  if (!result)
431  {
432  return NULL;
433  }
434  }
435 
436  /* copy content from source to destination */
437  for (i = 0; i < src->_length; i++)
438  {
439  if (!T_copy(&self->_contiguous_buffer[i],
440  (const T*) &src->_contiguous_buffer[i],
441  max_str_len))
442  {
443  return NULL;
444  }
445  }
446 
447 #ifndef RTI_CERT
448  /* if replaced continguous buffer then free old contiguous buffer (do not
449  finalize string buffers since were transfered to new contiguous buffer */
450  if ((seq_copy._contiguous_buffer != NULL) &&
451  (seq_copy._contiguous_buffer != self->_contiguous_buffer))
452  {
453  OSAPI_Heap_free_buffer(seq_copy._contiguous_buffer);
454  }
455 #endif
456 
457  return self;
458 }
459 #endif
460 #undef _copy
461 #undef TSeq_copy
462 #endif /* TSeq_copy */
463 
464 #ifndef TSeq_is_equal
465 #define _is_equal(TSeq) concatenate(TSeq, _is_equal)
466 #define TSeq_is_equal _is_equal(TSeq)
467 RTI_BOOL
468 TSeq_is_equal(const struct TSeq *left, const struct TSeq *right)
469 {
470 #ifdef T_compare
471  RTI_INT32 i;
472  RTI_BOOL result;
473 
474  result = REDA_Sequence_is_equal((const struct REDA_Sequence*)left,
475  (const struct REDA_Sequence*)right,RTI_FALSE);
476  if (!result)
477  {
478  return result;
479  }
480 
481  for (i = 0; i < left->_length; i++)
482  {
483  if (T_compare((const T*)&left->_contiguous_buffer[i],
484  (const T*)&right->_contiguous_buffer[i]))
485  {
486  return RTI_FALSE;
487  }
488  }
489 
490  return RTI_TRUE;
491 #else
492  return REDA_Sequence_is_equal((const struct REDA_Sequence *)left,
493  (const struct REDA_Sequence *)right,RTI_TRUE);
494 #endif
495 #undef _is_equal
496 #undef TSeq_is_equal
497 }
498 #endif /* TSeq_is_equal */
499 
500 #ifndef TSeq_loan_contiguous
501 #define _loan_contiguous(TSeq) concatenate(TSeq, _loan_contiguous)
502 #define TSeq_loan_contiguous _loan_contiguous(TSeq)
503 RTI_BOOL
504 TSeq_loan_contiguous(struct TSeq *self, void *buffer,
505  RTI_INT32 new_length, RTI_INT32 new_max)
506 {
507  return REDA_Sequence_loan_contiguous(
508  (struct REDA_Sequence *)self, buffer, new_length, new_max);
509 }
510 #undef _loan_contiguous
511 #undef TSeq_loan_contiguous
512 #endif /* TSeq_loan_contiguous */
513 
514 #ifndef TSeq_loan_discontiguous
515 #define _loan_discontiguous(TSeq) concatenate(TSeq, _loan_discontiguous)
516 #define TSeq_loan_discontiguous _loan_discontiguous(TSeq)
517 RTI_BOOL
518 TSeq_loan_discontiguous(struct TSeq *self, void *buffer,
519  RTI_INT32 new_length, RTI_INT32 new_max)
520 {
521  return REDA_Sequence_loan_discontiguous(
522  (struct REDA_Sequence *)self, buffer, new_length, new_max);
523 }
524 #undef _loan_discontiguous
525 #undef TSeq_loan_discontiguous
526 #endif /* TSeq_loan_discontiguous */
527 
528 
529 #ifndef TSeq_has_ownership
530 #define _has_ownership(TSeq) concatenate(TSeq, _has_ownership)
531 #define TSeq_has_ownership _has_ownership(TSeq)
532 RTI_BOOL
533 TSeq_has_ownership(const struct TSeq *self)
534 {
535  return REDA_Sequence_has_ownership((const struct REDA_Sequence *)self);
536 }
537 
538 #undef _has_ownership
539 #undef TSeq_has_ownership
540 #endif /* TSeq_has_ownership */
541 
542 
543 #ifndef TSeq_has_discontiguous_buffer
544 #define _has_discontiguous_buffer(TSeq) concatenate(TSeq, _has_discontiguous_buffer)
545 #define TSeq_has_discontiguous_buffer _has_discontiguous_buffer(TSeq)
546 RTI_BOOL
547 TSeq_has_discontiguous_buffer(const struct TSeq *self)
548 {
549  return REDA_Sequence_has_discontiguous_buffer(
550  (const struct REDA_Sequence *)self);
551 }
552 
553 #undef _has_discontiguous_buffer
554 #undef TSeq_has_discontiguous_buffer
555 #endif /* TSeq_has_discontiguous_buffer */
556 
557 
558 #ifndef TSeq_get_buffer
559 #define _get_buffer(TSeq) concatenate(TSeq, _get_buffer)
560 #define TSeq_get_buffer _get_buffer(TSeq)
561 T*
562 TSeq_get_buffer(const struct TSeq *self)
563 {
564  return (T*)REDA_Sequence_get_buffer((const struct REDA_Sequence *)self);
565 }
566 
567 #undef _get_buffer
568 #undef TSeq_get_buffer
569 #endif /* TSeq_get_buffer */
570 
571 
572 #ifndef TSeq_set_buffer
573 #define _set_buffer(TSeq) concatenate(TSeq, _set_buffer)
574 #define TSeq_set_buffer _set_buffer(TSeq)
575 RTI_BOOL
576 TSeq_set_buffer(struct TSeq *self, T *buffer)
577 {
578  return REDA_Sequence_set_buffer((struct REDA_Sequence *)self,
579  (void *)buffer);
580 }
581 
582 #undef _set_buffer
583 #undef TSeq_set_buffer
584 #endif /* TSeq_set_buffer */
585 
586 
587 #ifndef TSeq_unloan
588 #define _unloan(TSeq) concatenate(TSeq, _unloan)
589 #define TSeq_unloan _unloan(TSeq)
590 RTI_BOOL
591 TSeq_unloan(struct TSeq *self)
592 {
593  return REDA_Sequence_unloan((struct REDA_Sequence *)self);
594 }
595 #undef _unloan
596 #undef TSeq_unloan
597 #endif /* TSeq_unloan */
598 
599 #ifndef TSeq_set_token
600 #define _set_token(TSeq) concatenate(TSeq, _set_token)
601 #define TSeq_set_token _set_token(TSeq)
602 void
603 TSeq_set_token(struct TSeq *self,void *token1,void *token2)
604 {
605  REDA_Sequence_set_token((struct REDA_Sequence *)self,token1,token2);
606 }
607 #undef _set_token
608 #undef TSeq_set_token
609 #endif /* TSeq_set_token */
610 
611 #ifndef TSeq_get_token
612 #define _get_token(TSeq) concatenate(TSeq, _get_token)
613 #define TSeq_get_token _get_token(TSeq)
614 void
615 TSeq_get_token(struct TSeq *self,void **token1,void **token2)
616 {
617  REDA_Sequence_get_token((struct REDA_Sequence *)self,token1,token2);
618 }
619 #undef _get_token
620 #undef TSeq_get_token
621 #endif /* TSeq_get_token */
622 
623 
624 #undef _map
625 #undef TSeq_map
626 #undef _map_w_length
627 #undef TSeq_map_w_length
628 #undef _binary
629 #undef TSeq_binary
630 #undef concatenate
631 
632 
633 #endif /* reda_sequence_gen_h */

RTI Connext Micro Version 2.4.1.0 Copyright © Thu Nov 20 2014 Real-Time Innovations, Inc