io.hpp 68.3 KB
Newer Older
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1
2
3
4
5
6
7
8
// Copyright (c) 2020 INRA Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef ORG_VLEPROJECT_IRRITATOR_IO_2020
#define ORG_VLEPROJECT_IRRITATOR_IO_2020

#include <irritator/core.hpp>
9
#include <irritator/external_source.hpp>
Gauthier Quesnel's avatar
Gauthier Quesnel committed
10
11

#include <algorithm>
12
13
#include <istream>
#include <ostream>
14
#include <streambuf>
Gauthier Quesnel's avatar
Gauthier Quesnel committed
15
#include <vector>
Gauthier Quesnel's avatar
Gauthier Quesnel committed
16
17
18

namespace irt {

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
static inline const char* dynamics_type_names[] = { "none",
                                                    "qss1_integrator",
                                                    "qss1_multiplier",
                                                    "qss1_cross",
                                                    "qss1_power",
                                                    "qss1_square",
                                                    "qss1_sum_2",
                                                    "qss1_sum_3",
                                                    "qss1_sum_4",
                                                    "qss1_wsum_2",
                                                    "qss1_wsum_3",
                                                    "qss1_wsum_4",
                                                    "qss2_integrator",
                                                    "qss2_multiplier",
                                                    "qss2_cross",
                                                    "qss2_power",
                                                    "qss2_square",
                                                    "qss2_sum_2",
                                                    "qss2_sum_3",
                                                    "qss2_sum_4",
                                                    "qss2_wsum_2",
                                                    "qss2_wsum_3",
                                                    "qss2_wsum_4",
                                                    "qss3_integrator",
                                                    "qss3_multiplier",
                                                    "qss3_cross",
                                                    "qss3_power",
                                                    "qss3_square",
                                                    "qss3_sum_2",
                                                    "qss3_sum_3",
                                                    "qss3_sum_4",
                                                    "qss3_wsum_2",
                                                    "qss3_wsum_3",
                                                    "qss3_wsum_4",
                                                    "integrator",
                                                    "quantifier",
                                                    "adder_2",
                                                    "adder_3",
                                                    "adder_4",
                                                    "mult_2",
                                                    "mult_3",
                                                    "mult_4",
                                                    "counter",
62
63
64
                                                    "queue",
                                                    "dynamic_queue",
                                                    "priority_queue",
65
66
67
68
69
70
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
                                                    "generator",
                                                    "constant",
                                                    "cross",
                                                    "time_func",
                                                    "accumulator_2",
                                                    "flow" };

static_assert(std::size(dynamics_type_names) ==
              static_cast<sz>(dynamics_type_size()));

static inline const char* str_empty[] = { "" };
static inline const char* str_integrator[] = { "x-dot", "reset" };
static inline const char* str_adaptative_integrator[] = { "quanta",
                                                          "x-dot",
                                                          "reset" };
static inline const char* str_in_1[] = { "in" };
static inline const char* str_in_2[] = { "in-1", "in-2" };
static inline const char* str_in_3[] = { "in-1", "in-2", "in-3" };
static inline const char* str_in_4[] = { "in-1", "in-2", "in-3", "in-4" };
static inline const char* str_value_if_else[] = { "value",
                                                  "if",
                                                  "else",
                                                  "threshold" };
static inline const char* str_in_2_nb_2[] = { "in-1", "in-2", "nb-1", "nb-2" };

template<typename Dynamics>
static constexpr const char**
get_input_port_names() noexcept
{
    if constexpr (std::is_same_v<Dynamics, none>)
        return str_empty;

    if constexpr (std::is_same_v<Dynamics, qss1_integrator> ||
                  std::is_same_v<Dynamics, qss2_integrator> ||
                  std::is_same_v<Dynamics, qss3_integrator>)
        return str_integrator;

    if constexpr (std::is_same_v<Dynamics, qss1_multiplier> ||
                  std::is_same_v<Dynamics, qss1_sum_2> ||
                  std::is_same_v<Dynamics, qss1_wsum_2> ||
                  std::is_same_v<Dynamics, qss2_multiplier> ||
                  std::is_same_v<Dynamics, qss2_sum_2> ||
                  std::is_same_v<Dynamics, qss2_wsum_2> ||
                  std::is_same_v<Dynamics, qss3_multiplier> ||
                  std::is_same_v<Dynamics, qss3_sum_2> ||
                  std::is_same_v<Dynamics, qss3_wsum_2> ||
                  std::is_same_v<Dynamics, adder_2> ||
                  std::is_same_v<Dynamics, mult_2>)
        return str_in_2;

    if constexpr (std::is_same_v<Dynamics, qss1_sum_3> ||
                  std::is_same_v<Dynamics, qss1_wsum_3> ||
                  std::is_same_v<Dynamics, qss2_sum_3> ||
                  std::is_same_v<Dynamics, qss2_wsum_3> ||
                  std::is_same_v<Dynamics, qss3_sum_3> ||
                  std::is_same_v<Dynamics, qss3_wsum_3> ||
                  std::is_same_v<Dynamics, adder_3> ||
                  std::is_same_v<Dynamics, mult_3>)
        return str_in_3;

    if constexpr (std::is_same_v<Dynamics, qss1_sum_4> ||
                  std::is_same_v<Dynamics, qss1_wsum_4> ||
                  std::is_same_v<Dynamics, qss2_sum_4> ||
                  std::is_same_v<Dynamics, qss2_wsum_4> ||
                  std::is_same_v<Dynamics, qss3_sum_4> ||
                  std::is_same_v<Dynamics, qss3_wsum_4> ||
                  std::is_same_v<Dynamics, adder_4> ||
                  std::is_same_v<Dynamics, mult_4>)
        return str_in_4;

    if constexpr (std::is_same_v<Dynamics, integrator>)
        return str_adaptative_integrator;

    if constexpr (std::is_same_v<Dynamics, quantifier> ||
                  std::is_same_v<Dynamics, counter> ||
140
141
142
                  std::is_same_v<Dynamics, queue> ||
                  std::is_same_v<Dynamics, dynamic_queue> ||
                  std::is_same_v<Dynamics, priority_queue> ||
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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
208
209
210
211
212
213
214
215
216
217
218
                  std::is_same_v<Dynamics, qss1_power> ||
                  std::is_same_v<Dynamics, qss2_power> ||
                  std::is_same_v<Dynamics, qss3_power> ||
                  std::is_same_v<Dynamics, qss1_square> ||
                  std::is_same_v<Dynamics, qss2_square> ||
                  std::is_same_v<Dynamics, qss3_square>)
        return str_in_1;

    if constexpr (std::is_same_v<Dynamics, generator> ||
                  std::is_same_v<Dynamics, constant> ||
                  std::is_same_v<Dynamics, time_func> ||
                  std::is_same_v<Dynamics, flow>)
        return str_empty;

    if constexpr (std::is_same_v<Dynamics, qss1_cross> ||
                  std::is_same_v<Dynamics, qss2_cross> ||
                  std::is_same_v<Dynamics, qss3_cross> ||
                  std::is_same_v<Dynamics, cross>)
        return str_value_if_else;

    if constexpr (std::is_same_v<Dynamics, accumulator_2>)
        return str_in_2_nb_2;

    irt_unreachable();
}

static constexpr const char**
get_input_port_names(const dynamics_type type) noexcept
{
    switch (type) {
    case dynamics_type::none:
        return str_empty;

    case dynamics_type::qss1_integrator:
    case dynamics_type::qss2_integrator:
    case dynamics_type::qss3_integrator:
        return str_integrator;

    case dynamics_type::qss1_multiplier:
    case dynamics_type::qss1_sum_2:
    case dynamics_type::qss1_wsum_2:
    case dynamics_type::qss2_multiplier:
    case dynamics_type::qss2_sum_2:
    case dynamics_type::qss2_wsum_2:
    case dynamics_type::qss3_multiplier:
    case dynamics_type::qss3_sum_2:
    case dynamics_type::qss3_wsum_2:
    case dynamics_type::adder_2:
    case dynamics_type::mult_2:
        return str_in_2;

    case dynamics_type::qss1_sum_3:
    case dynamics_type::qss1_wsum_3:
    case dynamics_type::qss2_sum_3:
    case dynamics_type::qss2_wsum_3:
    case dynamics_type::qss3_sum_3:
    case dynamics_type::qss3_wsum_3:
    case dynamics_type::adder_3:
    case dynamics_type::mult_3:
        return str_in_3;

    case dynamics_type::qss1_sum_4:
    case dynamics_type::qss1_wsum_4:
    case dynamics_type::qss2_sum_4:
    case dynamics_type::qss2_wsum_4:
    case dynamics_type::qss3_sum_4:
    case dynamics_type::qss3_wsum_4:
    case dynamics_type::adder_4:
    case dynamics_type::mult_4:
        return str_in_4;

    case dynamics_type::integrator:
        return str_adaptative_integrator;

    case dynamics_type::quantifier:
    case dynamics_type::counter:
219
220
221
    case dynamics_type::queue:
    case dynamics_type::dynamic_queue:
    case dynamics_type::priority_queue:
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
    case dynamics_type::qss1_power:
    case dynamics_type::qss2_power:
    case dynamics_type::qss3_power:
    case dynamics_type::qss1_square:
    case dynamics_type::qss2_square:
    case dynamics_type::qss3_square:
        return str_in_1;

    case dynamics_type::generator:
    case dynamics_type::constant:
    case dynamics_type::time_func:
    case dynamics_type::flow:
        return str_empty;

    case dynamics_type::qss1_cross:
    case dynamics_type::qss2_cross:
    case dynamics_type::qss3_cross:
    case dynamics_type::cross:
        return str_value_if_else;

    case dynamics_type::accumulator_2:
        return str_in_2_nb_2;
    }

    irt_unreachable();
}

static inline const char* str_out_1[] = { "out" };
static inline const char* str_out_cross[] = { "if-value",
                                              "else-value",
                                              "event" };

template<typename Dynamics>
static constexpr const char**
get_output_port_names() noexcept
{
    if constexpr (std::is_same_v<Dynamics, none>)
        return str_empty;

    if constexpr (std::is_same_v<Dynamics, qss1_integrator> ||
                  std::is_same_v<Dynamics, qss1_multiplier> ||
                  std::is_same_v<Dynamics, qss1_power> ||
                  std::is_same_v<Dynamics, qss1_square> ||
                  std::is_same_v<Dynamics, qss1_sum_2> ||
                  std::is_same_v<Dynamics, qss1_sum_3> ||
                  std::is_same_v<Dynamics, qss1_sum_4> ||
                  std::is_same_v<Dynamics, qss1_wsum_2> ||
                  std::is_same_v<Dynamics, qss1_wsum_3> ||
                  std::is_same_v<Dynamics, qss1_wsum_4> ||
                  std::is_same_v<Dynamics, qss2_integrator> ||
                  std::is_same_v<Dynamics, qss2_multiplier> ||
                  std::is_same_v<Dynamics, qss2_power> ||
                  std::is_same_v<Dynamics, qss2_square> ||
                  std::is_same_v<Dynamics, qss2_sum_2> ||
                  std::is_same_v<Dynamics, qss2_sum_3> ||
                  std::is_same_v<Dynamics, qss2_sum_4> ||
                  std::is_same_v<Dynamics, qss2_wsum_2> ||
                  std::is_same_v<Dynamics, qss2_wsum_3> ||
                  std::is_same_v<Dynamics, qss2_wsum_4> ||
                  std::is_same_v<Dynamics, qss3_integrator> ||
                  std::is_same_v<Dynamics, qss3_multiplier> ||
                  std::is_same_v<Dynamics, qss3_power> ||
                  std::is_same_v<Dynamics, qss3_square> ||
                  std::is_same_v<Dynamics, qss3_sum_2> ||
                  std::is_same_v<Dynamics, qss3_sum_3> ||
                  std::is_same_v<Dynamics, qss3_sum_4> ||
                  std::is_same_v<Dynamics, qss3_wsum_2> ||
                  std::is_same_v<Dynamics, qss3_wsum_3> ||
                  std::is_same_v<Dynamics, qss3_wsum_4> ||
                  std::is_same_v<Dynamics, integrator> ||
                  std::is_same_v<Dynamics, quantifier> ||
                  std::is_same_v<Dynamics, adder_2> ||
                  std::is_same_v<Dynamics, adder_3> ||
                  std::is_same_v<Dynamics, adder_4> ||
                  std::is_same_v<Dynamics, mult_2> ||
                  std::is_same_v<Dynamics, mult_3> ||
                  std::is_same_v<Dynamics, mult_4> ||
                  std::is_same_v<Dynamics, counter> ||
300
301
302
                  std::is_same_v<Dynamics, queue> ||
                  std::is_same_v<Dynamics, dynamic_queue> ||
                  std::is_same_v<Dynamics, priority_queue> ||
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
                  std::is_same_v<Dynamics, generator> ||
                  std::is_same_v<Dynamics, constant> ||
                  std::is_same_v<Dynamics, time_func> ||
                  std::is_same_v<Dynamics, flow>)
        return str_out_1;

    if constexpr (std::is_same_v<Dynamics, cross> ||
                  std::is_same_v<Dynamics, qss1_cross> ||
                  std::is_same_v<Dynamics, qss2_cross> ||
                  std::is_same_v<Dynamics, qss3_cross>)
        return str_out_cross;

    if constexpr (std::is_same_v<Dynamics, accumulator_2>)
        return str_empty;

    irt_unreachable();
}

static constexpr const char**
get_output_port_names(const dynamics_type type) noexcept
{
    switch (type) {
    case dynamics_type::none:
        return str_empty;

    case dynamics_type::qss1_integrator:
    case dynamics_type::qss1_multiplier:
    case dynamics_type::qss1_power:
    case dynamics_type::qss1_square:
    case dynamics_type::qss1_sum_2:
    case dynamics_type::qss1_sum_3:
    case dynamics_type::qss1_sum_4:
    case dynamics_type::qss1_wsum_2:
    case dynamics_type::qss1_wsum_3:
    case dynamics_type::qss1_wsum_4:
    case dynamics_type::qss2_integrator:
    case dynamics_type::qss2_multiplier:
    case dynamics_type::qss2_power:
    case dynamics_type::qss2_square:
    case dynamics_type::qss2_sum_2:
    case dynamics_type::qss2_sum_3:
    case dynamics_type::qss2_sum_4:
    case dynamics_type::qss2_wsum_2:
    case dynamics_type::qss2_wsum_3:
    case dynamics_type::qss2_wsum_4:
    case dynamics_type::qss3_integrator:
    case dynamics_type::qss3_multiplier:
    case dynamics_type::qss3_power:
    case dynamics_type::qss3_square:
    case dynamics_type::qss3_sum_2:
    case dynamics_type::qss3_sum_3:
    case dynamics_type::qss3_sum_4:
    case dynamics_type::qss3_wsum_2:
    case dynamics_type::qss3_wsum_3:
    case dynamics_type::qss3_wsum_4:
    case dynamics_type::integrator:
    case dynamics_type::quantifier:
    case dynamics_type::adder_2:
    case dynamics_type::adder_3:
    case dynamics_type::adder_4:
    case dynamics_type::mult_2:
    case dynamics_type::mult_3:
    case dynamics_type::mult_4:
    case dynamics_type::counter:
367
368
369
    case dynamics_type::queue:
    case dynamics_type::dynamic_queue:
    case dynamics_type::priority_queue:
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
    case dynamics_type::generator:
    case dynamics_type::constant:
    case dynamics_type::time_func:
    case dynamics_type::flow:
        return str_out_1;

    case dynamics_type::cross:
    case dynamics_type::qss1_cross:
    case dynamics_type::qss2_cross:
    case dynamics_type::qss3_cross:
        return str_out_cross;

    case dynamics_type::accumulator_2:
        return str_empty;
    }

    irt_unreachable();
}

389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
class streambuf : public std::streambuf
{
public:
    std::streambuf* m_stream_buffer = { nullptr };
    std::streamsize m_file_position = { 0 };
    int m_line_number = { 1 };
    int m_last_line_number = { 1 };
    int m_column = { 0 };
    int m_prev_column = { -1 };

