Array.hh 99.8 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
Adam Knapp's avatar
Adam Knapp committed
2
 * Copyright (c) 2000-2021 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v2.0
Elemer Lelik's avatar
Elemer Lelik committed
5
 * which accompanies this distribution, and is available at
6
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
Elemer Lelik's avatar
Elemer Lelik committed
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * Contributors:
 *   Baji, Laszlo
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Beres, Szabolcs
 *   Delic, Adam
 *   Kovacs, Ferenc
 *   Raduly, Csaba
 *   Szabados, Kristof
 *   Szabo, Janos Zoltan – initial implementation
 *
 ******************************************************************************/
Elemer Lelik's avatar
Elemer Lelik committed
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef ARRAY_HH
#define ARRAY_HH

#include "Types.h"
#include "Param_Types.hh"
#include "Error.hh"
#include "Logger.hh"
#include "Basetype.hh"
#include "Template.hh"
#include "Optional.hh"
#include "Parameters.h"
#include "Integer.hh"
32
#include "Float.hh"
Elemer Lelik's avatar
Elemer Lelik committed
33
34
#include "Struct_of.hh"
#include "memory.h"
35
#include "Component.hh"
Elemer Lelik's avatar
Elemer Lelik committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

class INTEGER;

extern unsigned int get_timer_array_index(int index_value,
  unsigned int array_size, int index_offset);
extern unsigned int get_timer_array_index(const INTEGER& index_value,
  unsigned int array_size, int index_offset);

/** @brief Runtime implementation of timer arrays.
 *
 * @param T_type the type of the array element. This can be \c TIMER_ARRAY
 * 		 for multi-dimensional arrays.
 * @param array_size the number of elements in the array
 * @param index_offset the lowest index
 *
 * */
template <typename T_type, unsigned int array_size, int index_offset>
Elemer Lelik's avatar
Elemer Lelik committed
53
54
55
56
57
class TIMER_ARRAY 
#ifdef TITAN_RUNTIME_2
  : public RefdIndexInterface
#endif
{
Elemer Lelik's avatar
Elemer Lelik committed
58
59
60
61
62
63
64
65
66
67
68
69
  T_type array_elements[array_size];
  char * names[array_size];

  /// Copy constructor disallowed.
  TIMER_ARRAY(const TIMER_ARRAY& other_value);
  /// Assignment disallowed.
  TIMER_ARRAY& operator=(const TIMER_ARRAY& other_value);

public:
  TIMER_ARRAY() { }

  ~TIMER_ARRAY() {
Kristof Szabados's avatar
Kristof Szabados committed
70
    for (unsigned int i = 0; i < array_size; ++i) {
Elemer Lelik's avatar
Elemer Lelik committed
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
      Free(names[i]);
    }
  }

  T_type& operator[](int index_value) { return array_elements[
    get_timer_array_index(index_value, array_size, index_offset)]; }
  T_type& operator[](const INTEGER& index_value) { return array_elements[
    get_timer_array_index(index_value, array_size, index_offset)]; }

  int n_elem()   const { return array_size; }
  int size_of()  const { return array_size; }
  int lengthof() const { return array_size; }

  T_type& array_element(unsigned int index_value)
    { return array_elements[index_value]; }

  void set_name(const char * name_string)
  {
    for (int i = 0; i < (int)array_size; ++i) {
      // index_offset may be negative, hence i must be int (not size_t)
      // to ensure that signed arithmetic is used.
      names[i] = mputprintf(mcopystr(name_string), "[%d]", index_offset+i);
      array_elements[i].set_name(names[i]);
    }
  }

  void log() const
  {
    TTCN_Logger::log_event_str("{ ");
    for (unsigned int v_index = 0; v_index < array_size; v_index++) {
      if (v_index > 0) TTCN_Logger::log_event_str(", ");
      array_elements[v_index].log();
    }
    TTCN_Logger::log_event_str(" }");
  }
106
  
107
  // alt-status priority: ALT_YES (return immediately) > ALT_REPEAT > ALT_MAYBE > ALT_NO
108
109
  alt_status timeout(Index_Redirect* index_redirect)
  {
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].timeout(index_redirect);
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
132
133
134
135
  }
  
  boolean running(Index_Redirect* index_redirect) const
  {
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    boolean ret_val = FALSE;
    for (unsigned int i = 0; i < array_size; ++i) {
      ret_val = array_elements[i].running(index_redirect);
      if (ret_val) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        break;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return ret_val;
153
  }
Elemer Lelik's avatar
Elemer Lelik committed
154
155
156
157
158
159
160
161
};

extern unsigned int get_port_array_index(int index_value,
  unsigned int array_size, int index_offset);
extern unsigned int get_port_array_index(const INTEGER& index_value,
  unsigned int array_size, int index_offset);

template <typename T_type, unsigned int array_size, int index_offset>
Elemer Lelik's avatar
Elemer Lelik committed
162
163
164
165
166
class PORT_ARRAY
#ifdef TITAN_RUNTIME_2
  : public RefdIndexInterface
