itpp_sci  1.0.0
it++ based simulation framework for scicoslab, scilab and scipy
_sci_if_emu.hpp
1 /*xxx
2  * \author maki
3  */
4 #ifndef SCI_IF_EMU_HPP
5 #define SCI_IF_EMU_HPP
6 
7 #include "sci\_sci_base.hpp"
8 #include "sci\_sci_macros.hpp"
9 #include "sci\_sci_var.hpp"
10 #include "sci\_sci_if_create_var.hpp"
11 #include "sci\_sci_if_emu_var_struct.hpp"
12 #include "sci\_sci_emu.hpp"
13 
14 #include "_sci_if_var_struct.h"
15 
16 #define nFAST_EMU
17 
18 namespace SCI
19 {
22 
23 // simulate a struct var_struct using double
24 struct var_struct emu_var_struct(const double &v);
25 // simulate a struct var_struct using vec
26 struct var_struct emu_var_struct(const vec &v);
27 // simulate a struct var_struct using mat
28 struct var_struct emu_var_struct(const mat &m);
29 
30 // simulate a struct var_struct using int
31 struct var_struct emu_var_struct(const int &v);
32 // simulate a struct var_struct using ivec
33 struct var_struct emu_var_struct(const ivec &v);
34 // simulate a struct var_struct using imat
35 struct var_struct emu_var_struct(const imat &m);
36 
37 // simulate a struct var_struct using int
38 struct var_struct emu_var_struct(const bool &v);
39 // simulate a struct var_struct using bvec
40 struct var_struct emu_var_struct(const bvec &v);
41 // simulate a struct var_struct using bmat
42 struct var_struct emu_var_struct(const bmat &m);
43 
44 // simulate a struct var_struct using complex
45 struct var_struct emu_var_struct(const std::complex<double> &v);
46 // simulate a struct var_struct using cvec
47 struct var_struct emu_var_struct(const cvec &v);
48 // simulate a struct var_struct using cmat
49 struct var_struct emu_var_struct(const cmat &m);
50 
51 // no overload just keep function name conventions
52 void* EMU_SCI_IF_CREATE(int sci_type);
53 
54 // no overload just keep function name conventions
55 void EMU_SCI_IF_EXEC(void *p_dev, int sci_command);
56 
57 // no overload just keep function name conventions
58 void EMU_SCI_IF_DESTROY(void *p_dev);
59 
60 template <class P >
61 void EMU_SCI_IF_SET(void *p_dev, int sci_param, P param)
62 {
63  struct var_struct s_v;
64 
65  // create an external variable using param
66  s_v = emu_var_struct(param);
67  // call if api function
68  sci_if_set(p_dev, sci_param, &s_v);
69  // remove data allocated by emu_var_struct
70  delete_emu_var_struct_data(&s_v);
71 }
72 
73 template <class P >
74 P EMU_SCI_IF_GET(void *p_dev, int sci_param)
75 {
76  struct var_struct s_v;
77  P v;
78 
79  // sci_var variable is created and there is a description of it in s_v
80  s_v = sci_if_get(p_dev, sci_param);
81 #if defined (FAST_EMU)
82  // be fast - not accurate - don't test upload
83  sci_var_value_get((sci_var *)(s_v.p_var), &v);
84  sci_if_delete_var(&s_v);
85 #else
86  // be inefficient
87  // allocate memory for external variable using struct_var, copy sci_var to external, delete sci_var created by sci_if_get() as gateway
88  // fetch external data into temporary sci_var, extract returned object, delete external data and temporary sci_var
89  sci_var *p_var_v;
90 
91  emu_sci_if_malloc(&s_v);
92  // use gateway functions to upload data to emulated external memory
93  sci_if_copy_var(&s_v);
94  // delete sci_var created by sci_if_get(), since return variable is copied by sci_if_copy_var(), yet in stack format
95  sci_if_delete_var(&s_v);
96  // get data in stack format back into c++ as a sci_var object
97  p_var_v = create_var(&s_v);
98  // store value in returned object
99  sci_var_value_get(p_var_v, &v);
100  // delete stack data
101  delete_emu_var_struct_data(&s_v);
102  // delete sci_var as return is now safe in y
103  delete(p_var_v);
104 #endif
105 
106  return (v);
107 
108 }
109 
110 template <class Y, class CE>
111 Y EMU_SCI_IF_GEN(void *p_dev, CE v_ce)
112 {
113  Y y;
114  struct var_struct s_y, s_ce;
115 
116  s_ce = emu_var_struct(v_ce);
117  s_y = sci_if_gen(p_dev, &s_ce);
118  delete_emu_var_struct_data(&s_ce); // emu creates data directly on heap
119 #if defined (FAST_EMU)
120  // be fast - not accurate - don't test upload
121  sci_var_value_get((sci_var *)(s_y.p_var), &y);
122  sci_if_delete_var(&s_y);
123 #else
124  // be inefficient
125  // allocate memory for external variable using struct_var, copy sci_var to external, delete sci_var created by sci_if_get() as gateway
126  // fetch external data into temporary sci_var, extract returned object, delete external data and temporary sci_var
127 
128  sci_var *p_var_y;
129 
130  // allocate memory for external variable using struct_var
131  emu_sci_if_malloc(&s_y);
132  // use gateway functions to upload data to emulated external memory
133  sci_if_copy_var(&s_y);
134  // delete sci_var created by sci_if_get(), since return variable is copied by sci_if_copy_var(), yet in stack format
135  sci_if_delete_var(&s_y);
136  // get data in stack format back into c++ as a sci_var object
137  p_var_y = create_var(&s_y);
138  // get what is to be returned in y
139  sci_var_value_get(p_var_y, &y);
140  // delete stack data
141  delete_emu_var_struct_data(&s_y);
142  // delete sci_var as return variable is now safe in y
143  delete(p_var_y);
144 #endif
145 
146  return (y);
147 }
148 
149 template <class Y, class CE, class X>
150 Y EMU_SCI_IF_PROC(void *p_dev, CE v_ce, X v_x)
151 {
152  Y y;
153  struct var_struct s_y, s_ce, s_x;
154 
155  s_ce = emu_var_struct(v_ce);
156  s_x = emu_var_struct(v_x);
157  s_y = sci_if_proc(p_dev, &s_ce, &s_x);
158  delete_emu_var_struct_data(&s_ce); // emu creates data directly on heap
159  delete_emu_var_struct_data(&s_x); // emu creates data directly on heap
160 #if defined (FAST_EMU)
161  // be fast - not accurate - don't test upload
162  sci_var_value_get((sci_var *)(s_y.p_var), &y);
163  sci_if_delete_var(&s_y);
164 #else
165  // be inefficient
166  // allocate memory for external variable using struct_var, copy sci_var to external, delete sci_var created by sci_if_get() as gateway
167  // fetch external data into temporary sci_var, extract returned object, delete external data and temporary sci_var
168 
169  sci_var *p_var_y;
170 
171  //allocate memory for external variable using struct_var
172  emu_sci_if_malloc(&s_y);
173  //use gateway functions to upload data to emulated external memory
174  sci_if_copy_var(&s_y);
175  //delete sci_var created by sci_if_get(), since return variable is copied by sci_if_copy_var(), yet in stack format
176  sci_if_delete_var(&s_y);
177  //get data in stack format back into c++ as a sci_var object
178  p_var_y = create_var(&s_y);
179  // get what is to be returned in y
180  sci_var_value_get(p_var_y, &y);
181  // delete stack data
182  delete_emu_var_struct_data(&s_y);
183  // delete sci_var as return is now safe in y
184  delete(p_var_y);
185 #endif
186 
187  return (y);
188 }
189 
192 
193 }
194 
195 #endif
SCI layer implements SCI_API interface as wrappers for SIM layer models.
Definition: _sci_create_sci_var.cpp:8
Abstract base for encapsulation of sci_variables.
Definition: _sci_var.hpp:23