    streambuf(std::streambuf* sbuf)
      : m_stream_buffer(sbuf)
    {}

    streambuf(const streambuf&) = delete;
    streambuf& operator=(const streambuf&) = delete;

protected:
    std::streambuf::int_type underflow() override final
    {
        return m_stream_buffer->sgetc();
    }

    std::streambuf::int_type uflow() override final
    {
        int_type rc = m_stream_buffer->sbumpc();

        m_last_line_number = m_line_number;
        if (traits_type::eq_int_type(rc, traits_type::to_int_type('\n'))) {
            ++m_line_number;
            m_prev_column = m_column + 1;
            m_column = -1;
        }

        ++m_column;
        ++m_file_position;

        return rc;
    }

429
430
    std::streambuf::int_type pbackfail(
      std::streambuf::int_type c) override final
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
    {
        if (traits_type::eq_int_type(c, traits_type::to_int_type('\n'))) {
            --m_line_number;
            m_last_line_number = m_line_number;
            m_column = m_prev_column;
            m_prev_column = 0;
        }

        --m_column;
        --m_file_position;

        if (c != traits_type::eof())
            return m_stream_buffer->sputbackc(traits_type::to_char_type(c));

        return m_stream_buffer->sungetc();
    }

    std::ios::pos_type seekoff(std::ios::off_type pos,
                               std::ios_base::seekdir dir,
                               std::ios_base::openmode mode) override final
    {
        if (dir == std::ios_base::beg &&
            pos == static_cast<std::ios::off_type>(0)) {
            m_last_line_number = 1;
            m_line_number = 1;
            m_column = 0;
            m_prev_column = -1;
            m_file_position = 0;

            return m_stream_buffer->pubseekoff(pos, dir, mode);
        }

        return std::streambuf::seekoff(pos, dir, mode);
    }