#endif
{
Elemer Lelik's avatar
Elemer Lelik committed
167
168
169
170
171
172
173
174
175
176
177
178
  T_type array_elements[array_size];
  char * names[array_size];

  /// Copy constructor disallowed.
  PORT_ARRAY(const PORT_ARRAY& other_value);
  /// Assignment disallowed.
  PORT_ARRAY& operator=(const PORT_ARRAY& other_value);

public:
  PORT_ARRAY() { }

  ~PORT_ARRAY() {
Kristof Szabados's avatar
Kristof Szabados committed
179
    for (unsigned int i = 0; i < array_size; ++i) {
Elemer Lelik's avatar
Elemer Lelik committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
      Free(names[i]);
    }
  }

  T_type& operator[](int index_value) { return array_elements[
    get_port_array_index(index_value, array_size, index_offset)]; }
  T_type& operator[](const INTEGER& index_value) { return array_elements[
    get_port_array_index(index_value, array_size, index_offset)]; }

  int n_elem()   const { return array_size; }
  int size_of() const { return array_size; }
  int lengthof()const { return array_size; }

  void set_name(const char * name_string)
  {
    for (int i = 0; i < (int)array_size; ++i) {
      // i must be int, see comment in TIMER_ARRAY::set_name
      names[i] = mputprintf(mcopystr(name_string), "[%d]", index_offset+i);
      array_elements[i].set_name(names[i]);
    }
  }

  void activate_port()
  {
    for (unsigned int v_index = 0; v_index < array_size; v_index++) {
      array_elements[v_index].activate_port();
    }
  }
208
209
210
211
212
213
214
215
  
  // needed by the init_system_port function
  void safe_start()
  {
    for (unsigned int v_index = 0; v_index < array_size; v_index++) {
      array_elements[v_index].safe_start();
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
216
217
218
219
220
221
222
223
224
225

  void log() const
  {
    TTCN_Logger::log_event_str("{ ");
    for (unsigned int v_index = 0; v_index < array_size; v_index++) {
      if (v_index > 0) TTCN_Logger::log_event_str(", ");
      array_elements[v_index].log();
    }
    TTCN_Logger::log_event_str(" }");
  }
226
  
227
  // alt-status priority: ALT_YES (return immediately) > ALT_REPEAT > ALT_MAYBE > ALT_NO
228
  alt_status receive(const COMPONENT_template& sender_template,
229
230
                     COMPONENT *sender_ptr, FLOAT* timestamp_redirect,
                     Index_Redirect* index_redirect)
231
  {
232
233
234
235
236
237
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].receive(sender_template,
238
        sender_ptr, timestamp_redirect, index_redirect);
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
255
256
257
258
259
260
261
262
263
264
265
  }
  
  template <typename T_template>
#ifdef TITAN_RUNTIME_2
  alt_status receive(const T_template& value_template,
                     Value_Redirect_Interface* value_redirect,
#else
  // the C++ compiler cannot determine the type of the template argument
  // if it's NULL, so a separate function is needed for this case in RT1
  alt_status receive(const T_template& value_template, int /* NULL */,
                     const COMPONENT_template& sender_template,
266
267
                     COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                     Index_Redirect* index_redirect)
268
  {
269
270
271
272
273
274
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].receive(value_template, NULL,
275
        sender_template, sender_ptr, timestamp_redirect, index_redirect);
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
292
293
294
295
296
297
298
  }
  
  template <typename T_value_redirect, typename T_template>
  alt_status receive(const T_template& value_template,
                     T_value_redirect* value_redirect,
#endif
                     const COMPONENT_template& sender_template,
299
300
                     COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                     Index_Redirect* index_redirect)
301
  {
302
303
304
305
306
307
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].receive(value_template,
308
        value_redirect, sender_template, sender_ptr, timestamp_redirect, index_redirect);
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
325
326
327
  }
  
  alt_status check_receive(const COMPONENT_template& sender_template,
328
329
                           COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                           Index_Redirect* index_redirect)
330
  {
331
332
333
334
335
336
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].check_receive(sender_template,
337
        sender_ptr, timestamp_redirect, index_redirect);
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
354
355
356
357
358
359
360
361
362
  }
  
  template <typename T_template>
#ifdef TITAN_RUNTIME_2
  alt_status check_receive(const T_template& value_template,
                           Value_Redirect_Interface* value_redirect,
#else
  alt_status check_receive(const T_template& value_template, int /* NULL */,
                           const COMPONENT_template& sender_template,
363
364
                           COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                           Index_Redirect* index_redirect)
365
  {
366
367
368
369
370
371
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].check_receive(value_template, NULL,
372
        sender_template, sender_ptr, timestamp_redirect, index_redirect);
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
389
390
391
392
393
394
395
  }
  
  template <typename T_value_redirect, typename T_template>
  alt_status check_receive(const T_template& value_template,
                           T_value_redirect* value_redirect,
#endif
                           const COMPONENT_template& sender_template,
396
397
                           COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                           Index_Redirect* index_redirect)
398
  {
399
400
401
402
403
404
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].check_receive(value_template,
405
        value_redirect, sender_template, sender_ptr, timestamp_redirect, index_redirect);
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
422
423
424
  }
  
  alt_status trigger(const COMPONENT_template& sender_template,
425
426
                     COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                     Index_Redirect* index_redirect)
427
  {
428
429
430
431
432
433
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].trigger(sender_template,
434
        sender_ptr, timestamp_redirect, index_redirect);
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
451
452
453
454
455
456
457
458
459
  }
  
  template <typename T_template>
#ifdef TITAN_RUNTIME_2
  alt_status trigger(const T_template& value_template,
                     Value_Redirect_Interface* value_redirect,
#else
  alt_status trigger(const T_template& value_template, int /* NULL */,
                     const COMPONENT_template& sender_template,
460
461
                     COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                     Index_Redirect* index_redirect)
462
  {
463
464
465
466
467
468
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].trigger(value_template, NULL,
469
        sender_template, sender_ptr, timestamp_redirect, index_redirect);
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
486
487
488
489
490
491
492
  }
  
  template <typename T_value_redirect, typename T_template>
  alt_status trigger(const T_template& value_template,
                     T_value_redirect* value_redirect,
#endif
                     const COMPONENT_template& sender_template,
493
494
                     COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                     Index_Redirect* index_redirect)
