RPCWrappers.hh 9.69 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3 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
*                                                                            *
*                              OpenFlipper                                   *
*      Copyright (C) 2001-2010 by Computer Graphics Group, RWTH Aachen       *
*                           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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

//
// C++ Interface: RPCInterface
//
// Description:
//
//
// Author: Jan Moebius <jan_moebius@web.de>, (C) 2007
//

/**
 * \file RPCWrappers.hh
 * This file contains functions to call functions and procedures across plugins.
 * The Qt Scripting system is used to pass the calls between different plugins.
 */

#ifndef RPCWRAPPERS_HH
#define RPCWRAPPERS_HH

#include <QtScript>
#include <vector>
Jan Möbius's avatar
Jan Möbius committed
63
#include <OpenFlipper/common/Types.hh>
Jan Möbius's avatar
Jan Möbius committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

/** Namespace containing RPC helper functions used to call functions across plugins
 */

/**
 * Example code :
 * QString version = RPC::callFunctionValue< QString >("backup","version");
 * int count       = RPC::callFunctionValue< int >("info","vertexCount",2);
 *
 */
namespace RPC {

/** \brief get a pointer to the scripting engine
 *
 */
Jan Möbius's avatar
Jan Möbius committed
79
DLLEXPORT
Jan Möbius's avatar
Jan Möbius committed
80 81 82 83 84 85 86
QScriptEngine* getScriptEngine();

/** \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
87
DLLEXPORT
Jan Möbius's avatar
Jan Möbius committed
88 89 90 91 92 93 94 95
QScriptValue callFunction( QString _plugin, QString _functionName );

/** \brief call a function provided by a plugin getting multiple parameters
 *
 * @param _plugin Plugin name ( Scripting name )
 * @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
96
DLLEXPORT
Jan Möbius's avatar
Jan Möbius committed
97 98 99 100 101 102 103
QScriptValue callFunction( QString _plugin, QString _functionName , std::vector< QScriptValue > _parameters );


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
104 105
//   QVariant bla = qVariantFromValue(_t0);
//
Jan Möbius's avatar
Jan Möbius committed
106
//   IdList list = qVariantValue<IdList>( bla ) ;
Jan Möbius's avatar
Jan Möbius committed
107 108 109
//   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
110
//   std::cerr << "Type id is : " << QMetaType::type("IdList") << std::endl;
Jan Möbius's avatar
Jan Möbius committed
111 112 113
//   engine->globalObject().setProperty("ParameterData22",engine->toScriptValue(list));
/*
  QVariant blubb = engine->globalObject().property("ParameterData22").toVariant();
Jan Möbius's avatar
Jan Möbius committed
114
  IdList list1 = qVariantValue<IdList>( blubb ) ;
Jan Möbius's avatar
Jan Möbius committed
115 116 117
  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
118
  std::cerr << "Type id is : " << QMetaType::type("IdList") << std::endl;*/
Jan Möbius's avatar
Jan Möbius committed
119 120 121


  parameters.push_back( engine->toScriptValue(_t0) );
Jan Möbius's avatar
Jan Möbius committed
122 123 124 125 126 127 128
  callFunction(_plugin,_functionName,parameters);
}

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
129 130
  parameters.push_back( engine->toScriptValue( _t0 ) );
  parameters.push_back( engine->toScriptValue( _t1 ) );
Jan Möbius's avatar
Jan Möbius committed
131 132 133 134 135 136 137
  callFunction(_plugin,_functionName,parameters);
}

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
138 139 140
  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
141 142 143 144 145 146 147
  callFunction(_plugin,_functionName,parameters);
}

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
148 149 150 151
  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
152 153 154
  callFunction(_plugin,_functionName,parameters);
}

155 156 157 158 159 160 161 162 163 164 165 166
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);
}

Jan Möbius's avatar
Jan Möbius committed
167 168 169 170 171 172 173 174 175
template <typename ReturnValue >
ReturnValue callFunctionValue( QString _plugin, QString _functionName) {
  return qscriptvalue_cast< ReturnValue >( callFunction(_plugin,_functionName) );
}

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
176
  parameters.push_back( engine->toScriptValue( _t0 ) );
Jan Möbius's avatar
Jan Möbius committed
177 178 179 180 181 182 183
  return qscriptvalue_cast<ReturnValue>( callFunction(_plugin,_functionName,parameters) );
}

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
184 185
  parameters.push_back( engine->toScriptValue( _t0 ) );
  parameters.push_back( engine->toScriptValue( _t1 ) );
Jan Möbius's avatar
Jan Möbius committed
186 187 188
  return qscriptvalue_cast<ReturnValue>( callFunction(_plugin,_functionName,parameters) );
}

Jan Möbius's avatar
Jan Möbius committed
189 190 191 192
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
193 194 195
  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
196 197 198
  return qscriptvalue_cast<ReturnValue>( callFunction(_plugin,_functionName,parameters) );
}

Jan Möbius's avatar
Jan Möbius committed
199 200 201 202
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
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 210 211 212 213 214
  return qscriptvalue_cast<ReturnValue>( callFunction(_plugin,_functionName,parameters) );
}


/** \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
 */
Jan Möbius's avatar
Jan Möbius committed
215
DLLEXPORT
Jan Möbius's avatar
Jan Möbius committed
216 217 218 219 220
void setScriptEngine( QScriptEngine* _engine );

}

#endif // RPCWRAPPERS_HH