    std::ios::pos_type seekpos(std::ios::pos_type pos,
                               std::ios_base::openmode mode) override final
    {
        if (pos == static_cast<std::ios::pos_type>(0)) {
            m_last_line_number = 1;
            m_line_number = 1;
            m_column = 0;
            m_prev_column = -1;
            m_file_position = 0;

            return m_stream_buffer->pubseekpos(pos, mode);
        }

        return std::streambuf::seekpos(pos, mode);
    }
};

Gauthier Quesnel's avatar
Gauthier Quesnel committed
483
484
485
class reader
{
private:
486
487
    streambuf buf;
    std::istream is;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
488

489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
    struct mapping
    {
        mapping(int index_, u64 value_)
          : index(index_)
          , value(value_)
        {}

        int index = 0;
        u64 value = 0u;

        bool operator<(const mapping& other) const noexcept
        {
            return index < other.index;
        }
    };

505
506
507
508
509
510
511
512
513
514
515
516
    struct position
    {
        position() noexcept = default;

        position(const float x_, const float y_) noexcept
            : x(x_)
            , y(y_)
        {}

        float x, y;
    };

517
    std::vector<model_id> map;
518
    std::vector<position> positions; /* Stores positions of the models. */
519
520
521
522
    std::vector<mapping> constant_mapping;
    std::vector<mapping> binary_file_mapping;
    std::vector<mapping> random_mapping;
    std::vector<mapping> text_file_mapping;
523

524
    int source_number = 0;
525
    int model_number = 0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
526

527
528
529
    char temp_1[32];
    char temp_2[32];

Gauthier Quesnel's avatar
Gauthier Quesnel committed
530
public:
531
    reader(std::istream& is_) noexcept
532
533
      : buf(is_.rdbuf())
      , is(&buf)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
534
535
    {}

536
    ~reader() noexcept = default;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
537

538
539
540
    int model_error = 0;
    int connection_error = 0;

541
    int line_error() const noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
542
    {
543
544
545
546
547
548
549
        return buf.m_line_number;
    }