495
  {
496
497
498
499
500
501
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].trigger(value_template,
502
        value_redirect, sender_template, sender_ptr, timestamp_redirect, index_redirect);
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
519
520
521
  }
  
  alt_status getcall(const COMPONENT_template& sender_template,
522
523
                     COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                     Index_Redirect* index_redirect)
524
  {
525
526
527
528
529
530
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].getcall(sender_template,
531
        sender_ptr, timestamp_redirect, index_redirect);
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
548
549
550
551
552
553
  }
  
  template <typename T_template, typename T_parameter_redirect>
  alt_status getcall(const T_template& getcall_template,
                     const COMPONENT_template& sender_template,
                     const T_parameter_redirect& param_ref,
554
555
                     COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                     Index_Redirect* index_redirect)
556
  {
557
558
559
560
561
562
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].getcall(getcall_template,
563
        sender_template, param_ref, sender_ptr, timestamp_redirect, index_redirect);
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
580
581
582
  }
  
  alt_status check_getcall(const COMPONENT_template& sender_template,
583
584
                           COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                           Index_Redirect* index_redirect)
585
  {
586
587
588
589
590
591
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].check_getcall(sender_template,
592
        sender_ptr, timestamp_redirect, index_redirect);
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
609
610
611
612
613
614
  }
  
  template <typename T_template, typename T_parameter_redirect>
  alt_status check_getcall(const T_template& getcall_template,
                           const COMPONENT_template& sender_template,
                           const T_parameter_redirect& param_ref,
615
616
                           COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                           Index_Redirect* index_redirect)
617
  {
618
619
620
621
622
623
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].check_getcall(getcall_template,
624
        sender_template, param_ref, sender_ptr, timestamp_redirect, index_redirect);
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
641
642
643
  }
  
  alt_status getreply(const COMPONENT_template& sender_template,
644
645
                      COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                      Index_Redirect* index_redirect)
646
  {
647
648
649
650
651
652
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].getreply(sender_template,
653
        sender_ptr, timestamp_redirect, index_redirect);
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
670
671
672
673
674
675
  }
  
  template <typename T_template, typename T_parameter_redirect>
  alt_status getreply(const T_template& getreply_template,
                      const COMPONENT_template& sender_template,
                      const T_parameter_redirect& param_ref,
676
677
                      COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                      Index_Redirect* index_redirect)
678
  {
679
680
681
682
683
684
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].getreply(getreply_template,
685
        sender_template, param_ref, sender_ptr, timestamp_redirect, index_redirect);
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
702
703
704
  }
  
  alt_status check_getreply(const COMPONENT_template& sender_template,
705
706
                            COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                            Index_Redirect* index_redirect)
707
  {
708
709
710
711
712
713
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].check_getreply(sender_template,
714
        sender_ptr, timestamp_redirect, index_redirect);
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
731
732
733
734
735
736
  }
  
  template <typename T_template, typename T_parameter_redirect>
  alt_status check_getreply(const T_template& getreply_template,
                            const COMPONENT_template& sender_template,
                            const T_parameter_redirect& param_ref,
737
738
                            COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                            Index_Redirect* index_redirect)
739
  {
740
741
742
743
744
745
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].check_getreply(getreply_template,
746
        sender_template, param_ref, sender_ptr, timestamp_redirect, index_redirect);
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
763
764
765
  }
  
  alt_status get_exception(const COMPONENT_template& sender_template,
766
767
                           COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                           Index_Redirect* index_redirect)
768
  {
769
770
771
772
773
774
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].get_exception(sender_template,
775
        sender_ptr, timestamp_redirect, index_redirect);
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
792
793
794
795
796
  }
  
  template <typename T_template>
  alt_status get_exception(const T_template& catch_template,
                           const COMPONENT_template& sender_template,
797
798
                           COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                           Index_Redirect* index_redirect)
799
  {
800
801
802
803
804
805
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].get_exception(catch_template,
806
        sender_template, sender_ptr, timestamp_redirect, index_redirect);
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
823
824
825
  }
  
  alt_status check_catch(const COMPONENT_template& sender_template,
826
827
                         COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                         Index_Redirect* index_redirect)
828
  {
829
830
831
832
833
834
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].check_catch(sender_template,
835
        sender_ptr, timestamp_redirect, index_redirect);
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
852
853
854
855
856
  }
  
  template <typename T_template>
  alt_status check_catch(const T_template& catch_template,
                         const COMPONENT_template& sender_template,
857
858
                         COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                         Index_Redirect* index_redirect)
859
  {
860
861
862
863
864
865
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].check_catch(catch_template,
866
        sender_template, sender_ptr, timestamp_redirect, index_redirect);
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
883
884
885
  }
  
  alt_status check(const COMPONENT_template& sender_template,
886
887
                   COMPONENT* sender_ptr, FLOAT* timestamp_redirect,
                   Index_Redirect* index_redirect)
888
  {
889
890
891
892
893
894
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].check(sender_template, sender_ptr,
895
        timestamp_redirect, index_redirect);
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
912
  }
Elemer Lelik's avatar
Elemer Lelik committed
913
914
915
916
917
918
919
920
921
922
923
};

////////////////////////////////////////////////////////////////////////////////

