trikRuntime
trikScriptRunnerInterface.h
Go to the documentation of this file.
1 /* Copyright 2014 CyberTech Labs Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License. */
14 
15 #pragma once
16 
17 #include <functional>
18 #include <type_traits>
19 #include <QtCore/QObject>
20 #include <QtCore/QScopedPointer>
21 #include <QtCore/QThread>
22 #include <QtScript/QScriptEngine>
23 
24 #ifndef TRIKSCRIPTRUNNER_EXPORT
25 # if defined(TRIKSCRIPTRUNNER_LIBRARY)
26 # define TRIKSCRIPTRUNNER_EXPORT Q_DECL_EXPORT
27 # else
28 # define TRIKSCRIPTRUNNER_EXPORT Q_DECL_IMPORT
29 # endif
30 #endif
31 
53 
54 #define REGISTER_METATYPE(TYPE) \
55  qRegisterMetaType<TYPE*>(TYPE::staticMetaObject.className());
56 
62 #define REGISTER_DEVICES_WITH_TEMPLATE(TEMPLATE) \
63  TEMPLATE(trikControl::BatteryInterface) \
64  TEMPLATE(trikControl::ColorSensorInterface) \
65  TEMPLATE(trikControl::FifoInterface) \
66  TEMPLATE(trikControl::DisplayInterface) \
67  TEMPLATE(trikControl::EncoderInterface) \
68  TEMPLATE(trikControl::EventCodeInterface) \
69  TEMPLATE(trikControl::EventDeviceInterface) \
70  TEMPLATE(trikControl::EventInterface) \
71  TEMPLATE(trikControl::GamepadInterface) \
72  TEMPLATE(trikControl::GyroSensorInterface) \
73  TEMPLATE(trikControl::I2cDeviceInterface) \
74  TEMPLATE(trikControl::KeysInterface) \
75  TEMPLATE(trikControl::LedInterface) \
76  TEMPLATE(trikControl::LineSensorInterface) \
77  TEMPLATE(trikControl::MarkerInterface) \
78  TEMPLATE(trikControl::MotorInterface) \
79  TEMPLATE(trikControl::ObjectSensorInterface) \
80  TEMPLATE(trikControl::SoundSensorInterface) \
81  TEMPLATE(trikControl::SensorInterface) \
82  TEMPLATE(trikControl::VectorSensorInterface) \
83  TEMPLATE(trikNetwork::MailboxInterface) \
84 
85 namespace trikScriptRunner {
86 
87 enum class ScriptType { // must be 0, 1, ..
88  UNINITIALIZED = -1,
89  JAVASCRIPT,
90  PYTHON,
91  Size // should always be the last
92 };
93 
94 static constexpr typename std::underlying_type<ScriptType>::type to_underlying(ScriptType t) noexcept
95 {
96  return static_cast<std::underlying_type<ScriptType>::type>(t);
97 }
98 
101 {
102  Q_OBJECT
103 
104 public:
106  virtual void registerUserFunction(const QString &name, QScriptEngine::FunctionSignature function) = 0;
107 
109  virtual void addCustomEngineInitStep(const std::function<void (QScriptEngine *)> &step) = 0;
110 
113  virtual QStringList knownMethodNames() const = 0;
114 
115  struct Helper
116  {
120  static void collectMethodNames(QSet<QString> &result, const QMetaObject *obj);
121  };
122 
123 
125  virtual bool wasError() = 0;
126 
127 public slots:
139  virtual void run(const QString &script, const QString &fileName = "") = 0;
140 
141 
147  virtual void runDirectCommand(const QString &command) = 0;
148 
152  virtual void abort() = 0;
153 
155  virtual void brickBeep() = 0;
156 
157 signals:
163  void completed(const QString &error, int scriptId);
164 
168  void startedScript(const QString &fileName, int scriptId);
169 
172  void startedDirectScript(int scriptId);
173 
175  void sendMessage(const QString &text);
176 };
177 
178 }
#define TRIKSCRIPTRUNNER_EXPORT
Definition: trikScriptRunnerInterface.h:28
Definition: trikCommunicator.h:23
Interface for all script executors.
Definition: trikScriptRunnerInterface.h:100
ScriptType
Definition: trikScriptRunnerInterface.h:87
static constexpr std::underlying_type< ScriptType >::type to_underlying(ScriptType t) noexcept
Definition: trikScriptRunnerInterface.h:94
Definition: trikScriptRunnerInterface.h:115
script
Definition: system.py:83