    int column_error() const noexcept
    {
        return buf.m_column;
    }
550

551
552
553
554
555
556
557
    position get_position(const sz index) const noexcept
    {
        return index < positions.size() ?
            positions[index] :
            position{ 0.f, 0.f };
    }

558
559
560
561
562
    status operator()(simulation& sim, external_source& srcs) noexcept
    {
        irt_return_if_bad(do_read_data_source(srcs));

        irt_return_if_bad(do_read_model_number());
563
564
        for (int i = 0; i != model_number; ++i, ++model_error) {
            int id;
565
            irt_return_if_bad(do_read_model(sim, &id));
566
567
568
569
570
571
572
        }

        irt_return_if_bad(do_read_connections(sim));

        return status::success;
    }

573
    status operator()(simulation& sim,
574
                      external_source& srcs,
575
                      function_ref<void(const model_id)> f) noexcept
576
    {
577
        irt_return_if_bad(do_read_data_source(srcs));
578

579
        irt_return_if_bad(do_read_model_number());
580
581
582
583
584
585
586
587
588
589
590
591
        for (int i = 0; i != model_number; ++i, ++model_error) {
            int id;
            irt_return_if_bad(do_read_model(sim, &id));
            f(map[id]);
        }

        irt_return_if_bad(do_read_connections(sim));

        return status::success;
    }

private:
592
    status do_read_binary_file_source(external_source& srcs) noexcept
593
    {
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
        u32 id;

        if (!(is >> id))
            return status::io_file_format_error;

        auto& elem = srcs.binary_file_sources.alloc();
        if (auto ret = elem.init(srcs.block_size, srcs.block_number);
            is_bad(ret)) {
            srcs.binary_file_sources.free(elem);
            return ret;
        }

        auto elem_id = srcs.binary_file_sources.get_id(elem);

        std::string file_path;
        if (!(is >> std::quoted(file_path)))
            return status::io_file_format_error;

        elem.file_path = file_path;
        binary_file_mapping.emplace_back(id, ordinal(elem_id));

        return status::success;
    }

    status do_read_text_file_source(external_source& srcs) noexcept
    {
        u32 id;

        if (!(is >> id))
            return status::io_file_format_error;

        auto& elem = srcs.text_file_sources.alloc();
        if (auto ret = elem.init(srcs.block_size, srcs.block_number);
            is_bad(ret)) {
            srcs.text_file_sources.free(elem);
            return ret;
        }

        auto elem_id = srcs.text_file_sources.get_id(elem);

        std::string file_path;
        if (!(is >> std::quoted(file_path)))
            return status::io_file_format_error;

        elem.file_path = file_path;
        text_file_mapping.emplace_back(id, ordinal(elem_id));

        return status::success;
    }

    status do_read_constant_source(external_source& srcs) noexcept
    {
        u32 id;
        sz size;

        if (!(is >> id >> size))
            return status::io_file_format_error;

        auto& cst = srcs.constant_sources.alloc();
Gauthier Quesnel's avatar
Gauthier Quesnel committed
653
        if (auto ret = cst.init(srcs.block_size); is_bad(ret)) {
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
            srcs.constant_sources.free(cst);
            return ret;
        }

        auto cst_id = srcs.constant_sources.get_id(cst);

        try {
            constant_mapping.emplace_back(id, ordinal(cst_id));
        } catch (const std::bad_alloc& /*e*/) {
            return status::io_not_enough_memory;
        }

        for (size_t i = 0; i < size; ++i) {
            if (!(is >> cst.buffer[i]))
                return status::io_file_format_error;
        }

        return status::success;
    }

