RPCWrappers.hh 18.1 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2014 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
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
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper 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 with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper 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 LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
33 34 35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36 37 38 39 40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41
\*===========================================================================*/
Jan Möbius's avatar
Jan Möbius committed
42 43 44 45

/**
 * \file RPCWrappers.hh
 * This file contains functions to call functions and procedures across plugins.
Jan Möbius's avatar
Jan Möbius committed
46 47 48
 * The QT Scripting system is used to pass the calls between different plugins.
 *
 * Usage is described in \ref RPCInterfacePage
Jan Möbius's avatar
Jan Möbius committed
49 50 51 52 53 54 55
 */

#ifndef RPCWRAPPERS_HH
#define RPCWRAPPERS_HH

#include <QtScript>
#include <vector>
Jan Möbius's avatar
Jan Möbius committed
56
#include <OpenFlipper/common/Types.hh>
Jan Möbius's avatar
Jan Möbius committed
57 58


Jan Möbius's avatar
Jan Möbius committed
59
/** Namespace containing RPC helper functions used to call functions across plugins
Jan Möbius's avatar
Jan Möbius committed
60
 *
Jan Möbius's avatar
Jan Möbius committed
61
 * Usage is described in \ref RPCInterfacePage
Jan Möbius's avatar
Jan Möbius committed
62 63 64
 */