extern unsigned int get_array_index(int index_value,
  unsigned int array_size, int index_offset);
extern unsigned int get_array_index(const INTEGER& index_value,
  unsigned int array_size, int index_offset);

template <typename T_type, unsigned int array_size, int index_offset>
class VALUE_ARRAY : public Base_Type
Elemer Lelik's avatar
Elemer Lelik committed
924
925
926
#ifdef TITAN_RUNTIME_2
  , public RefdIndexInterface
#endif
Elemer Lelik's avatar
Elemer Lelik committed
927
928
929
930
931
{
  T_type array_elements[array_size];
public:
  // This class use the compiler-generated copy constructor and
  // copy assignment.
ebensza's avatar
ebensza committed
932
    
933
  // User defined default constructor
ebensza's avatar
ebensza committed
934
  VALUE_ARRAY() : array_elements(){};
Elemer Lelik's avatar
Elemer Lelik committed
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
  boolean operator==(const VALUE_ARRAY& other_value) const;
  inline boolean operator!=(const VALUE_ARRAY& other_value) const
    { return !(*this == other_value); }

  T_type& operator[](int index_value) { return array_elements[
    get_array_index(index_value, array_size, index_offset)]; }
  T_type& operator[](const INTEGER& index_value) { return array_elements[
    get_array_index(index_value, array_size, index_offset)]; }
  const T_type& operator[](int index_value) const { return array_elements[
      get_array_index(index_value, array_size, index_offset)]; }
  const T_type& operator[](const INTEGER& index_value) const {
    return array_elements[
      get_array_index(index_value, array_size, index_offset)];
  }

  // rotation
  VALUE_ARRAY operator<<=(int rotate_count) const;
  VALUE_ARRAY operator<<=(const INTEGER& rotate_count) const;
  VALUE_ARRAY operator>>=(int rotate_count) const;
  VALUE_ARRAY operator>>=(const INTEGER& rotate_count) const;

  T_type& array_element(unsigned int index_value)
    { return array_elements[index_value]; }
  const T_type& array_element(unsigned int index_value) const
    { return array_elements[index_value]; }

  void set_implicit_omit();

  boolean is_bound() const;
  boolean is_value() const;
  void clean_up();
  void log() const;

  inline int n_elem()  const { return array_size; }
  inline int size_of() const { return array_size; }
  int lengthof() const;

  void set_param(Module_Param& param);

#ifdef TITAN_RUNTIME_2
975
  Module_Param* get_param(Module_Param_Name& param_name) const;
Elemer Lelik's avatar
Elemer Lelik committed
976
977
978
979
980
981
982
983
984
985
986
  boolean is_equal(const Base_Type* other_value) const { return *this == *(static_cast<const VALUE_ARRAY*>(other_value)); }
  void set_value(const Base_Type* other_value) { *this = *(static_cast<const VALUE_ARRAY*>(other_value)); }
  Base_Type* clone() const { return new VALUE_ARRAY(*this); }
  const TTCN_Typedescriptor_t* get_descriptor() const { TTCN_error("Internal error: VALUE_ARRAY<>::get_descriptor() called."); }
#else
  inline boolean is_present() const { return is_bound(); }
#endif

  void encode_text(Text_Buf& text_buf) const;
  void decode_text(Text_Buf& text_buf);

987
988
  void encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, int, ...) const;
  void decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, int, ...);
Elemer Lelik's avatar
Elemer Lelik committed
989
990
991
992
  
  virtual const TTCN_Typedescriptor_t* get_elem_descr() const 
  { TTCN_error("Internal error: VALUE_ARRAY<>::get_elem_descr() called."); }
  
Elemer Lelik's avatar
Elemer Lelik committed
993
994
  virtual ~VALUE_ARRAY() { } // just to avoid warnings
  
Elemer Lelik's avatar
Elemer Lelik committed
995
996
  /** Encodes accordingly to the JSON encoding rules.
    * Returns the length of the encoded data. */
997
  int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean) const;
Elemer Lelik's avatar
Elemer Lelik committed
998
999
1000
  
  /** Decodes accordingly to the JSON encoding rules.
    * Returns the length of the decoded data. */
1001
  int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean, boolean, int p_chosen_field = CHOSEN_FIELD_UNSET);
1002
  
1003
  // alt-status priority: ALT_YES (return immediately) > ALT_REPEAT > ALT_MAYBE > ALT_NO
1004
  alt_status done(VERDICTTYPE* value_redirect, Index_Redirect* index_redirect) const