    status do_read_random_source(external_source& srcs) noexcept
    {
        u32 id;

        if (!(is >> id))
            return status::io_file_format_error;

        char type_str[30];
        if (!(is >> type_str))
            return status::io_file_format_error;

685
686
        auto it = binary_find(std::begin(distribution_type_string),
                              std::end(distribution_type_string),
687
688
689
690
691
                              type_str,
                              [](const char* left, const char* right) {
                                  return std::strcmp(left, right) == 0;
                              });

692
        if (it == std::end(distribution_type_string))
693
694
695
            return status::io_file_format_error;

        const auto dist_id =
696
          std::distance(std::begin(distribution_type_string), it);
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864

        auto& elem = srcs.random_sources.alloc();
        if (auto ret = elem.init(srcs.block_size, srcs.block_number);
            is_bad(ret)) {
            srcs.random_sources.free(elem);
            return ret;
        }

        auto elem_id = srcs.random_sources.get_id(elem);

        try {
            random_mapping.emplace_back(id, ordinal(elem_id));
        } catch (const std::bad_alloc& /*e*/) {
            return status::io_not_enough_memory;
        }

        elem.distribution = enum_cast<distribution_type>(dist_id);

        switch (elem.distribution) {
        case distribution_type::uniform_int:
            if (!(is >> elem.a32 >> elem.b32))
                return status::io_file_format_error;
            break;

        case distribution_type::uniform_real:
            if (!(is >> elem.a >> elem.b))
                return status::io_file_format_error;
            break;

        case distribution_type::bernouilli:
            if (!(is >> elem.p))
                return status::io_file_format_error;
            break;

        case distribution_type::binomial:
            if (!(is >> elem.t32 >> elem.p))
                return status::io_file_format_error;
            break;

        case distribution_type::negative_binomial:
            if (!(is >> elem.t32 >> elem.p))
                return status::io_file_format_error;
            break;

        case distribution_type::geometric:
            if (!(is >> elem.p))
                return status::io_file_format_error;
            break;

        case distribution_type::poisson:
            if (!(is >> elem.mean))
                return status::io_file_format_error;
            break;

        case distribution_type::exponential:
            if (!(is >> elem.lambda))
                return status::io_file_format_error;
            break;

        case distribution_type::gamma:
            if (!(is >> elem.alpha >> elem.beta))
                return status::io_file_format_error;
            break;

        case distribution_type::weibull:
            if (!(is >> elem.a >> elem.b))
                return status::io_file_format_error;
            break;

        case distribution_type::exterme_value:
            if (!(is >> elem.a >> elem.b))
                return status::io_file_format_error;
            break;

        case distribution_type::normal:
            if (!(is >> elem.mean >> elem.stddev))
                return status::io_file_format_error;
            break;

        case distribution_type::lognormal:
            if (!(is >> elem.m >> elem.s))
                return status::io_file_format_error;
            break;

        case distribution_type::chi_squared:
            if (!(is >> elem.n))
                return status::io_file_format_error;
            break;

        case distribution_type::cauchy:
            if (!(is >> elem.a >> elem.b))
                return status::io_file_format_error;
            break;

        case distribution_type::fisher_f:
            if (!(is >> elem.m >> elem.n))
                return status::io_file_format_error;
            break;

        case distribution_type::student_t:
            if (!(is >> elem.n))
                return status::io_file_format_error;
            break;
        }

        return status::success;
    }

    status do_read_data_source(external_source& srcs) noexcept
    {
        size_t number;

        /* Read the constant sources */
        if (!(is >> number))
            return status::io_file_format_source_number_error;

        if (number > 0u) {
            if (!srcs.constant_sources.can_alloc(number))
                return status::io_file_source_full;

            for (sz i = 0; i < number; ++i)
                irt_return_if_bad(do_read_constant_source(srcs));
        }

        /* Read the binary sources */
        if (!(is >> number))
            return status::io_file_format_error;

        if (number > 0u) {
            if (!srcs.binary_file_sources.can_alloc(number))
                return status::io_file_source_full;

            for (sz i = 0; i < number; ++i)
                irt_return_if_bad(do_read_binary_file_source(srcs));
        }

        /* Read the text file sources */
        if (!(is >> number))
            return status::io_file_format_error;

        if (number > 0u) {
            if (!srcs.text_file_sources.can_alloc(number))
                return status::io_file_source_full;

            for (sz i = 0; i < number; ++i)
                irt_return_if_bad(do_read_text_file_source(srcs));
        }

        /* Read the random sources */
        if (!(is >> number))
            return status::io_file_format_error;

        if (number > 0u) {
            if (!srcs.random_sources.can_alloc(number))
                return status::io_file_source_full;

            for (sz i = 0; i < number; ++i) {
                irt_return_if_bad(do_read_random_source(srcs));
            }
        }

        std::sort(std::begin(constant_mapping), std::end(constant_mapping));
        std::sort(std::begin(binary_file_mapping),
                  std::end(binary_file_mapping));
        std::sort(std::begin(text_file_mapping), std::end(text_file_mapping));
        std::sort(std::begin(random_mapping), std::end(random_mapping));

        return status::success;
865
866
    }

867
868
869
    status do_read_model_number() noexcept
    {
        model_number = 0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
870

871
        irt_return_if_fail((is >> model_number), status::io_file_format_error);
872

Gauthier Quesnel's avatar
Gauthier Quesnel committed
873
874
875
        irt_return_if_fail(model_number > 0,
                           status::io_file_format_model_number_error);

876
877
        try {
            map.resize(model_number, model_id{ 0 });
878
            positions.resize(model_number);
879
880
881
        } catch (const std::bad_alloc& /*e*/) {
            return status::io_not_enough_memory;
        }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
882

883
884
        return status::success;
    }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
885

886
    status do_read_model(simulation& sim, int* id) noexcept
887
    {
888
        irt_return_if_fail((is >> *id), status::io_file_format_model_error);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
889

890
891
        irt_return_if_fail(0 <= *id && *id < model_number,
                           status::io_file_format_model_error);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
892

893
894
895
896
897
        irt_return_if_fail((is >> positions[*id].x >> positions[*id].y),
                           status::io_file_format_model_error);

        irt_return_if_fail((is >> temp_1), status::io_file_format_model_error);

898
        irt_return_if_bad(do_read_dynamics(sim, *id, temp_1));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
899

900
901
902
903
904
        return status::success;
    }

