scriptFunctionData.cpp
Go to the documentation of this file.
1 // Copyright 2006-2016 Coppelia Robotics GmbH. All rights reserved.
2 // marc@coppeliarobotics.com
3 // www.coppeliarobotics.com
4 //
5 // -------------------------------------------------------------------
6 // THIS FILE IS DISTRIBUTED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
7 // WARRANTY. THE USER WILL USE IT AT HIS/HER OWN RISK. THE ORIGINAL
8 // AUTHORS AND COPPELIA ROBOTICS GMBH WILL NOT BE LIABLE FOR DATA LOSS,
9 // DAMAGES, LOSS OF PROFITS OR ANY OTHER KIND OF LOSS WHILE USING OR
10 // MISUSING THIS SOFTWARE.
11 //
12 // You are free to use/modify/distribute this file for whatever purpose!
13 // -------------------------------------------------------------------
14 //
15 // This file was automatically created for V-REP release V3.3.2 on August 29th 2016
16 
17 #include "scriptFunctionData.h"
18 #include <sstream>
19 #include <cstring>
20 
22 {
23 }
24 
26 {
27 }
28 
29 std::vector<CScriptFunctionDataItem>* CScriptFunctionData::getInDataPtr()
30 { // use this when reading data from a script from inside of a custom script function call
31  return(&_inData);
32 }
33 
34 std::vector<CScriptFunctionDataItem>* CScriptFunctionData::getOutDataPtr_scriptFunctionCall()
35 { // use this when reading data returned from a script function call from a plugin
36  return(&_outData);
37 }
38 
39 bool CScriptFunctionData::readDataFromStack(int stackHandle,const int* expectedArguments,int requiredArgumentCount,const char* functionName)
40 { // use this when reading data from a script from inside of a custom script function call
41  bool retVal=_readData(stackHandle,expectedArguments,requiredArgumentCount,functionName,"arguments.","Argument ",_inData);
42  simPopStackItem(stackHandle,0); // clears the stack (which will serve as return value container from now)
43  return(retVal);
44 }
45 
46 bool CScriptFunctionData::readDataFromStack_scriptFunctionCall(int stackHandle,const int* expectedArguments,int requiredArgumentCount,const char* functionName)
47 { // use this when reading data returned from a Lua function call from a plugin
48  bool retVal=_readData(stackHandle,expectedArguments,requiredArgumentCount,functionName,"return arguments.","Return argument ",_outData);
49  simPopStackItem(stackHandle,0); // clears the stack
50  return(retVal);
51 }
52 
54 { // use this when returning data from inside of a custom Lua function call
55  _outData.push_back(dataItem);
56 }
57 
59 { // use this when doing a Lua function call from a plugin
60  _inData.push_back(dataItem);
61 }
62 
64 { // use this when returning data from inside of a custom Lua function call
65  _writeData(stackHandle,_outData);
66 }
67 
69 { // use this when doing a Lua function call from a plugin
70  _writeData(stackHandle,_inData);
71 }
72 
73 bool CScriptFunctionData::_readData(int stack,const int* expectedArguments,int requiredArgumentCount,const char* functionName,const char* argumentText1,const char* argumentText2,std::vector<CScriptFunctionDataItem>& inOutData)
74 { // use this when reading data from a script from inside of a custom script function call
75  inOutData.clear();
76  int argCnt=simGetStackSize(stack);
77  if (argCnt<requiredArgumentCount)
78  {
79  std::ostringstream str;
80  str << "Not enough " << argumentText1;
81  simSetLastError(functionName,str.str().c_str());
82  return(false);
83  }
84 
85  for (int i=0;i<argCnt;i++)
86  {
87  if (i>=expectedArguments[0])
88  break;
89  bool done=false;
90  simMoveStackItemToTop(stack,0);
91  if (simIsStackValueNull(stack)==1)
92  {
93  // is nil explicitely allowed?
94  if (expectedArguments[1+i*2+0]&SIM_SCRIPT_ARG_NULL_ALLOWED)
95  { // yes. This is for an argument that can optionally also be nil.
97  inOutData.push_back(dat);
98  done=true;
99  }
100  else
101  { // no
102  if (int(inOutData.size())<requiredArgumentCount)
103  {
104  std::ostringstream str;
105  str << argumentText2 << i+1 << " is not correct.";
106  simSetLastError(functionName,str.str().c_str());
107  return(false);
108  }
109  break; // this argument is nil, so it is like inexistant. But we also won't explore any more arguments, we have enough.
110  }
111  }
112  if (!done)
113  {
114  int tableSize=simGetStackTableInfo(stack,0);
115  bool expectingATable=(((expectedArguments[1+i*2+0]|SIM_SCRIPT_ARG_NULL_ALLOWED)-SIM_SCRIPT_ARG_NULL_ALLOWED)&sim_script_arg_table)!=0;
116  if ( (tableSize>=0)!=expectingATable )
117  {
118  std::ostringstream str;
119  str << argumentText2 << i+1 << " is not correct.";
120  simSetLastError(functionName,str.str().c_str());
121  return(false);
122  }
124  if (expectingATable)
125  { // we have a table
126  int infoType=0;
127  if (expectingType==sim_script_arg_null)
128  infoType=1;
129  if (expectingType==sim_script_arg_bool)
130  infoType=3;
131  if (expectingType==sim_script_arg_float)
132  infoType=2;
133  if (expectingType==sim_script_arg_int32)
134  infoType=2;
135  if (expectingType==sim_script_arg_string)
136  infoType=4;
137  if (expectingType==sim_script_arg_charbuff)
138  infoType=4;
139  if (expectingType==sim_script_arg_double)
140  infoType=2;
141  if (simGetStackTableInfo(stack,infoType)<1)
142  { // table content cannot be converted
143  std::ostringstream str;
144  str << argumentText2 << i+1 << " is not correct.";
145  simSetLastError(functionName,str.str().c_str());
146  return(false);
147  }
148 
149  if ( (tableSize<expectedArguments[1+i*2+1])&&(expectedArguments[1+i*2+1]!=0) )
150  {
151  std::ostringstream str;
152  str << argumentText2 << i+1 << " is not correct (wrong table size).";
153  simSetLastError(functionName,str.str().c_str());
154  return(false);
155  }
156  else
157  {
158  int t=expectingType;
159  int itemCnt=tableSize;
160 
161  if (t==sim_script_arg_null)
162  {
164  a->setNilTable(itemCnt);
166  dat.setNilTable(itemCnt);
167  inOutData.push_back(dat);
168  }
169  if (t==sim_script_arg_bool)
170  {
171  std::vector<bool> vect;
172  simUnfoldStackTable(stack); // this removes the table and exposes the inside
173  for (int j=0;j<itemCnt;j++)
174  {
175  simBool val;
176  simGetStackBoolValue(stack,&val);
177  vect.insert(vect.begin(),val!=0);
178  simPopStackItem(stack,2);
179  }
180  simPushTableOntoStack(stack); // empty table, will be removed at the end of the loop
181  CScriptFunctionDataItem dat(vect);
182  inOutData.push_back(dat);
183  }
184  if (t==sim_script_arg_int32)
185  {
186  std::vector<int> vect;
187  if (itemCnt>0)
188  {
189  vect.resize(itemCnt);
190  simGetStackInt32Table(stack,&vect[0],itemCnt);
191  }
192  CScriptFunctionDataItem dat(vect);
193  inOutData.push_back(dat);
194  }
195  if (t==sim_script_arg_float)
196  {
197  std::vector<float> vect;
198  if (itemCnt>0)
199  {
200  vect.resize(itemCnt);
201  simGetStackFloatTable(stack,&vect[0],itemCnt);
202  }
203  CScriptFunctionDataItem dat(vect);
204  inOutData.push_back(dat);
205  }
206  if (t==sim_script_arg_double)
207  {
208  std::vector<double> vect;
209  if (itemCnt>0)
210  {
211  vect.resize(itemCnt);
212  simGetStackDoubleTable(stack,&vect[0],itemCnt);
213  }
214  CScriptFunctionDataItem dat(vect);
215  inOutData.push_back(dat);
216  }
217  if (t==sim_script_arg_string)
218  {
219  std::vector<std::string> vect;
220  simUnfoldStackTable(stack); // this removes the table and exposes the inside
221  for (int j=0;j<itemCnt;j++)
222  {
223  int l;
224  char* str=simGetStackStringValue(stack,&l);
225  std::string str2(str); // treat it as a char string, not buffer
226  simReleaseBuffer(str);
227  vect.insert(vect.begin(),str2);
228  simPopStackItem(stack,2);
229  }
230  simPushTableOntoStack(stack); // empty table, will be removed at the end of the loop
231  CScriptFunctionDataItem dat(vect);
232  inOutData.push_back(dat);
233  }
235  {
236  std::ostringstream str;
237  str << argumentText2 << i+1 << " cannot be a table.";
238  simSetLastError(functionName,str.str().c_str());
239  return(false);
240  }
241  }
242  }
243  else
244  { // we do not have a table
245  int t=expectingType;
246  bool failedMsgAndLeave=false;
247  if (t==sim_script_arg_null)
248  {
249  if (simIsStackValueNull(stack)>0)
250  {
252  inOutData.push_back(dat);
253  }
254  else
255  failedMsgAndLeave=true;
256  }
257  if (t==sim_script_arg_bool)
258  {
259  simBool val=0;
260  if (simGetStackBoolValue(stack,&val)==1)
261  {
262  CScriptFunctionDataItem dat(val!=0);
263  inOutData.push_back(dat);
264  }
265  else
266  failedMsgAndLeave=true;
267  }
268  if (t==sim_script_arg_int32)
269  {
270  int val=0;
271  if (simGetStackInt32Value(stack,&val)==1)
272  {
273  CScriptFunctionDataItem dat(val);
274  inOutData.push_back(dat);
275  }
276  else
277  failedMsgAndLeave=true;
278  }
279  if (t==sim_script_arg_float)
280  {
281  float val=0.0;
282  if (simGetStackFloatValue(stack,&val)==1)
283  {
284  CScriptFunctionDataItem dat(val);
285  inOutData.push_back(dat);
286  }
287  else
288  failedMsgAndLeave=true;
289  }
290  if (t==sim_script_arg_double)
291  {
292  double val=0.0;
293  if (simGetStackDoubleValue(stack,&val)==1)
294  {
295  CScriptFunctionDataItem dat(val);
296  inOutData.push_back(dat);
297  }
298  else
299  failedMsgAndLeave=true;
300  }
301  if (t==sim_script_arg_string)
302  {
303  int l;
304  char* str=simGetStackStringValue(stack,&l);
305  if (str!=NULL)
306  {
307  std::string str2(str);
308  simReleaseBuffer(str);
309  CScriptFunctionDataItem dat(str2); // treat it as a char string, not buffer
310  inOutData.push_back(dat);
311  }
312  else
313  failedMsgAndLeave=true;
314  }
316  {
317  int l;
318  char* str=simGetStackStringValue(stack,&l);
319  if (str!=NULL)
320  {
321  if ( (l<expectedArguments[1+i*2+1])&&(expectedArguments[1+i*2+1]!=0) )
322  {
323  simReleaseBuffer(str);
324  std::ostringstream str;
325  str << argumentText2 << i+1 << " is not correct (wrong buffer size).";
326  simSetLastError(functionName,str.str().c_str());
327  return(false);
328  }
329  else
330  {
331  CScriptFunctionDataItem dat(str,l);
332  inOutData.push_back(dat);
333  simReleaseBuffer(str);
334  }
335  }
336  else
337  failedMsgAndLeave=true;
338  }
339  if (failedMsgAndLeave)
340  {
341  std::ostringstream str;
342  str << argumentText2 << i+1 << " is not correct.";
343  simSetLastError(functionName,str.str().c_str());
344  return(false);
345  }
346  }
347  }
348  simPopStackItem(stack,1);
349  }
350  return(true);
351 }
352 
353 void CScriptFunctionData::_writeData(int stack,std::vector<CScriptFunctionDataItem>& inOutData)
354 {
355  simPopStackItem(stack,0); // Clear the stack
356 
357  int itemCnt=int(inOutData.size());
358 
359  for (int i=0;i<itemCnt;i++)
360  {
361  if (inOutData[i].isTable())
362  { // table
363  if (inOutData[i].getType()==-1)
364  { // nil table
365  simPushTableOntoStack(stack);
366  for (int j=0;j<inOutData[i].getNilTableSize();j++)
367  {
368  simPushInt32OntoStack(stack,j+1); // the key
369  simPushNullOntoStack(stack); // the value
371  }
372  }
373  if (inOutData[i].getType()==0)
374  { // bool table
375  simPushTableOntoStack(stack);
376  for (size_t j=0;j<inOutData[i].boolData.size();j++)
377  {
378  simPushInt32OntoStack(stack,(int)j+1); // the key
379  simPushBoolOntoStack(stack,inOutData[i].boolData[j]); // the value
381  }
382  }
383  if (inOutData[i].getType()==1)
384  { // int table
385  if (inOutData[i].int32Data.size()>0)
386  simPushInt32TableOntoStack(stack,&inOutData[i].int32Data[0],int(inOutData[i].int32Data.size()));
387  else
388  simPushTableOntoStack(stack);
389  }
390  if (inOutData[i].getType()==2)
391  { // float table
392  if (inOutData[i].floatData.size()>0)
393  simPushFloatTableOntoStack(stack,&inOutData[i].floatData[0],int(inOutData[i].floatData.size()));
394  else
395  simPushTableOntoStack(stack);
396  }
397  if (inOutData[i].getType()==5)
398  { // double table
399  if (inOutData[i].doubleData.size()>0)
400  simPushDoubleTableOntoStack(stack,&inOutData[i].doubleData[0],int(inOutData[i].doubleData.size()));
401  else
402  simPushTableOntoStack(stack);
403  }
404  if (inOutData[i].getType()==3)
405  { // string table
406  simPushTableOntoStack(stack);
407  for (size_t j=0;j<inOutData[i].stringData.size();j++)
408  {
409  simPushInt32OntoStack(stack,(int)j+1); // the key
410  simPushStringOntoStack(stack,inOutData[i].stringData[j].c_str(),(int)inOutData[i].stringData[j].length()); // the value
412  }
413  }
414  }
415  else
416  { // non-table values:
417  if (inOutData[i].getType()==-1)
418  simPushNullOntoStack(stack);
419  if (inOutData[i].getType()==0)
420  simPushBoolOntoStack(stack,inOutData[i].boolData[0]);
421  if (inOutData[i].getType()==1)
422  simPushInt32OntoStack(stack,inOutData[i].int32Data[0]);
423  if (inOutData[i].getType()==2)
424  simPushFloatOntoStack(stack,inOutData[i].floatData[0]);
425  if (inOutData[i].getType()==5)
426  simPushDoubleOntoStack(stack,inOutData[i].doubleData[0]);
427  if ( (inOutData[i].getType()==3)||(inOutData[i].getType()==4) )
428  simPushStringOntoStack(stack,inOutData[i].stringData[0].c_str(),(int)inOutData[i].stringData[0].length());
429  }
430  }
431 }
std::vector< CScriptFunctionDataItem > _outData
ptrSimGetStackFloatValue simGetStackFloatValue
Definition: v_repLib.cpp:453
ptrSimGetStackFloatTable simGetStackFloatTable
Definition: v_repLib.cpp:459
ptrSimPushDoubleTableOntoStack simPushDoubleTableOntoStack
Definition: v_repLib.cpp:444
void _writeData(int stack, std::vector< CScriptFunctionDataItem > &inOutData)
bool readDataFromStack_scriptFunctionCall(int stackHandle, const int *expectedArguments, int requiredArgumentCount, const char *functionName)
std::vector< CScriptFunctionDataItem > * getInDataPtr()
ptrSimGetStackSize simGetStackSize
Definition: v_repLib.cpp:447
ptrSimGetStackInt32Value simGetStackInt32Value
Definition: v_repLib.cpp:452
#define SIM_SCRIPT_ARG_NULL_ALLOWED
ptrSimGetStackInt32Table simGetStackInt32Table
Definition: v_repLib.cpp:458
ptrSimPopStackItem simPopStackItem
Definition: v_repLib.cpp:448
std::vector< CScriptFunctionDataItem > * getOutDataPtr_scriptFunctionCall()
ptrSimPushBoolOntoStack simPushBoolOntoStack
Definition: v_repLib.cpp:436
void writeDataToStack_scriptFunctionCall(int stackHandle)
void pushOutData_scriptFunctionCall(const CScriptFunctionDataItem &dataItem)
ptrSimMoveStackItemToTop simMoveStackItemToTop
Definition: v_repLib.cpp:449
ptrSimPushTableOntoStack simPushTableOntoStack
Definition: v_repLib.cpp:445
bool _readData(int stack, const int *expectedArguments, int requiredArgumentCount, const char *functionName, const char *argumentText1, const char *argumentText2, std::vector< CScriptFunctionDataItem > &inOutData)
ptrSimPushFloatOntoStack simPushFloatOntoStack
Definition: v_repLib.cpp:438
std::vector< CScriptFunctionDataItem > _inData
ptrSimPushInt32OntoStack simPushInt32OntoStack
Definition: v_repLib.cpp:437
ptrSimGetStackBoolValue simGetStackBoolValue
Definition: v_repLib.cpp:451
ptrSimReleaseBuffer simReleaseBuffer
Definition: v_repLib.cpp:172
ptrSimInsertDataIntoStackTable simInsertDataIntoStackTable
Definition: v_repLib.cpp:446
ptrSimGetStackDoubleTable simGetStackDoubleTable
Definition: v_repLib.cpp:460
ptrSimGetStackDoubleValue simGetStackDoubleValue
Definition: v_repLib.cpp:454
bool readDataFromStack(int stackHandle, const int *expectedArguments, int requiredArgumentCount, const char *functionName)
ptrSimGetStackStringValue simGetStackStringValue
Definition: v_repLib.cpp:455
ptrSimGetStackTableInfo simGetStackTableInfo
Definition: v_repLib.cpp:456
ptrSimPushStringOntoStack simPushStringOntoStack
Definition: v_repLib.cpp:440
unsigned char simBool
Definition: v_repTypes.h:36
ptrSimSetLastError simSetLastError
Definition: v_repLib.cpp:197
ptrSimPushNullOntoStack simPushNullOntoStack
Definition: v_repLib.cpp:435
void pushOutData(const CScriptFunctionDataItem &dataItem)
ptrSimIsStackValueNull simIsStackValueNull
Definition: v_repLib.cpp:450
ptrSimPushFloatTableOntoStack simPushFloatTableOntoStack
Definition: v_repLib.cpp:443
ptrSimUnfoldStackTable simUnfoldStackTable
Definition: v_repLib.cpp:461
void writeDataToStack(int stackHandle)
ptrSimPushInt32TableOntoStack simPushInt32TableOntoStack
Definition: v_repLib.cpp:442
ptrSimPushDoubleOntoStack simPushDoubleOntoStack
Definition: v_repLib.cpp:439