1005
  {
1006
1007
1008
1009
1010
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
1011
      alt_status ret_val = array_elements[i].done(value_redirect, index_redirect);
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
1028
1029
1030
1031
  }
  
  alt_status killed(Index_Redirect* index_redirect) const
  {
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    alt_status result = ALT_NO;
    for (unsigned int i = 0; i < array_size; ++i) {
      alt_status ret_val = array_elements[i].killed(index_redirect);
      if (ret_val == ALT_YES) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        result = ret_val;
        break;
      }
      else if (ret_val == ALT_REPEAT ||
               (ret_val == ALT_MAYBE && result == ALT_NO)) {
        result = ret_val;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return result;
1054
1055
1056
1057
  }
  
  boolean running(Index_Redirect* index_redirect) const
  {
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    boolean ret_val = FALSE;
    for (unsigned int i = 0; i < array_size; ++i) {
      ret_val = array_elements[i].running(index_redirect);
      if (ret_val) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        break;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return ret_val;
1075
1076
1077
1078
  }
  
  boolean alive(Index_Redirect* index_redirect) const
  {
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
    if (index_redirect != NULL) {
      index_redirect->incr_pos();
    }
    boolean ret_val = FALSE;
    for (unsigned int i = 0; i < array_size; ++i) {
      ret_val = array_elements[i].alive(index_redirect);
      if (ret_val) {
        if (index_redirect != NULL) {
          index_redirect->add_index((int)i + index_offset);
        }
        break;
      }
    }
    if (index_redirect != NULL) {
      index_redirect->decr_pos();
    }
    return ret_val;
1096
  }
Elemer Lelik's avatar
Elemer Lelik committed
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
};

template <typename T_type, unsigned int array_size, int index_offset>
boolean VALUE_ARRAY<T_type,array_size,index_offset>::operator==
  (const VALUE_ARRAY& other_value) const
{
  for (unsigned int elem_count = 0; elem_count < array_size; elem_count++)
    if (array_elements[elem_count] != other_value.array_elements[elem_count])
      return FALSE;
  return TRUE;
}

template <typename T_type, unsigned int array_size, int index_offset>
VALUE_ARRAY<T_type,array_size,index_offset>
VALUE_ARRAY<T_type,array_size,index_offset>::
operator<<=(int rotate_count) const {
  return *this >>= (-rotate_count);
}

template <typename T_type, unsigned int array_size, int index_offset>
VALUE_ARRAY<T_type,array_size,index_offset>
VALUE_ARRAY<T_type,array_size,index_offset>::
operator<<=(const INTEGER& rotate_count) const {
  rotate_count.must_bound("Unbound integer operand of rotate left "
                          "operator.");
  return *this >>= (int)(-rotate_count);
}

template <typename T_type, unsigned int array_size, int index_offset>
VALUE_ARRAY<T_type,array_size,index_offset>
VALUE_ARRAY<T_type,array_size,index_offset>::
operator>>=(int rotate_count) const {
  unsigned int rc;
1130
1131
  if (rotate_count>=0) rc = static_cast<unsigned int>(rotate_count) % array_size;
  else rc = array_size - (static_cast<unsigned int>(-rotate_count) % array_size);
Elemer Lelik's avatar
Elemer Lelik committed
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
  if (rc == 0) return *this;
  VALUE_ARRAY<T_type,array_size,index_offset> ret_val;
  for (unsigned int i=0; i<array_size; i++) {
      ret_val.array_elements[(i+rc)%array_size] = array_elements[i];
  }
  return ret_val;
}

template <typename T_type, unsigned int array_size, int index_offset>
VALUE_ARRAY<T_type,array_size,index_offset>
VALUE_ARRAY<T_type,array_size,index_offset>::
operator>>=(const INTEGER& rotate_count) const {
  rotate_count.must_bound("Unbound integer operand of rotate right "
                          "operator.");
  return *this >>= (int)rotate_count;
}

template <typename T_type, unsigned int array_size, int index_offset>
void VALUE_ARRAY<T_type,array_size,index_offset>::set_implicit_omit()
{
  for (unsigned int i = 0; i < array_size; ++i) {
    if (array_elements[i].is_bound())
      array_elements[i].set_implicit_omit();
  }
}

template <typename T_type, unsigned int array_size, int index_offset>
boolean VALUE_ARRAY<T_type,array_size,index_offset>::is_bound() const
{
  for (unsigned int i = 0; i < array_size; ++i) {
1162
1163
    if (array_elements[i].is_bound()) {
    	return TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
1164
1165
    }
  }
1166
  return FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
1167
1168
1169
1170
1171
1172
1173
}

template <typename T_type, unsigned int array_size, int index_offset>
boolean VALUE_ARRAY<T_type,array_size,index_offset>::is_value() const
{
  for (unsigned int i = 0; i < array_size; ++i) {
    if (!array_elements[i].is_value()) {
1174
    	return FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
1175
1176
    }
  }
1177
  return TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
}

template <typename T_type, unsigned int array_size, int index_offset>
void VALUE_ARRAY<T_type,array_size,index_offset>::clean_up()
{
  for (unsigned int i = 0; i < array_size; ++i) {
    array_elements[i].clean_up();
  }
}

template <typename T_type, unsigned int array_size, int index_offset>
void VALUE_ARRAY<T_type,array_size,index_offset>::log() const
{
  TTCN_Logger::log_event_str("{ ");
  for (unsigned int elem_count = 0; elem_count < array_size; elem_count++)
  {
    if (elem_count > 0) TTCN_Logger::log_event_str(", ");
    array_elements[elem_count].log();
  }
  TTCN_Logger::log_event_str(" }");
}

template <typename T_type, unsigned int array_size, int index_offset>
int VALUE_ARRAY<T_type,array_size,index_offset>::lengthof() const
{

  for (unsigned int my_length=array_size; my_length>0; my_length--)
  {
    if (array_elements[my_length-1].is_bound()) return my_length;
  }
  return 0;
}

template <typename T_type, unsigned int array_size, int index_offset>
void VALUE_ARRAY<T_type,array_size,index_offset>::set_param(
  Module_Param& param)
{
1215
#ifdef TITAN_RUNTIME_2
Elemer Lelik's avatar
Elemer Lelik committed
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
  if (dynamic_cast<Module_Param_Name*>(param.get_id()) != NULL &&
      param.get_id()->next_name()) {
    // Haven't reached the end of the module parameter name
    // => the name refers to one of the elements, not to the whole array
    char* param_field = param.get_id()->get_current_name();
    if (param_field[0] < '0' || param_field[0] > '9') {
      param.error("Unexpected record field name in module parameter, expected a valid"
        " array index");
    }
    unsigned int param_index = -1;
    sscanf(param_field, "%u", &param_index);
    if (param_index >= array_size) {
      param.error("Invalid array index: %u. The array only has %u elements.", param_index, array_size);
    }
    array_elements[param_index].set_param(param);
    return;
  }
1233
#endif
Elemer Lelik's avatar
Elemer Lelik committed
1234
  
Elemer Lelik's avatar
Elemer Lelik committed
1235
1236
  param.basic_check(Module_Param::BC_VALUE, "array value");
  Module_Param_Ptr mp = &param;
1237
#ifdef TITAN_RUNTIME_2
Elemer Lelik's avatar
Elemer Lelik committed
1238
1239
1240
  if (param.get_type() == Module_Param::MP_Reference) {
    mp = param.get_referenced_param();
  }
1241
#endif
Elemer Lelik's avatar
Elemer Lelik committed
1242
  switch (mp->get_type()) {
Elemer Lelik's avatar
Elemer Lelik committed
1243
  case Module_Param::MP_Value_List:
Elemer Lelik's avatar
Elemer Lelik committed
1244
1245
    if (mp->get_size()!=array_size) {
      param.error("The array value has incorrect number of elements: %lu was expected instead of %lu.", (unsigned long)mp->get_size(), (unsigned long)array_size);
Elemer Lelik's avatar
Elemer Lelik committed
1246
    }
Elemer Lelik's avatar
Elemer Lelik committed
1247
1248
    for (size_t i=0; i<mp->get_size(); ++i) {
      Module_Param* const curr = mp->get_elem(i);
Elemer Lelik's avatar
Elemer Lelik committed
1249
1250
1251
1252
1253
1254
      if (curr->get_type()!=Module_Param::MP_NotUsed) {
        array_elements[i].set_param(*curr);
      }
    }
    break;
  case Module_Param::MP_Indexed_List:
Elemer Lelik's avatar
Elemer Lelik committed
1255
1256
    for (size_t i=0; i<mp->get_size(); ++i) {
      Module_Param* const curr = mp->get_elem(i);
Elemer Lelik's avatar
Elemer Lelik committed
1257
1258
1259
1260
1261
1262
1263
1264
      array_elements[curr->get_id()->get_index()].set_param(*curr);
    }
    break;
  default:
    param.type_error("array value");
  }
}

1265
#ifdef TITAN_RUNTIME_2
Elemer Lelik's avatar
Elemer Lelik committed
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
template <typename T_type, unsigned int array_size, int index_offset>
Module_Param* VALUE_ARRAY<T_type,array_size,index_offset>::get_param
  (Module_Param_Name& param_name) const
{
  if (!is_bound()) {
    return new Module_Param_Unbound();
  }
  if (param_name.next_name()) {
    // Haven't reached the end of the module parameter name
    // => the name refers to one of the elements, not to the whole array
    char* param_field = param_name.get_current_name();
    if (param_field[0] < '0' || param_field[0] > '9') {
      TTCN_error("Unexpected record field name in module parameter reference, "
        "expected a valid array index");
    }
    unsigned int param_index = -1;
    sscanf(param_field, "%u", &param_index);
    if (param_index >= array_size) {
      TTCN_error("Invalid array index: %u. The array only has %u elements.", param_index, array_size);
    }
    return array_elements[param_index].get_param(param_name);
  }
  Vector<Module_Param*> values;
Kristof Szabados's avatar
Kristof Szabados committed
1289
  for (unsigned int i = 0; i < array_size; ++i) {
Elemer Lelik's avatar
Elemer Lelik committed
1290
1291
1292
1293
1294
1295
1296
    values.push_back(array_elements[i].get_param(param_name));
  }
  Module_Param_Value_List* mp = new Module_Param_Value_List();
  mp->add_list_with_implicit_ids(&values);
  values.clear();
  return mp;
}
1297
#endif
Elemer Lelik's avatar
Elemer Lelik committed
1298

Elemer Lelik's avatar
Elemer Lelik committed
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
template <typename T_type, unsigned int array_size, int index_offset>
void VALUE_ARRAY<T_type,array_size,index_offset>::encode_text
  (Text_Buf& text_buf) const
{
  for (unsigned int elem_count = 0; elem_count < array_size; elem_count++)
    array_elements[elem_count].encode_text(text_buf);
}

template <typename T_type, unsigned int array_size, int index_offset>
void VALUE_ARRAY<T_type,array_size,index_offset>::decode_text
  (Text_Buf& text_buf)
{
  for (unsigned int elem_count = 0; elem_count < array_size; elem_count++)
    array_elements[elem_count].decode_text(text_buf);
}

template <typename T_type, unsigned int array_size, int index_offset>
void VALUE_ARRAY<T_type,array_size,index_offset>::encode(
1317
  const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, int p_coding, ...) const
Elemer Lelik's avatar
Elemer Lelik committed
1318
1319
1320
1321
1322
1323
1324
1325
1326
{
  va_list pvar;
  va_start(pvar, p_coding);
  switch(p_coding) {
  case TTCN_EncDec::CT_JSON: {
    TTCN_EncDec_ErrorContext ec("While JSON-encoding type '%s': ", p_td.name);
    if(!p_td.json) TTCN_EncDec_ErrorContext::error_internal
                     ("No JSON descriptor available for type '%s'.", p_td.name);
    JSON_Tokenizer tok(va_arg(pvar, int) != 0);
1327
    JSON_encode(p_td, tok, FALSE);
Elemer Lelik's avatar
Elemer Lelik committed
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
    p_buf.put_s(tok.get_buffer_length(), (const unsigned char*)tok.get_buffer());
    break;}
  default:
    TTCN_error("Unknown coding method requested to encode type '%s'", p_td.name);
  }
  va_end(pvar);
}

template <typename T_type, unsigned int array_size, int index_offset>
void VALUE_ARRAY<T_type,array_size,index_offset>::decode(
1338
  const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, int p_coding, ...)
Elemer Lelik's avatar
Elemer Lelik committed
1339
1340
1341
1342
1343
1344
1345
{
  switch(p_coding) {
  case TTCN_EncDec::CT_JSON: {
    TTCN_EncDec_ErrorContext ec("While JSON-decoding type '%s': ", p_td.name);
    if(!p_td.json) TTCN_EncDec_ErrorContext::error_internal
                     ("No JSON descriptor available for type '%s'.", p_td.name);
    JSON_Tokenizer tok((const char*)p_buf.get_data(), p_buf.get_len());
1346
    if(JSON_decode(p_td, tok, FALSE, FALSE)<0)
Elemer Lelik's avatar
Elemer Lelik committed
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
      ec.error(TTCN_EncDec::ET_INCOMPL_MSG,"Can not decode type '%s', "
        "because invalid or incomplete message was received", p_td.name);
    p_buf.set_pos(tok.get_buf_pos());
    break;}
  default:
    TTCN_error("Unknown coding method requested to decode type '%s'", p_td.name);
  }
}

template <typename T_type, unsigned int array_size, int index_offset>
int VALUE_ARRAY<T_type,array_size,index_offset>::JSON_encode(
1358
  const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean) const
Elemer Lelik's avatar
Elemer Lelik committed
1359
{
Elemer Lelik's avatar
Elemer Lelik committed
1360
  if (!is_bound() && (NULL == p_td.json || !p_td.json->metainfo_unbound)) {
Elemer Lelik's avatar
Elemer Lelik committed
1361
1362
1363
1364
1365
1366
1367
    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
      "Encoding an unbound array value.");
    return -1;
  }
  
  int enc_len = p_tok.put_next_token(JSON_TOKEN_ARRAY_START, NULL);
  
Elemer Lelik's avatar
Elemer Lelik committed
1368
  for (unsigned int i = 0; i < array_size; ++i) {
1369
    if (p_td.json->metainfo_unbound && !array_elements[i].is_bound()) {
Elemer Lelik's avatar
Elemer Lelik committed
1370
1371
1372
1373
1374
1375
1376
      // unbound elements are encoded as { "metainfo []" : "unbound" }
      enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_START, NULL);
      enc_len += p_tok.put_next_token(JSON_TOKEN_NAME, "metainfo []");
      enc_len += p_tok.put_next_token(JSON_TOKEN_STRING, "\"unbound\"");
      enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_END, NULL);
    }
    else {
1377
      int ret_val = array_elements[i].JSON_encode(*get_elem_descr(), p_tok, FALSE);
Elemer Lelik's avatar
Elemer Lelik committed
1378
1379
1380
      if (0 > ret_val) break;
      enc_len += ret_val;
    }