    status do_read_connections(simulation& sim) noexcept
    {
905
        while (is) {
906
            int mdl_src_id, port_src_index, mdl_dst_id, port_dst_index;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
907

908
            if (!(is >> mdl_src_id >> port_src_index >> mdl_dst_id >>
909
910
911
                  port_dst_index)) {
                if (is.eof())
                    break;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
912

913
914
                irt_bad_return(status::io_file_format_error);
            }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
915

916
917
918
919
920
921
            irt_return_if_fail(0 <= mdl_src_id && mdl_src_id < model_number,
                               status::io_file_format_model_error);
            irt_return_if_fail(0 <= mdl_dst_id && mdl_dst_id < model_number,
                               status::io_file_format_model_error);

            auto* mdl_src = sim.models.try_to_get(map[mdl_src_id]);
922
            irt_return_if_fail(mdl_src, status::io_file_format_model_unknown);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
923

924
            auto* mdl_dst = sim.models.try_to_get(map[mdl_dst_id]);
925
            irt_return_if_fail(mdl_dst, status::io_file_format_model_unknown);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
926

927
928
            port* output_port = nullptr;
            port* input_port = nullptr;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
929

930
            irt_return_if_bad(
931
932
933
934
              sim.get_output_port(*mdl_src, port_src_index, output_port));
            irt_return_if_bad(
              sim.get_input_port(*mdl_dst, port_dst_index, input_port));

935
            irt_return_if_bad(
936
              sim.connect(*mdl_src, port_src_index, *mdl_dst, port_dst_index));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
937

938
            ++connection_error;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
939
940
941
942
943
        }

        return status::success;
    }

944
945
    bool convert(const std::string_view dynamics_name,
                 dynamics_type* type) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
946
    {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
        struct string_to_type
        {
            constexpr string_to_type(const std::string_view n,
                                     const dynamics_type t)
              : name(n)
              , type(t)
            {}

            const std::string_view name;
            dynamics_type type;
        };

        static constexpr string_to_type table[] = {
            { "accumulator_2", dynamics_type::accumulator_2 },
            { "adder_2", dynamics_type::adder_2 },
            { "adder_3", dynamics_type::adder_3 },
            { "adder_4", dynamics_type::adder_4 },
            { "constant", dynamics_type::constant },
            { "counter", dynamics_type::counter },
            { "cross", dynamics_type::cross },
967
            { "dynamic_queue", dynamics_type::dynamic_queue },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
968
            { "flow", dynamics_type::flow },
969
            { "generator", dynamics_type::generator },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
970
971
972
973
974
            { "integrator", dynamics_type::integrator },
            { "mult_2", dynamics_type::mult_2 },
            { "mult_3", dynamics_type::mult_3 },
            { "mult_4", dynamics_type::mult_4 },
            { "none", dynamics_type::none },
975
            { "priority_queue", dynamics_type::priority_queue },
976
            { "qss1_cross", dynamics_type::qss1_cross },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
977
            { "qss1_integrator", dynamics_type::qss1_integrator },
978
            { "qss1_multiplier", dynamics_type::qss1_multiplier },
979
980
            { "qss1_power", dynamics_type::qss1_power },
            { "qss1_square", dynamics_type::qss1_square },
981
982
983
984
985
986
            { "qss1_sum_2", dynamics_type::qss1_sum_2 },
            { "qss1_sum_3", dynamics_type::qss1_sum_3 },
            { "qss1_sum_4", dynamics_type::qss1_sum_4 },
            { "qss1_wsum_2", dynamics_type::qss1_wsum_2 },
            { "qss1_wsum_3", dynamics_type::qss1_wsum_3 },
            { "qss1_wsum_4", dynamics_type::qss1_wsum_4 },
987
            { "qss2_cross", dynamics_type::qss2_cross },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
988
989
            { "qss2_integrator", dynamics_type::qss2_integrator },
            { "qss2_multiplier", dynamics_type::qss2_multiplier },
990
991
            { "qss2_power", dynamics_type::qss2_power },
            { "qss2_square", dynamics_type::qss2_square },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
992
993
994
995
996
997
            { "qss2_sum_2", dynamics_type::qss2_sum_2 },
            { "qss2_sum_3", dynamics_type::qss2_sum_3 },
            { "qss2_sum_4", dynamics_type::qss2_sum_4 },
            { "qss2_wsum_2", dynamics_type::qss2_wsum_2 },
            { "qss2_wsum_3", dynamics_type::qss2_wsum_3 },
            { "qss2_wsum_4", dynamics_type::qss2_wsum_4 },
998
            { "qss3_cross", dynamics_type::qss3_cross },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
999
1000
            { "qss3_integrator", dynamics_type::qss3_integrator },
            { "qss3_multiplier", dynamics_type::qss3_multiplier },
1001
1002
            { "qss3_power", dynamics_type::qss3_power },
            { "qss3_square", dynamics_type::qss3_square },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1003
1004
1005
1006
1007
1008
            { "qss3_sum_2", dynamics_type::qss3_sum_2 },
            { "qss3_sum_3", dynamics_type::qss3_sum_3 },
            { "qss3_sum_4", dynamics_type::qss3_sum_4 },
            { "qss3_wsum_2", dynamics_type::qss3_wsum_2 },
            { "qss3_wsum_3", dynamics_type::qss3_wsum_3 },
            { "qss3_wsum_4", dynamics_type::qss3_wsum_4 },
1009
            { "quantifier", dynamics_type::quantifier },
1010
            { "queue", dynamics_type::queue },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1011
1012
1013
            { "time_func", dynamics_type::time_func }
        };

1014
1015
        static_assert(std::size(table) ==
                      static_cast<sz>(dynamics_type_size()));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025

        const auto it =
          std::lower_bound(std::begin(table),
                           std::end(table),
                           dynamics_name,
                           [](const string_to_type& l,
                              const std::string_view r) { return l.name < r; });

        if (it != std::end(table) && it->name == dynamics_name) {
            *type = it->type;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1026
1027
1028
1029
1030
1031
            return true;
        }

        return false;
    }

1032
1033
1034
    status do_read_dynamics(simulation& sim,
                            int id,
                            const char* dynamics_name) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1035
1036
1037
1038
1039
1040
    {
        dynamics_type type;

        irt_return_if_fail(convert(dynamics_name, &type),
                           status::io_file_format_dynamics_unknown);

1041
        auto& mdl = sim.alloc(type);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1042

1043
1044
1045
        auto ret = sim.dispatch(
          mdl, [this, &sim]<typename Dynamics>(Dynamics& dyn) -> status {
              irt_return_if_fail(this->read(sim, dyn),
1046
                                 status::io_file_format_dynamics_init_error);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1047

1048
1049
              return status::success;
          });
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1050

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1051
1052
        irt_return_if_bad(ret);

1053
        map[id] = sim.models.get_id(mdl);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1054
1055

        return status::success;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1056
1057
    }

1058
    bool read(simulation& /*sim*/, none& /*dyn*/) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1059
1060
1061
1062
    {
        return true;
    }

1063
    bool read(simulation& /*sim*/, qss1_integrator& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1064
1065
1066
1067
1068
1069
1070
    {
        double& x1 = *(const_cast<double*>(&dyn.default_X));
        double& x2 = *(const_cast<double*>(&dyn.default_dQ));

        return !!(is >> x1 >> x2);
    }

1071
    bool read(simulation& /*sim*/, qss2_integrator& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1072
1073
1074
1075
1076
1077
1078
    {
        double& x1 = *(const_cast<double*>(&dyn.default_X));
        double& x2 = *(const_cast<double*>(&dyn.default_dQ));

        return !!(is >> x1 >> x2);
    }

1079
    bool read(simulation& /*sim*/, qss3_integrator& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1080
1081
1082
1083
1084
1085
1086
    {
        double& x1 = *(const_cast<double*>(&dyn.default_X));
        double& x2 = *(const_cast<double*>(&dyn.default_dQ));

        return !!(is >> x1 >> x2);
    }

1087
    bool read(simulation& /*sim*/, qss1_multiplier& /*dyn*/) noexcept
1088
1089
1090
1091
    {
        return true;
    }

1092
    bool read(simulation& /*sim*/, qss1_sum_2& /*dyn*/) noexcept
1093
1094
1095
1096
    {
        return true;
    }

1097
    bool read(simulation& /*sim*/, qss1_sum_3& /*dyn*/) noexcept
1098
1099
1100
1101
    {
        return true;
    }

1102
    bool read(simulation& /*sim*/, qss1_sum_4& /*dyn*/) noexcept
1103
1104
1105
1106
    {
        return true;
    }

1107
    bool read(simulation& /*sim*/, qss1_wsum_2& dyn) noexcept
1108
1109
1110
1111
1112
1113
1114
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));

        return !!(is >> x1 >> x2);
    }

1115
    bool read(simulation& /*sim*/, qss1_wsum_3& dyn) noexcept
1116
1117
1118
1119
1120
1121
1122
1123
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));
        double& x3 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));

        return !!(is >> x1 >> x2 >> x3);
    }

