RPCWrappers.hh 18.1 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
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
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
*                                                                            *
40 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
#pragma once
Jan Möbius's avatar
Jan Möbius committed
52 53 54

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


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

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

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

/** \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
102
DLLEXPORT
Jan Möbius's avatar
Jan Möbius committed
103 104
QScriptValue callFunction( QString _plugin, QString _functionName );

Jan Möbius's avatar
Jan Möbius committed
105
/** \brief Call a function provided by a plugin getting multiple parameters
Jan Möbius's avatar
Jan Möbius committed
106
 *
Jan Möbius's avatar
Jan Möbius committed
107 108 109
 * 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
110 111 112
 * @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
113
DLLEXPORT
Jan Möbius's avatar
Jan Möbius committed
114 115
QScriptValue callFunction( QString _plugin, QString _functionName , std::vector< QScriptValue > _parameters );

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

//===========================================================================
/** @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
124

Jan Möbius's avatar
Jan Möbius committed
125 126 127 128 129 130
/** \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
131 132 133 134
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
135 136
//   QVariant bla = qVariantFromValue(_t0);
//
Jan Möbius's avatar
Jan Möbius committed
137
//   IdList list = qVariantValue<IdList>( bla ) ;
Jan Möbius's avatar
Jan Möbius committed
138 139 140
//   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
141
//   std::cerr << "Type id is : " << QMetaType::type("IdList") << std::endl;
Jan Möbius's avatar
Jan Möbius committed
142 143 144
//   engine->globalObject().setProperty("ParameterData22",engine->toScriptValue(list));
/*
  QVariant blubb = engine->globalObject().property("ParameterData22").toVariant();
Jan Möbius's avatar
Jan Möbius committed
145
  IdList list1 = qVariantValue<IdList>( blubb ) ;
Jan Möbius's avatar
Jan Möbius committed
146 147 148
  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
149
  std::cerr << "Type id is : " << QMetaType::type("IdList") << std::endl;*/
Jan Möbius's avatar
Jan Möbius committed
150 151 152


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

Jan Möbius's avatar
Jan Möbius committed
156 157 158 159 160 161 162
/** \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
163 164 165 166
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
167 168
  parameters.push_back( engine->toScriptValue( _t0 ) );
  parameters.push_back( engine->toScriptValue( _t1 ) );
Jan Möbius's avatar
Jan Möbius committed
169 170 171
  callFunction(_plugin,_functionName,parameters);
}

Jan Möbius's avatar
Jan Möbius committed
172 173 174 175 176 177 178 179
/** \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
180 181 182 183
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
184 185 186
  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
187 188 189
  callFunction(_plugin,_functionName,parameters);
}

Jan Möbius's avatar
Jan Möbius committed
190 191 192 193 194 195 196 197 198
/** \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
199 200 201 202
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
203 204 205 206
  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
207 208 209
  callFunction(_plugin,_functionName,parameters);
}

Jan Möbius's avatar
Jan Möbius committed
210 211 212 213 214 215 216 217 218 219
/** \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
 */
220 221 222 223 224 225 226 227 228 229 230 231
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);
}

232 233 234 235 236 237 238 239 240
/** \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
241
 * @param _t5           Parameter 6 passed to the function
242 243 244 245 246 247 248 249 250 251 252 253 254 255
 */
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
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
/** @} */

//===========================================================================
/** @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
272 273 274 275 276
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
277 278 279 280 281 282 283
/** \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
284 285 286 287
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
288
  parameters.push_back( engine->toScriptValue( _t0 ) );
Jan Möbius's avatar
Jan Möbius committed
289 290 291
  return qscriptvalue_cast<ReturnValue>( callFunction(_plugin,_functionName,parameters) );
}

Jan Möbius's avatar
Jan Möbius committed
292 293 294 295 296 297 298 299
/** \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
300 301 302 303
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
304 305
  parameters.push_back( engine->toScriptValue( _t0 ) );
  parameters.push_back( engine->toScriptValue( _t1 ) );
Jan Möbius's avatar
Jan Möbius committed
306 307 308
  return qscriptvalue_cast<ReturnValue>( callFunction(_plugin,_functionName,parameters) );
}

Jan Möbius's avatar
Jan Möbius committed
309 310 311 312 313 314 315 316 317
/** \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
318 319 320 321
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
322 323 324
  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
325 326 327
  return qscriptvalue_cast<ReturnValue>( callFunction(_plugin,_functionName,parameters) );
}

Jan Möbius's avatar
Jan Möbius committed
328 329 330 331 332 333 334 335 336 337
/** \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
338 339 340 341
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
342 343 344 345
  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
346 347 348
  return qscriptvalue_cast<ReturnValue>( callFunction(_plugin,_functionName,parameters) );
}

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
/** \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
397 398 399
/** @} */


Jan Möbius's avatar
Jan Möbius committed
400 401
}