Elemer Lelik's avatar
Elemer Lelik committed
1381
1382
1383
1384
1385
1386
1387
1388
  }
  
  enc_len += p_tok.put_next_token(JSON_TOKEN_ARRAY_END, NULL);
  return enc_len;
}

template <typename T_type, unsigned int array_size, int index_offset>
int VALUE_ARRAY<T_type,array_size,index_offset>::JSON_decode(
1389
  const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent, boolean, int)
Elemer Lelik's avatar
Elemer Lelik committed
1390
1391
{
  json_token_t token = JSON_TOKEN_NONE;
1392
  size_t dec_len = p_tok.get_next_token(&token, NULL, NULL);
Elemer Lelik's avatar
Elemer Lelik committed
1393
1394
1395
1396
1397
1398
1399
1400
  if (JSON_TOKEN_ERROR == token) {
    JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_BAD_TOKEN_ERROR, "");
    return JSON_ERROR_FATAL;
  }
  else if (JSON_TOKEN_ARRAY_START != token) {
    return JSON_ERROR_INVALID_TOKEN;
  } 
  
Elemer Lelik's avatar
Elemer Lelik committed
1401
1402
  for (unsigned int i = 0; i < array_size; ++i) {
    size_t buf_pos = p_tok.get_buf_pos();
1403
    size_t ret_val;
1404
    if (p_td.json->metainfo_unbound) {
Elemer Lelik's avatar
Elemer Lelik committed
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
      // check for metainfo object
      ret_val = p_tok.get_next_token(&token, NULL, NULL);
      if (JSON_TOKEN_OBJECT_START == token) {
        char* value = NULL;
        size_t value_len = 0;
        ret_val += p_tok.get_next_token(&token, &value, &value_len);
        if (JSON_TOKEN_NAME == token && 11 == value_len &&
            0 == strncmp(value, "metainfo []", 11)) {
          ret_val += p_tok.get_next_token(&token, &value, &value_len);
          if (JSON_TOKEN_STRING == token && 9 == value_len &&
              0 == strncmp(value, "\"unbound\"", 9)) {
            ret_val = p_tok.get_next_token(&token, NULL, NULL);
            if (JSON_TOKEN_OBJECT_END == token) {
              dec_len += ret_val;
              continue;
            }
          }
        }
      }
      // metainfo object not found, jump back and let the element type decode it
      p_tok.set_buf_pos(buf_pos);
    }
1427
    int ret_val2 = array_elements[i].JSON_decode(*get_elem_descr(), p_tok, p_silent, FALSE);
1428
    if (JSON_ERROR_INVALID_TOKEN == ret_val2) {
Elemer Lelik's avatar
Elemer Lelik committed
1429
1430
1431
1432
      JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_ARRAY_ELEM_TOKEN_ERROR,
        array_size - i, (array_size - i > 1) ? "s" : "");
      return JSON_ERROR_FATAL;
    } 
