1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2011 - DIGITEO - Calixte DENIZET
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15 
16 #ifndef __SCILABTOJAVA_HXX__
17 #define __SCILABTOJAVA_HXX__
18 
19 #include <string>
20 #include <vector>
21 
22 #include "../src/jni/ScilabVariables.hxx"
23 #include "../src/jni/ScilabVariablesRefresh.hxx"
24 #include "../src/jni/ScilabPolynomialToJava.hxx"
25 
26 extern "C"
27 {
28 #include "Scierror.h"
29 #include "api_scilab.h"
30 #include "getScilabJavaVM.h"
31 #include "GetScilabVariable.h"
32 }
33 
34 using namespace org_scilab_modules_types;
35 
36 namespace org_modules_types
37 {
38 
39 /**
40  * @file
41  * @author Calixte DENIZET <calixte.denizet@scilab.org>
42  *
43  * Class to send Scilab datas in Java environment
44  */
45 class ScilabToJava
46 {
47 
48     static int refreshId;
49 
50     /**
51      * Get the pointer on the indexes
52      * @param indexes a vector containing indexes
53      * @return the pointer
54      */
55     static int * getIndexesPointer(std::vector<int> & indexes);
56 
57     /**
58      * Get a double dimension matrix from a one dimension one
59      * The result must be delete in using deleteMatrix
60      *
61      * @param T the data type
62      * @param row the row number
63      * @param col the col number
64      * @param data the data
65      * @param swaped true if the matrix is stored row by row
66      * @return the double dimension matrix
67      */
68     template<typename T>
69     static T ** getMatrix(int row, int col, T * data, bool swaped);
70 
71     /**
72      * Get a double dimension matrix from a one dimension one
73      * The result must be delete in using deleteMatrix
74      * Each data element is casted from T to U
75      *
76      * @param T the in data type
77      * @param U the out data type
78      * @param row the row number
79      * @param col the col number
80      * @param data the data
81      * @param swaped true if the matrix is stored row by row
82      * @return the double dimension matrix
83      */
84     template<typename T, typename U>
85     static T ** getConvertedMatrix(int row, int col, U * data, bool swaped);
86 
87     /**
88      * Convert a single row matrix into a double dimension one
89      *
90      * @param row the row number
91      * @param col the col number
92      * @return the double dimension matrix
93      */
94     template<typename T>
95     static T ** convertMatrix(int row, int col, T * data);
96 
97     /**
98      * Delete a matrix previously created with getMatrix or getConvertedMatrix
99      * or convertMatrix
100      *
101      * @param T the in data type
102      * @param data the data
103      * @param swaped true if the matrix is stored row by row
104      */
105     template<typename T>
106     static void deleteMatrix(T ** data, bool swaped);
107 
108     /**
109      * Send the list items
110      *
111      * @param name the variable name
112     * @param nbItems the number of items
113            * @param indexes an integer array with the indexes of the (sub)*-list which will contain the data
114            * @param addr the address of the variable in the Scilab stack
115            * @param swaped true if the matrix is stored row by row
116      * @param byref true if the variable is passed by reference
117            * @param handlerId the handler id
118            * @param pvApiCtx a pointer to the context
119            */
120     static bool sendItems(const std::string & name, const int nbItems, std::vector<int> & indexes, int * addr, bool swaped, bool byref, int handlerId, void * pvApiCtx);
121 
122     /**
123      * Send a variable in the Java environment
124      *
125      * @param name the variable name
126      * @param indexes an integer array with the indexes of the (sub)*-list which will contain the data
127      * @param addr the address of the variable in the Scilab stack
128      * @param swaped true if the matrix is stored row by row
129     * @param byref true if the variable is passed by reference
130            * @param handlerId the handler id
131            * @param pvApiCtx a pointer to the context
132            */
133     static bool sendVariable(const std::string & name, std::vector<int> & indexes, int * addr, bool swaped, bool byref, int handlerId, void * pvApiCtx);
134 
135     /**
136      * Send a list or a tlist or mlist in the Java environment
137      *
138      * @param name the variable name
139      * @param indexes an integer array with the indexes of the (sub)*-list which will contain the data
140      * @param type a char with the value 'l' or 'm' or 't'
141     * @param byref true if the variable is passed by reference
142            * @param handlerId the handler id
143            */
144     static void sendVariable(const std::string & name, const int nbItems, std::vector<int> & indexes, char type, bool byref, int handlerId);
145 
146     /**
147      * Close a list or a tlist or mlist in the Java environment
148      *
149      * @param indexes an integer array with the indexes of the (sub)*-list which will contain the data
150      * @param handlerId the handler id
151      */
152     static void closeList(std::vector<int> & indexes, int handlerId);
153 
154     /**
155      * Send double sparse matrix
156      *
157      * @param T the type of the data
158      * @param name the variable name
159      * @param indexes an integer array with the indexes of the (sub)*-list which will contain the data
160      * @param nbItem the number of non null elements
161      * @param nbItemRow the number by row of non null elements
162      * @param colPos the column position of the non null elements
163      * @param row the row number
164      * @param col the col number
165      * @param data the data
166     * @param byref true if the variable is passed by reference
167            * @param handlerId the handler id
168            */
169     template<typename T>
170     static void sendVariable(const std::string & name, std::vector<int> & indexes, int nbItem, int * nbItemRow, int * colPos, int row, int col, T * data, bool byref, int handlerId);
171 
172     /**
173      * Send double, int* and uint64 matrices
174      *
175      * @param T the type of the data
176      * @param name the variable name
177      * @param indexes an integer array with the indexes of the (sub)*-list which will contain the data
178      * @param row the row number
179      * @param col the col number
180      * @param data the data
181      * @param swaped true if the matrix is stored row by row
182     * @param byref true if the variable is passed by reference
183            * @param handlerId the handler id
184            */
185     template<typename T>
186     static void sendVariable(const std::string & name, std::vector<int> & indexes, int row, int col, T * data, bool swaped, bool byref, int handlerId);
187 
188     /**
189      * Send String matrices
190      *
191      * @param name the variable name
192      * @param indexes an integer array with the indexes of the (sub)*-list which will contain the data
193      * @param row the row number
194      * @param col the col number
195      * @param data the data
196      * @param swaped true if the matrix is stored row by row
197     * @param byref true if the variable is passed by reference
198            * @param handlerId the handler id
199            */
200     static void sendStringVariable(const std::string & name, std::vector<int> & indexes, int row, int col, char ** data, bool swaped, bool byref, int handlerId);
201 
202     /**
203      * Send boolean sparse matrix
204      *
205      * @param name the variable name
206      * @param indexes an integer array with the indexes of the (sub)*-list which will contain the data
207      * @param nbItem the number of true elements
208      * @param nbItemRow the number by row of true elements
209      * @param colPos the column position of the true elements
210      * @param row the row number
211      * @param col the col number
212     * @param byref true if the variable is passed by reference
213            * @param handlerId the handler id
214            */
215     static void sendBooleanSparseVariable(const std::string & name, std::vector<int> & indexes, int nbItem, int * nbItemRow, int * colPos, int row, int col, bool byref, int handlerId);
216 
217     /**
218      * Send uint* matrices (datas are converted to have the good Java type)
219      *
220      * @param T the type of the data
221      * @param U the type of the converted data
222      * @param name the variable name
223      * @param indexes an integer array with the indexes of the (sub)*-list which will contain the data
224      * @param row the row number
225      * @param col the col number
226      * @param data the data
227      * @param swaped true if the matrix is stored row by row
228      * @param handlerId the handler id
229      */
230     template<typename T, typename U>
231     static void sendUnsignedVariableWithCast(const std::string & name, std::vector<int> & indexes, int row, int col, U * data, bool swaped, int handlerId);
232 
233     /**
234      * Send uint* matrices
235      *
236      * @param T the type of the data
237      * @param name the variable name
238      * @param indexes an integer array with the indexes of the (sub)*-list which will contain the data
239      * @param row the row number
240      * @param col the col number
241      * @param data the data
242      * @param swaped true if the matrix is stored row by row
243     * @param byref true if the variable is passed by reference
244            * @param handlerId the handler id
245            */
246     template<typename T>
247     static void sendUnsignedVariable(const std::string & name, std::vector<int> & indexes, int row, int col, T * data, bool swaped, bool byref, int handlerId);
248 
249     /**
250      * Send boolean matrix (data are converted from int to bool)
251      *
252      * @param name the variable name
253      * @param indexes an integer array with the indexes of the (sub)*-list which will contain the data
254      * @param row the row number
255      * @param col the col number
256      * @param data the data
257      * @param swaped true if the matrix is stored row by row
258     * @param byref true if the variable is passed by reference
259            * @param handlerId the handler id
260            */
261     static void sendConvertedBooleanVariable(const std::string & name, std::vector<int> & indexes, int row, int col, int * data, bool swaped, bool byref, int handlerId);
262 
263     /**
264      * Send complex sparse matrix
265      *
266      * @param T the type of the data
267      * @param name the variable name
268      * @param indexes an integer array with the indexes of the (sub)*-list which will contain the data
269      * @param nbItem the number of non null elements
270      * @param nbItemRow the number by row of non null elements
271      * @param colPos the column position of the non null elements
272      * @param row the row number
273      * @param col the col number
274      * @param real the real data
275      * @param img the imaginary data
276     * @param byref true if the variable is passed by reference
277            * @param handlerId the handler id
278            */
279     template<typename T>
280     static void sendVariable(const std::string & name, std::vector<int> & indexes, int nbItem, int * nbItemRow, int * colPos, int row, int col, T * real, T * img, bool byref, int handlerId);
281 
282     /**
283      * Send complex matrices
284      *
285      * @param T the type of the data
286      * @param name the variable name
287      * @param indexes an integer array with the indexes of the (sub)*-list which will contain the data
288      * @param row the row number
289      * @param col the col number
290      * @param real the real data
291      * @param img the imaginary data
292      * @param swaped true if the matrix is stored row by row
293     * @param byref true if the variable is passed by reference
294            * @param handlerId the handler id
295            */
296     template<typename T>
297     static void sendVariable(const std::string & name, std::vector<int> & indexes, int row, int col, T * real, T * img, bool swaped, bool byref, int handlerId);
298 
299     /**
300      * Send double polynomial matrices
301      *
302      * @param T the type of the data
303      * @param name the variable name
304      * @param indexes an integer array with the indexes of the (sub)*-list which will contain the data
305      * @param row the row number
306      * @param col the col number
307      * @param nbcoeff the number of coefficient of each polynomial
308      * @param data the data
309      * @param swaped true if the matrix is stored row by row
310     * @param byref true if the variable is passed by reference
311            * @param handlerId the handler id
312            */
313     template<typename T>
314     static void sendVariable(const std::string & name, std::vector<int> & indexes, const char * varName, int row, int col, int * nbcoeff, T ** data, bool swaped, bool byref, int handlerId);
315 
316     /**
317      * Send complex polynomial matrices
318      *
319      * @param T the type of the data
320      * @param name the variable name
321      * @param indexes an integer array with the indexes of the (sub)*-list which will contain the data
322      * @param row the row number
323      * @param col the col number
324      * @param nbcoeff the number of coefficient of each polynomial
325      * @param real the real data
326      * @param img the imaginary data
327      * @param swaped true if the matrix is stored row by row
328     * @param byref true if the variable is passed by reference
329            * @param handlerId the handler id
330            */
331     template<typename T>
332     static void sendVariable(const std::string & name, std::vector<int> & indexes, const char * varName, int row, int col, int * nbcoeff, T ** real, T ** img, bool swaped, bool byref, int handlerId);
333 
334 public :
335 
336     /**
337      * Send all the listened variables
338      */
339     static void sendAllListenedVariables();
340 
341     /**
342      * Send a Scilab variable to the Java environment
343      *
344      * @param name the variable name
345      * @param swaped true if the matrix is stored row by row
346      * @param handlerId the handler id
347      * @return true if all was ok
348      */
349     static bool sendVariable(const std::string & name, bool swaped, int handlerId);
350 
351     /**
352      * Send a Scilab variable to the Java environment as a reference when it is possible
353      *
354      * @param name the variable name
355      * @param handlerId the handler id
356      * @return true if all was ok
357      */
358     static bool sendVariableAsReference(const std::string & name, int handlerId);
359 
360     /**
361      * Send a Scilab variable to the Java environment
362      *
363      * @param name the variable name
364      * @param addr the variable address in the Scilab stack
365      * @param swaped true if the matrix is stored row by row
366      * @param handlerId the handler id
367      * @param pvApiCtx a pointer to the context
368      * @return true if all was ok
369      */
370     static bool sendVariable(const std::string & name, int * addr, bool swaped, int handlerId, void * pvApiCtx);
371 };
372 }
373 
374 #endif
375