-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibdyn_cpp.h
More file actions
1040 lines (774 loc) · 28.3 KB
/
libdyn_cpp.h
File metadata and controls
1040 lines (774 loc) · 28.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
62
63
64
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
140
141
142
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
219
220
221
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
300
301
302
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
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
429
430
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
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
653
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
685
686
687
688
689
690
691
692
693
694
695
696
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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
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
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2010, 2011 Christian Klauer
This file is part of OpenRTDynamics, the Real Time Dynamic Toolbox
OpenRTDynamics is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenRTDynamics is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with OpenRTDynamics. If not, see <http://www.gnu.org/licenses/>.
*/
//
#ifndef _LIBDYN_CPP_H
#define _LIBDYN_CPP_H 1
#include <stdlib.h>
#ifndef __APPLE__
#include <malloc.h>
#endif
extern "C" {
#include "libdyn.h"
#include "irpar.h"
}
#define REMOTE
#ifdef REMOTE
// #include "modules/rt_server/parameter_manager.h"
#include "parameter_manager.h"
#include "libdyn_cpp.h"
#endif
// The different realtime envoronments
#define RTENV_UNDECIDED 0
#define RTENV_SIMULATION 1
#define RTENV_RTP 2
#define RTENV_RTAI 3
class irpar;
class libdyn_master;
class libdyn;
class libdyn_nested;
class libdyn_nested2;
class ortd_io_internal;
// Änderungen:
// - dynlib_simulation_t soll einen void *cpp_huelle - link zu libdyn_cpp Klasse erhalten
// - blöcke die den communication_server benutzen können über diesen link, über die masterklasse auf die Serverklasse zugreifen.
// - global_comp_func_list wird einmal vom master angelegt (alle module werden aufgerufen um die blöcke zu registrieren) und allen simulationen als pointer weitergereicht
// - libdyn_cpp Klassen können auch ohne master betrieben werden communication_server blöcke müssen diesen fall abfangen. (cpp_huelle==NULL) test
// - libdyn_cpp
// - nested simulations
// NEU Class which is created once and <used for multiple simulations e.g. nested simulations
#define BUILD_COMMUNICATION_SEVER
/*
* Move these irpar classes to something like irpar_cpp.h
*/
class irpar {
private:
char fname_ipar[256];
char fname_rpar[256];
unsigned int magic;
public:
irpar();
~irpar();
// allocate buffers. Usage of load_from_afile not possible for this case
irpar(int Nipar, int Nrpar);
void destruct();
int err; // variable for errors
// use only one of the following functions to initialise
bool load_from_afile( char* fname_i, char* fname_r );
bool load_from_afile( char* fname );
int Nipar, Nrpar;
int *ipar;
double *rpar;
};
class irpar_ivec {
public:
irpar_ivec(int *ipar, double *rpar, int id) {
struct irpar_ivec_t vec;
if ( irpar_get_ivec(&vec, ipar, rpar, id) < 0 ) throw 1;
// printf("vec[0] = %d\n", vec.v[0]);
v = vec.v; n = vec.n;
}
int n;
int *v;
};
class irpar_rvec {
public:
irpar_rvec(int *ipar, double *rpar, int id) {
struct irpar_rvec_t vec;
if ( irpar_get_rvec(&vec, ipar, rpar, id) < 0 ) throw 1;
// printf("vec[0] = %d\n", vec.v[0]);
v = vec.v; n = vec.n;
}
int n;
double *v;
};
class irpar_string {
public:
irpar_string(int *ipar, double *rpar, int id) {
struct irpar_ivec_t str_;
char *str;
if ( irpar_get_ivec(&str_, ipar, rpar, id) < 0 ) throw 1;
irpar_getstr(&str, str_.v, 0, str_.n);
// printf("str = %s\n", str);
s = new std::string(str);
free(str); // do not forget to free the memory allocated by irpar_getstr
}
~irpar_string() {
delete s;
}
std::string *s;
};
#include "io.h"
class libdyn_master {
private:
// Daten, die vererbt werden sollen
struct lindyn_comp_func_list_head_t *global_comp_func_list;
void *communication_server;
// ... //
#ifdef REMOTE
#endif
// Die Verzeichnisstruktur aus communication_server hier herein bauen
unsigned int magic;
public:
// return communication_server variable
rt_server_threads_manager *get_communication_server();
libdyn_master(int realtime_env, int remote_control_tcpport);
libdyn_master();
void check_memory(); // For error detection purposes
// gemeinsam genutzte systeme initialisieren
#ifdef REMOTE
rt_server_threads_manager * rts_mgr;
directory_tree * dtree;
parameter_manager * pmgr;
ortd_stream_manager *stream_mgr;
int init_communication(int tcpport);
void close_communication();
// ... //
#endif
// The io framework ADded onn
// 4.9.14
ortd_io_internal *ortd_io;
// calls every module to register its blocks
int init_blocklist();
void destruct();
// store the type of the realtime environment. rtp, rtai, simulation ...
int realtime_environment;
};
// The old, ugnly interface, which is also used by the Scicos block
class libdyn_nested {
private:
bool internal_init(int Nin, const int* insizes_, const int *intypes, int Nout, const int* outsizes_, const int *outtypes);
bool allocate_inbuffers();
void set_buffer_inptrs();
void* InputBuffer;
public:
libdyn_master * ld_master;
private:
// slot management
void free_slots();
int slots_available();
bool slotindexOK(int nSim); // Test if nSim is in correct range
void lock_slots() {
pthread_mutex_lock(&slots_mutex);
};
void unlock_slots() {
pthread_mutex_unlock(&slots_mutex);
};
// Array of size Nslots
pthread_mutex_t slots_mutex;
libdyn **sim_slots;
int Nslots;
int slot_addsim_pointer;
int current_slot;
int usedSlots; // The number of slots with actually contain simulations (index from 0 to usedSlots-1)
// bool set_current_simulation(struct dynlib_simulation_t *sim);
bool is_current_simulation(int slotID);
public:
libdyn_nested(int Nin, const int* insizes_, const int *intypes, int Nout, const int* outsizes_, const int *outtypes);
libdyn_nested(int Nin, const int* insizes_, const int *intypes, int Nout, const int* outsizes_, const int *outtypes, bool use_buffered_input);
void destruct();
void set_master(libdyn_master *master) {
this->ld_master = master;
}
libdyn_master * get_master() {
return this->ld_master;
}
/**
* \brief Configure pointer to input port source variables
* only usefull if use_buffered_input == false
* This function has to be called BEFORE add_simulation
*
* \param in number of input port
* \param inptr array of a double variables that will be used as input vector
*/
bool cfg_inptr(int in, void *inptr);
// uses a buffer for copies of the input data - necessary within scicos blocks or for a threaded nested simulation
bool use_buffered_input;
// slot management; call BEFORE add_simulation
void allocate_slots(int n);
// Add a simulation into the next free slot (slotID == -1) or to the specified slot
// This can also occur while other simulations are running
int add_simulation(int slotID, irpar* param, int boxid); //
int add_simulation(irpar* param, int boxid); // same as above with slotid = -1
int add_simulation(int slotID, int *ipar, double *rpar, int boxid);
int add_simulation(int slotID, libdyn* sim);
// remove a simulation from the list
// the simulation instance will be destructed
int del_simulation(int slotID);
// before removal switch to another simulation
int del_simulation(int slotID, int switchto_slotID);
// bool reset_states_of_simulation(struct dynlib_simulation_t *sim);
// Activate a simulation from the slots
bool set_current_simulation(int nSim);
/**
* \brief reset the states of all blocks in the current simulation (Flag COMPF_FLAG_RESETSTATES will be called for each block)
*/
void reset_blocks();
// laods NSimulations schematics from an irpar container with increasing irparid starting irparid = at start_boxid.
// add_simulation is called for each
bool load_simulations(int *ipar, double *rpar, int start_boxid, int NSimulations);
// length of one element depends on datatype
void copy_outport_vec(int nPort, void *dest);
//
void copy_inport_vec(int nPort, void *src);
void event_trigger_mask(int mask);
void simulation_step(int update_states);
libdyn *current_sim; // FIXME: Needs volatile
struct libdyn_io_config_t iocfg;
};
class libdyn_simple_if {
};
class libdyn_nested2 { // TODO derive from libdyn_simple_if
public:
libdyn_nested2(int Nin, const int* insizes_, const int *intypes, int Nout, const int* outsizes_, const int *outtypes);
libdyn_nested2(int Nin, const int* insizes_, const int *intypes, int Nout, const int* outsizes_, const int *outtypes, bool use_buffered_input);
libdyn_nested2(int Nin, int Nout, bool use_buffered_input);
void FinishConfiguration();
private:
bool internal_init(int Nin, const int* insizes_, const int *intypes, int Nout, const int* outsizes_, const int *outtypes);
void allocate_structures(int Nin, int Nout);
bool allocate_inbuffers();
void set_buffer_inptrs();
void* InputBuffer;
bool ConfigurationFinished;
class libdyn_nested2* Parent_SimnestClassPtr; // pointer to parent simulation nest
public:
libdyn_master * ld_master;
//
// slot management
//
private:
void free_slots();
int slots_available();
bool slotindexOK(int nSim); // Test if nSim is in correct range
void lock_slots() {
pthread_mutex_lock(&slots_mutex);
};
void unlock_slots() {
pthread_mutex_unlock(&slots_mutex);
};
pthread_mutex_t slots_mutex;
// libdyn **sim_slots;
typedef struct {
libdyn *sim; // If sim == NULL --> slot is empty
bool replaceable_simulation_initialised;
} sim_slot_t;
sim_slot_t *sim_slots; // Array of size Nslots, if sim_slots == NULL then no slots are configured
int Nslots;
int slot_addsim_pointer;
int current_slot_nr; // FIXME VOLATILE
sim_slot_t *current_slot; // replacement for the old current_sim variable
int usedSlots; // The number of slots with actually contain simulations (index from 0 to usedSlots-1)
dynlib_simulation_t *ParentSim;
int NestedLevel;
int NestedLevel_AtWhichResetAppreared;
public:
// slot management; call BEFORE add_simulation
void allocate_slots(int n);
bool is_current_simulation(int slotID);
void destruct();
// Vererbung
void set_master(libdyn_master *ld_master) {
this->ld_master = ld_master;
}
libdyn_master * get_master() {
return this->ld_master;
}
void set_parent_simulation( dynlib_simulation_t *ParentSim );
static class libdyn_nested2* GetSimnestClassPtrFromC(dynlib_simulation_t *sim) {
return (class libdyn_nested2*) sim->SimnestClassPtr;
}
int getNestedLevel() {
return NestedLevel;
}
void setNestedLevel( int Level ) {
NestedLevel = Level;
}
// List of elements TODO Supposed to remove all directory.cpp stuff
void addElement(char *name, int type, void *userptr); // TODO Implement add an element to the list of objects
void *lockupElement(char *name, int type);
void deleteElement(char *name, int type);
typedef struct {
void * userptr;
int type;
int ID; //
} Element_t;
typedef std::map<std::string, Element_t> Element_map_t;
Element_map_t Elements;
int ElementIDcounter;
/**
* \brief Configure pointer to input port source variables
* only usefull if use_buffered_input == false
* This function has to be called BEFORE add_simulation
*
* \param in number of input port
* \param inptr array of a double variables that will be used as input vector
*/
bool cfg_inptr(int in, void *inptr);
bool cfg_inptr(int in, void *inptr, int size, int datatype);
void cfg_output(int out, int size, int datatype);
// uses a buffer for copies of the input data - necessary within scicos blocks or for a threaded nested simulation
bool use_buffered_input;
// Add a simulation into the next free slot (slotID == -1) or to the specified slot
// This can also occur while other simulations are running
int add_simulation(int slotID, irpar* param, int boxid); //
int add_simulation(irpar* param, int boxid); // same as above with slotid = -1
int add_simulation(int slotID, int *ipar, double *rpar, int boxid);
int add_simulation(int slotID, libdyn* sim);
// remove a simulation from the list
// the simulation instance will be destructed
int del_simulation(int slotID);
// before removal switch to another simulation
int del_simulation(int slotID, int switchto_slotID);
// for module nested
int CallSyncCallbackDestructor();
// bool reset_states_of_simulation(struct dynlib_simulation_t *sim);
// Activate a simulation from the slots
bool set_current_simulation(int nSim);
// get active simulation
dynlib_simulation_t* get_current_simulation_libdynSimStruct();
/**
* \brief reset the states of all blocks in the current simulation (Flag COMPF_FLAG_RESETSTATES will be called for each block)
*/
void reset_blocks();
void reset_blocks(int slotId); // UNTESTED AND UNUSED FOR NOW
void forwardReset();
// laods NSimulations schematics from an irpar container with increasing irparid starting irparid = at start_boxid.
// add_simulation is called for each
bool load_simulations(int *ipar, double *rpar, int start_boxid, int NSimulations);
// length of one element depends on datatype
void copy_outport_vec(int nPort, void *dest);
//
void copy_inport_vec(int nPort, void *src);
void event_trigger_mask(int mask);
void simulation_step(int update_states);
// Similar to simulation_step bur split up into two functions
// Additionally they are checking wheter after simulation
// initialisation, at first the outputs are calculated.
// This has to be used, if there shall be the possibility to
// online replace the simulation.
void simulation_step_outpute();
void simulation_step_supdate();
// pointer to the currently active simulation
libdyn *current_sim; // FIXME: Needs volatile
// configuration for in and output ports
struct libdyn_io_config_t iocfg;
};
class libdyn {
private:
libdyn_master *ld_master; // NEU pointer to libdyn_master; initial == NULL;
// int NestedLevel;
struct dynlib_simulation_t *sim;
struct libdyn_io_config_t iocfg;
// NEU währen die Simulation sim läuft kann eine neue Simulation vorbereitet werden
struct dynlib_simulation_t *sim_prepare;
struct libdyn_io_config_t iocfg_prepare;
int prepare_replacement_sim(int *ipar, double *rpar, int boxid);
void switch_to_replacement_sim();
// Liste eingebetter Simulationen. Diese kann sich durch laden einer neuen simulation ändern
libdyn **nested_sim;
// // NEU Simulation der höheren Ebene, wenn keine =NULL
// libdyn *higher_level_sim;
// Fehler ??
int error;
// main construtor
void libdyn_internal_constructor(int Nin, const int* insizes_, int Nout, const int* outsizes_);
public:
struct dynlib_simulation_t * get_C_SimulationObject();
// For Synchronisation ,for module nested
int CallSyncCallbackDestructor();
bool IsSyncronised();
int RunSyncCallbackFn();
// NEU set a new master
void set_master(libdyn_master *ld_master) {
this->ld_master = ld_master;
}
libdyn_master * get_master() {
return this->ld_master;
}
// void setNestedLevel( int Level );
/**
* \brief Set-up a new libdyn instance
* \param Nin number of inputs
* \param insizes_ an array of size Nin containing port sizes for the input ports
* \param Nout number of outputs
* \param outsizes_ an array of size Nout containing port sizes for the output ports
*/
libdyn(int Nin, const int* insizes_, int Nout, const int* outsizes_);
libdyn(int Nin, const int* insizes_, const int*intypes, int Nout, const int* outsizes_, const int *outtypes); // the more recent version
// FIXME does not work.
libdyn(libdyn_io_config_t * iocfg);
/**
* \brief Delete everything
*/
void destruct();
/**
* \brief Configure pointer to input port source variables
* \param in number of input port
* \param inptr array of a double variables that will be used as input vector
*/
bool cfg_inptr(int in, double *inptr);
/**
* \brief Set-up a simulation schematic based on integer and real vectors (irpar encoding; see irpar.c)
* \brief this was encoded by a scilab script
*
* \brief irpar_load_from_afile is commonly used for loading these vector from files
*
* \param ipar array of integers containing irpar encoding
* \param rpar array of doubles containing irpar encoding
* \param boxid irpar-ID of the schematic container
*
* \return if there is an error compiling the schematic a value less than 0 is returned
*/
int irpar_setup(int *ipar, double *rpar, int boxid);
/**
* \brief Trigger events according to a given bitmask
* \brief This should be called before "simulation_step".
* \brief These events are routed to the blocks
*
* \param mask each bit stands for an event, which is one if the event should occur
*/
void event_trigger_mask(int mask);
/**
* \brief One step in simulation
* \brief if update_states == 1, then COMPF_FLAG_UPDATESTATES is send to all computational functions
* \brief if update_states == 0, then COMPF_FLAG_CALCOUTPUTS is send to all computational functions in the correct sequence
*/
void simulation_step(int update_states);
/**
* \brief Check wheter the simulation wants to pause its execution for synchronisation reasons. If true the simulation does not want to be called by "simulation_step" anymore FIXME obsolete
*/
bool getSyncState();
/**
* \brief reset the states of all block in the simulation (Flag COMPF_FLAG_RESETSTATES will be called for each block)
*/
void reset_blocks();
/**
* \brief Get a simulation output value (only for output ports of size one)
* \param out Number of output port
*/
double get_skalar_out(int out);
/**
* \brief get a pointer to an array of a simulation output port
* \param out Number of output port
*/
double * get_vec_out(int out);
/**
* \brief Add a user defined computational function (UNTESTED)
* \param blockid the block id to be used (this has to be a unique one)
* \param comp_fn pointer to the compuational function
*/
bool add_libdyn_block(int blockid, void *comp_fn);
/**
* \brief Dump a list of all created blocks to stdout
*/
void dump_all_blocks();
};
/*
A nicer C++ interface for compuational functions
This is a template for a C-function that forwards the calls to a c++ class given by T
*/
template <class T>
inline int LibdynCompFnTempate(int flag, struct dynlib_block_t *block) {
// This is the main C-Callback function, which forwards requests to the functions of the C++-Class
// uncomment this if you want to know when this block is called by the simulator
// printf("comp_func Template: flag==%d\n", flag);
// the blocks raw parameter sets for integers and doubles
double *rpar = libdyn_get_rpar_ptr(block);
int *ipar = libdyn_get_ipar_ptr(block);
switch (flag) {
case COMPF_FLAG_CALCOUTPUTS:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->calcOutputs();
}
return 0;
break;
case COMPF_FLAG_UPDATESTATES:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->updateStates();
}
return 0;
break;
case COMPF_FLAG_PREPARERESET:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->PrepareReset();
}
return 0;
break;
case COMPF_FLAG_RESETSTATES:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->resetStates();
}
return 0;
break;
case COMPF_FLAG_HIGHERLEVELRESET:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->HigherLevelResetStates();
}
return 0;
break;
case COMPF_FLAG_POSTINIT:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->PostInit();
}
return 0;
break;
case COMPF_FLAG_CONFIGURE: // configure. NOTE: do not reserve memory or open devices. Do this within COMPF_FLAG_INIT instead!
{
return libdyn_AutoConfigureBlock(block, ipar, rpar);
}
return 0;
break;
case COMPF_FLAG_INIT: // init
{
T *worker = new T(block);
libdyn_set_work_ptr(block, (void*) worker); // remember the instance of the C++ Class
int ret = worker->init();
if (ret < 0)
return -1;
}
return 0;
break;
case COMPF_FLAG_PREINIT:
{ // Added on 20.4.18 (The init flags shall return well defined values!)
}
return 0;
break;
case COMPF_FLAG_DESTUCTOR: // destroy instance
{
T *worker = (T *) libdyn_get_work_ptr(block);
delete worker;
}
return 0;
break;
case COMPF_FLAG_PRINTINFO:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->printInfo();
}
return 0;
break;
}
}
/*
A nicer C++ interface for compuational functions
This is a template for a C-function that forwards the calls to a c++ class given by T
*/
// Version 2: The C++ class will be initialised with preinit flags
// Normal init will be called
template <class T>
inline int LibdynCompFnTempate2(int flag, struct dynlib_block_t *block) {
// This is the main C-Callback function, which forwards requests to the functions of the C++-Class
// Block based on this template are initialized before and destroyed after normal blocks
// uncomment this if you want to know when this block is called by the simulator
// printf("comp_func Template: flag==%d\n", flag);
// the blocks raw parameter sets for integers and doubles
double *rpar = libdyn_get_rpar_ptr(block);
int *ipar = libdyn_get_ipar_ptr(block);
switch (flag) {
case COMPF_FLAG_CALCOUTPUTS:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->calcOutputs();
}
return 0;
break;
case COMPF_FLAG_UPDATESTATES:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->updateStates();
}
return 0;
break;
case COMPF_FLAG_PREPARERESET:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->PrepareReset();
}
return 0;
break;
case COMPF_FLAG_RESETSTATES:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->resetStates();
}
return 0;
break;
case COMPF_FLAG_HIGHERLEVELRESET:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->HigherLevelResetStates();
}
return 0;
break;
case COMPF_FLAG_POSTINIT:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->PostInit();
}
return 0;
break;
case COMPF_FLAG_PREINIT:
{
// Get the irpar parameters Uipar, Urpar
int *Uipar; double *Urpar;
libdyn_AutoConfigureBlock_GetUirpar(block, &Uipar, &Urpar);
// create the instance of the C++ Class
T *worker = new T(block, &Uipar, &Urpar);
libdyn_set_work_ptr(block, (void*) worker); // remember the instance of the C++ Class
worker->PreInit();
//int ret = worker->init();
//if (ret < 0)
// return -1;
}
return 0;
break;
case COMPF_FLAG_POSTDESTUCTOR: // previously: COMPF_FLAG_PREINITUNDO: // destroy instance
{
T *worker = (T *) libdyn_get_work_ptr(block);
delete worker;
}
return 0;
break;
case COMPF_FLAG_CONFIGURE: // configure. NOTE: do not reserve memory or open devices. Do this within COMPF_FLAG_INIT instead!
{
return libdyn_AutoConfigureBlock(block, ipar, rpar);
}
return 0;
break;
case COMPF_FLAG_INIT: // init
{
T *worker = (T *) libdyn_get_work_ptr(block);
int ret = worker->init();
if (ret < 0)
return -1;
}
return 0;
break;
case COMPF_FLAG_DESTUCTOR: // destroy instance (will later be destroyed by COMPF_FLAG_POSTDESTUCTOR)
{
// T *worker = (T *) libdyn_get_work_ptr(block);
// delete worker;
}
return 0;
break;
case COMPF_FLAG_PRINTINFO:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->printInfo();
}
return 0;
break;
}
}
template <class T>
inline int LibdynCompFnTempate_PreInit(int flag, struct dynlib_block_t *block) {
// This is the main C-Callback function, which forwards requests to the functions of the C++-Class
//
// Blocks based on this template are initialized prior and destructed after normal blocks.
// This is especially useful for e.g. blocks that share instances of objects with with other blocks
// uncomment this if you want to know when this block is called by the simulator
// printf("comp_func Template: flag==%d\n", flag);
// the blocks raw parameter sets for integers and doubles
double *rpar = libdyn_get_rpar_ptr(block);
int *ipar = libdyn_get_ipar_ptr(block);
switch (flag) {
case COMPF_FLAG_CALCOUTPUTS:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->calcOutputs();
}
return 0;
break;
case COMPF_FLAG_UPDATESTATES:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->updateStates();
}
return 0;
break;
case COMPF_FLAG_PREPARERESET:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->PrepareReset();
}
return 0;
break;
case COMPF_FLAG_RESETSTATES:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->resetStates();
}
return 0;
break;
case COMPF_FLAG_HIGHERLEVELRESET:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->HigherLevelResetStates();
}
return 0;
break;
case COMPF_FLAG_POSTINIT:
{
T *worker = (T *) libdyn_get_work_ptr(block);
worker->PostInit();
}
return 0;
break;
case COMPF_FLAG_CONFIGURE: // configure. NOTE: do not reserve memory or open devices. Do this within COMPF_FLAG_INIT instead!
{
return libdyn_AutoConfigureBlock(block, ipar, rpar);
}
return 0;
break;
case COMPF_FLAG_PREINIT: // init