1433
    else if (JSON_ERROR_FATAL == ret_val2) {
Elemer Lelik's avatar
Elemer Lelik committed
1434
1435
1436
1437
1438
      if (p_silent) {
        clean_up();
      }
      return JSON_ERROR_FATAL;
    }
1439
    dec_len += (size_t)ret_val2;
Elemer Lelik's avatar
Elemer Lelik committed
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
  }
  
  dec_len += p_tok.get_next_token(&token, NULL, NULL);
  if (JSON_TOKEN_ARRAY_END != token) {
    JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_ARRAY_END_TOKEN_ERROR, "");
    if (p_silent) {
      clean_up();
    }
    return JSON_ERROR_FATAL;
  }

  return dec_len;
}


template <typename T_value_type, typename T_template_type,
          unsigned int array_size, int index_offset>
class TEMPLATE_ARRAY : public Restricted_Length_Template
{
private:
  union {
    struct {
      int n_elements;
      T_template_type **value_elements;
    } single_value;
    struct {
      unsigned int n_values;
      TEMPLATE_ARRAY *list_value;
    } value_list;
  };

  struct Pair_of_elements;
  Pair_of_elements *permutation_intervals;
  unsigned int number_of_permutations;

  void clean_up_intervals();
  void copy_value(const VALUE_ARRAY<T_value_type, array_size, index_offset>&
                  other_value);
  void copy_template(const TEMPLATE_ARRAY& other_value);
  void set_selection(template_sel new_selection);
  void set_selection(const TEMPLATE_ARRAY& other_value);
  void encode_text_permutation(Text_Buf& text_buf) const;
  void decode_text_permutation(Text_Buf& text_buf);

public:
  TEMPLATE_ARRAY() 
    {
      number_of_permutations = 0;
      permutation_intervals = NULL;
    }
  TEMPLATE_ARRAY(template_sel other_value)
    : Restricted_Length_Template(other_value)
    { 
      check_single_selection(other_value);
      number_of_permutations = 0;
      permutation_intervals = NULL;
    }
  TEMPLATE_ARRAY(null_type other_value);
  TEMPLATE_ARRAY(const VALUE_ARRAY<T_value_type, array_size, index_offset>&
                 other_value)
    {
      number_of_permutations = 0;
      permutation_intervals = NULL;
      copy_value(other_value); 
    }
  TEMPLATE_ARRAY(const
    OPTIONAL< VALUE_ARRAY<T_value_type,array_size,index_offset> >& other_value);
  TEMPLATE_ARRAY(const TEMPLATE_ARRAY& other_value)
    : Restricted_Length_Template()
    {
      number_of_permutations = 0;
      permutation_intervals = NULL;
      copy_template(other_value);
    }