1124
    bool read(simulation& /*sim*/, qss1_wsum_4& dyn) noexcept
1125
1126
1127
1128
1129
1130
1131
1132
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));
        double& x3 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));
        double& x4 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));

        return !!(is >> x1 >> x2 >> x3 >> x4);
    }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1133

1134
    bool read(simulation& /*sim*/, qss2_multiplier& /*dyn*/) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1135
1136
1137
1138
    {
        return true;
    }

1139
    bool read(simulation& /*sim*/, qss2_sum_2& /*dyn*/) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1140
1141
1142
1143
    {
        return true;
    }

1144
    bool read(simulation& /*sim*/, qss2_sum_3& /*dyn*/) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1145
1146
1147
1148
    {
        return true;
    }

1149
    bool read(simulation& /*sim*/, qss2_sum_4& /*dyn*/) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1150
1151
1152
1153
    {
        return true;
    }

1154
    bool read(simulation& /*sim*/, qss2_wsum_2& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1155
1156
1157
1158
1159
1160
1161
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));

        return !!(is >> x1 >> x2);
    }

1162
    bool read(simulation& /*sim*/, qss2_wsum_3& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1163
1164
1165
1166
1167
1168
1169
1170
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));
        double& x3 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));

        return !!(is >> x1 >> x2 >> x3);
    }

1171
    bool read(simulation& /*sim*/, qss2_wsum_4& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1172
1173
1174
1175
1176
1177
1178
1179
1180
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));
        double& x3 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));
        double& x4 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));

        return !!(is >> x1 >> x2 >> x3 >> x4);
    }

1181
    bool read(simulation& /*sim*/, qss3_multiplier& /*dyn*/) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1182
1183
1184
1185
    {
        return true;
    }

1186
    bool read(simulation& /*sim*/, qss3_sum_2& /*dyn*/) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1187
1188
1189
1190
    {
        return true;
    }

1191
    bool read(simulation& /*sim*/, qss3_sum_3& /*dyn*/) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1192
1193
1194
1195
    {
        return true;
    }

1196
    bool read(simulation& /*sim*/, qss3_sum_4& /*dyn*/) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1197
1198
1199
1200
    {
        return true;
    }

1201
    bool read(simulation& /*sim*/, qss3_wsum_2& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1202
1203
1204
1205
1206
1207
1208
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));

        return !!(is >> x1 >> x2);
    }

1209
    bool read(simulation& /*sim*/, qss3_wsum_3& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1210
1211
1212
1213
1214
1215
1216
1217
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));
        double& x3 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));

        return !!(is >> x1 >> x2 >> x3);
    }

1218
    bool read(simulation& /*sim*/, qss3_wsum_4& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1219
1220
1221
1222
1223
1224
1225
1226
1227
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));
        double& x3 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));
        double& x4 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));

        return !!(is >> x1 >> x2 >> x3 >> x4);
    }

