[964] | 1 | #ifndef XMLCUSTOMCODE_H
|
---|
| 2 | #define XMLCUSTOMCODE_H
|
---|
| 3 |
|
---|
| 4 | #include "utilxmltools.h"
|
---|
| 5 |
|
---|
| 6 | #include <omp.h> // OpenMP support
|
---|
| 7 | #include <ctime> // script execution time calculation
|
---|
| 8 | #include <QScriptEngine>
|
---|
| 9 | #include <QTextStream>
|
---|
| 10 | #include <QCoreApplication>
|
---|
| 11 |
|
---|
[1056] | 12 | #include <QThreadPool>
|
---|
| 13 | #include <QtConcurrent/QtConcurrent>
|
---|
| 14 |
|
---|
[964] | 15 | #define SLOW_SCRIPT_TIME 0.1 // if a user script takes more than 0.1 seconds to execute give a warning.
|
---|
[1056] | 16 | #define CUSTOM_FILES_PER_THREAD 4
|
---|
[964] | 17 |
|
---|
[980] | 18 | // Uses a singleton implementation (based on here: http://www.yolinux.com/TUTORIALS/C++Singleton.html)
|
---|
| 19 | // which allows each thread to keep one script engine without always create/destruct them
|
---|
[964] | 20 | class XmlCustomCode
|
---|
| 21 | {
|
---|
| 22 | public:
|
---|
[980] | 23 | static XmlCustomCode* getInstance();
|
---|
[964] | 24 | void executeCustomCode(const QString &jsString, const QVector<QString> &filesToProcess, const bool backupsEnabled, const bool verboseEnabled);
|
---|
| 25 | private:
|
---|
[980] | 26 | static XmlCustomCode* uniqueInstance;
|
---|
[1056] | 27 |
|
---|
[964] | 28 | const int numThreads;
|
---|
[1056] | 29 | QThreadPool myThreadPool;
|
---|
| 30 | QMutex mutexIsAvailable;
|
---|
[964] | 31 |
|
---|
[1056] | 32 | struct jsCustomCodeEngine{
|
---|
| 33 | QScriptEngine* scriptEngine;
|
---|
| 34 | QScriptValue* jsFunction;
|
---|
| 35 | QScriptValue* getXmlDataFunction;
|
---|
| 36 | QScriptValue* setXmlDataFunction;
|
---|
| 37 | bool isAvailable;
|
---|
| 38 | };
|
---|
| 39 |
|
---|
| 40 | QVector<jsCustomCodeEngine> jsScriptEngines;
|
---|
| 41 |
|
---|
[980] | 42 | XmlCustomCode(); // constructor is private (use getInstance)
|
---|
| 43 | XmlCustomCode(XmlCustomCode const&); // copy constructor is private
|
---|
| 44 | XmlCustomCode& operator=(XmlCustomCode const&); // assignment operator is private
|
---|
| 45 |
|
---|
[964] | 46 | void displayJsException(QScriptEngine &engine, QScriptValue &engineResult);
|
---|
[1056] | 47 | jsCustomCodeEngine& getAvailableJsEngine();
|
---|
[964] | 48 |
|
---|
| 49 | __attribute__((always_inline)) inline void customCodeUnwinding(const QString &fileName, QString &currXmlFileString,
|
---|
| 50 | QScriptEngine &engine, clock_t &begin, double elapsed_secs, QScriptValue &engineResult, QScriptValue &jsFunction,
|
---|
| 51 | QScriptValue &getXmlDataFunction, QScriptValue &setXmlDataFunction, const bool &backupsEnabled, const bool &verboseEnabled){
|
---|
| 52 | if(backupsEnabled){
|
---|
| 53 | UtilXmlTools::backupFile(fileName, verboseEnabled);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | QFile currXmlFile(fileName);
|
---|
| 57 |
|
---|
| 58 | if(!currXmlFile.open(QFile::ReadOnly | QFile::Text)){
|
---|
| 59 | UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Error loading '" + fileName + "' file for read operation.");
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | currXmlFileString=QTextStream(&currXmlFile).readAll();
|
---|
| 63 |
|
---|
| 64 | currXmlFile.close(); // close reading
|
---|
| 65 |
|
---|
| 66 | setXmlDataFunction.call(setXmlDataFunction,QScriptValueList() << currXmlFileString);
|
---|
| 67 |
|
---|
| 68 | begin = clock();
|
---|
| 69 |
|
---|
| 70 | engineResult=jsFunction.call(); // main funtion allows to use return to exit prematurely from user code
|
---|
| 71 |
|
---|
| 72 | if(verboseEnabled){
|
---|
| 73 | elapsed_secs = double(clock() - begin) / CLOCKS_PER_SEC;
|
---|
| 74 |
|
---|
| 75 | // Warn the user if the script took much time
|
---|
| 76 | if(elapsed_secs>SLOW_SCRIPT_TIME){
|
---|
| 77 | std::cout << "Warning: Slow javascript code detected.\n" <<
|
---|
| 78 | "Warning: Script execution seconds: " << elapsed_secs
|
---|
| 79 | << std::endl;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | if (engine.hasUncaughtException()) {
|
---|
| 84 | displayJsException(engine,engineResult);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | if(!currXmlFile.open(QFile::WriteOnly | QFile::Text | QIODevice::Truncate)){
|
---|
| 88 | UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Error loading '" + fileName + "' file for @CUSTOM_CODE write operation.");
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | engineResult=getXmlDataFunction.call();
|
---|
| 92 |
|
---|
| 93 | if (engine.hasUncaughtException()) {
|
---|
| 94 | displayJsException(engine,engineResult);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | QTextStream(&currXmlFile) << engineResult.toString(); // retreive the modified xml by javascript and save it to the file
|
---|
| 98 |
|
---|
| 99 | currXmlFile.close();
|
---|
| 100 | }
|
---|
| 101 | };
|
---|
| 102 |
|
---|
| 103 | #endif // XMLCUSTOMCODE_H
|
---|