namespace RPC {

Jan Möbius's avatar
Jan Möbius committed
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

//===========================================================================
/** @name Script Engine Controls
 *
 * Functions to get the scripting engine. Normally you don't need them. And
 * RPC::setScriptEngine should never be used!
 * @{ */
//===========================================================================

/** \brief get a pointer to OpenFlippers core scripting engine
 *
 */
DLLEXPORT
QScriptEngine* getScriptEngine();


/** \brief DONT USE! (Function to set the internal reference to the script Engine)
 *
 * Function to set the internal reference to the script engine from the core
 */
DLLEXPORT
void setScriptEngine( QScriptEngine* _engine );

/** @} */


Jan Möbius's avatar
Jan Möbius committed
91 92
//===========================================================================
/** @name Call functions across plugins (simple calls)
Jan Möbius's avatar
Jan Möbius committed
93
 *
Jan Möbius's avatar
Jan Möbius committed
94 95 96
 * These functions can be used to call functions in other plugins.
 * @{ */
//===========================================================================
Jan Möbius's avatar
Jan Möbius committed
97 98 99 100 101 102

/** \brief call a function provided by a plugin
 *
 * @param _plugin Plugin name ( Scripting name )
 * @param _functionName Name of the remote function
 */
Jan Möbius's avatar
Jan Möbius committed
103
DLLEXPORT
Jan Möbius's avatar
Jan Möbius committed
104 105
QScriptValue callFunction( QString _plugin, QString _functionName );

Jan Möbius's avatar
Jan Möbius committed
106
/** \brief Call a function provided by a plugin getting multiple parameters
Jan Möbius's avatar
Jan Möbius committed
107
 *
Jan Möbius's avatar
Jan Möbius committed
108 109 110
 * This function gets the parameters which are converted to a QScriptValue on your own.
 *
 * @param _plugin Plugin name ( Scripting name of the plugin )
Jan Möbius's avatar
Jan Möbius committed
111 112 113
 * @param _functionName Name of the remote function
 * @param _parameters vector of scriptvalues containing the functions parameters in the right order
 */
Jan Möbius's avatar
Jan Möbius committed
114
DLLEXPORT
Jan Möbius's avatar
Jan Möbius committed
115 116
QScriptValue callFunction( QString _plugin, QString _functionName , std::vector< QScriptValue > _parameters );

Jan Möbius's avatar
Jan Möbius committed
117 118 119 120 121 122 123 124
/** @} */

//===========================================================================
/** @name Call functions across plugins
 *
 * These templates can be used to call functions in other plugins.
 * @{ */
//===========================================================================
Jan Möbius's avatar
Jan Möbius committed
125

Jan Möbius's avatar
Jan Möbius committed
126 127 128 129 130 131
/** \brief call a function in another plugin
 *
 * @param _plugin       Plugin name ( Scripting name of the plugin )
 * @param _functionName Name of the remote function
 * @param _t0           Parameter passed to the function
 */
Jan Möbius's avatar
Jan Möbius committed
132 133 134 135
template <typename T0>
void callFunction( QString _plugin, QString _functionName, T0 _t0) {
  QScriptEngine* engine = getScriptEngine();
  std::vector< QScriptValue > parameters;
Jan Möbius's avatar
Jan Möbius committed
136 137
//   QVariant bla = qVariantFromValue(_t0);
//
Jan Möbius's avatar
Jan Möbius committed
138
//   IdList list = qVariantValue<IdList>( bla ) ;
Jan Möbius's avatar
Jan Möbius committed
139 140 141
//   std::cerr << "iDList size:" <<  list.size() << std::endl;;
//   std::cerr << list[0] << std::endl;
//   std::cerr << list[1] << std::endl;
Jan Möbius's avatar
Jan Möbius committed
142
//   std::cerr << "Type id is : " << QMetaType::type("IdList") << std::endl;
Jan Möbius's avatar
Jan Möbius committed
143 144 145
//   engine->globalObject().setProperty("ParameterData22",engine->toScriptValue(list));
/*
  QVariant blubb = engine->globalObject().property("ParameterData22").toVariant();
Jan Möbius's avatar
Jan Möbius committed
146
  IdList list1 = qVariantValue<IdList>( blubb ) ;
Jan Möbius's avatar
Jan Möbius committed
147 148 149
  std::cerr << "iDList1 size:" <<  list1.size() << std::endl;;
  std::cerr << list1[0] << std::endl;
  std::cerr << list1[1] << std::endl;
Jan Möbius's avatar
Jan Möbius committed
150
  std::cerr << "Type id is : " << QMetaType::type("IdList") << std::endl;*/
Jan Möbius's avatar
Jan Möbius committed
151 152 153


  parameters.push_back( engine->toScriptValue(_t0) );
Jan Möbius's avatar
Jan Möbius committed
154 155 156
  callFunction(_plugin,_functionName,parameters);
}

Jan Möbius's avatar
Jan Möbius committed
157 158 159 160 161 162 163
/** \brief call a function in another plugin
 *
 * @param _plugin       Plugin name ( Scripting name of the plugin )
 * @param _functionName Name of the remote function
 * @param _t0           Parameter 1 passed to the function
 * @param _t1           Parameter 2 passed to the function
 */
Jan Möbius's avatar
Jan Möbius committed
164 165 166 167
template <typename T0, typename T1>
void callFunction( QString _plugin, QString _functionName, T0 _t0 , T1 _t1) {
  QScriptEngine* engine = getScriptEngine();
  std::vector< QScriptValue > parameters;
Jan Möbius's avatar
Jan Möbius committed
168 169
  parameters.push_back( engine->toScriptValue( _t0 ) );
  parameters.push_back( engine->toScriptValue( _t1 ) );
Jan Möbius's avatar
Jan Möbius committed
170 171 172
  callFunction(_plugin,_functionName,parameters);
}

Jan Möbius's avatar
Jan Möbius committed
173 174 175 176 177 178 179 180
/** \brief call a function in another plugin
 *
 * @param _plugin       Plugin name ( Scripting name of the plugin )
 * @param _functionName Name of the remote function
 * @param _t0           Parameter 1 passed to the function
 * @param _t1           Parameter 2 passed to the function
 * @param _t2           Parameter 3 passed to the function
 */
Jan Möbius's avatar
Jan Möbius committed
181 182 183 184
template <typename T0, typename T1 , typename T2>
void callFunction( QString _plugin, QString _functionName, T0 _t0 , T1 _t1 , T2 _t2 ) {
  QScriptEngine* engine = getScriptEngine();
  std::vector< QScriptValue > parameters;
Jan Möbius's avatar
Jan Möbius committed
185 186 187
  parameters.push_back( engine->toScriptValue(_t0 ) );
  parameters.push_back( engine->toScriptValue( _t1 ) );
  parameters.push_back( engine->toScriptValue( _t2 ) );
Jan Möbius's avatar
Jan Möbius committed
188 189 190
  callFunction(_plugin,_functionName,parameters);
}

Jan Möbius's avatar
Jan Möbius committed
191 192 193 194 195 196 197 198 199
/** \brief call a function in another plugin
 *
 * @param _plugin       Plugin name ( Scripting name of the plugin )
 * @param _functionName Name of the remote function
 * @param _t0           Parameter 1 passed to the function
 * @param _t1           Parameter 2 passed to the function
 * @param _t2           Parameter 3 passed to the function
 * @param _t3           Parameter 4 passed to the function
 */
Jan Möbius's avatar
Jan Möbius committed
200 201 202 203
template <typename T0, typename T1 , typename T2,  typename T3>
void callFunction( QString _plugin, QString _functionName, T0 _t0 , T1 _t1 , T2 _t2 , T3 _t3 ) {
  QScriptEngine* engine = getScriptEngine();
  std::vector< QScriptValue > parameters;
Jan Möbius's avatar
Jan Möbius committed
204 205 206 207
  parameters.push_back( engine->toScriptValue( _t0 ) );
  parameters.push_back( engine->toScriptValue( _t1 ) );
  parameters.push_back( engine->toScriptValue( _t2 ) );
  parameters.push_back( engine->toScriptValue( _t3 ) );
Jan Möbius's avatar
Jan Möbius committed
208 209 210
  callFunction(_plugin,_functionName,parameters);
}

Jan Möbius's avatar
Jan Möbius committed
211 212 213 214 215 216 217 218 219 220
/** \brief call a function in another plugin
 *
 * @param _plugin       Plugin name ( Scripting name of the plugin )
 * @param _functionName Name of the remote function
 * @param _t0           Parameter 1 passed to the function
 * @param _t1           Parameter 2 passed to the function
 * @param _t2           Parameter 3 passed to the function
 * @param _t3           Parameter 4 passed to the function
 * @param _t4           Parameter 5 passed to the function
 */
221 222 223 224 225 226 227 228 229 230 231 232
template <typename T0, typename T1 , typename T2,  typename T3, typename T4>
void callFunction( QString _plugin, QString _functionName, T0 _t0 , T1 _t1 , T2 _t2 , T3 _t3 , T4 _t4) {
  QScriptEngine* engine = getScriptEngine();
  std::vector< QScriptValue > parameters;
  parameters.push_back( engine->toScriptValue( _t0 ) );
  parameters.push_back( engine->toScriptValue( _t1 ) );
  parameters.push_back( engine->toScriptValue( _t2 ) );
  parameters.push_back( engine->toScriptValue( _t3 ) );
  parameters.push_back( engine->toScriptValue( _t4 ) );
  callFunction(_plugin,_functionName,parameters);
}

233 234 235 236 237 238 239 240 241
/** \brief call a function in another plugin
 *
 * @param _plugin       Plugin name ( Scripting name of the plugin )
 * @param _functionName Name of the remote function
 * @param _t0           Parameter 1 passed to the function
 * @param _t1           Parameter 2 passed to the function
 * @param _t2           Parameter 3 passed to the function
 * @param _t3           Parameter 4 passed to the function
 * @param _t4           Parameter 5 passed to the function
Jan Möbius's avatar
Jan Möbius committed
242
 * @param _t5           Parameter 6 passed to the function
243 244 245 246 247 248 249 250 251 252 253 254 255 256
 */
template <typename T0, typename T1 , typename T2,  typename T3, typename T4, typename T5>
void callFunction( QString _plugin, QString _functionName, T0 _t0 , T1 _t1 , T2 _t2 , T3 _t3 , T4 _t4, T5 _t5) {
  QScriptEngine* engine = getScriptEngine();
  std::vector< QScriptValue > parameters;
  parameters.push_back( engine->toScriptValue( _t0 ) );
  parameters.push_back( engine->toScriptValue( _t1 ) );
  parameters.push_back( engine->toScriptValue( _t2 ) );
  parameters.push_back( engine->toScriptValue( _t3 ) );
  parameters.push_back( engine->toScriptValue( _t4 ) );
  parameters.push_back( engine->toScriptValue( _t5 ) );
  callFunction(_plugin,_functionName,parameters);
}

Jan Möbius's avatar
Jan Möbius committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
/** @} */

//===========================================================================
/** @name Call functions across plugins which return a value
 *
 * These templates can be used to call functions that return a value.
 * You have to pass the type of return value as the first template parameter.
  * @{ */
//===========================================================================

/** \brief call a function in another plugin and get a return parameter
 *
 * @param _plugin       Plugin name ( Scripting name of the plugin )
 * @param _functionName Name of the remote function
 * @return value returned by the called function
 */
Jan Möbius's avatar
Jan Möbius committed
273 274 275 276 277
template <typename ReturnValue >
ReturnValue callFunctionValue( QString _plugin, QString _functionName) {
  return qscriptvalue_cast< ReturnValue >( callFunction(_plugin,_functionName) );
}

Jan Möbius's avatar
Jan Möbius committed
278 279 280 281 282 283 284
/** \brief call a function in another plugin and get a return parameter
 *
 * @param _plugin       Plugin name ( Scripting name of the plugin )
 * @param _functionName Name of the remote function
 * @param _t0           Parameter 1 passed to the function
 * @return value returned by the called function
 */
Jan Möbius's avatar
Jan Möbius committed
285 286 287 288
template <typename ReturnValue , typename T0>
ReturnValue callFunctionValue( QString _plugin, QString _functionName, T0 _t0) {
  QScriptEngine* engine = getScriptEngine();
  std::vector< QScriptValue > parameters;
Jan Möbius's avatar
Jan Möbius committed
289
  parameters.push_back( engine->toScriptValue( _t0 ) );
Jan Möbius's avatar
Jan Möbius committed
290 291 292
  return qscriptvalue_cast<ReturnValue>( callFunction(_plugin,_functionName,parameters) );
}

Jan Möbius's avatar
Jan Möbius committed
293 294 295 296 297 298 299 300
/** \brief call a function in another plugin and get a return parameter
 *
 * @param _plugin       Plugin name ( Scripting name of the plugin )
 * @param _functionName Name of the remote function
 * @param _t0           Parameter 1 passed to the function
 * @param _t1           Parameter 2 passed to the function
 * @return value returned by the called function
 */
Jan Möbius's avatar
Jan Möbius committed
301 302 303 304
template <typename ReturnValue , typename T0, typename T1>
ReturnValue callFunctionValue( QString _plugin, QString _functionName, T0 _t0 , T1 _t1) {
  QScriptEngine* engine = getScriptEngine();
  std::vector< QScriptValue > parameters;
Jan Möbius's avatar
Jan Möbius committed
305 306
  parameters.push_back( engine->toScriptValue( _t0 ) );
  parameters.push_back( engine->toScriptValue( _t1 ) );
Jan Möbius's avatar
Jan Möbius committed
307 308 309
  return qscriptvalue_cast<ReturnValue>( callFunction(_plugin,_functionName,parameters) );
}

Jan Möbius's avatar
Jan Möbius committed
310 311 312 313 314 315 316 317 318
/** \brief call a function in another plugin and get a return parameter
 *
 * @param _plugin       Plugin name ( Scripting name of the plugin )
 * @param _functionName Name of the remote function
 * @param _t0           Parameter 1 passed to the function
 * @param _t1           Parameter 2 passed to the function
 * @param _t2           Parameter 3 passed to the function
 * @return value returned by the called function
 */
Jan Möbius's avatar
Jan Möbius committed
319 320 321 322
template <typename ReturnValue , typename T0, typename T1 , typename T2 >
ReturnValue callFunctionValue( QString _plugin, QString _functionName, T0 _t0 , T1 _t1 , T2 _t2 ) {
  QScriptEngine* engine = getScriptEngine();
  std::vector< QScriptValue > parameters;
Jan Möbius's avatar
Jan Möbius committed
323 324 325
  parameters.push_back( engine->toScriptValue( _t0 ) );
  parameters.push_back( engine->toScriptValue( _t1 ) );
  parameters.push_back( engine->toScriptValue( _t2 ) );
Jan Möbius's avatar
Jan Möbius committed
326 327 328
  return qscriptvalue_cast<ReturnValue>( callFunction(_plugin,_functionName,parameters) );
}

Jan Möbius's avatar
Jan Möbius committed
329 330 331 332 333 334 335 336 337 338
/** \brief call a function in another plugin and get a return parameter
 *
 * @param _plugin       Plugin name ( Scripting name of the plugin )
 * @param _functionName Name of the remote function
 * @param _t0           Parameter 1 passed to the function
 * @param _t1           Parameter 2 passed to the function
 * @param _t2           Parameter 3 passed to the function
 * @param _t3           Parameter 4 passed to the function
 * @return value returned by the called function
 */
Jan Möbius's avatar
Jan Möbius committed
339 340 341 342
template <typename ReturnValue , typename T0, typename T1 , typename T2, typename T3>
ReturnValue callFunctionValue( QString _plugin, QString _functionName, T0 _t0 , T1 _t1 , T2 _t2 , T3 _t3 ) {
  QScriptEngine* engine = getScriptEngine();
  std::vector< QScriptValue > parameters;
Jan Möbius's avatar
Jan Möbius committed
343 344 345 346
  parameters.push_back( engine->toScriptValue( _t0 ) );
  parameters.push_back( engine->toScriptValue( _t1 ) );
  parameters.push_back( engine->toScriptValue( _t2 ) );
  parameters.push_back( engine->toScriptValue( _t3 ) );
Jan Möbius's avatar
Jan Möbius committed
347 348 349
  return qscriptvalue_cast<ReturnValue>( callFunction(_plugin,_functionName,parameters) );
}

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
/** \brief call a function in another plugin and get a return parameter
 *
 * @param _plugin       Plugin name ( Scripting name of the plugin )
 * @param _functionName Name of the remote function
 * @param _t0           Parameter 1 passed to the function
 * @param _t1           Parameter 2 passed to the function
 * @param _t2           Parameter 3 passed to the function
 * @param _t3           Parameter 4 passed to the function
 * @param _t4           Parameter 5 passed to the function
 * @return value returned by the called function
 */
template <typename ReturnValue , typename T0, typename T1 , typename T2, typename T3, typename T4>
ReturnValue callFunctionValue( QString _plugin, QString _functionName, T0 _t0 , T1 _t1 , T2 _t2 , T3 _t3, T4 _t4 ) {
  QScriptEngine* engine = getScriptEngine();
  std::vector< QScriptValue > parameters;
  parameters.push_back( engine->toScriptValue( _t0 ) );
  parameters.push_back( engine->toScriptValue( _t1 ) );
  parameters.push_back( engine->toScriptValue( _t2 ) );
  parameters.push_back( engine->toScriptValue( _t3 ) );
  parameters.push_back( engine->toScriptValue( _t4 ) );
  return qscriptvalue_cast<ReturnValue>( callFunction(_plugin,_functionName,parameters) );
}

/** \brief call a function in another plugin and get a return parameter
 *
 * @param _plugin       Plugin name ( Scripting name of the plugin )
 * @param _functionName Name of the remote function
 * @param _t0           Parameter 1 passed to the function
 * @param _t1           Parameter 2 passed to the function
 * @param _t2           Parameter 3 passed to the function
 * @param _t3           Parameter 4 passed to the function
 * @param _t4           Parameter 5 passed to the function
 * @param _t5           Parameter 6 passed to the function
 * @return value returned by the called function
 */
template <typename ReturnValue , typename T0, typename T1 , typename T2, typename T3, typename T4, typename T5>
ReturnValue callFunctionValue( QString _plugin, QString _functionName, T0 _t0 , T1 _t1 , T2 _t2 , T3 _t3, T4 _t4 , T5 _t5 ) {
  QScriptEngine* engine = getScriptEngine();
  std::vector< QScriptValue > parameters;
  parameters.push_back( engine->toScriptValue( _t0 ) );
  parameters.push_back( engine->toScriptValue( _t1 ) );
  parameters.push_back( engine->toScriptValue( _t2 ) );
  parameters.push_back( engine->toScriptValue( _t3 ) );
  parameters.push_back( engine->toScriptValue( _t4 ) );
  parameters.push_back( engine->toScriptValue( _t5 ) );
  return qscriptvalue_cast<ReturnValue>( callFunction(_plugin,_functionName,parameters) );
}

Jan Möbius's avatar
Jan Möbius committed
398 399 400
/** @} */


Jan Möbius's avatar
Jan Möbius committed
401 402 403
}

#endif // RPCWRAPPERS_HH