1228
    bool read(simulation& /*sim*/, integrator& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1229
    {
1230
        return !!(is >> dyn.default_current_value >> dyn.default_reset_value);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1231
1232
    }

1233
    bool read(simulation& /*sim*/, quantifier& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1234
    {
1235
1236
        if (!(is >> dyn.default_step_size >> dyn.default_past_length >>
              temp_1 >> temp_2))
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1237
1238
            return false;

1239
        if (std::strcmp(temp_1, "possible") == 0)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1240
            dyn.default_adapt_state = quantifier::adapt_state::possible;
1241
        else if (std::strcmp(temp_1, "impossible") == 0)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1242
            dyn.default_adapt_state = quantifier::adapt_state::impossible;
1243
        else if (std::strcmp(temp_1, "done") == 0)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1244
1245
1246
1247
            dyn.default_adapt_state = quantifier::adapt_state::done;
        else
            return false;

1248
        if (std::strcmp(temp_2, "true") == 0)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1249
            dyn.default_zero_init_offset = true;
1250
        else if (std::strcmp(temp_2, "false") == 0)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1251
1252
1253
1254
1255
1256
1257
            dyn.default_zero_init_offset = false;
        else
            return false;

        return true;
    }

1258
    bool read(simulation& /*sim*/, adder_2& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1259
    {
1260
1261
        return !!(is >> dyn.default_values[0] >> dyn.default_values[1] >>
                  dyn.default_input_coeffs[0] >> dyn.default_input_coeffs[1]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1262
1263
    }

1264
    bool read(simulation& /*sim*/, adder_3& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1265
    {
1266
1267
1268
        return !!(is >> dyn.default_values[0] >> dyn.default_values[1] >>
                  dyn.default_values[2] >> dyn.default_input_coeffs[0] >>
                  dyn.default_input_coeffs[1] >> dyn.default_input_coeffs[2]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1269
1270
    }

1271
    bool read(simulation& /*sim*/, adder_4& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1272
    {
1273
1274
1275
1276
        return !!(is >> dyn.default_values[0] >> dyn.default_values[1] >>
                  dyn.default_values[2] >> dyn.default_values[3] >>
                  dyn.default_input_coeffs[0] >> dyn.default_input_coeffs[1] >>
                  dyn.default_input_coeffs[2] >> dyn.default_input_coeffs[3]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1277
1278
    }

1279
    bool read(simulation& /*sim*/, mult_2& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1280
    {
1281
1282
        return !!(is >> dyn.default_values[0] >> dyn.default_values[1] >>
                  dyn.default_input_coeffs[0] >> dyn.default_input_coeffs[1]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1283
1284
    }

1285
    bool read(simulation& /*sim*/, mult_3& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1286
    {
1287
1288
1289
        return !!(is >> dyn.default_values[0] >> dyn.default_values[1] >>
                  dyn.default_values[2] >> dyn.default_input_coeffs[0] >>
                  dyn.default_input_coeffs[1] >> dyn.default_input_coeffs[2]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1290
1291
    }

1292
    bool read(simulation& /*sim*/, mult_4& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1293
    {
1294
1295
1296
1297
        return !!(is >> dyn.default_values[0] >> dyn.default_values[1] >>
                  dyn.default_values[2] >> dyn.default_values[3] >>
                  dyn.default_input_coeffs[0] >> dyn.default_input_coeffs[1] >>
                  dyn.default_input_coeffs[2] >> dyn.default_input_coeffs[3]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1298
1299
    }

1300
    bool read(simulation& /*sim*/, counter& /*dyn*/) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1301
1302
1303
1304
    {
        return true;
    }

1305
    bool read(simulation& /*sim*/, queue& dyn) noexcept
1306
    {
1307
1308
1309
        return !!(is >> dyn.default_ta);
    }

1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
    bool read_source(simulation& /*sim*/,
                     source& src,
                     int index,
                     external_source_type type)
    {
        switch (type) {
        case external_source_type::binary_file: {
            auto it = binary_find(
              binary_file_mapping.begin(),
              binary_file_mapping.end(),
              index,
              [](const auto elem, int i) { return i == elem.index; });

            if (it == binary_file_mapping.end())
                return false;

            src.type = ordinal(type);
            src.id = it->value;
            return true;
        };

        case external_source_type::constant: {
            auto it = binary_find(
              constant_mapping.begin(),
              constant_mapping.end(),
              index,
              [](const auto elem, int i) { return i == elem.index; });
            if (it == constant_mapping.end())
                return false;

            src.type = ordinal(type);
            src.id = it->value;
            return true;
        };

        case external_source_type::text_file: {
            auto it = binary_find(
              text_file_mapping.begin(),
              text_file_mapping.end(),
              index,
              [](const auto elem, int i) { return i == elem.index; });
            if (it == text_file_mapping.end())
                return false;

            src.type = ordinal(type);
            src.id = it->value;
            return true;
        };

        case external_source_type::random: {
            auto it = binary_find(
              random_mapping.begin(),
              random_mapping.end(),
              index,
              [](const auto elem, int i) { return i == elem.index; });
            if (it == random_mapping.end())
                return false;

            src.type = ordinal(type);
            src.id = it->value;
            return true;
        };
        }

        irt_unreachable();
    }

    bool read(simulation& sim, dynamic_queue& dyn) noexcept
1378
    {
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
        int index;
        int type;

        if (!(is >> index >> type))
            return false;

        external_source_type source_type;
        if (external_source_type_cast(type, &source_type)) {
            if (!read_source(sim, dyn.default_source_ta, index, source_type))
                return false;
        }

        return true;
1392
1393
    }

1394
    bool read(simulation& sim, priority_queue& dyn) noexcept
1395
    {
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
        int index;
        int type;

        if (!(is >> index >> type))
            return false;

        external_source_type source_type;
        if (external_source_type_cast(type, &source_type)) {
            if (!read_source(sim, dyn.default_source_ta, index, source_type))
                return false;
        }

        return true;
1409
1410
    }

1411
    bool read(simulation& sim, generator& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1412
    {
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
        int index[2];
        int type[2];

        if (!(is >> dyn.default_offset >> index[0] >> type[0] >> index[1] >>
              type[1]))
            return false;

        external_source_type source_type;
        if (external_source_type_cast(type[0], &source_type)) {
            if (!read_source(sim, dyn.default_source_ta, index[0], source_type))
                return false;
        }

        if (external_source_type_cast(type[1], &source_type)) {
            if (!read_source(
                  sim, dyn.default_source_value, index[1], source_type))
                return false;
        }

        return true;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1433
1434
    }

1435
    bool read(simulation& /*sim*/, constant& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1436
    {
1437
        return !!(is >> dyn.default_value);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1438
1439
    }

1440
    bool read(simulation& /*sim*/, qss1_cross& dyn) noexcept
1441
1442
1443
1444
    {
        return !!(is >> dyn.default_threshold);
    }

1445
    bool read(simulation& /*sim*/, qss2_cross& dyn) noexcept
1446
1447
1448
1449
    {
        return !!(is >> dyn.default_threshold);
    }

1450
    bool read(simulation& /*sim*/, qss3_cross& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1451
1452
1453
1454
    {
        return !!(is >> dyn.default_threshold);
    }

1455
    bool read(simulation& /*sim*/, qss1_power& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1456
    {
1457
1458
1459
        return !!(is >> dyn.default_n);
    }

1460
    bool read(simulation& /*sim*/, qss2_power& dyn) noexcept
1461
1462
1463
1464
    {
        return !!(is >> dyn.default_n);
    }

1465
    bool read(simulation& /*sim*/, qss3_power& dyn) noexcept
1466
1467
1468
1469
    {
        return !!(is >> dyn.default_n);
    }

1470
    bool read(simulation& /*sim*/, qss1_square& /*dyn*/) noexcept
1471
1472
1473
1474
    {
        return true;
    }

1475
    bool read(simulation& /*sim*/, qss2_square& /*dyn*/) noexcept
1476
1477
1478
1479
    {
        return true;
    }

1480
    bool read(simulation& /*sim*/, qss3_square& /*dyn*/) noexcept
1481
1482
1483
1484
    {
        return true;
    }

1485
    bool read(simulation& /*sim*/, cross& dyn) noexcept
1486
    {
1487
        return !!(is >> dyn.default_threshold);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1488
1489
    }

1490
    bool read(simulation& /*sim*/, accumulator_2& /*dyn*/) noexcept
1491
1492
1493
1494
    {
        return true;
    }

1495
    bool read(simulation& /*sim*/, time_func& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1496
    {
1497
        if (!(is >> temp_1))
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1498
1499
            return false;

1500
1501