ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/XmlTools2/trunk/xmlcustomcode.h
Revision: 980
Committed: Thu Mar 6 16:40:35 2014 UTC (11 years, 7 months ago) by s10k
Content type: text/x-chdr
File size: 3627 byte(s)
Log Message:
XmlTools
Singleton implementation

File Contents

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