  ~TEMPLATE_ARRAY() 
    {
      clean_up_intervals();
      clean_up();
    }
  void clean_up();

  TEMPLATE_ARRAY& operator=(template_sel other_value);
  TEMPLATE_ARRAY& operator=(null_type other_value);
  TEMPLATE_ARRAY& operator=(const
    VALUE_ARRAY<T_value_type, array_size, index_offset>& other_value);
  TEMPLATE_ARRAY& operator=(const
    OPTIONAL< VALUE_ARRAY<T_value_type,array_size,index_offset> >& other_value);
  TEMPLATE_ARRAY& operator=(const TEMPLATE_ARRAY& other_value);

  T_template_type& operator[](int index_value);
  T_template_type& operator[](const INTEGER& index_value);
  const T_template_type& operator[](int index_value) const;
  const T_template_type& operator[](const INTEGER& index_value) const;

  void set_size(int new_size);

  int n_elem() const;
  int size_of(boolean is_size) const;
  inline int size_of() const { return size_of(TRUE); }
  inline int lengthof() const { return size_of(FALSE); }

  void add_permutation(unsigned int start_index, unsigned int end_index);

  /** Removes all permutations set on this template, used when template variables
    *  are given new values. */
  void remove_all_permutations() { clean_up_intervals(); }

  unsigned int get_number_of_permutations() const;
  unsigned int get_permutation_start(unsigned int index_value) const;
  unsigned int get_permutation_end(unsigned int index_value) const;
  unsigned int get_permutation_size(unsigned int index_value) const;
  boolean permutation_starts_at(unsigned int index_value) const;
  boolean permutation_ends_at(unsigned int index_value) const;

private:
  static boolean match_function_specific(
    const Base_Type *value_ptr, int value_index,
Elemer Lelik's avatar
Elemer Lelik committed
1558
1559
    const Restricted_Length_Template *template_ptr, int template_index,
    boolean legacy);
Elemer Lelik's avatar
Elemer Lelik committed
1560
1561
public:
  boolean match(const VALUE_ARRAY<T_value_type, array_size, index_offset>&
Elemer Lelik's avatar
Elemer Lelik committed
1562
                other_value, boolean legacy = FALSE) const;
Elemer Lelik's avatar
Elemer Lelik committed
1563
1564